【问题标题】:Lifting methods to function values in Scala在 Scala 中提升函数值的方法
【发布时间】:2010-11-11 23:20:41
【问题描述】:

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

例如,假设我想解除String.length。我会写

val f: String => Int = _.length

或

val f = { s: String => s.length }

但是,这种语法并不总是理想的(尤其是在较大的表达式中)。我想我正在寻找可以启用诸如

之类的表达式的东西
Lift[String](_.length)
Lift[Option[Int]].lift(_.filter)

我想到的是这样的:

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]
}

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

问题2:如果不是,是否可以写成Option.filter可以像上面那样提升(而不是Lift[Option[Int]].lift[Int => Boolean, Option[Int]](_.filter)) ?如果不提供 lift 方法的类型参数,我会收到以下错误:

错误:缺少扩展函数的参数类型 ((x$1) => x$1.filter) Lift[Option[Int]].lift(_.filter) ^

更新:

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

【问题讨论】:

    标签: scala


    【解决方案1】:

    有什么问题

    (_: String).length
    (_: Option[Int]).filter _
    

    ?

    【讨论】:

    • 应该是 (_: Option[Int]).filter _ (注意后面的下划线!)
    • 不错。我希望得到一个完全没有咖喱的表格,但我可能不得不接受这个。
    • 我想出了一个解决方案,它提供了一个语法至少如此简单的单一提升函数,但它也让调用者可以控制提升函数的形式(咖喱,部分咖喱,或未咖喱)。如果您有兴趣,请参阅stackoverflow.com/questions/4160239/lifting-methods-in-scala/…。
    • @DanielC.Sobral,我怎样才能在你的答案中读取后一个函数的结果?在我的示例中,我将 g() 设置为等于它。 scala> g(Some(3)) res23: (Int => Boolean) => Option[Int] = <function1>
    • @Kevin 它需要更多参数列表。例如,g(Some(3))(_ % 2== 0)。
    【解决方案2】:

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

    示例:

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

    class Foo {
       def m1: Int = 1
       def m2(i: Int): Int = i
       def m3(i: Int, j: Int): Int = i + j
    }
    

    最简单的提升形式是将方法作为部分应用函数返回,相当于调用((_: 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
    

    通过导入一些隐式,可以请求 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>
    

    实施:

    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
    

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

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

    实施:

    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. ...
    }
    

    【讨论】:

      【解决方案3】:

      将过滤器作为部分应用的方法传递似乎可以完成这项工作:

      scala> class Lift[T] {                                        
           |    def apply[R](f: T => R): T => R = f
           | }
      defined class Lift
      
      scala> object Lift {
           |    def apply[T] = new Lift[T]
           | }
      defined module Lift
      
      scala> val ls = Lift[String](_.length)
      ls: (String) => Int = <function1>
      
      scala> val los = Lift[Option[Int]](_.filter _)     
      los: (Option[Int]) => ((Int) => Boolean) => Option[Int] = <function1>
      

      【讨论】:

      • 这可行,但我希望得到(Option[Int], (Int) =&gt; Boolean) =&gt; Option[Int]。
      • 我想出了一个解决方案,它提供了一个语法简单的单一提升函数,但它也让调用者可以控制提升函数的形式(咖喱、部分柯里化或非柯里化)。如果您有兴趣,请参阅stackoverflow.com/questions/4160239/lifting-methods-in-scala/…。
      猜你喜欢
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2019-03-02
      相关资源
      最近更新 更多