Hire people that extend your company’s boundaries. Avoid those that try to stay within them.
Monday, 23 July 2012
Wednesday, 27 June 2012
Scala: Applicative with scalaz example
I’ve talked on Functor previously, will repeat myself that
construction is very useful in the Scala's world. We covered Comonad as well, it is extending Functor and adds functionality for the extraction value or the wrapping of already wrapped.
This post is related to Applicative. Applicative is coupled with Functor, even has an alternative name: Applicative Functor. At first let look into class hierarchy for Applicative in
scalaz library:
Labels:
Applicative,
Comonad,
Functor,
Monad,
scalaz
Monday, 25 June 2012
Scala: constant in pattern matching or always do a Unit testing!
Every Scala coder should remember the difference between Constant and Variable patterns in
pattern matching. Short refresh from this topic: if pattern starts from low case letter: it is counted as a variable. We can still use a lowercase name for a pattern constant, using one of two tricks:
1). If the constant is a field of any object, we can prefix it with a qualifier. For example, name is a variable pattern, but this.name or objInstance.name.
pattern matching. Short refresh from this topic: if pattern starts from low case letter: it is counted as a variable. We can still use a lowercase name for a pattern constant, using one of two tricks:
1). If the constant is a field of any object, we can prefix it with a qualifier. For example, name is a variable pattern, but this.name or objInstance.name.
2) Second workaround is enclosing the variable name with back ticks. For instance, `name` would again be interpreted as a constant, not as a variable.
Friday, 22 June 2012
Scala: Comonad (scalaz example)
When we were talking on Functor - we described it as a construction that can apply function to incapsulated value and return a new instance of the same container with function result value inside. Quite useful construction, it is even implemented by scala library's classes: like Option, List and others. To be honest Scala's standart library classes mixing Functor and Monad, but lets do not concentrate on this now.
Thursday, 21 June 2012
Scala: Polymorphism rework
As dude came from C# and Java to Function Programming I'm thinking on polymorphism in therms of OOP only. I was believing in: Polymorphism is coming only as a monolithic bundle based on Inheritance. But look into this:
This is the method that is accepting ... type T, which should be any type, and this is good example of ... Polymorphism. Method second is getting a list of T and should return extracted T (we can try to guess that the best fit logic here is returning second element). Doesn't matter which T is inside - method declared to work with All types: Int, String, List[List[String]], AnyRef, whatever. This part for OOP world dudes is unnatural, then let's move to a new: Ad-hoc Polymorphism.
This is the method that is accepting ... type T, which should be any type, and this is good example of ... Polymorphism. Method second is getting a list of T and should return extracted T (we can try to guess that the best fit logic here is returning second element). Doesn't matter which T is inside - method declared to work with All types: Int, String, List[List[String]], AnyRef, whatever. This part for OOP world dudes is unnatural, then let's move to a new: Ad-hoc Polymorphism.
Monday, 18 June 2012
Scala: Functor (scalaz example)
Function programming got a proven handy in structuring the code, from where we have common structures/classes presented in many functional languages. All this magic comes from mathematic and in the most parts aren't so easy to get for regular developers with imperative experience. I've been googling a lot to get essence and still getting a lot of "news". Looks like the best way to review FP types in Scala is using examples from scalaz library. Let try to start from the Functor.
Functor
Functor is a something you can map over, it has only one method map (called fmap scalaz). Class diagram is very simple:
scala.Either
Even newcomers to Scala know about Option class. We are starting from it as from solution to Nullpointer errors, later we finding it as a very useful Monad implementation. It is dual natured - one is Monadic Container - value is inside - and second is Monadic Zero with no value. Actually sometimes it is useful to have some value even in Zero container. For example non breaking error handing. Errors are important and even can be accumulated into the List.
Subscribe to:
Posts (Atom)

