4 Cool things on Functional Programming with Scala

Scala has many cool things around functional programming. I have mixed feelings with some of this features like case classes and patterns marchers, macros, generics, implicits, i question how good they are and if you really need them!? Besides there are some things that are practical and let you as a developer writes less code and code thats is more expressive, thats whats matter in the end of the day.

 Some of this things might sound silly but if you consider that systems tend to be more and more complex now a days you will endup have todo more complex operations and less code you right, it would be better.




#1 Case Class Pattern Matching

As i said i`m not the biggest fun of case class. Current implementation in scala(2.11.x) still kind of dumb and honestly thats is cool because java is so heavy on syntax or lack of syntactical sugar. Pattern matcher for several guys on FP front is consider not functional programming at all. The main advantage for me will be the case for not going through the callback hell you do in JavaScript, Node.js and other languages now a dayz. So when you are doing some High Order functions with composition you can use case classes + pattern matcher on a filter scenario this is useful, check this out,


#2 andThem Function Composition

Most of times when i see some sample code with andThem end up being some bizarre sample doing some crazy monodic thing nobody understand. Chaining functions is useful, working with data, doing services integration, any kinda of orchestration code can take benefit of this. This is more straightforward them the previous one, check it out.














andThem gets the first function and chains with the second one, this is cool, now lets see another way todo something similar but different.

3# Compose Functions
















Whats is the difference? Compose Function will get the result from the function add2 and call add3 and andThem will work on a different front, andThem is a method. Compose expects a function on its argument .

#4 Partial



This is another one i got mixed feelings but i think in some cases this could be good. Its possible to define partial functions with partial case statements(from pattern matcher) besides composition this could be good on case you have many options to decide something, someone maybe say well your design is bad this is a small? could be or you could be just dealing with a complex business.

When you can break your code into chunks is beneficial, i used Apache camel for years, its a great framework, but with all this features cover here you might think of using some functional solution with pure scala. For more complex scenarios i would still use camel :-).

Popular posts from this blog

Telemetry and Microservices part2

Installing and Running ntop 2 on Amazon Linux OS

Fun with Apache Kafka