在Scala中提升函数值的方法

Aar*_*rup 8 scala

Scala库是否为将给定类型的方法提升为函数值提供了任何支持?

例如,假设我想举起String.length.我可以写

val f: String => Int = _.length
Run Code Online (Sandbox Code Playgroud)

要么

val f = { s: String => s.length }
Run Code Online (Sandbox Code Playgroud)

但是,这种语法并不总是理想的(特别是在较大的表达式中).我想我正在寻找能够实现表达式的东西

Lift[String](_.length)
Lift[Option[Int]].lift(_.filter)
Run Code Online (Sandbox Code Playgroud)

我记得这样的事情:

class Lift[T] {                                                          
   def apply[R](f: T => R): T => R = f

   def lift[A, R](f: (T) => (A) => R): (T, A) => R = 
         f(_)(_) 
   def lift[A1, A2, R](f: (T) => (A1, A2) => R): (T, A1, A2) => R =
         f(_)(_,_)
   // ... etc. ...
}
object Lift {
   def apply[T] = new Lift[T]
}
Run Code Online (Sandbox Code Playgroud)

问题1: 标准库(或任何库)是否提供类似的内容?

问题2:如果没有,是否可以以Option.filter上述方式(而不是如此方式Lift[Option[Int]].lift[Int => Boolean, Option[Int]](_.filter))解除它?如果不在方法上提供类型参数,lift我会收到以下错误:

error: missing parameter type for expanded function ((x$1) => x$1.filter)
       Lift[Option[Int]].lift(_.filter)
                              ^

更新:

显然,我遇到的问题与重载lift方法有关.如果我重命名重载,我可以在Option.filter没有所有额外类型参数的情况下解除.

Aar*_*rup 8

我终于想出了一个我很满意的解决方案.此版本支持简单的语法和单个入口点到API,同时还提供控制在提升函数的形式(即uncurried,部分咖喱,或完全咖喱).

示例:

我将在下面的示例中使用以下类定义:

class Foo {
   def m1: Int = 1
   def m2(i: Int): Int = i
   def m3(i: Int, j: Int): Int = i + j
}
Run Code Online (Sandbox Code Playgroud)

提升的最简单形式是返回方法作为部分应用的功能,相当于调用((_: Foo).method _):

scala> lift[Foo](_.m1)                         // NOTE: trailing _ not required
res0: (Foo) => Int = <function1>

scala> lift[Foo](_.m2 _)                       // NOTE: trailing _ required
res1: (Foo) => (Int) => Int = <function1>

scala> lift[Foo](_.m3 _)
res2: (Foo) => (Int, Int) => Int = <function1> // NOTE: the result is partly curried
Run Code Online (Sandbox Code Playgroud)

通过导入一些implicits,可以请求curried或uncurried表单:

scala> {                        
     | import CurriedLiftables._
     | lift[Foo](_.m3 _)        
     | }
res3: (Foo) => (Int) => (Int) => Int = <function1>

scala> {                          
     | import UncurriedLiftables._
     | lift[Foo](_.m3 _)          
     | }
res4: (Foo, Int, Int) => Int = <function3>
Run Code Online (Sandbox Code Playgroud)

执行:

class Lift[T] {
   def apply[R,F](f: T => R)(implicit e: (T => R) Liftable F): F = e.lift(f)
}
object lift {
   def apply[T] = new Lift[T]
}

class Liftable[From, To](val lift: From => To)

class DefaultLiftables {
   implicit def lift[F]: F Liftable F = new Liftable(identity)
}
object Liftable extends DefaultLiftables

class UncurriedLiftable1 extends DefaultLiftables {
   implicit def lift1[T, A, R]: (T => A => R) Liftable ((T, A) => R) = 
      new Liftable( f => f(_)(_) )
}
class UncurriedLiftable2 extends UncurriedLiftable1 {
   implicit def lift2[T, A1, A2, R]: (T => (A1, A2) => R) Liftable ((T, A1, A2) => R) = 
      new Liftable ( f => f(_)(_,_) )
}
// UncurriedLiftable3, UncurriedLiftable4, ...
object UncurriedLiftables extends UncurriedLiftable2

class CurriedLiftable2 extends DefaultLiftables {
   implicit def lift2[T, A1, A2, R]: (T => (A1, A2) => R) Liftable (T => A1 => A2 => R) =
      new Liftable( f => (x: T) => (a1: A1) => (a2: A2) => f(x)(a1, a2) )
}
// CurriedLiftable3, CurriedLiftable4, ...
object CurriedLiftables extends CurriedLiftable2
Run Code Online (Sandbox Code Playgroud)

我之前的解决方案需要为每个arity提供单独的提升方法:

import Lift._
val f1 = lift0[String](_.length)
val f2 = lift1[Option[Int]](_.filter)
val f3 = lift2[Either[String, Int]](_.fold)
Run Code Online (Sandbox Code Playgroud)

执行:

class Lift0[T] {
   def apply[R](f: T => R): T => R = f
}
class Lift1[T] {
   def apply[A, R](f: (T) => (A) => R): (T, A) => R = 
      f(_)(_) 
}
class Lift2[T] {
   def apply[A1, A2, R](f: (T) => (A1, A2) => R): (T, A1, A2) => R =
      f(_)(_,_)
}
// ... etc. ...

object Lift {
   def lift0[T] = new Lift0[T]
   def lift1[T] = new Lift1[T]
   def lift2[T] = new Lift2[T]
   // ... etc. ...
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*ral 7

有什么问题

(_: String).length
(_: Option[Int]).filter _
Run Code Online (Sandbox Code Playgroud)