5 kyu

Monads: The list monad

Description
Loading description...
Functional Programming
Arrays
Fundamentals
View
AllIssues6Questions1Suggestions1Show Resolved
  • Please sign in or sign up to leave a comment.
  • alex1yaremchuk Avatar

    I like the flow of this task.

    It is just lovely!

    Going to solve more tasks of the author.

    Thank you.

  • FArekkusu Avatar

    No sample tests.

  • C.c. Avatar

    I learn some connection between bind and unit, thank you. 🍻

  • Len512 Avatar

    Excellent! Thank you.

  • kostia Avatar

    Thanks for the kata! Two minor things:

    1 One of the tests for knightEngine is checking if bind was called -- will fail if one would define a "bound" version of moveKnight before. e.g.

    var boundMoveKnight = bind(moveKnight);
    function knightEngine(from, movements){
     // ...
     boundMoveKnight(...)
     // ...
    }
    

    2 The bind function is being redefined in tests, which will fail if bind was defined as const. e.g.

    const bind = f => ...;
    

    (though I understand that this kata was written in days of es5 standard)

  • chocomel Avatar

    This comment has been hidden.

  • hufftheweevil Avatar

    We need more katas like this! Half tutorial / half figure-it-out-on-your-own. Enjoyed it a lot

  • Unihedron Avatar

    Typo: "Carefull", your bind "implementaion" should do this too

    should be careful and implementation

  • hhummel Avatar

    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.

  • gabbek Avatar

    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.

  • JohanWiltink Avatar

    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 for compose really, but I have a feeling that's really do 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 write bind" or "now write unit" 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?

  • s-lugo Avatar

    One of my favorite kata I have solved on the site, really fun and educational one. Thanks!

  • DealPete Avatar

    This comment has been hidden.

  • SgiobairOg Avatar

    This comment has been hidden.

  • bundgaard Avatar

    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

  • jressey Avatar

    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.

  • draconar Avatar

    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?

  • draconar Avatar

    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!

  • democraci Avatar

    awesome kata, it help me a lot on understanding monad.

  • Prick Avatar

    Thx for the awesome kata. Definitely the best I've solved so far.

  • DeanBrown Avatar

    This was a fun one.
    After finishing it I still don't really get monads.
    This kata has inspired me to learn more though.

  • Flonk Avatar

    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.

  • harish Avatar

    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; }

  • glebec Avatar

    I really enjoyed this problem, even if it was a bit on the long side. Thanks!

  • joeframbach Avatar
    * moveKnight :: Int -> [Square]
    
    * moveKnightRandom :: Int -> Square
    

    Surely this should be:

    * moveKnight :: Square -> [Square]
    
    * moveKnightRandom :: Square -> Square
    
  • outergod Avatar

    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.

  • croceflamenco Avatar

    Wikipedia link is broken, and first line should read "What's a monad?" in kata description.

  • SteveRuble Avatar

    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?