Created
November 3, 2015 15:37
-
-
Save unclebean/898c97182733dffa2763 to your computer and use it in GitHub Desktop.
scala sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait PipeOperator[-T, +U]{ | |
def |> (data:T):Option[U] | |
} | |
class _FCT[+T](val _fct: T) { | |
def map[U](c: T => U): _FCT[U] = new _FCT[U]( c(_fct)) | |
def flatMap[U](f: T =>_FCT[U]): _FCT[U] = f(_fct) | |
//def filter(p: T =>Boolean): _FCT[T] = if( p(_fct) ) new _FCT[T](_fct) else zeroFCT(_fct) | |
def reduceLeft[U](f: (U,T) => U)(implicit c: T=> U): U = f(c(_fct),_fct) | |
def foldLeft[U](zero: U)(f: (U, T) => U)(implicit c: T=> U): U = f(c(_fct), _fct) | |
def foreach(p: T => Unit): Unit = p(_fct) | |
} | |
class Transform[-T, +U](val op: PipeOperator[T, U]) extends _FCT[Function[T, Option[U]]](op.|>) { | |
def |>(data: T): Option[U] = _fct(data) | |
} | |
object Operator{ | |
def main(args:Array[String]){ | |
val op = new PipeOperator[Int, Double] { | |
def |> (n: Int):Option[Double] =Some(Math.sin(n.toDouble)) } | |
def g(f: Int =>Option[Double]): (Int=> Long) = { | |
(n: Int) => { | |
f(n) match { | |
case Some(x) => x.toLong | |
case None => -1L | |
} | |
} | |
} | |
val gof = new Transform[Int,Double](op).map(g(_)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment