lovebetScalaz(陆)- typeclass:Functor-just map

 
Functor是范畴学(Category theory)里的定义。然而不用担心,大家在scala
FP编程里并不供给先精通范畴学知识的。在scalaz里,Functor就是2个司空眼惯的typeclass,具备map
over特性。我的知情中,Functor的主要用途是在FP进度中更新包嵌在容器(高阶类)F[T]兰月素T值。典型事例如:List[String],
Option[Int]等。我们已经介绍过FP与OOP的个中1项非凡不同在于FP会尽量制止中间变量(temp
variables)。FP的变量V是以F[V]那种样式存在的,如:List[Int]里五个Int变量是包嵌在容器List里的。所以FP供给新鲜的艺术来更新变量V,那正是Functor
map over的趣味。scalaz提供了Functor typeclass不但使用户能map
over自定义的高阶类型F[T],并且用户通过提供自定义类型的Functor实例就可避防费使用scalaz
Functor typeclass提供的壹层层组件函数(combinator functions)。

Functor是范畴学(Category theory)里的定义。但是不用担心,我们在scala
FP编制程序里并不需求先通晓范畴学知识的。在scalaz里,Functor正是三个不以为奇的typeclass,具备map
over性格。笔者的理解中,Functor的主要用途是在FP进度中创新包嵌在容器F[T]4月素T值。典型例证如:List[String],
Option[Int]等。我们已经介绍过FP与OOP的当中壹项优异区别在于FP会尽量防止中间变量(temp
variables)。FP的变量V是以F[V]这种样式存在的,如:List[Int]里2个Int变量是包嵌在容器List里的。所以FP须要尤其的措施来更新变量V,这正是Functor
map over的意味。scalaz提供了Functor typeclass不但使用户能map
over自定义的高阶类型F[T],并且用户通过提供自定义类型的Functor实例就足防止费应用scalaz
Functor typeclass提供的一两种组件函数(combinator functions)。

 
scalaz中Functor的trait是那般定义的:scalaz/Functor.scala

scalaz中Functor的trait是如此定义的:scalaz/Functor.scala

1 trait Functor[F[_]] extends InvariantFunctor[F] { self =>
2   ////
3   import Liskov.<~<
4 
5   /** Lift `f` into `F` and apply to `F[A]`. */
6   def map[A, B](fa: F[A])(f: A => B): F[B]
7 
8 ...
1 trait Functor[F[_]] extends InvariantFunctor[F] { self =>2   ////3   import Liskov.<~<4 5   /** Lift `f` into `F` and apply to `F[A]`. */6   def map[A, B](f: A => B): F[B]7 8 ...

别的项目标实例只必要完毕那些抽象函数map就能够动用scalaz
Functor
typeclass的这个注入方法了:scalaz/syntax/FunctorSyntax.scala

别的类型的实例只须求贯彻那么些抽象函数map就足以采取scalaz
Functor
typeclass的这么些注入方法了:scalaz/syntax/FunctorSyntax.scala

 1 final class FunctorOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Functor[F]) extends Ops[F[A]] {
 2   ////
 3   import Leibniz.===
 4   import Liskov.<~<
 5 
 6   final def map[B](f: A => B): F[B] = F.map(self)(f)
 7   final def distribute[G[_], B](f: A => G[B])(implicit D: Distributive[G]): G[F[B]] = D.distribute(self)(f)
 8   final def cosequence[G[_], B](implicit ev: A === G[B], D: Distributive[G]): G[F[B]] = D.distribute(self)(ev(_))
 9   final def cotraverse[G[_], B, C](f: F[B] => C)(implicit ev: A === G[B], D: Distributive[G]): G[C] = D.map(cosequence)(f)
10   final def ∘[B](f: A => B): F[B] = F.map(self)(f)
11   final def strengthL[B](b: B): F[(B, A)] = F.strengthL(b, self)
12   final def strengthR[B](b: B): F[(A, B)] = F.strengthR(self, b)
13   final def fpair: F[(A, A)] = F.fpair(self)
14   final def fproduct[B](f: A => B): F[(A, B)] = F.fproduct(self)(f)
15   final def void: F[Unit] = F.void(self)
16   final def fpoint[G[_]: Applicative]: F[G[A]] = F.map(self)(a => Applicative[G].point(a))
17   final def >|[B](b: => B): F[B] = F.map(self)(_ => b)
18   final def as[B](b: => B): F[B] = F.map(self)(_ => b)
19   final def widen[B](implicit ev: A <~< B): F[B] = F.widen(self)
20   ////
21 }
 1 final class FunctorOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Functor[F]) extends Ops[F[A]] { 2   //// 3   import Leibniz.=== 4   import Liskov.<~< 5  6   final def map[B](f: A => B): F[B] = F.map 7   final def distribute[G[_], B](f: A => G[B])(implicit D: Distributive[G]): G[F[B]] = D.distribute 8   final def cosequence[G[_], B](implicit ev: A === G[B], D: Distributive[G]): G[F[B]] = D.distribute 9   final def cotraverse[G[_], B, C](f: F[B] => C)(implicit ev: A === G[B], D: Distributive[G]): G[C] = D.map(cosequence)10   final def ∘[B](f: A => B): F[B] = F.map11   final def strengthL[B]: F[] = F.strengthL12   final def strengthR[B]: F[] = F.strengthR13   final def fpair: F[] = F.fpair14   final def fproduct[B](f: A => B): F[] = F.fproduct15   final def void: F[Unit] = F.void16   final def fpoint[G[_]: Applicative]: F[G[A]] = F.map(a => Applicative[G].point17   final def >|[B](b: => B): F[B] = F.map(_ => b)18   final def as[B](b: => B): F[B] = F.map(_ => b)19   final def widen[B](implicit ev: A <~< B): F[B] = F.widen20   ////21 }

上述的流入方法中除了map外其余方法的应用场景小编还未曾适合的想法,然而那不会妨碍大家演示它们的用法。Functor必须遵守壹些定律:

如上的注入方法中除了map外其它格局的使用场景作者还未曾确切的想法,然则那不会妨碍大家演示它们的用法。Functor必须遵守一些定律:

1、map(fa)(x => x)
=== fa

1、map(x => x) ===
fa

2、map(map(fa)(f1))(f2) ===
map(fa)(f2 compose f1)

2、map === map(f2
compose f1)

scalaz/Functor.scala

scalaz/Functor.scala

 1   trait FunctorLaw extends InvariantFunctorLaw {
 2     /** The identity function, lifted, is a no-op. map(fa)(x => x*/
 3     def identity[A](fa: F[A])(implicit FA: Equal[F[A]]): Boolean = FA.equal(map(fa)(x => x), fa)
 4 
 5     /**
 6      * A series of maps may be freely rewritten as a single map on a
 7      * composed function.
 8      */
 9     def composite[A, B, C](fa: F[A], f1: A => B, f2: B => C)(implicit FC: Equal[F[C]]): Boolean = FC.equal(map(map(fa)(f1))(f2), map(fa)(f2 compose f1))
10   }
 1   trait FunctorLaw extends InvariantFunctorLaw { 2     /** The identity function, lifted, is a no-op. map(x => x*/ 3     def identity[A](implicit FA: Equal[F[A]]): Boolean = FA.equal(x => x), fa) 4  5     /** 6      * A series of maps may be freely rewritten as a single map on a 7      * composed function. 8      */ 9     def composite[A, B, C](fa: F[A], f1: A => B, f2: B => C)(implicit FC: Equal[F[C]]): Boolean = FC.equal(map, map(f2 compose f1))10   }

作者们能够用List来表达:map(fa)(x
=> x) === fa

笔者们能够用List来验证:map(x
=> x) === fa

