5 kyu
Monads: The list monad
307surtich
Loading description...
Functional Programming
Arrays
Fundamentals
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.
I like the flow of this task.
It is just lovely!
Going to solve more tasks of the author.
Thank you.
No sample tests.
I learn some connection between bind and unit, thank you. 🍻
Excellent! Thank you.
Thanks for the kata! Two minor things:
1 One of the tests for
knightEngine
is checking ifbind
was called -- will fail if one would define a "bound" version ofmoveKnight
before. e.g.2 The
bind
function is being redefined in tests, which will fail ifbind
was defined asconst
. e.g.(though I understand that this kata was written in days of es5 standard)
This comment has been hidden.
We need more katas like this! Half tutorial / half figure-it-out-on-your-own. Enjoyed it a lot
Typo: "Carefull", your bind "implementaion" should do this too
should be careful and implementation
Fixed. Thanks for reporting.
Outstanding kata, both in grappling with monads and in delving into how to use arguments in a more sophisticated way. The functional way of thinking has changed how I code.
Thank you very much for this awesome kata! I've learned a lot thanks to it. Very good explanation as well, I've enjoyed it.
I like how you did the compose function. I used recursion, but yours is cleaner.
Very nicely done! A couple of remarks:
What's the use of
moveKnightRandom
? It might be in the original text (I haven't looked it up), but it doesn't really add anything. I'd take it out; anything to make this shorter can only help. (The same goes forcompose
really, but I have a feeling that's reallydo
and you don't want to take it out. But it's also not essential.)The description might clarify that you have to generate knight moves in a particular order. It shouldn't be that hard to sort the moves array before testing (there's even code for this?), but specifying that they have to be generated in some order is not really a problem. Having the random test generate one position in the middle, where there will always be all eight possible moves, would help with this. (Having the another position nearer to the edge, where there will always be less than eight possible moves, might help people debug their filtering.)
Finally, where all the
moveKnight
functions are very clearly specified with type declarations and explicitly asked for, you never have a line saying "now writebind
" or "now writeunit
" in the Description. This is not the end of the world, but it distracted me - by then I was expecting there to be such an instruction, so I went back hrough the text to see what I had missed.Any chance there is, or is going to be, something like this for the State monad?
One of my favorite kata I have solved on the site, really fun and educational one. Thanks!
Thanks!
This comment has been hidden.
This comment has been hidden.
Your function will involve a recursive invocation of "compose". Figure out how to do it with two functions then extend that to the general case.
Hi,
should I for knightEngine make it run through a list of bind(moveKnight) and sort of replicate the compose function ?
Having a hard time to see how I would go from
var knightEngine = compose(bind(moveKnight), bind(moveKnight))(from)
to
function knightEngine(from, movements) {}
Any directions/hints you could provide..
My initial thought was to make a list and replicate functionality from compose but that seems counter intuitive as I dont want to replicate my code... but I might have too?
//David
Please provide unit tests for the JS implementation. The description for writing my own tests for the compose method was not sufficient to continue the exercise.
in the knightEngine() function what exactly is a second move? I mean, when I run the knightMovement() function with an array as parameter (because the first run got me an array as result), what should I do? go thru every position?
Hi, I need some help implementing the knightEngine function. I'm not being able to figure out what should I do. Maybe I'm not getting the .bind() correctly enough or something else. What exactly is the .knightEngine() function supposed to accomplish?
thanks and cheers for the awesome Kata!
In the description is explained:
You can implement this function using
compose
,bind
andmoveKnight
functions.awesome kata, it help me a lot on understanding monad.
You are welcome.
Thx for the awesome kata. Definitely the best I've solved so far.
Thanks!
This was a fun one.
After finishing it I still don't really get monads.
This kata has inspired me to learn more though.
Thanks! It happens to me the same thing: when I think I've already understood it at the time I forget it.
The main idea is: Monads is about function composition. Here's a great resource that I review occasionally.
Thanks that helped. I still didn't get it so I watched and read several other tutorials. I remained confused until I watched this one Brian Beckman, don't fear the monad I imagine there are better ones but this is what made it click for me. I think its one of those things that you have to hear about from many different perspectives before it sinks in. Now it seems simple but still hard to explain correctly. I bookmarked the page you gave me because now it makes perfect sense and will be a great way to remind me how monads work
Anyways thanks for the kata I learned a lot because of it.
Yes, I know the Beckman video too and it's a great resource.
Minor thing - found a typo:
var knightEngine = compose(moveKnightRandom, moveKnightRandom, moveKnightRandom); //It will perform tree random movements
Should probably be 'three'!Apart from that, really cool kata! I'm all about shoe-horning functional paradigms into my favourite languages.
Fixed it! Thanks!
Until today, I didn't know there was a concept called Monad. This is truly the best Kata I have solved (solving - canReach) so far. Really thanks for this Kata surtich. This is bloody awesome. I got stuck at bind method for quite some time but figured out finally. Truly thoughtful Kata, I suppose.
Though the below shouldn't work and return false even when there is a match, I still used this for canReach - surprisingly it gave me 100% test results. I am going to redo this part.
function canReach(from, to, movements) { var validMoves = knightEngine(from, movements); if (validMoves.indexOf(to) < 0) return false; return true; }
I really enjoyed this problem, even if it was a bit on the long side. Thanks!
Thanks!
Surely this should be:
Fixed it! Thanks!
I do really like this Kata for its educational value since it opens up the world of Haskell and elaborate functional programming to the JavaScript hacker. The step-by-step style exerted that starts without actually touching Monads is a great way of brining the Kata's student closer to a deeper understanding.
I see a lot of issues with the description however. It is apparent that English is not your native tongue (and neither it's mine), which is no big deal by itself - but the quality of the Kata suffers from quirky phrasings, wrong grammar and typos. Fixing those will especially help understanding the advanced Monad topics towards the ends, which is apparently the core point of this Kata. Maybe you'd like to ask someone for help with the description. I'd also help, if I can.
Yes you're right: English is not my native tongue. I would be very grateful for your help. Thanks!
What follows are some suggestions; feel free to accept or reject them as you like. Some minor issues remain, but they are negligible and addressing them would be nitpicking :)
Change:
To:
Change:
To:
Change:
To:
Change:
To:
Change:
To:
Change:
To:
Change:
To:
Change:
To:
In the example for
moveKnightRandom
, then
is missing inRandom
- it saysmoveKnightRadom
.Everywhere, change
can not
tocannot
. In the places where it's left, try to avoid talking in first person, i.e. "... I ... (did something)".I hope this helps. Maybe someone else will want to contribute as well..? :)
Thank you very much. You have helped me alot. Not only you have helped me to improve the kata description but I have also gotten a English class for free. Thanks again!
Wikipedia link is broken, and first line should read "What's a monad?" in kata description.
Fixed it! Thanks!
The moveKnight tests require that our solution generates a list of move destinations which is in the same order as the list you're generating, but that order is not specified anywhere. Does the ordering of moves really matter, or can that test be modified to accept the moves in any order?
You are right. I have refactored the tests and now the order doesn't matter.