Start United States USA — software Diving into Scala Cats – Functors

Diving into Scala Cats – Functors

288
0
TEILEN

The Functor type class in Cats provides a base class for mappable types that allow us to write generic code for Futures, Options, Lists and more.
Join the DZone community and get the full member experience. This blog was first published on Knoldus Blogs If you’re new to the concept of type classes I suggest you read my other article explaining them. The Cats library makes extensive use of type classes and a basic understanding is a prerequisite for this article. In previous articles, we talked about Semigroups and Monoids, which are abstractions that let us combine values of the same type together. In this post, we’ll take a look at Functors, which allow us to operate on values inside containers without having to know any of the implementation details of the containers themselves. In terms of functional programming, a Functor is simply something that can be mapped over, or we can call it a Mappable instead. It is basically an abstraction that allows us torepresent sequences of operations within a context such as Collections, Futures and Options. Cats’ Functor type class provides a base class for mappable types that allow us to write generic code for Futures, Options, Lists and more. Standard Scala has no base type/trait to represent following generically: However using cats’ Functor type class we can write a generic method like this: A Functor is a type F[A] with a map operation (A => B) => F[B]: It accepts the initial F[A] as a parameter alongside the transformation function.

Continue reading...