1 scala> List(1,2,3).map(x => x) assert_=== List(1,2,3)
2 
3 scala> List(1,2,3).map(identity) assert_=== List(1,2)
4 java.lang.RuntimeException: [1,2,3] ≠ [1,2]
5   at scala.sys.package$.error(package.scala:27)
6   at scalaz.syntax.EqualOps.assert_$eq$eq$eq(EqualSyntax.scala:16)
7   ... 43 elided
1 scala> List(1,2,3).map(x => x) assert_=== List(1,2,3)2 3 scala> List(1,2,3).map assert_=== List(1,2)4 java.lang.RuntimeException: [1,2,3] ≠ [1,2]5   at scala.sys.package$.error(package.scala:27)6   at scalaz.syntax.EqualOps.assert_$eq$eq$eq(EqualSyntax.scala:16)7   ... 43 elided

map(map(fa)(f1))(f2)
=== map(fa)(f2 compose f1)

map === map(f2 compose
f1)

1 scala> Functor[List].map(List(1,2,3).map(i => i + 1))(i2 => i2 * 3) assert_=== List(1,2,3).map(((i2:Int) => i2 * 3) compose ((i:Int) => i + 1))
2 
3 scala> Functor[List].map(List(1,2,3).map(i => i + 1))(i2 => i2 * 3) assert_=== List(1,2,3).map(((i:Int) => i + 1) compose ((i2:Int) => i2 * 3))
4 java.lang.RuntimeException: [6,9,12] ≠ [4,7,10]
5   at scala.sys.package$.error(package.scala:27)
6   at scalaz.syntax.EqualOps.assert_$eq$eq$eq(EqualSyntax.scala:16)
7   ... 43 elided
1 scala> Functor[List].map(List(1,2,3).map(i => i + 1))(i2 => i2 * 3) assert_=== List(1,2,3).map => i2 * 3) compose  => i + 1))2 3 scala> Functor[List].map(List(1,2,3).map(i => i + 1))(i2 => i2 * 3) assert_=== List(1,2,3).map => i + 1) compose  => i2 * 3))4 java.lang.RuntimeException: [6,9,12] ≠ [4,7,10]5   at scala.sys.package$.error(package.scala:27)6   at scalaz.syntax.EqualOps.assert_$eq$eq$eq(EqualSyntax.scala:16)7   ... 43 elided

瞩目:compose对f1,f二的行使是互换的。

注意:compose对f一,f二的应用是沟通的。

本着大家自定义的类别,我们要是达成map函数就足以拿走这一个类型的Functor实例。壹旦达成了这一个项指标Functor实例,大家就足以应用上述scalaz提供的有着Functor组件函数了。

本着大家自定义的档次,我们假使落成map函数就足以拿走这一个类其余Functor实例。壹旦完结了那一个类型的Functor实例,大家就足以应用上述scalaz提供的富有Functor组件函数了。

我们先试着创立三个项目然后推算它的Functor实例:

咱俩先试着创设1个连串然后推算它的Functor实例:

1 case class Item3[A](i1: A, i2: A, i3: A)
2 val item3Functor = new Functor[Item3] {
3     def map[A,B](ia: Item3[A])(f: A => B): Item3[B] = Item3(f(ia.i1),f(ia.i2),f(ia.i3))
4 }                                                 //> item3Functor  : scalaz.Functor[scalaz.functor.Item3] = scalaz.functor$$anonf
5                                                   //| un$main$1$$anon$1@5e265ba4
1 case class Item3[A](i1: A, i2: A, i3: A)2 val item3Functor = new Functor[Item3] {3     def map[A,B](ia: Item3[A])(f: A => B): Item3[B] = Item3,f,f4 }                                                 //> item3Functor  : scalaz.Functor[scalaz.functor.Item3] = scalaz.functor$$anonf5                                                   //| un$main$1$$anon$1@5e265ba4

scalaz同时在scalaz-tests下提供了1套scalacheck测试库。大家能够对Item三的Functor实例实行测试:

scalaz同时在scalaz-tests下提供了1套scalacheck测试库。大家能够对Item三的Functor实例举办测试:

