Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created March 6, 2013 07:26
Show Gist options
  • Select an option

  • Save debasishg/5097410 to your computer and use it in GitHub Desktop.

Select an option

Save debasishg/5097410 to your computer and use it in GitHub Desktop.
scala> :paste
// Entering paste mode (ctrl-D to finish)
val doOne = (user: String) => List("one" + "*" + user)
val doTwo = (user: String) => List("two" + "*" + user)
val doThree = (user: String) => List("three" + "*" + user)
def collectAll(funs: List[String => List[String]])(user: String): List[String] =
(List[String]() /: funs)((a, b) => a ::: b(user))
val actMap = Map(
"one" -> List(doOne),
"one+two" -> List(doOne, doTwo)
)
def act(action: String, user: String): List[String] = {
collectAll(actMap(action))(user)
}
// Exiting paste mode, now interpreting.
doOne: String => List[String] = <function1>
doTwo: String => List[String] = <function1>
doThree: String => List[String] = <function1>
collectAll: (funs: List[String => List[String]])(user: String)List[String]
actMap: scala.collection.immutable.Map[String,List[String => List[String]]] = Map(one -> List(<function1>), one+two -> List(<function1>, <function1>))
act: (action: String, user: String)List[String]
scala>
scala>
scala> act("one+two", "void")
res5: List[String] = List(one*void, two*void)
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment