Applicative in Scala Cats

I always heard about Applicative, but I don’t know what really is. So, I write this post to understand myself. Below picture is from adit.io and example is from mastering advanced Scala.

What is Applicative?

During programming, you will encounter this situation.

```def getSome(a: Int): Option[Int] = Some(a)

def getNone(a: Int): Option[Int] = None

def add(a: Int, b: Int): Int = a + b

val aOpt = getSome(1)

val bOpt = getSome(2)

```

And I want to get aOpt and bOpt add together using add. But aOpt and bOpt is Option. So, the usual way to do is by using flatMap and map like this.

```
aOpt.flatMap(a => bOpt.map(b => add(a, b)))
// res0: Option[Int] = Some(3)

```

In here, we can use Applicative.

What is Applicative?

I don’t know that my understanding of Applicative is right. But I think, Applicative is

In here there is a wrapped value: 2. And there is a wrapped function. We unwrapped both function and value, and process some operation and wrap it again! And we can also adapt these things to above example.

```
import cats.instances.option._
import cats.Applicative

// res1: Option[Int] = Some(3)

```

We can also use like this.

```
import cats.syntax.all._

```

```
@typeclass trait Monad[F[_]] extends FlatMap[F] with Applicative[F] {
override def map[A, B](fa: F[A])(f: A => B): F[B] =
flatMap(fa)(a => pure(f(a)))
}

```

As you can see, Monad extends Applicative. And Applicative extends Functor. So, hierarchy is ‘Monad <: Applicative <: Functor’

Tip – Traverse

While studying with Mastering Advanced Scala, there are some tips in Applicative, So, I write down this. If there is a List[Int] and want all element adapt ‘getSome’ function, for example

```
val ints = List(1,2,3,4,5)
ints.map(getSome)
// res3: List[Option[Int]] = List(Some(1), Some(2), Some(3), Some(4), Some(5))
```

The result is ‘List(Some(1), Some(2), Some(3), Some(4), Some(5))’ and if you want to make ‘Some(List(1, 2, 3, 4, 5))’ you can use Traverse like this.

```
import cats.Traverse
import cats.instances.list._
import cats.instances.option._

Traverse[List].traverse(ints)(getSome)

// res4: Option[List[Int]] = Some(List(1, 2, 3, 4, 5))

```

This will return None if any element return None

```
def getSomeOrNone(a: Int): Option[Int] =
if(a % 2 == 0) Some(a)
else None

Traverse[List].traverse(ints)(getSomeOrNone)

// res5: Option[List[Int]] = None

```

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def getSome(a: Int): Option[Int] = Some(a) def getNone(a: Int): Option[Int] = None def add(a: Int, b: Int): Int = a + b val aOpt = getSome(1) val bOpt = getSome(2) aOpt.flatMap(a => bOpt.map(b => add(a, b))) import cats.instances.option._ import cats.Applicative Applicative[Option].map2(aOpt, bOpt)(add) import cats.syntax.all._ (aOpt |@| bOpt).map(add) val ints = List(1,2,3,4,5) ints.map(getSome) import cats.Traverse import cats.instances.list._ import cats.instances.option._ Traverse[List].traverse(ints)(getSome) def getSomeOrNone(a: Int): Option[Int] = if(a % 2 == 0) Some(a) else None Traverse[List].traverse(ints)(getSomeOrNone)

Writer Monad is stack something while operating.

Hm… I think it’s not a very well summarized sentence. If any reader has nicer summarized one, comment me. Below example is from mastering advanced Scala.

Example

type Writer id defined in cats like this.

```type Writer[L, V] = WriterT[Id, L, V]
```

As you can see, Writer Monad accept type L & V. type L is a type which stacks something. and V is a result(return) value. Id position in WirterT is something wraps the result. Here is Id so, never mind.

You can simply use Writer Monad like.

```import cats.data.Writer

def greetW(name: String, logged: Boolean): Writer[List[String], String] =
Writer(List("Compose a greeting"), {
val userName = if(logged) name else "User"
})

def isLoggedW(name: String): Writer[List[String], Boolean] =
Writer(List("Checking if user is logged in"), name.length == 3)

import cats.instances.list._

val name = "Joe"

val resultW: Writer[List[String], String] = for {
logged <- isLoggedW(name)
greeting <- greetW(name, logged)
} yield greeting
val (log, result) = resultW.run

// log: List[String] = List(Checking if user is logged in, Compose a greeting)
//result: String = Hello Joe
```

Yes. It’s very simple. It just stacks some strings in L(List[String]) and returns value in V(String). I think it’s very useful.

How is it works?

Of course, you can have curious that how’s is it works? As you can see in ‘for comprehension’, there is nowhere to pass List[String]. But after finishing for comprehension, it splits out log: List[String] and result: String.

Let’s remind this.

For comprehension is syntactic sugar of flatMap.

You can also write like this.

```val flatMapResult: Writer[List[String], String] =
isLoggedW(name).flatMap(logged => greetW(name, logged))
```

It’s the same result of for comprehension statements.

Ok. It’s flatMap. Then, Let’s deep dive to WriterT.flatMap

```final case class WriterT[F[_], L, V](run: F[(L, V)]) {
...
def flatMap[U](f: V => WriterT[F, L, U])(implicit flatMapF: FlatMap[F], semigroupL: Semigroup[L]): WriterT[F, L, U] =
WriterT {
flatMapF.flatMap(run) { lv =>
flatMapF.map(f(lv._2).run) { lv2 =>
(semigroupL.combine(lv._1, lv2._1), lv2._2)
}
}
}
...
}
```

In this code, never mind another but line:7. In Line 7, they combine two List[String]. Stacking is occurred in here. WriterT is different from others. value run’s type is F[(L, V)] and it does flatMap with it. So we can stack List[String]

WriterT Example

WriterT is Monad Transformer of Writer. So, you can use like this.

```import cats.data.WriterT
import cats.instances.future._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

def greetWFuture(name: String, logged: Boolean): WriterT[Future, List[String], String] =
WriterT(Future(List("Compose a greeting"), {
val userName = if(logged) name else "User"
}))

def isLoggedWFuture(name: String): WriterT[Future, List[String], Boolean] =
WriterT(Future(List("Checking if user is logged in"), name.length == 3))

val resultWFuture: WriterT[Future, List[String], String] = for {
logged <- isLoggedWFuture(name)
greeting <- greetWFuture(name, logged)
} yield greeting

val futureResult: Future[(List[String], String)] = resultWFuture.run

import scala.concurrent.Await
import scala.concurrent.duration._

val (logAwait, resultAwait) = Await.result(futureResult, 2 second)
```

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import cats.data.Writer def greetW(name: String, logged: Boolean): Writer[List[String], String] = Writer(List("Compose a greeting"), { val userName = if(logged) name else "User" s"Hello \$userName" }) def isLoggedW(name: String): Writer[List[String], Boolean] = Writer(List("Checking if user is logged in"), name.length == 3) import cats.instances.list._ val name = "Joe" val resultW: Writer[List[String], String] = for { logged <- isLoggedW(name) greeting <- greetW(name, logged) } yield greeting val (log, result) = resultW.run val flatMapResult: Writer[List[String], String] = isLoggedW(name).flatMap(logged => greetW(name, logged)) import cats.data.WriterT import cats.instances.future._ import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global def greetWFuture(name: String, logged: Boolean): WriterT[Future, List[String], String] = WriterT(Future(List("Compose a greeting"), { val userName = if(logged) name else "User" s"Hello \$userName" })) def isLoggedWFuture(name: String): WriterT[Future, List[String], Boolean] = WriterT(Future(List("Checking if user is logged in"), name.length == 3)) val resultWFuture: WriterT[Future, List[String], String] = for { logged <- isLoggedWFuture(name) greeting <- greetWFuture(name, logged) } yield greeting val futureResult: Future[(List[String], String)] = resultWFuture.run import scala.concurrent.Await import scala.concurrent.duration._ val (logAwait, resultAwait) = Await.result(futureResult, 2 second)

Below example is from mastering advanced Scala

At first, I thought Reader Monad is used to read something. It’s true. But another definition is effective.

Yes. I think it’s a better definition. Reader Monad object is like this.

```object Reader {
def apply[A, B](f: A => B): Reader[A, B] = ReaderT[Id, A, B](f)
}
```

Anonymous function f is the function we want to run. And ‘A’ will be injected later when we call like this.

```

```

ATypeInstance is an instance which type is A. And as you can see, is passed when you call run function.

Example

First, Let’s suppose some Service like this.

```trait AuthService {
def isLogged(name: String): Boolean
}

class AuthServiceChar3 extends AuthService{
override def isLogged(name: String): Boolean = name.length == 3
}

class AuthServiceChar5 extends AuthService{
override def isLogged(name: String): Boolean = name.length == 5
}

trait UserService {
def greet(name: String, isLogged: Boolean): String
}

class UserServiceDefaultUser extends UserService{
override def greet(name: String, isLogged: Boolean): String = {
val actualName = if(isLogged) name else "User"

s"Hello \$actualName"
}
}

class UserServiceNoDefault extends UserService{
override def greet(name: String, isLogged: Boolean): String = {
if(isLogged) s"Hello \$name" else "No authorization"
}
}

case class Environment(userName: String, userService: UserService, authService: AuthService)
```

AuthService has a simple auth logic and actualized class is AuthServiceChar3 and AuthServiceChar5. And UserSertivce has a simple greet logic and actualized class is UserServiceDefaultUser and UserServiceNoDefault.

And Environment is case class which needs a user name, UserService, and AuthService. And here are some Reader Monad use environment and return something.

```import cats.data.Reader

}

}
// In Intellij if you use auto complete, Kleisli[Id, Environment, String] will be written. I will write post later
val resultR: Reader[Environment, String] = for {
logged <- isLoggedUser
greeting <- greetUser(logged)
} yield greeting
```

Value resultR is a Reader Monad get logged value from isLoggedUser and return greet String.

In here yet, we didn’t pass Environment instance. Now, let’s pass Environment.

```
val environment1 = Environment("Joe", new UserServiceDefaultUser, new AuthServiceChar3)

println(resultR.run(environment1))  // print Hello Joe

val environment2 = Environment("Joe", new UserServiceNoDefault, new AuthServiceChar5)

println(resultR.run(environment2))  // print No authorization

```

As you can see, we inject environment when we call run function. And when we inject different instance, print different output.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 trait AuthService { def isLogged(name: String): Boolean } class AuthServiceChar3 extends AuthService{ override def isLogged(name: String): Boolean = name.length == 3 } class AuthServiceChar5 extends AuthService{ override def isLogged(name: String): Boolean = name.length == 5 } trait UserService { def greet(name: String, isLogged: Boolean): String } class UserServiceDefaultUser extends UserService{ override def greet(name: String, isLogged: Boolean): String = { val actualName = if(isLogged) name else "User" s"Hello \$actualName" } } class UserServiceNoDefault extends UserService{ override def greet(name: String, isLogged: Boolean): String = { if(isLogged) s"Hello \$name" else "No authorization" } } case class Environment(userName: String, userService: UserService, authService: AuthService) import cats.data.Reader def isLoggedUser: Reader[Environment, Boolean] = Reader[Environment, Boolean] { env => env.authService.isLogged(env.userName) } def greetUser(logged: Boolean): Reader[Environment, String] = Reader[Environment, String] { env => env.userService.greet(env.userName, logged) } // In Intellij if you use auto complete, Kleisli[Id, Environment, String] will be written. I will write post later val resultR: Reader[Environment, String] = for { logged <- isLoggedUser greeting <- greetUser(logged) } yield greeting val environment1 = Environment("Joe", new UserServiceDefaultUser, new AuthServiceChar3) println(resultR.run(environment1)) val environment2 = Environment("Joe", new UserServiceNoDefault, new AuthServiceChar5) println(resultR.run(environment2))