1 scala> functor.laws[Item3].check
2 <console>:27: error: could not find implicit value for parameter af: org.scalacheck.Arbitrary[Item3[Int]]
3               functor.laws[Item3].check
4                           ^
1 scala> functor.laws[Item3].check2 <console>:27: error: could not find implicit value for parameter af: org.scalacheck.Arbitrary[Item3[Int]]3               functor.laws[Item3].check4                           ^

因而看来大家需求提供自定义类型Item3的私下发生器(Generator):

如上所述大家供给提供自定义类型Item三的妄动发生器(Generator):

 1 scala> implicit def item3Arbi[A](implicit a: Arbitrary[A]): Arbitrary[Item3[A]] = Arbitrary {
 2      | def genItem3: Gen[Item3[A]]  = for {
 3      | b <- Arbitrary.arbitrary[A]
 4      | c <- Arbitrary.arbitrary[A]
 5      | d <- Arbitrary.arbitrary[A]
 6      | } yield Item3(b,c,d)
 7      | genItem3
 8      | }
 9 item3Arbi: [A](implicit a: org.scalacheck.Arbitrary[A])org.scalacheck.Arbitrary[Item3[A]]
10 
11 scala> functor.laws[Item3].check
12 + functor.invariantFunctor.identity: OK, passed 100 tests.
13 + functor.invariantFunctor.composite: OK, passed 100 tests.
14 + functor.identity: OK, passed 100 tests.
15 + functor.composite: OK, passed 100 tests.
 1 scala> implicit def item3Arbi[A](implicit a: Arbitrary[A]): Arbitrary[Item3[A]] = Arbitrary { 2      | def genItem3: Gen[Item3[A]]  = for { 3      | b <- Arbitrary.arbitrary[A] 4      | c <- Arbitrary.arbitrary[A] 5      | d <- Arbitrary.arbitrary[A] 6      | } yield Item3 7      | genItem3 8      | } 9 item3Arbi: [A](implicit a: org.scalacheck.Arbitrary[A])org.scalacheck.Arbitrary[Item3[A]]10 11 scala> functor.laws[Item3].check12 + functor.invariantFunctor.identity: OK, passed 100 tests.13 + functor.invariantFunctor.composite: OK, passed 100 tests.14 + functor.identity: OK, passed 100 tests.15 + functor.composite: OK, passed 100 tests.

Item3的Functor实例是创造的。

Item三的Functor实例是理所当然的。

实际上map就是(A =>
B) => (F[A] => F[B]),就是把(A => B)升格(lift)成(F[A]
=> F[B]):

实际上map就是(A =>
B) => (F[A] => F[B]),就是把(A => B)升格成(F[A] =>
F[B]):

 1 case class Item3[A](i1: A, i2: A, i3: A)
 2 implicit val item3Functor = new Functor[Item3] {
 3     def map[A,B](ia: Item3[A])(f: A => B): Item3[B] = Item3(f(ia.i1),f(ia.i2),f(ia.i3))
 4 }                                                 //> item3Functor  : scalaz.Functor[scalaz.functor.Item3] = scalaz.functor$$anonf
 5                                                   //| un$main$1$$anon$1@5e265ba4
 6 val F = Functor[Item3]                            //> F  : scalaz.Functor[scalaz.functor.Item3] = scalaz.functor$$anonfun$main$1$$
 7                                                   //| anon$1@5e265ba4
 8 F.map(Item3("Morning","Noon","Night"))(_.length)  //> res0: scalaz.functor.Item3[Int] = Item3(7,4,5)
 9 F.apply(Item3("Morning","Noon","Night"))(_.length)//> res1: scalaz.functor.Item3[Int] = Item3(7,4,5)
10 F(Item3("Morning","Noon","Night"))(_.length)      //> res2: scalaz.functor.Item3[Int] = Item3(7,4,5)
11 F.lift((s: String) => s.length)(Item3("Morning","Noon","Night"))
12                                                   //> res3: scalaz.functor.Item3[Int] = Item3(7,4,5)
 1 case class Item3[A](i1: A, i2: A, i3: A) 2 implicit val item3Functor = new Functor[Item3] { 3     def map[A,B](ia: Item3[A])(f: A => B): Item3[B] = Item3,f,f 4 }                                                 //> item3Functor  : scalaz.Functor[scalaz.functor.Item3] = scalaz.functor$$anonf 5                                                   //| un$main$1$$anon$1@5e265ba4 6 val F = Functor[Item3]                            //> F  : scalaz.Functor[scalaz.functor.Item3] = scalaz.functor$$anonfun$main$1$$ 7                                                   //| anon$1@5e265ba4 8 F.map(Item3("Morning","Noon","Night"))  //> res0: scalaz.functor.Item3[Int] = Item3 9 F.apply(Item3("Morning","Noon","Night"))//> res1: scalaz.functor.Item3[Int] = Item310 F(Item3("Morning","Noon","Night"))      //> res2: scalaz.functor.Item3[Int] = Item311 F.lift((s: String) => s.length)(Item3("Morning","Noon","Night"))12                                                   //> res3: scalaz.functor.Item3[Int] = Item3

纵然函数升格(function
lifting (A => B) => (F[A] =>
F[B])是Functor的关键功用,但大家说过:1旦能够获得Item三类型的Functor实例咱们就能免费使用全数的流入方法:

就算如此函数升格(function
lifting (A => B) => (F[A] =>
F[B])是Functor的首要效能,但大家说过:壹旦能够获得Item3类型的Functor实例大家就能免费使用具有的注入方法:

scalaz提供了Function壹的Functor实例。Function1Functor的map正是 andThen 也就是操作方调换的compose:

scalaz提供了Function壹的Functor实例。Function壹Functor的map便是 andThen 也正是操作方调换的compose:

 1 scala> (((_: Int) + 1) map((k: Int) => k * 3))(2)
 2 res20: Int = 9
 3 
 4 scala> (((_: Int) + 1) map((_: Int) * 3))(2)
 5 res21: Int = 9
 6 
 7 scala> (((_: Int) + 1) andThen ((_: Int) * 3))(2)
 8 res22: Int = 9
 9 
10 scala> (((_: Int) * 3) compose ((_: Int) + 1))(2)
11 res23: Int = 9
 1 scala>  + 1) map => k * 3))(2) 2 res20: Int = 9 3  4 scala>  + 1) map * 3))(2) 5 res21: Int = 9 6  7 scala>  + 1) andThen  * 3))(2) 8 res22: Int = 9 9 10 scala>  * 3) compose  + 1))(2)11 res23: Int = 9

大家也能够对Functor举行compose:

我们也足以对Functor实行compose:

1 scala> val f = Functor[List] compose Functor[Item3]
2 f: scalaz.Functor[[α]List[Item3[α]]] = scalaz.Functor$$anon$1@647ce8fd
3 
4 scala> val item3 = Item3("Morning","Noon","Night")
5 item3: Item3[String] = Item3(Morning,Noon,Night)
6 
7 scala> f.map(List(item3,item3))(_.length)
8 res25: List[Item3[Int]] = List(Item3(7,4,5), Item3(7,4,5))
1 scala> val f = Functor[List] compose Functor[Item3]2 f: scalaz.Functor[[α]List[Item3[α]]] = scalaz.Functor$$anon$1@647ce8fd3 4 scala> val item3 = Item3("Morning","Noon","Night")5 item3: Item3[String] = Item3(Morning,Noon,Night)6 7 scala> f.map(List(item3,item3))8 res25: List[Item3[Int]] = List(Item3(7,4,5), Item3(7,4,5))

扭曲操作:

扭转操作:

