<!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG-spv-->{"id":1830053,"date":"2021-01-31T21:48:00","date_gmt":"2021-01-31T19:48:00","guid":{"rendered":"http:\/\/nhub.news\/?p=1830053"},"modified":"2021-02-01T00:03:07","modified_gmt":"2021-01-31T22:03:07","slug":"diving-into-scala-cats-functors","status":"publish","type":"post","link":"http:\/\/nhub.news\/de\/2021\/01\/diving-into-scala-cats-functors\/","title":{"rendered":"Diving into Scala Cats \u2013 Functors"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>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.<\/b><br \/>\nJoin the DZone community and get the full member experience. This blog was first published on Knoldus Blogs If you\u2019re 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\u2019ll 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\u2019 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\u2019 Functor type class we can write a generic method like this: A Functor is a type F[A] with a map operation (A =&gt; B) =&gt; F[B]: It accepts the initial F[A] as a parameter alongside the transformation function. Let\u2019s rewrite calcBudget using Functor: The Functor[F].map(&#8230;) method is parameterized based on a type of F[_]. Here F[_] means any type constructor (type wrapping another type) that has a map method e.g. Option, List, Future etc. The implicit parameter ev: Functor[F] means that we must have a type class implementation in place that allows us to treat F as a cats Functor. If we are creating our own Functors, then the Functor must follow these laws. The Functor type class is cats. Functor. Cats library provide us with inbuilt Functors and their implementation for predefined types like Option, List, Future etc. So far so good, let\u2019s try to use type class implementation of Functor for List: Now let\u2019s try to use type class implementation of Functor for Option: Now let\u2019s try to use type class implementation of Functor for Future: Whenever we summon a Functor for Future, the compiler willlocate futureFunctor by implicit resolution and recursively search for anExecutionContext at the call site. This is what the expansion might looklike: By using the Functor type class we can abstract over anything that can be mapped. We\u2019re not restricted to the types in the standard library, we could add a map method to our own types. Cats allows us to do this by using the concept of syntax or extension methods. This concept is implemented using implicit classes and allows us to write order.map(&#8230;) instead of Functor[F].map(order)(&#8230;). We can also drop the implicit parameter by specifying that type F[_] is a Functor: But we can also build a higher order function which can deal with any type and perform any mapping operation: Of course this is a contrived example as withFunctor adds no value over a simple inline call but it illustrates the point that A, B &amp; F can be anything so long as the caller of the method: In this article, we\u2019ve looked at Functors provided by Scala Cats library. Functors represent sequencing behaviors. We talked about why do we need Functors and how can we define them. For a type class to be a Functor it must obey two laws: Firstly it should be possible to compose two map calls. Secondly mapping with the identity function should have no effect. We saw that we can write Functor type class implementation for all the mappable types present in Scala ecosystem like Futures, Options, Lists, etc. We\u2019re not restricted to the types in the standard library, we can also add map method to our own types by using the concept of syntax or extension methods. We\u2019ll learn about more core concepts of Cats in our upcoming article. Stay tuned!!! You can download the learning template Scala Cats &#8211; Functors demonstrating the code implementations provided in this article. The best two Scala Cats resources I know are here: Opinions expressed by DZone contributors are their own.<\/p>\n<script>jQuery(function(){jQuery(\".vc_icon_element-icon\").css(\"top\", \"0px\");});<\/script><script>jQuery(function(){jQuery(\"#td_post_ranks\").css(\"height\", \"10px\");});<\/script><script>jQuery(function(){jQuery(\".td-post-content\").find(\"p\").find(\"img\").hide();});<\/script>","protected":false},"excerpt":{"rendered":"<p>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\u2019re new to the concept of type classes I suggest [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1830052,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1830053"}],"collection":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/comments?post=1830053"}],"version-history":[{"count":1,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1830053\/revisions"}],"predecessor-version":[{"id":1830054,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1830053\/revisions\/1830054"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media\/1830052"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media?parent=1830053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/categories?post=1830053"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/tags?post=1830053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}