|
View:
New views
20 Messages
—
Rating Filter:
Alert me
|
| < Prev | 1 - 2 | Next > |
|
|
something like Clojure's "doto"?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"?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"?> 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"?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){ |
|
|
Re: something like Clojure's "doto"?Wow, wow. So Scala has to catch up now!...
Yes, this is a beautiful construct.
2009/7/8 Raoul Duke <raould@...> hi, -- Thanks, -Vlad |
|
|
Re: something like Clojure's "doto"?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
|
|
|
Re: something like Clojure's "doto"?> 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. |
|
|
|
|
|
RE: something like Clojure's "doto"?> 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"?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"?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"?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"?
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"?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"?> 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"?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"?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"?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"?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:
-- 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"?
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 > |
| Free embeddable forum powered by Nabble | Forum Help |