Self-Learn Yourself Scala in 21 Blogs – #5
Self-Learn Yourself Scala in 21 Blogs – #5
Blog 5 – Does functional programming matters and what are monads?
Missed the previous blogs have a quick look with Self-Learn Yourself Scala in 21 Blogs (#1, #2, #3, #4). In this blog let’s understand for Scala developers does the functional programming matters and also what is all about design pattern of functional programming which is called monads.
Functional programming is actually different from the imperative programming and the functional programming is another way of thinking about programming. But what the functional programming brings to the table for developers and architects, and the famous answers are concurrency and multicore programming. Along with that it also handles the business complexity well.
For an example if we take all UNIX processes follow two simple rules: write programs that do one thing and do it well and Write programs to work together. So what the above rules gain us is and the answer is composability. Hence UNIX processes show us the power of composability. UNIX processes are like LEGO blocks and we can pick them in any order, combine them to create new processes, name these processes, build new processes on top of them, and so on. And how does all of this map to functional programming, so now a UNIX pipe is like a functional programming language. If we think of each process as a function, a UNIX pipe lets us to compose these functions using | notation and in Scala it is functional composition.
Quick example from the Scala code,
def doeven: Int => Boolean = _ % 2 == 0
def donot: Boolean => Boolean = !_
def dofilter[A](criteria: A => Boolean)(col: Traversable[A])=col.filter(criteria)
def domap[A, B](f: A => B)(col: Traversable[A]) = col.map(f)
And the above functions are like UNIX processes in that each one does exactly one thing. And the example in our hand is simple and naïve, but one point is clear: composability allows us to build solutions from smaller building blocks, which is important because that’s how we solve complex problems. When we design a solution for a large, complex problem, we break the problem into smaller problems and further break them down into even smaller problems until we get to a point where comprehension is easy.
Solving these individual pieces and then bonding them together to build the final piece is a centuries-old technique. This breaking down of problems into smaller digestible pieces happens across all the layers of software development.
Functional composability lets us to build these mathematical microworlds in our application. And in these microworlds we can be certain about things because they’re all made of pure functions, and we can easily build them using function composition. And the other benefit of the this pure functional world is debugging.
As an object functional language, Scala offers the benefits of both object-oriented and functional programming. Functional programming gives us the additional benefit of composition that makes the core and more complex parts of our application easier to write and maintain.
What is Design Patterns for FP? Is it monads?
Here we will discuss about the functional programming design pattern called monads. And the problem with monads is the general misconceptions about monads are that they are hard to understand and we need a good mathematical back ground to fully understand this. It’s true that monads originated from a branch of mathematics called collections and arrows. But we have nice abstraction layer like design pattern to help and structure our code.
In fact we have already used monads which are the two common ones so far are List and Option. The List monad abstracts out the computation that might return 0, 1, or more possible results. The Option monad abstracts out the computation that may not return anything (Some or None). Monads are usually considered an advanced functional programming concept, but it’s foundational part of Scala’s functional programming.
The two most important benefits are monads let us to compose functions that don’t compose well, such as functions that have side effects and monads let us to order computation within functional programming so that we can model sequences of actions.
Both of these are critical and powerful properties to be aware of when designing applications using functional programming techniques.
See you in next blog with details for how to building web applications using funcitonal programming style?
Reference – Scala in Action by Nilanjan, and Communities.
Interesting? Please subscribe to our blogs at www.dataottam.com, to keep you trendy and for future reads on Big Data, Analytics, and IoT.
As, always please feel free to suggest or comment firstname.lastname@example.org.