« Return to Thread: something like Clojure's "doto"?

Re: something like Clojure's "doto"?

by Raoul Duke :: Rate this Message:

Reply to Author | View in Thread

> scala> def doto[A](a: A)(todos: (A=>Any)*) = todos.foldLeft(a){
> (a,todo) => todo(a); a }
> doto: [A](A)((A) => Any*)A
>
> scala> doto(new java.util.HashMap[String,Int])(_.put("a",1),_.put("b",2))
> res0: java.util.HashMap[String,Int] = {b=2, a=1}

there are extra () around the "todos", is that required to make this
all work? when i try to not have them the REPL says that the
underscores are confusing "missing parameter type blah blah".

i'm a little worried about how hard it is to understand the seemingly
special cases of magic syntax in Scala to get ideas in my head
translated into code that really does what i want :-}

thanks.

 « Return to Thread: something like Clojure's "doto"?