Programming languages enthusiast. Author of Learn Type Driven Development: https://www.packtpub.com/application-development/learn-type-driven-development
Hi Alex, this is a great write-up. The natural next step is an isLegalMove implementation using case analysis, that shows the power of pattern matching at work. Do you have something in the works?
I chose to leave out the implementation change that fixes the bug because I thought it was pretty simple and wanted to be conscious of the post length. But here it is in the example code repo.
Or were you thinking that pattern matching should be used for something else?
Programming languages enthusiast. Author of Learn Type Driven Development: https://www.packtpub.com/application-development/learn-type-driven-development
That does show the power of pattern matching. You can capture a lot of permutations in a relatively small amount of code this way.
I'm partial to having descriptive names for operations, so I like how the current isLegalMove reads like English: areCardsDifferentColor && areRanksInDescendingOrder reads exactly like the game rules are stated.
That can remain the same and we can use this technique to reimplement areCardsDifferentColor:
Programming languages enthusiast. Author of Learn Type Driven Development: https://www.packtpub.com/application-development/learn-type-driven-development
Yeah, the way you broke down the implementation into helper functions at a lower level of abstraction–the step method–is probably more maintainable overall!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Alex, this is a great write-up. The natural next step is an
isLegalMove
implementation using case analysis, that shows the power of pattern matching at work. Do you have something in the works?Thanks for replying.
I chose to leave out the implementation change that fixes the bug because I thought it was pretty simple and wanted to be conscious of the post length. But here it is in the example code repo.
Or were you thinking that pattern matching should be used for something else?
I had a little fun implementing the function directly partitioning the inputs by case:
That does show the power of pattern matching. You can capture a lot of permutations in a relatively small amount of code this way.
I'm partial to having descriptive names for operations, so I like how the current
isLegalMove
reads like English:areCardsDifferentColor && areRanksInDescendingOrder
reads exactly like the game rules are stated.That can remain the same and we can use this technique to reimplement
areCardsDifferentColor
:It's definitely fun to think about the best way to express things.
Yeah, the way you broke down the implementation into helper functions at a lower level of abstraction–the step method–is probably more maintainable overall!