Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
A couple of times I have been asked “How would you do X in functional programming?” I absolutely love these types of questions.
I do my best to answer every question, but I think there are many questions interesting enough to warrant their own articles.
So for this article I would like to demonstrate how I could recreate this imperative function in a more functional manner.
This function has an if statement without an else. So while a ternary operator could work, it is not ideal.
Here we need to run `dispatch` only when we have a `value`, otherwise we do nothing.
One option is to use a short circuit operator:
Short circuit and ternary and both ways you can solve this problem, but let’s take a look at a couple more.
Solution A: ifVal Helper Function
To do this in a more functional way, I will create a helper function. I’ll write this helper old school first then boil it down piece by piece so everyone can understand it easily.
Now we can modify our someAction function to use ifVal instead of the classic if block.
Here’s the comparison for a quick and easy twitter screenshot:
Further reading
Take a look at Ramda’s when, unless, and ifelse for other useful and similar functions.
Solution B: Monads!!!
We could also use the Maybe Monad. And since Monads are oh so much fun, I thought it would be a fun solution also!
The Maybe Monad is very simple, all it does is hold either a value known as a Just or no value, known as a Nothing.
For this example, I’ll use the Maybe Monad from the Sanctuary library.
It looks a little like this:
The Maybe Monad is pretty simple and the reason why I want to use it is because map and Maybe are a match made in heaven. If the Maybe is a Nothing, then map is ignored. If the value is a Just, only then it is mapped.
All together doSomething with the Maybe will look like this:
Here we have three functions, getItem, toMaybe and map that can be composed together into a new single function. We can pass value into that new function and it will flow through first into getItem, then toMaybe and finally that value will flow into our map.
And for one last picture perfect Tweet:
End
I hope you enjoyed this small functional journey. Let me know in the comments if you did, if you got lost, if you have any questions, or if you want more articles like this one. If you really want to motivate me, follow me here or Twitter and if you loved this article, pass it around!
Cheers!
p.s. this article was originally posted on dev.to.
Functional Programming: Alternatives to the IF #Functional #JavaScript was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.