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

Re: something like Clojure's "doto"?

by Alex Boisvert-3 :: Rate this Message:

Reply to Author | View in Thread

Or by importing all the object's members into the scope,

scala> def doto[X](x: X)(f: X => Unit) = { f(x); x }
doto: [X](X)((X) => Unit)X

scala> doto(new java.util.HashMap[String,Int]) { x => import x._
     |   put("a", 1)
     |   put("b", 2)
     | }
res1: java.util.HashMap[String,Int] = {b=2, a=1}

alex

On Wed, Jul 8, 2009 at 3:10 PM, David Hall <dlwh@...> wrote:
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}


On Wed, Jul 8, 2009 at 3:03 PM, Raoul Duke<raould@...> wrote:
> hi,
>
> is there / how might one implement something like
> http://clojure.org/java_interop#toc15 to get (even more) succinctness
> in Scala?
>
> many thanks.
> (i am experimenting with writing it on my own but am so far kinda lost.)
>

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