6 kyu
The takeWhile Function
1,365 of 2,459jgdodson
Description:
Here's another staple for the functional programmer. You have a sequence of values and some predicate for those values. You want to get the longest prefix of elements such that the predicate is true for each element. We'll call this the takeWhile
function. It accepts two arguments. The first is the sequence of values, and the second is the predicate function. The function does not change the value of the original sequence.
Example:
sequence : [2,4,6,8,1,2,5,4,3,2]
predicate: is an even number
result : [2,4,6,8]
Your task is to implement the takeWhile function.
If you've got a span function lying around, this is a one-liner! Also, if you liked this problem, you'll surely love the dropWhile function.
Functional Programming
Arrays
Algorithms
Lists
Data Structures
Similar Kata:
Stats:
Created | Mar 6, 2015 |
Published | Mar 6, 2015 |
Warriors Trained | 4587 |
Total Skips | 225 |
Total Code Submissions | 9256 |
Total Times Completed | 2459 |
JavaScript Completions | 1365 |
C# Completions | 243 |
CoffeeScript Completions | 19 |
C++ Completions | 306 |
Python Completions | 575 |
λ Calculus Completions | 15 |
OCaml Completions | 35 |
C Completions | 53 |
Total Stars | 62 |
% of votes with a positive feedback rating | 89% of 479 |
Total "Very Satisfied" Votes | 393 |
Total "Somewhat Satisfied" Votes | 64 |
Total "Not Satisfied" Votes | 22 |
Total Rank Assessments | 96 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 8 kyu |