1 scala> val f1 = Functor[Item3] compose Functor[List]
2 f1: scalaz.Functor[[α]Item3[List[α]]] = scalaz.Functor$$anon$1@5b6a0166
3 
4 scala> f1.map(Item3(List("1"),List("22"),List("333")))(_.length)
5 res26: Item3[List[Int]] = Item3(List(1),List(2),List(3))
1 scala> val f1 = Functor[Item3] compose Functor[List]2 f1: scalaz.Functor[[α]Item3[List[α]]] = scalaz.Functor$$anon$1@5b6a01663 4 scala> f1.map(Item3(List("1"),List("22"),List("333")))5 res26: Item3[List[Int]] = Item3(List(1),List(2),List(3))

大家再试着在Item三类型上调用那个免费的注入方法:

咱俩再试着在Item叁类型上调用这些免费的流入方法:

 1 scala> item3.fpair
 2 res28: Item3[(String, String)] = Item3((Morning,Morning),(Noon,Noon),(Night,Night))
 3 
 4 scala> item3.strengthL(3)
 5 res29: Item3[(Int, String)] = Item3((3,Morning),(3,Noon),(3,Night))
 6 
 7 scala> item3.strengthR(3)
 8 res30: Item3[(String, Int)] = Item3((Morning,3),(Noon,3),(Night,3))
 9 
10 scala> item3.fproduct(_.length)
11 res31: Item3[(String, Int)] = Item3((Morning,7),(Noon,4),(Night,5))
12 
13 scala> item3 as "Day"
14 res32: Item3[String] = Item3(Day,Day,Day)
15 
16 scala> item3 >| "Day"
17 res33: Item3[String] = Item3(Day,Day,Day)
18 
19 scala> item3.void
20 res34: Item3[Unit] = Item3((),(),())
 1 scala> item3.fpair 2 res28: Item3[(String, String)] = Item3((Morning,Morning),(Noon,Noon),(Night,Night)) 3  4 scala> item3.strengthL(3) 5 res29: Item3[(Int, String)] = Item3((3,Morning),(3,Noon),(3,Night)) 6  7 scala> item3.strengthR(3) 8 res30: Item3[(String, Int)] = Item3((Morning,3),(Noon,3),(Night,3)) 9 10 scala> item3.fproduct11 res31: Item3[(String, Int)] = Item3((Morning,7),(Noon,4),(Night,5))12 13 scala> item3 as "Day"14 res32: Item3[String] = Item3(Day,Day,Day)15 16 scala> item3 >| "Day"17 res33: Item3[String] = Item3(Day,Day,Day)18 19 scala> item3.void20 res34: Item3[Unit] = Item3

本身未来还未有想到这么些函数的切实用处。不过从运算结果来看,用那么些函数来产生局地数据模型用在玩耍或许测试的模仿(simulation)倒是大概的。

自作者未来还未有想到这几个函数的现实用处。但是从运算结果来看,用那些函数来发出一些数据模型用在玩耍大概测试的东施效颦(simulation)倒是也许的。

scalaz提供了成都百货上千现成的Functor实例。我们先看看壹些归纳直接的实例:

scalaz提供了众多现成的Functor实例。大家先看看一些不难直接的实例:

 1 scala> Functor[List].map(List(1,2,3))(_ + 3)
 2 res35: List[Int] = List(4, 5, 6)
 3 
 4 scala> Functor[Option].map(Some(3))(_ + 3)
 5 res36: Option[Int] = Some(6)
 6 
 7 scala> Functor[java.util.concurrent.Callable]
 8 res37: scalaz.Functor[java.util.concurrent.Callable] = scalaz.std.java.util.concurrent.CallableInstances$$anon$1@4176ab89
 9 
10 scala> Functor[Stream]
11 res38: scalaz.Functor[Stream] = scalaz.std.StreamInstances$$anon$1@4f5374b9
12 
13 scala> Functor[Vector]
14 res39: scalaz.Functor[Vector] = scalaz.std.IndexedSeqSubInstances$$anon$1@4367920a
 1 scala> Functor[List].map(List(1,2,3))(_ + 3) 2 res35: List[Int] = List(4, 5, 6) 3  4 scala> Functor[Option].map(Some(3))(_ + 3) 5 res36: Option[Int] = Some(6) 6  7 scala> Functor[java.util.concurrent.Callable] 8 res37: scalaz.Functor[java.util.concurrent.Callable] = scalaz.std.java.util.concurrent.CallableInstances$$anon$1@4176ab89 9 10 scala> Functor[Stream]11 res38: scalaz.Functor[Stream] = scalaz.std.StreamInstances$$anon$1@4f5374b912 13 scala> Functor[Vector]14 res39: scalaz.Functor[Vector] = scalaz.std.IndexedSeqSubInstances$$anon$1@4367920a

