splat operator

962 views
Skip to first unread message

Martin DeMello

unread,
Oct 15, 2008, 8:49:17 PM10/15/08
to Clojure
Is there any sort of "splat" operator that expands a list into an
inline sequence of arguments? Failing that, is there any way to use
apply within a doto block?

e..g

(def search-replace '("ll" "")
(def target "hello world")

(doto target
;(apply replace search-replace) <-- that, except something that
works

martin

Martin DeMello

unread,
Oct 15, 2008, 9:08:34 PM10/15/08
to Clojure
(okay, bad example, i forgot the string was immutable anyway :) but
it's mostly the general syntax i'm after)

martin

Michel Salim

unread,
Oct 15, 2008, 10:53:40 PM10/15/08
to Clojure


On Oct 15, 9:08 pm, Martin DeMello <martindeme...@gmail.com> wrote:
> On Oct 15, 5:49 pm, Martin DeMello <martindeme...@gmail.com> wrote:
>
> > Is there any sort of "splat" operator that expands a list into an
> > inline sequence of arguments? Failing that, is there any way to use
> > apply within a doto block?
>
> > e..g
>
> > (def search-replace '("ll" "")
> > (def target "hello world")
>
> > (doto target
> >   ;(apply replace search-replace) <-- that, except something that
> > works
>
(replace {"ll" ""} "hello world")

(notice the use of a map rather than a list). This produces a
sequence, though; and wrapping it with (into "" ...) does not work
since a string does not appear to have full Clojure sequence
semantics.

I'm probably missing something regarding string handling, though. But
from what I've seen, it appears that the recommended way is to use
Java's built-in string methods here.

Regards,

--
Michel Salim

Michel Salim

unread,
Oct 15, 2008, 11:07:55 PM10/15/08
to Clojure


On Oct 15, 9:08 pm, Martin DeMello <martindeme...@gmail.com> wrote:
> On Oct 15, 5:49 pm, Martin DeMello <martindeme...@gmail.com> wrote:
>
> > Is there any sort of "splat" operator that expands a list into an
> > inline sequence of arguments? Failing that, is there any way to use
> > apply within a doto block?

Ah, so to clarify, you need some way to say

(. "hello" (replace \l \m))

but if \l and \m are given in a list, rather than as two separate
things? Sounds like you might need to write a function to do that.

Regards,

--
Michel

Martin DeMello

unread,
Oct 16, 2008, 12:38:52 AM10/16/08
to Clojure
On Oct 15, 8:07 pm, Michel Salim <michel.syl...@gmail.com> wrote:
>
> Ah, so to clarify, you need some way to say
>
> (. "hello" (replace \l \m))
>
> but if \l and \m are given in a list, rather than as two separate
> things? Sounds like you might need to write a function to do that.

Yes, apply works in general, but breaks within a doto block.

martin

Timothy Pratley

unread,
Oct 16, 2008, 8:13:44 PM10/16/08
to Clojure
Maybe something along these lines?

(defn myreplace [str [a b]]
(.replace str a b))
(myreplace target search-replace)
-> "heo world"

I can see how it would be nice to 'splice in' from a variable like the
~@list splicing from within a macro

Michel Salim

unread,
Oct 17, 2008, 3:13:02 PM10/17/08
to clo...@googlegroups.com
On Thu, Oct 16, 2008 at 8:13 PM, Timothy Pratley
<timothy...@gmail.com> wrote:
>
> Maybe something along these lines?
>
> (defn myreplace [str [a b]]
> (.replace str a b))
> (myreplace target search-replace)
> -> "heo world"
>
Clojure's ML/Haskell-style deconstructing of sequences takes a while
to sink in -- I wish Scheme has it :)


--
miʃel salim • http://hircus.jaiku.com/
IUCS • msa...@cs.indiana.edu
Fedora • sal...@fedoraproject.org
MacPorts • hir...@macports.org

Martin DeMello

unread,
Oct 17, 2008, 5:33:44 PM10/17/08
to Clojure
On Oct 16, 5:13 pm, Timothy Pratley <timothyprat...@gmail.com> wrote:
> Maybe something along these lines?
>
> (defn myreplace [str [a b]]
>   (.replace str a b))
> (myreplace target search-replace)
> -> "heo world"

Same problem - it doesn't work within a doto block. This is where
JRuby-style reopening of the class and adding another method would be
handy. Here's an example from an app I'm currently writing, which
illustrates what I want to do:

(defn inner-square [i j]
(+ i 1) (+ j 1) (- scale 1) (- scale 1))

(defmacro in-square [[i j] bg x y & body]
`(let [[~i ~j] (topleft ~x ~y)]
(doto ~bg
~@body)))

(defn fill-square [bg x y color]
(in-square [i j] bg x y
(setColor color)
(fillRect (+ i 1) (+ j 1) (- scale 1) (- scale 1))))

Note that in fill-square I can either use in-square or inner-square,
but not both - in-square wraps everything in a doto block, and
fillRect expects an inline list of parameters.

martin
Reply all
Reply to author
Forward
0 new messages