• Scalaz Semigroup, Monoid, Equal, Order, Enum, Show and Standard Scala Classes

    Scalaz has so many abstractions and higher kinds that model lot of algebraic structures, monads, hight level types to generalize common structures and offers lot of tools that work with those abstractions. In this article I wanted to cover the very basics. Ones we see every day in standard scala library. What the anomalies of basic types are in standard scala library, what are the comment things between them that can be made generic, how to use scalaz with standard classes defined in scala library.

    read more
  • Scalaz

    Scala is an object oriented language with functional aspects. Some people like the object oriented aspect, some are attracted by the functional part. Add to that the fact that it’s sitting on top of JVM and of course it’s influenced by Java a lot, we still have nulls, we still have exceptions, IO and state mutation, etc. And since Scala isn’t purely functional it still allows us to write imperative code, call a Java library that will throw an exception, etc. I personally think that functional code is easier to read and understand, unit test and maintain(not event talking about state and multiple threads). So when it comes to writing purely functional code (or as close to it as possible) Scala sure misses certain things and scalaz has a lot built to help. I decided to write series or posts to explain scalaz. I’ll start with the basics, most common abstractions then explain more real words cases. I’ll put the links to all posts here so it’s easy to navigate.

    read more
  • Methods Scalaz Adds To Standard Classes - Integer, String and Boolean

    In my previous article about Semigroup, Monoid, Equal, Order, Enum and Show. I covered what those are, what method they have and what derived and syntax sugar methods scalaz bring for them. Also I showed that there are implicit implementations of those for scala standard classes that once are brought into the scope you’ll have access to those. In this article I’m gonna show those methods in action for integer, string and boolean.

    read more
  • Scalaz Functor

    Functor is a mapping from type F[A] hight kind to F[B]. Scalaz defines Functor[F[_]] trait with map abstract method.

    def map[A, B](fa: F[A])(f: A  B): F[B]

    Having a higher kind A and A to B mapping we can get higher kind B.

    read more
  • Scalaz Apply

    Apply is Functor that also has apply method. Scalaz defines Apply[F[_]] trait with ap abstract method.

    def ap[A,B](fa:  F[A])(f:  F[A  B]): F[B]

    Having a higher kind A and hight kind of A to B mapping we can get higher kind B. And since Apply is also a functor we also have

    def map[A, B](fa: F[A])(f: A  B): F[B]
    read more
  • Scalaz Applicative

    Applicative is Apply that also has point or pure methods. Scalaz defines Applicative[F[_]] trait with point abstract method.

    def point[A](a: => A): F[A]

    point method lifts A to F[A]. Since Applicative is Apply it inherits all the methods that Apply offers plus it needs to provide implementation for map and ap methods. Note that Applicative implements map using point and ap methods:

    override def map[A, B](fa: F[A])(f: A => B): F[B] = ap(fa)(point(f))
    read more
  • Scalaz Bind

    Bind is an Apply that also has bind method. Scalaz defines Bind[F[_]] trait with bind abstract method.

    def bind[A, B](fa: F[A])(f: A => F[B]): F[B]

    Having F[A] and mapping A to F[A] it returns F[B].

    Since Bind is Apply it inherits all the methods that Apply offers plus it needs to provide implementation for map and ap methods. Note that Bind implements ap using bind and map methods:

    override def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B] = {
        lazy val fa0 = fa
        bind(f)(map(fa0))
    }
    read more
  • Scalaz Cobind

    Cobind is a Functor that also has cobind method. Scalaz defines Cobind[F[_]] trait with cobind abstract method.

    def cobind[A, B](fa: F[A])(f: F[A] => B): F[B]

    Having F[A] and mapping F[A] to B it returns F[B]. Note that unlike bind method in Bind functor, cobind takes F[A] => B instead of A => F[B] function.

    Since we have cobind method we can define cojoin method bases on it. Unlike to join in Bind, it turns F[A] to F[F[A]] instead of F[F[A]] to F[A].

    def cojoin[A](fa: F[A]): F[F[A]] = cobind(fa)(fa => fa)
    read more
  • Scalaz Comonad

    Comonad is a Cobind that also has copoint method. Scalaz defines Comonad[F[_]] with copoint abstract method.

    def copoint[A](p: F[A]): A

    Note that when point method of Monad turns A to F[A], copoint method of Comonad turns F[A] to A.

    read more
  • Scalaz Foldable

    Scalaz provides foldable to define all fold operations. It has Foldable[F[_]] trait with two foldMap and foldRight abstract methods:

    def foldMap[A,B](fa: F[A])(f: A  B)(implicit F: Monoid[B]): B
    def foldRight[A, B](fa: F[A], z:  B)(f: (A, => B) => B): B
    read more
  • Scalaz Monad

    Monad is Applicative and Bind. With that we deal with:

    def ap[A,B](fa:  F[A])(f:  F[A  B]): F[B]
    def map[A, B](fa: F[A])(f: A  B): F[B]

    from Apply

    def point[A](a: => A): F[A]
    override def map[A, B](fa: F[A])(f: A => B): F[B] = ap(fa)(point(f))

    from Applicative

    def bind[A, B](fa: F[A])(f: A => F[B]): F[B]
    override def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B] = bind(f)(map(fa))

    from Bind.

    Note that in Applicative, map method is implemented using ap method and in Bind, ap is implemented using bind method. With that, two methods that are left to be implemented for a Monad are point and bind.

    read more
  • Scalaz Plus, PlusEmpty, IsEmpty, ApplicativePlus and MonadPlus

    read more