Create a gist now

Instantly share code, notes, and snippets.

proof of concept
package pftest
import scala.collection.mutable.{Map => MMap}
object ChainedFunctions extends Logging {
// thanks to dpp in http://scala-programming-language.1934581.n4.nabble.com/quot-Empty-quot-PartialFunction-td2399351.html
private val nada: PartialFunction[Any, Any] = scala.collection.immutable.Map()
protected val moarFns = MMap.empty[String, PartialFunction[Any, Any]]
def register(label: String, f: PartialFunction[Any, Any]) {
moarFns.put(label, f)
log.info("register: ADDED transformer '%s'", label)
}
def unregister(label: String) {
moarFns.remove(label)
log.info("register: REMOVED transformer '%s'", label)
}
def transformString: PartialFunction[Any, Any] = {
case x: String => x.reverse.toUpperCase
}
def transformInt: PartialFunction[Any, Any] = {
case i: Int => i * 2
}
def straightThrough: PartialFunction[Any, Any] = {
case x => x
}
def processCustom: PartialFunction[Any, Any] = {
// // tip of the hat to http://scalaeveryday.com/?p=67 - note that 2.9 reduce delegates to reduceLeft
moarFns.values.reduceLeftOption(_ orElse _).getOrElse(nada)
}
def valueChain = processCustom orElse
transformString orElse
transformInt orElse
straightThrough
def optionTransformer: PartialFunction[Any, Any] = {
// case x: Option[_] => x.map(valueChain)
case x: Option[_] => x.map(chain)
}
def mapTransformer: PartialFunction[Any, Any] = {
// case m: Map[String, _] => m.mapValues(optionTransformer orElse listTransformer orElse valueChain)
case m: Map[String, _] => m.mapValues(chain)
}
def listTransformer: PartialFunction[Any, Any] = {
// case list: List[_] => list.map(optionTransformer orElse mapTransformer orElse valueChain)
case list: List[_] => list.map(chain)
}
def chain = optionTransformer orElse
mapTransformer orElse
listTransformer orElse
valueChain
}
scala> import pftest.ChainedFunctions._
import pftest.ChainedFunctions._
scala> chain("whatever")
res0: Any = REVETAHW
scala> chain(2)
res1: Any = 4
scala> chain(Some("whatever"))
res2: Any = Some(REVETAHW)
scala> chain(Some(2))
res3: Any = Some(4)
scala> chain(new java.util.Date())
res4: Any = Sun Jul 10 14:14:41 EDT 2011
scala> chain(Some(new java.util.Date()))
res5: Any = Some(Sun Jul 10 14:14:52 EDT 2011)
scala> val m = Map("a" -> "hi", "b" -> "bye")
m: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(a -> hi, b -> bye)
scala> chain(m)
res6: Any = Map(a -> IH, b -> EYB)
scala> chain(m.mapValues(v => Option(v)))
res7: Any = Map(a -> Some(IH), b -> Some(EYB))
scala> val l = List(1, 2, 3, 4, 5)
l: List[Int] = List(1, 2, 3, 4, 5)
scala> chain(l)
res9: Any = List(2, 4, 6, 8, 10)
scala> chain(l.map(Option(_)))
res10: Any = List(Some(2), Some(4), Some(6), Some(8), Some(10))
scala> val m2 = Map("a" -> List(1, 2), "b" -> List(3, 4))
m2: scala.collection.immutable.Map[java.lang.String,List[Int]] = Map(a -> List(1, 2), b -> List(3, 4))
scala> chain(m2)
res11: Any = Map(a -> List(2, 4), b -> List(6, 8))
scala> chain(m2.mapValues(_.map(Option(_)))
| )
res12: Any = Map(a -> List(Some(2), Some(4)), b -> List(Some(6), Some(8)))
scala> val l2 = List(Map("a" -> List(1, 2), "b" -> List(3, 4)), Map("c" -> List(5, 6), "d" -> List(7, 8)))
l2: List[scala.collection.immutable.Map[java.lang.String,List[Int]]] = List(Map(a -> List(1, 2), b -> List(3, 4)), Map(c -> List(5, 6), d -> List(7, 8)))
scala> chain(l2)
res13: Any = List(Map(a -> List(2, 4), b -> List(6, 8)), Map(c -> List(10, 12), d -> List(14, 16)))
scala> chain(l2.map(Option(_)))
res14: Any = List(Some(Map(a -> List(2, 4), b -> List(6, 8))), Some(Map(c -> List(10, 12), d -> List(14, 16))))
scala> chain(1L)
res15: Any = 1
scala> def transformLong: PartialFunction[Any, Any] = { case l: Long => l * 42L }
transformLong: PartialFunction[Any,Any]
scala> register("customLongTransformer", transform
transformInt transformLong transformString
scala> register("customLongTransformer", transformLong)
1 [Thread-302] INFO pftest.ChainedFunctions$ - register: ADDED transformer 'customLongTransformer'
scala> chain(1L)
res17: Any = 42
scala> unregister("customLongTransformer")
17911 [Thread-304] INFO pftest.ChainedFunctions$ - register: REMOVED transformer 'customLongTransformer'
scala> chain(1L)
res19: Any = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment