Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using functions as applicative functors/cartesians

I'm using the cats lib for this.

It's easy enough to combine two lists using their applicative functor instance (or Cartesian, to be precise):

import cats._
import cats.implicits._

(List(23, 4), List(55, 56)).mapN(_ + _)
>> List(78, 79, 59, 60)

However, I can't seem to be able to do the same with two functions:

val strLength: String => Int = _.length

(strLength, strLength).mapN(_ + _)
>> value mapN is not a member of (String => Int, String => Int)

It does work if I do some of the implicit conversions explicitly, and if I create a type alias to give the compiler a hand:

type F[A] = Function1[String, A]
val doubleStrLength = catsSyntaxTuple2Cartesian[F, Int, Int]((strLength, strLength)).mapN(_ + _)

doubleStrLength("hello")
>> 10

Is there an easier way to do this? Seems excessively verbose

Edit: I create a worksheet here if you wanna play with it: https://scastie.scala-lang.org/dcastro/QhnD8gwEQEyfnr14g34d9g/2

like image 748
dcastro Avatar asked Oct 04 '17 10:10

dcastro


People also ask

Could you comfortably explain the difference between a monad and an applicative functor?

Functors apply a function to a wrapped value: Applicatives apply a wrapped function to a wrapped value: Monads apply a function that returns a wrapped value to a wrapped value. Monads have a function >>= (pronounced "bind") to do this.

How are functions functors?

In functional programming, a functor is a design pattern inspired by the definition from category theory, that allows for a generic type to apply a function inside without changing the structure of the generic type. Simple examples of this are Option and collection types.

Is applicative a functor?

Applicative functors are the programming equivalent of lax monoidal functors with tensorial strength in category theory. Applicative functors were introduced in 2008 by Conor McBride and Ross Paterson in their paper Applicative programming with effects.

How many methods should a functor instance have?

Functor in Haskell is a typeclass that provides two methods – fmap and (<$) – for structure-preserving transformations. To implement a Functor instance for a data type, you need to provide a type-specific implementation of fmap – the function we already covered.


1 Answers

This only works if you have partial-unification enabled. The easiest way to do so, is to add the sbt-partial-unification plugin.

If you're on Scala 2.11.9 or newer, you can also simply add the compiler flag:

scalacOptions += "-Ypartial-unification"
like image 134
Luka Jacobowitz Avatar answered Mar 26 '23 08:03

Luka Jacobowitz