Skip to content

Instantly share code, notes, and snippets.

@Atry
Created September 2, 2014 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atry/79ae3af43da65d265c72 to your computer and use it in GitHub Desktop.
Save Atry/79ae3af43da65d265c72 to your computer and use it in GitHub Desktop.
#!/bin/env scala
trait 所有操作 {
protected type 操作符
protected def 创建操作符(操作: (Int, Int) => Int):操作符
final def 加 = 创建操作符(_ + _)
final def 乘 = 创建操作符(_ * _)
}
implicit final class 整数(左值: Int) extends 所有操作 {
protected type 操作符 = Int => Int
protected final def 创建操作符(操作: (Int, Int) => Int) = { 右值 => 操作(左值, 右值) }
case class 一直(操作: (Int, Int) => Int) {
def 到(右值: Int) = {
(左值 to 右值).reduce(操作)
}
}
def 到(右值: Int) = 左值 to 右值
}
object 延后计算 extends 所有操作 {
protected type 操作符 = (Int, Int) => Int
protected final def 创建操作符(操作: (Int, Int) => Int) = 操作
}
import 延后计算._
println(1 加 10)
println(1 乘 10)
println(1 到 10)
println(1 一直 加 到 10)
println(1 一直 乘 到 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment