(. rnd nextInt) vs (.nextInt rnd)

44 views
Skip to first unread message

James Keats

unread,
Jun 15, 2011, 9:52:57 AM6/15/11
to Clojure
Hi all. I'm struggling to see the point of this (from Pragmatic's
Programming Clojure):

Java => rnd.nextInt()
Clojure => (. rnd nextInt)
sugared => (.nextInt rnd)


What's the point of the sugared version? It's not any less to type.
It's also incomprehensible to me how it came about. In the middle one
it's simple, class and method, but the in sugared one it's just plain
simply bizarre looking. What was the intent?

Thanks.

Joop Kiefte

unread,
Jun 15, 2011, 10:04:53 AM6/15/11
to clo...@googlegroups.com
The difference is that the sugared version works just like a normal
Clojure function. It also eases a lot of things with macros like
(doto).

2011/6/15 James Keats <james....@gmail.com>:

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Ken Wesson

unread,
Jun 15, 2011, 10:13:21 AM6/15/11
to clo...@googlegroups.com
On Wed, Jun 15, 2011 at 9:52 AM, James Keats <james....@gmail.com> wrote:
> Hi all. I'm struggling to see the point of this (from Pragmatic's
> Programming Clojure):
>
> Java  =>  rnd.nextInt()
> Clojure => (. rnd nextInt)
> sugared => (.nextInt rnd)
>
>
> What's the point of the sugared version? It's not any less to type.

Actually there's one fewer character -- a space.

> It's also incomprehensible to me how it came about. In the middle one
> it's simple, class and method, but the in sugared one it's just plain
> simply bizarre looking. What was the intent?

It's closer to typical function-call form: (.doSomething someNoun)
resembles (do-something some-noun) more than does (. someNoun
doSomething).

--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

James Keats

unread,
Jun 15, 2011, 10:55:38 AM6/15/11
to Clojure

>
> > What's the point of the sugared version? It's not any less to type.
>
> Actually there's one fewer character -- a space.
>

Okay, I'll give you that.

> > It's also incomprehensible to me how it came about. In the middle one
> > it's simple, class and method, but the in sugared one it's just plain
> > simply bizarre looking. What was the intent?
>
> It's closer to typical function-call form: (.doSomething someNoun)
> resembles (do-something some-noun) more than does (. someNoun
> doSomething).
>

Right. That makes sense. I see the consistency here now. :-)

Thanks. Soldiering on now; I'll probably be back. :-D
Reply all
Reply to author
Forward
0 new messages