Re: Is this unquote dangerous?

2009-07-08 Thread Nathan Kitchen

On Jul 7, 5:02 am, Mike  wrote:
> (not sure where my reply to Chouser et al. went, but basically I said
> that I was writing a macro and I might be overdoing it.  I was right!)
>
> Here's what I was trying to accomplish, but in functions, not macros:
>
> (defn slice
>   "Returns a lazy sequence composed of every nth item of
>    coll, starting from offset."
>   [n offset coll]
>   (if (= offset 0)
>     (take-nth n coll)
>     (take-nth n (drop offset coll
[snip]

Your slice function looks similar to the built-in partition:

(defn slice [n offset coll]
  (apply concat (partition 1 n (drop offset coll
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Is this unquote dangerous?

2009-07-07 Thread Jonathan Smith



On Jul 6, 6:00 pm, Chouser  wrote:
> On Mon, Jul 6, 2009 at 4:18 PM, Meikel Brandmeyer wrote:
> > Hi,
>
> > Am 06.07.2009 um 22:00 schrieb Chouser:
>
> >> Or if you really do need a list:
>
> >>  (for [x [1 2 3]] (cons 'some-symbol (list x)))
>
> > o.O
>
> > *cough*(list 'some-symbol x)*cough* ;)
>
> Oh.  Right.  What he said.
>
> --Chouser

Why not this?
(map #(list 'some-symbol %1) [1 2 3])
or
(map list (repeatedly (constantly 'some-symbol)) [1 2 3])
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Is this unquote dangerous?

2009-07-07 Thread Mike

(not sure where my reply to Chouser et al. went, but basically I said
that I was writing a macro and I might be overdoing it.  I was right!)

Here's what I was trying to accomplish, but in functions, not macros:


(defn slice
  "Returns a lazy sequence composed of every nth item of
   coll, starting from offset."
  [n offset coll]
  (if (= offset 0)
(take-nth n coll)
(take-nth n (drop offset coll

(let [take-helper
  (fn [n slices coll]
(apply interleave (map #(slice n % coll) slices)))]
  (defn take-slices
"Returns a lazy sequence of items from coll.  Items are
rearranged according to non-negative indexes contained
in collection slices, in blocks of size n (which defaults
to (+ 1 (apply max slices)).

Ex: (take-slices [3 2 1] (range 14)) -> (3 2 1 7 6 5 11 10 9)
(take-slices 5 [2 0] (range 20)) -> (2 0 7 5 12 10 17 15)"
([slices coll]
   (take-helper (+ 1 (apply max slices)) slices coll))
([n slices coll]
   (take-helper n slices coll


Thanks again to everyone who replied!
Mike

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Is this unquote dangerous?

2009-07-07 Thread Mike

On Jul 6, 4:00 pm, Chouser  wrote:
> On Mon, Jul 6, 2009 at 3:47 PM, Sean Devlin wrote:
>
> > I think your unquote is okay.  ClojureQL does something similar.
>
> > However, my gut says this should be in a doseq, not a for statement.
> > Could be totally wrong, tough.
>
> I think the OP is trying to build and return a list, not
> trying to execute a series of operations.  If I'm right,
> then 'for' is better than 'doseq'.

[snip]

> I can't think of any way in which it's dangerous.  Is it
> important to you that the symbol become fully-qualified?  If
> so, then what you've got is fine -- are you writing a macro?

Yes, I am!  I'm slowly getting my head back into lispyness by diving
head-first into writing a macro.  =)

I'm working on something that will approximately do:

(take-ordered [3 0 1 2] coll)

->

(interleave
  (take-nth 4 (drop 3 coll))
  (take-nth 4 coll)
  (take-nth 4 (drop 1 coll))
  (take-nth 4 (drop 2 coll)))

This is a "rearranging" lazy seq operation, and the vector provided
for the "pick-list" is of variable length.  So I'm trying to build up
a variable-length list of take-nth and drop applications in the macro
substitution.

There probably is an easier way to do this just with a function.  I
looked at interleave in core.clj and was surprised to see it wasn't a
macro.  So I may be making a mountain out of a mole hill, but it's all
part of the learning process, right?  =)

Thanks for the help guys...

Mike

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Is this unquote dangerous?

2009-07-06 Thread John Harrop
>
> Or if you really do need a list:
>
>  (for [x [1 2 3]] (cons 'some-symbol (list x)))
>

Why not

(for [x [1 2 3]] (list 'some-symbol x))

?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Is this unquote dangerous?

2009-07-06 Thread Chouser

On Mon, Jul 6, 2009 at 4:18 PM, Meikel Brandmeyer wrote:
> Hi,
>
> Am 06.07.2009 um 22:00 schrieb Chouser:
>
>> Or if you really do need a list:
>>
>>  (for [x [1 2 3]] (cons 'some-symbol (list x)))
>
> o.O
>
> *cough*(list 'some-symbol x)*cough* ;)

Oh.  Right.  What he said.

--Chouser

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Is this unquote dangerous?

2009-07-06 Thread Meikel Brandmeyer

Hi,

Am 06.07.2009 um 22:00 schrieb Chouser:


Or if you really do need a list:

 (for [x [1 2 3]] (cons 'some-symbol (list x)))


o.O

*cough*(list 'some-symbol x)*cough* ;)

Sincerely
Meikel

smime.p7s
Description: S/MIME cryptographic signature


Re: Is this unquote dangerous?

2009-07-06 Thread Chouser

On Mon, Jul 6, 2009 at 3:47 PM, Sean Devlin wrote:
>
> I think your unquote is okay.  ClojureQL does something similar.
>
> However, my gut says this should be in a doseq, not a for statement.
> Could be totally wrong, tough.

I think the OP is trying to build and return a list, not
trying to execute a series of operations.  If I'm right,
then 'for' is better than 'doseq'.

> On Jul 6, 2:39 pm, Mike  wrote:
>> Newbie question here.  Probably answered in Stu's book, but I forgot
>> it at home today.
>>
>> is:
>>
>> (for [x [1 2 3]] `(some-symbol ~x))
>>
>> dangerous?  I mean, assuming that some-symbol is bound and all.  At
>> the REPL I get
>>
>> ((user/some-symbol 1) (user/some-symbol 2) (user/some-symbol 3))
>>
>> which is what I'm interested in getting, but somehow the fact that
>> "for" is a macro and I'm escaping x assuming it's there is
>> disconcerting.

I can't think of any way in which it's dangerous.  Is it
important to you that the symbol become fully-qualified?  If
so, then what you've got is fine -- are you writing a macro?

If some-symbol is not referring to a Var, though, it's more
likely you want a regular quote than a syntax quote, in
which case it may be more idiomatic to build a vector:

  (for [x [1 2 3]] ['some-symbol x])

Or if you really do need a list:

  (for [x [1 2 3]] (cons 'some-symbol (list x)))

--Chouser

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Is this unquote dangerous?

2009-07-06 Thread Sean Devlin

I think your unquote is okay.  ClojureQL does something similar.

However, my gut says this should be in a doseq, not a for statement.
Could be totally wrong, tough.

My $.02

Sean

On Jul 6, 2:39 pm, Mike  wrote:
> Newbie question here.  Probably answered in Stu's book, but I forgot
> it at home today.
>
> is:
>
> (for [x [1 2 3]] `(some-symbol ~x))
>
> dangerous?  I mean, assuming that some-symbol is bound and all.  At
> the REPL I get
>
> ((user/some-symbol 1) (user/some-symbol 2) (user/some-symbol 3))
>
> which is what I'm interested in getting, but somehow the fact that
> "for" is a macro and I'm escaping x assuming it's there is
> disconcerting.
>
> Thanks for any style tips...
> Mike
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Is this unquote dangerous?

2009-07-06 Thread Mike

Newbie question here.  Probably answered in Stu's book, but I forgot
it at home today.

is:

(for [x [1 2 3]] `(some-symbol ~x))

dangerous?  I mean, assuming that some-symbol is bound and all.  At
the REPL I get

((user/some-symbol 1) (user/some-symbol 2) (user/some-symbol 3))

which is what I'm interested in getting, but somehow the fact that
"for" is a macro and I'm escaping x assuming it's there is
disconcerting.

Thanks for any style tips...
Mike

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---