Furthermore, learning a more functional language has certain difficulties. You need to have a completely different mindset when facing the same problems and sometimes you need to use another mental pattern in order to find a more elegant solution.
Enough talking, let’s get to some examples and we’ll clearly see what we are talking about!
Removing a value from a list
This is a pretty common pattern and even our prophet, Dan Abramov, has used it on his infamous Redux egghead tutorial 👼 How could we achieve the same in Reason? 🤔 Lo and behold!
Find an element in a list and then use it
Another mundane thing that we do in our daily lives is looking up items and use them somewhere else, you know, this code you have probably typed a thousand times already…
Okay, that makes sense, besides the fact that the `find` method is called `getBy` in Reason, which is no big deal here, except is it? Well my friend, the best part is yet to come…
Yes, this is one of the many advantages of a type safe language, you cannot forget about error checking because the language does not allow you to make these kinds of mistakes! (You would get a beautiful error from the compiler saying something like a `string` was expected but instead it received an `option(string)`). This is awesome. 😎
Map over a list and use the indexes
That sounds familiar, doesn’t it? 😜 Well, interestingly enough, the default Belt.List.map function does not have such information as the index for the elements in the key so, how would we solve this?
What We Learned
These were some of the things I learned on my Reason journey and I’m really excited about it, hope you found it useful and that it helped you as well to see some of the goodies of the language and maybe hopefully you are now a bit more hyped and will share it with the world! 🌍
Shameless self promotion: 🙃 I’ll keep writing about the things I learn and some other interesting stuff related to functional programming so if you don’t want to miss out, stay tuned and follow me on Twitter! @FlavioCorpa 👋
Thanks to G2i for publishing this post. For those who are unfamiliar, G2i is a hiring platform run by engineers that matches clients with pre-vetted React, React Native, GraphQL, and native iOS/Android focused engineers you can trust.