extracting a tuple inside map function

Skip to first unread message

scott clasen

Aug 9, 2013, 8:35:22 PM8/9/13
to treehugg...@googlegroups.com
What is the syntax to do the following? Also the REF("Option") business is not ideal I think.

  case (k,v) => k -> Option(v)

REF("t") MAP LAMBDA(?How to get the case (k,v)?) ==> BLOCK(REF("k") ANY_-> REF("Option") APPLY REF("v"))

Ive got it working uglier like this 

t.map{ kv => kv._1 -> Option(kv._2) }

  REF("t") MAP LAMBDA(PARAM("kv")) ==> BLOCK(REF("kv._1") ANY_-> REF("Option") APPLY REF("kv._2"))

eugene yokota

Aug 9, 2013, 11:33:15 PM8/9/13
to treehugg...@googlegroups.com
Hi Scott,

The partial function literal is called "case sequence." See [1].
Option is already defined in definitions, but in general you can define symbols upfront.

import treehugger.forest._
import definitions._
import treehuggerDSL._

scala> val tree = REF("t") MAP BLOCK(
     |   CASE(TUPLE(ID("k"), ID("v"))) ==> (REF("k") ANY_-> (OptionClass APPLY REF("v")))
     | )
tree: treehugger.forest.Tree = Infix(Ident(t),map,List(Block(List(),CaseDef(Apply(Ident(Tuple2),List(Ident(k), Ident(v))),EmptyTree,Infix(Ident(k),->,List(Apply(Ident(Option),List(Ident(v)))))))))

scala> treeToString(tree)
res0: String = 
t map {
  case (k, v) => k -> Option(v)


You received this message because you are subscribed to the Google Groups "treehugger-scala" group.
To unsubscribe from this group and stop receiving emails from it, send an email to treehugger-sca...@googlegroups.com.
To post to this group, send email to treehugg...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/treehugger-scala/ef047254-233b-4772-be6e-47131eeacdac%40googlegroups.com?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

scott clasen

Aug 11, 2013, 12:45:03 PM8/11/13
to treehugg...@googlegroups.com
Nice thanks!
Reply all
Reply to author
0 new messages