对那一个四个档次变量的花色我们能够使用部分使用格局:即type
lambda来代表。2个名列三甲的档次:Either[E,A],大家能够把Left[E]定位下来:
Either[String, A],大家得以用type lambda来那样表述:

对那多少个三个品类变量的种类大家得以接纳局地行使格局:即type
lambda来表示。3个榜首的品类:Either[E,A],大家得以把Left[E]固定下来:
Either[String, A],大家能够用type lambda来如此表述:

1 scala> Functor[({type l[x] = Either[String,x]})#l].map(Right(3))(_ + 3)
2 res41: scala.util.Either[String,Int] = Right(6)
1 scala> Functor[({type l[x] = Either[String,x]})#l].map(Right(3))(_ + 3)2 res41: scala.util.Either[String,Int] = Right(6)

这么笔者得以对Either类型实行map操作了。

如此那般笔者可以对Either类型进行map操作了。

函数类型的Functor是针对重返类型的:

函数类型的Functor是针对性重返类型的:

1 scala> Functor[({type l[x] = String => x})#l].map((s: String) => s + "!")(_.length)("Hello")
2 res53: Int = 6
3 
4 scala> Functor[({type l[x] = (String,Int) => x})#l].map((s: String, i: Int) => s.length + i)(_ * 10)("Hello",5)
5 res54: Int = 100
6 
7 scala> Functor[({type l[x] = (String,Int,Boolean) => x})#l].map((s: String,i: Int, b: Boolean)=> s + i.toString + b.toString)(_.toUpperCase)("Hello",3,true)
8 res56: String = HELLO3TRUE
1 scala> Functor[({type l[x] = String => x})#l].map((s: String) => s + "!")("Hello")2 res53: Int = 63 4 scala> Functor[({type l[x] = (String,Int) => x})#l].map((s: String, i: Int) => s.length + i)(_ * 10)("Hello",5)5 res54: Int = 1006 7 scala> Functor[({type l[x] = (String,Int,Boolean) => x})#l].map((s: String,i: Int, b: Boolean)=> s + i.toString + b.toString)(_.toUpperCase)("Hello",3,true)8 res56: String = HELLO3TRUE

tuple类型的Functor是针对最终1个因素类型的: 

tuple类型的Functor是针对性最后三个成分类型的:

 

1 cala> Functor[({type l[x] = })#l].map(("a",1))(_ + 2)2 res57: (String, Int) = (a,3)3 4 scala> Functor[({type l[x] = (String,Int,x)})#l].map(("a",1,"b"))(_.toUpperCase)5 res58: (String, Int, String) = (a,1,B)6 7 scala> Functor[({type l[x] = (String,Int,Boolean,x)})#l].map(("a",1,true,Item3("a","b","c")))(i => i.map(_.toUpperCase))8 res62: (String, Int, Boolean, Item3[String]) = (a,1,true,Item3
1 cala> Functor[({type l[x] = (String,x)})#l].map(("a",1))(_ + 2)
2 res57: (String, Int) = (a,3)
3 
4 scala> Functor[({type l[x] = (String,Int,x)})#l].map(("a",1,"b"))(_.toUpperCase)
5 res58: (String, Int, String) = (a,1,B)
6 
7 scala> Functor[({type l[x] = (String,Int,Boolean,x)})#l].map(("a",1,true,Item3("a","b","c")))(i => i.map(_.toUpperCase))
8 res62: (String, Int, Boolean, Item3[String]) = (a,1,true,Item3(A,B,C))