6 kyu
Binding within the List Monad
350 of 1,129user578387
Loading description...
Monads
Lists
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
It is not properly explained how to handle functions which don't return a list.
Actually it is described that you should handle errors in the code examples. I added an extra sentence to make clear you should always throw errors in dynamically typed languages.
[Java] Actual and expected are swapped, at least in the Sample Tests
swapped
I am not sure I understand why errors need to be thrown for certain uses of the id function ...
the list monad bind operation will only work iff the given function also returns a list. the author's intention was to make sure an error is thrown in these cases, to make sure no invalid result is produced.
Good kata! Should give a little bit more details on Error throwing etc.
For the Java language, the first example of "Here's how it should work" is: bind(Arrays.asList(2,3,4), i -> Arrays.asList((int)i + 1)) //=> [2,3,4]
Shouldn't it be: bind(Arrays.asList(1,2,3), i -> Arrays.asList((int)i + 1)) //=> [2,3,4] ?
Ah, you're right! I'll fix that.
This comment has been hidden.
This comment has been hidden.
Yes; you're flattenning, not concatenating, which means that it will not automatically fail; a safer deal.
I don't understand why: "bind( [1,2,3] ) {|a| a } => # ERROR! The returned value is not a list!"
irb(main):0xx:0> bind( [1,2,3] ) {|a| a } => [1, 2, 3]
so the returned value ist a list, isn't it?
That shouldn't work on IRB, so something's wrong. The given procedure must return a list, so that this occurs:
Do you see what I mean?
bind
must behave in this way because monads at the purest level can only safely be constructed, and deconstruction can only be done within a binding. Even so, the bound value must remain inside the monad, keping everything kosher. As a result, lists returned are treated as all the same list, and bundled together at the end of the computation. Most other monads, likeMaybe
orIO
, or even the function monad, work in an entirely different way, but to the same effect.If we were going to implement this with more monadic types, I'd extend the typeclass to support polymorphism.
This comment has been hidden.
Flatten in this context will not work. You will need to concatenate each resultant list, automatically causing an error where necessary.
Flatten works perfectly... I don't pass your last test because you wait for an error at this place and I don't give one...:-)
There is no error because flatten doesn't work in the way it should in this context. Re-read the specification.
The specification makes no sense, over and out!
In the ruby implementation, I think you are missing the ampersand on the lambda when you are calling bind in the tests. such as: Test.assert_equals bind([1,2,3], &->(n){[n]}), [1,2,3]
versus: Test.assert_equals bind([1,2,3], ->(n){[n]}), [1,2,3]
without them, I keep getting this error:
bind': wrong number of arguments (2 for 1) (ArgumentError) from
'Fixed, thanks. I also updated the regular tests.
I'm not sure this is a proper way to write a monad, but I enjoyed the coding
It isn't, but binding is a key part of any monad.
I've added a Java translation.
What? Java? For a functional programming problem?
Yes. http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
*HUG*
THANK YOU!
This comment has been hidden.
Added!
It makes me far too happy when I make a suggestion that results in large swaths of solutions becoming obsolete...
"Some people dream of success, and others live to crush those dreams." :-D
We should start a club sometime, you and I: club schadenfreude...
Wasn't sure if I should be editing the original list in-place or creating a new one?
Ah, I should clarify the pureness of the function - thanks!
I'd recommend explicitly stating what that means, though. Something along the lines of
build a new array and return it
. Not everyone will know whatpure
andwithout side-effects
would mean.This comment has been hidden.
Added, thanks.
The tests mention String class that is not present and causes compilation fail.
Which language is this?
Python
Fixed, thanks.
In the example test cases, String(a) should be str(a)
Added, thanks!
I think your last suggestion could go amiss, but I'll remove the tag and add some example tests.