Binding within the List Monad
Description:
In Haskell, Monads are simple containers, or even 'box-like' datastructures, of which lists are included, which can respond to certain functions, which are defined in the Monad typeclass. (To put it simply!)
In this kata, you must implement the Bind function for lists, or arrays. In haskell, the function is represented by >>=
, but we'll just call it bind
.
Essentially, bind
should map the array with the function given, and then flatten it one time. Don't manipulate the original array; make you function pure: without side-effects, so that no variables are edited whilst the function is carried out. In dynamically typed languages, you should throw an error if the given function does not return a list.
Here's how it should work:
bind( [1,2,3], function(a){ return [a+1] } )
=> [2,3,4]
bind( [1,2,3], function(a){ return [[a]] } )
=> [[1],[2],[3]]
bind( [1,2,3], function(a){ return a } )
=> # ERROR! The returned value is not a list!
As per usual, the ruby function will be passed a Proc or Lambda. Remember that the function still takes two arguments!
Similar Kata:
Stats:
Created | Nov 20, 2014 |
Published | Dec 10, 2014 |
Warriors Trained | 2654 |
Total Skips | 686 |
Total Code Submissions | 6906 |
Total Times Completed | 1129 |
JavaScript Completions | 350 |
CoffeeScript Completions | 26 |
Ruby Completions | 106 |
Python Completions | 454 |
Java Completions | 259 |
Total Stars | 56 |
% of votes with a positive feedback rating | 89% of 171 |
Total "Very Satisfied" Votes | 137 |
Total "Somewhat Satisfied" Votes | 29 |
Total "Not Satisfied" Votes | 5 |