4 kyu
Haskell List Comprehension (ii)
212surtich
Description:
This is the second part. You should do the [previous part][previous] first. [previous]: http://www.codewars.com/kata/53c8b29750fe70e4a2000610
Haskell List Comprehension can generate lists by applying filters and transformations.
In this kata we are going to do the same in JavaScript.
To do this, copy the solution you gave in the previous kata and modify it so that the options
object can accept two parameters more:
filters[]
: Array of functions. Each function receives an integer and returns a boolean. Only values that pass the filter, belong to the list.transform(value)
: Is a function that takes a value and returns it muted.
For example:
//Filter
function isPrime(num) {
var result = true;
if (num !== 2) {
if (num % 2 === 0) {
result = false;
} else {
for (var x=3; result && x<=Math.sqrt(num); x+=2) {
if (num % x === 0) {
result = false;
}
}
}
}
return result;
}
//Transform
function arrayWrapper(num) {
return [num];
}
ArrayComprehension({
generator: "1..50",
filters: [isPrime],
transform: arrayWrapper
}); // returns [[2], [3], [5], [7], [11], [13], [17], [19], [23], [29], [31], [37], [41], [43], [47]]
Cool, is'nt it?
Functional Programming
Arrays
Similar Kata:
Stats:
Created | Jul 18, 2014 |
Published | Jul 18, 2014 |
Warriors Trained | 1057 |
Total Skips | 551 |
Total Code Submissions | 635 |
Total Times Completed | 212 |
JavaScript Completions | 212 |
Total Stars | 32 |
% of votes with a positive feedback rating | 95% of 77 |
Total "Very Satisfied" Votes | 70 |
Total "Somewhat Satisfied" Votes | 6 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 26 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |