something like Clojure's "doto"?

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

something like Clojure's "doto"?

by Raoul Duke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.)

Re: something like Clojure's "doto"?

by David Hall-15 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.)
>

Re: something like Clojure's "doto"?

by Raoul Duke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> scala> def doto[A](a: A)(todos: (A=>Any)*) = todos.foldLeft(a){
> (a,todo) => todo(a); a }

many thanks! i suspect i'll be unraveling that a while :-) i mean, i
do get the gist, i'm just behind on what-all Scala can do.

Re: something like Clojure's "doto"?

by Alex Boisvert-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.)
>


Re: something like Clojure's "doto"?

by Vlad Patryshev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Wow, wow. So Scala has to catch up now!...

Yes, this is a beautiful construct.

2009/7/8 Raoul Duke <raould@...>
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.)



--
Thanks,
-Vlad

Re: something like Clojure's "doto"?

by andreas s. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello!
This seems also impressive to me, now i try to see this construct in a broader context. So it's main purpose would be to encapsulate the instantiation  and method invocation with certain parameters in one construct. Am I missing something essential, else here?

regards andreas
Vlad Patryshev wrote:
Wow, wow. So Scala has to catch up now!...
Yes, this is a beautiful construct.

2009/7/8 Raoul Duke <raould@gmail.com>

> 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.)
>



--
Thanks,
-Vlad

Re: something like Clojure's "doto"?

by Raoul Duke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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.

Parent Message unknown Re: something like Clojure's "doto"?

by gabriele renzi-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

(sorry I originally replied only to andreas)

On Sat, Jul 11, 2009 at 11:36 AM, andreas s.<andreas_scheinert@...> wrote:
>
> Hello!
> This seems also impressive to me, now i try to see this construct in a
> broader context. So it's main purpose would be to encapsulate the
> instantiation  and method invocation with certain parameters in one
> construct. Am I missing something essential, else here?

I think it's main purpose is to change the self-object in a scope,
which can be used for pleasant DSLish effects :)
From what I understand this is akin to "with" in some languages
(pascal?), or instance_eval in ruby.

The first solutions (albeit cool) is slightly wrong in this sense, as
it forces you to use a receiver.

This:
 doto( new java.util.HashMap[String,Int] )( _.put("a",1) )
is no better than
 val x = new java.util.HashMap[String,Int]; x.put("a",1)

The second I guess would be the more correct but it forces client side
import. Ideally, one should be able to abstract it away, e.g.

doto(x) { //no additional boilerplate
 foo(1) // calls x.foo(1)
 bar(2) // calls x.bar(2)
}

and I'd love to know how to do that in scala :)

RE: something like Clojure's "doto"?

by Detering Dirk-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> The second I guess would be the more correct but it forces
> client side import. Ideally, one should be able to abstract
> it away, e.g.
>
> doto(x) { //no additional boilerplate
>  foo(1) // calls x.foo(1)
>  bar(2) // calls x.bar(2)
> }


Hmmm. That would allow for the following?

class MyClass {

  def someMethod(f: =>Unit) {
     doto(this) f
  }

  def otherMethod() { ... }
}

val x = new MyClass()

x.someMethod {  
    otherMethod()
}


If yes, that would be the answer (or better: it is the same question)
for thread "Question on DSL", because that resembles the Groovy
closure delegate feature. It would be great for creating
builder based DSLs.

KR
Det

Re: something like Clojure's "doto"?

by gabriele renzi-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

definitely, I just wrote this mail 4 days ago but sent it to the wrong
address and by the time I re-sent it the other thred had appeared :)

2009/7/17 Detering Dirk <Dirk.Detering@...>:

>> The second I guess would be the more correct but it forces
>> client side import. Ideally, one should be able to abstract
>> it away, e.g.
>>
>> doto(x) { //no additional boilerplate
>>  foo(1) // calls x.foo(1)
>>  bar(2) // calls x.bar(2)
>> }
>
>
> Hmmm. That would allow for the following?
>
> class MyClass {
>
>  def someMethod(f: =>Unit) {
>     doto(this) f
>  }
>
>  def otherMethod() { ... }
> }
>
> val x = new MyClass()
>
> x.someMethod {
>    otherMethod()
> }
>
>
> If yes, that would be the answer (or better: it is the same question)
> for thread "Question on DSL", because that resembles the Groovy
> closure delegate feature. It would be great for creating
> builder based DSLs.
>
> KR
> Det
>



--
blog en: http://www.riffraff.info
blog it: http://riffraff.blogsome.com

Re: something like Clojure's "doto"?

by Colin Bullock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hmmm... isn't this essentially just dynamic (as opposed to lexical) scoping? I don't believe there's any way to achieve that directly in scala (please correct me if I'm wrong though).

- Colin

Re: something like Clojure's "doto"?

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Jul 17, 2009 at 08:25:16AM -0500, Colin Bullock wrote:
> Hmmm... isn't this essentially just dynamic (as opposed to lexical)
> scoping? I don't believe there's any way to achieve that directly in
> scala (please correct me if I'm wrong though).

Depends on what you mean by "direct", I suppose.

http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/src/library/scala/util/DynamicVariable.scala

 *    DynamicVariables provide a binding mechanism where the current
 *    value is found through <em>dynamic scope</em>, but where
 *    access to the variable itself is resolved through <em>static
 *    scope</em>.

--
Paul Phillips      | The important thing here is that the music is not in
Everyman           | the piano.  And knowledge and edification is not in the
Empiricist         | computer.  The computer is simply an instrument whose
pull his pi pal!   | music is ideas.  -- Alan Kay

Re: something like Clojure's "doto"?

by Colin Bullock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Depends on what you mean by "direct", I suppose.

Directly in the syntax supported by the language/compiler. I thought about mentioning DynamicVariable, but it doesn't really add anything in this context that you can't already achieve by passing the receiver as in the examples above.

Plus, using InheritableThreadLocal in any non-trivial application scares the pants off me...

- Colin


Re: something like Clojure's "doto"?

by gabriele renzi-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Jul 17, 2009 at 3:25 PM, Colin Bullock<cmbullock@...> wrote:
> Hmmm... isn't this essentially just dynamic (as opposed to lexical) scoping?

yes, but "this" is already dynamically scoped, such a trick just
allows the user to control the value of it in a scope.

Without thinking of dynamic scope though, it is just syntax sugar in
the same way Pascal's "with" statement is.

> I don't believe there's any way to achieve that directly in scala (please
> correct me if I'm wrong though).

it's just that scala spoils us by making _almost_ everything possible :)


--
blog en: http://www.riffraff.info
blog it: http://riffraff.blogsome.com

Re: something like Clojure's "doto"?

by Raoul Duke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Hmmm... isn't this essentially just dynamic (as opposed to lexical) scoping?
> I don't believe there's any way to achieve that directly in scala (please
> correct me if I'm wrong though).

woah woah woah wait a second, it can be done with macros, w/out
dynamic scoping, no?? well, at least in languages that have macros :-)

Re: something like Clojure's "doto"?

by Arthur Peters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I suspect people are aware of this, but for people who are not: there
was talk of this before, even a draft SIP. See
http://www.nabble.com/Draft-SIP:-Curly-import-for-more-authentic-DSLs-td22719018.html

-Arthur



On Fri, Jul 17, 2009 at 10:44 AM, Raoul Duke<raould@...> wrote:
>> Hmmm... isn't this essentially just dynamic (as opposed to lexical) scoping?
>> I don't believe there's any way to achieve that directly in scala (please
>> correct me if I'm wrong though).
>
> woah woah woah wait a second, it can be done with macros, w/out
> dynamic scoping, no?? well, at least in languages that have macros :-)
>

Re: something like Clojure's "doto"?

by David MacIver :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/17 Arthur Peters <arthur.peters@...>:
> I suspect people are aware of this, but for people who are not: there
> was talk of this before, even a draft SIP. See
> http://www.nabble.com/Draft-SIP:-Curly-import-for-more-authentic-DSLs-td22719018.html

A draft SIP which got very thoroughly axed. :-)

Re: something like Clojure's "doto"?

by Chris Twiner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Jul 18, 2009 at 2:50 AM, David MacIver<david.maciver@...> wrote:
> 2009/7/17 Arthur Peters <arthur.peters@...>:
>> I suspect people are aware of this, but for people who are not: there
>> was talk of this before, even a draft SIP. See
>> http://www.nabble.com/Draft-SIP:-Curly-import-for-more-authentic-DSLs-td22719018.html
>
> A draft SIP which got very thoroughly axed. :-)
>

I would still love the option to more easily thread an implicit.

transaction { _t =>
 implicit val t = _t
 x
}

for me is very boilerplatey when x is only two or three lines, I'd be happy with

transaction { implicit t =>
 x
}

though.  Its far less distracting, does anyone know why this isn't
allowed, in terms of language design?  It seems to follow the general
language patterns quite closely.

Re: something like Clojure's "doto"?

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I like this syntax. It's very much in line with the other implicit declarations.

On Sat, Jul 18, 2009 at 5:40 AM, Chris Twiner <chris.twiner@...> wrote:
On Sat, Jul 18, 2009 at 2:50 AM, David MacIver<david.maciver@...> wrote:
> 2009/7/17 Arthur Peters <arthur.peters@...>:
>> I suspect people are aware of this, but for people who are not: there
>> was talk of this before, even a draft SIP. See
>> http://www.nabble.com/Draft-SIP:-Curly-import-for-more-authentic-DSLs-td22719018.html
>
> A draft SIP which got very thoroughly axed. :-)
>

I would still love the option to more easily thread an implicit.

transaction { _t =>
 implicit val t = _t
 x
}

for me is very boilerplatey when x is only two or three lines, I'd be happy with

transaction { implicit t =>
 x
}

though.  Its far less distracting, does anyone know why this isn't
allowed, in terms of language design?  It seems to follow the general
language patterns quite closely.



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

Re: something like Clojure's "doto"?

by Alex Boisvert-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

An alternative syntax for importing arguments could be,

{ |CONTEXT| =>
    BODY
}

as syntactic sugar for,

{ CONTEXT =>
    import CONTEXT._
    BODY
}

with the possibility of importing n arguments,

{ x, |y|, |z| => /* imports y._ and z._ but not x._ */ }

or without naming the argument,

{ |_| =>  /* imported argument remains unnamed */ }

and with explicit types,

{ |x: Foo|, |y: Bar| =>  ... }

alex
(with apologies to the Ruby syntax)

< Prev | 1 - 2 | Next >