4 kyu

Haskell List Comprehension (ii)

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:

CreatedJul 18, 2014
PublishedJul 18, 2014
Warriors Trained1057
Total Skips551
Total Code Submissions635
Total Times Completed212
JavaScript Completions212
Total Stars32
% of votes with a positive feedback rating95% of 77
Total "Very Satisfied" Votes70
Total "Somewhat Satisfied" Votes6
Total "Not Satisfied" Votes1
Total Rank Assessments26
Average Assessed Rank
4 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • surtich Avatar
  • user578387 Avatar
  • bkaes Avatar
  • Voile Avatar
Ad