Iterate through transient map?

2018-04-27 Thread Didier
Don't think so.

But the conversion back and forth is O(1). So it shouldn't affect performance.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Iterate through transient map?

2018-04-27 Thread Tom Locke
I can iterate through a transient vector, as count and nth can be used 
directly on the transient.

Is there any way to iterate through the keys (or key/value pairs) of a 
transient hash, other than just making a persistent version of it?

I guess the meta-question is, any advice on finding which functions work on 
transients, other than digging through the source?

Thanks

Tom

p.s. my project is actually ClojureScript but I figured this was a Clojure 
question. Just mentioning it in case the answer relies on details under the 
hood.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Iterate over transient map

2016-05-13 Thread JvJ
I'm implementing a data structure, cross-map 
<https://github.com/JvJ/cross-map>, which cross-indexes keys with the 
structure [row column].

I'm also trying to implement a transient version to speed things up a bit. 
 However, I would like to do certain iteration operations on the transient 
itself without having to call persistent! if possible.

I realize that the transient-to-persistent conversion should ideally be 
O(1), but there are certain cases where I have to iterate over some 
structures to achieve what I'm looking for.

Is there a way to get some kind of iterator for a transient map?  I 
understand that lazy-sequences are bad for mutability, but even a 
java-style imperative iterator would be good.

Thanks

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: arity-3 `iterate`

2015-11-16 Thread Ben Wolfson
ncomp would be faster if it's implemented a la exponentiation by squaring.

On Mon, Nov 16, 2015 at 12:55 PM, Ben Wolfson  wrote:

> If you rearrange the arguments of your proposed three-argument iterate:
>
> (defn iterate
>   ...
>   ([f n x]
>(if (zero? n)
>  x
>  (recur f (f x) (dec n
>
> it supports partial application of the multiply-composed function to
> different arguments more easily, which suggests redefinition as:
>
> (defn ncomp [f n]
>   (loop [g identity n n]
> (if (zero? n)
>g
>(recur (comp g f) (dec n)
>
> This seems to come closer to matching what's desired from the
> three-argument iterate without confusingly (as you note) overloading
> "iterate": you want f applied n times to x and the stream of intermediate
> values isn't really of interest.
>
> On Mon, Nov 16, 2015 at 12:37 PM, Jason Felice 
> wrote:
>
>>
>> I *frequently* see:
>>
>> (nth (iterate foo bar) n)
>>
>> And:
>>
>> (->> (iterate foo bar)
>>(drop n)
>>first)
>>
>> I've also coded this in `avi` after being surprised no such thing exists:
>>
>> (defn n-times
>>   [thing n a-fn]
>>   (reduce
>> (fn [thing n]
>>   (a-fn thing))
>> thing
>> (range n)))
>>
>> (which is kind of a bad implementation, now that I think of it):
>>
>> Would it be useful to file a ticket for a new arity for iterate?:
>>
>> (defn iterate
>>   ...
>>   ([f x n]
>>(if (zero? n)
>>  x
>>  (recur f (f x) (dec n
>>
>> There's a little bit of weirdness - this returns a single value, while
>> the other arity returns a lazy sequence.  However, I think it's the correct
>> name (and the original arity might  have better been named "iterations" for
>> symmetry with "reduce" and "reductions").  But ah well.
>>
>> Thoughts?
>>
>> --
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Ben Wolfson
> "Human kind has used its intelligence to vary the flavour of drinks, which
> may be sweet, aromatic, fermented or spirit-based. ... Family and social
> life also offer numerous other occasions to consume drinks for pleasure."
> [Larousse, "Drink" entry]
>
>


-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: arity-3 `iterate`

2015-11-16 Thread Ben Wolfson
If you rearrange the arguments of your proposed three-argument iterate:

(defn iterate
  ...
  ([f n x]
   (if (zero? n)
 x
 (recur f (f x) (dec n

it supports partial application of the multiply-composed function to
different arguments more easily, which suggests redefinition as:

(defn ncomp [f n]
  (loop [g identity n n]
(if (zero? n)
   g
   (recur (comp g f) (dec n)

This seems to come closer to matching what's desired from the
three-argument iterate without confusingly (as you note) overloading
"iterate": you want f applied n times to x and the stream of intermediate
values isn't really of interest.

On Mon, Nov 16, 2015 at 12:37 PM, Jason Felice 
wrote:

>
> I *frequently* see:
>
> (nth (iterate foo bar) n)
>
> And:
>
> (->> (iterate foo bar)
>(drop n)
>first)
>
> I've also coded this in `avi` after being surprised no such thing exists:
>
> (defn n-times
>   [thing n a-fn]
>   (reduce
> (fn [thing n]
>   (a-fn thing))
> thing
> (range n)))
>
> (which is kind of a bad implementation, now that I think of it):
>
> Would it be useful to file a ticket for a new arity for iterate?:
>
> (defn iterate
>   ...
>   ([f x n]
>(if (zero? n)
>  x
>  (recur f (f x) (dec n
>
> There's a little bit of weirdness - this returns a single value, while the
> other arity returns a lazy sequence.  However, I think it's the correct
> name (and the original arity might  have better been named "iterations" for
> symmetry with "reduce" and "reductions").  But ah well.
>
> Thoughts?
>
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


arity-3 `iterate`

2015-11-16 Thread Jason Felice
I *frequently* see:

(nth (iterate foo bar) n)

And:

(->> (iterate foo bar)
   (drop n)
   first)

I've also coded this in `avi` after being surprised no such thing exists:

(defn n-times
  [thing n a-fn]
  (reduce
(fn [thing n]
  (a-fn thing))
thing
(range n)))

(which is kind of a bad implementation, now that I think of it):

Would it be useful to file a ticket for a new arity for iterate?:

(defn iterate
  ...
  ([f x n]
   (if (zero? n)
 x
 (recur f (f x) (dec n

There's a little bit of weirdness - this returns a single value, while the
other arity returns a lazy sequence.  However, I think it's the correct
name (and the original arity might  have better been named "iterations" for
symmetry with "reduce" and "reductions").  But ah well.

Thoughts?

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: newbie Q: how to tweak file-seq (original: how to selectively iterate through a tree of directories) ?

2015-10-05 Thread Andy-
Try to adapt my code:

https://gist.github.com/rauhs/63054a06631c0be598d3

where you change your accept function to incorporate:

http://stackoverflow.com/questions/813710/java-1-6-determine-symbolic-links

HTH

On Saturday, October 3, 2015 at 5:14:51 PM UTC-4, hpw...@gmail.com wrote:
>
> The directory structure is
>/path/top/dir1
>/path/top/dir1/f1
>/path/top/dir1/f2
>/path/top/dir2 -> /another/path/dir2 
> --
>/another/path/dir2/g1
>/another/path/dir2/g2
>
> I tried this (following suggestion):
>
>   (for [file (file-seq dir) :while (.isFile file)] (.getPath file))
>
> It prints out a list with following items:
>   /path/top/dir1/f1
>   /path/top/dir1/f2
>   /path/top/dir2/g1
>   /path/top/dir2/g2
>
> BUT the last two items are the ones that I do NOT want.
> So, I guess I will need to tweak  file-seq so that it will NOT traverse
> /path/top/dir2  since it is a symbolic link.
>
> Is there anyway to tweak file-seq ??
>
> THanks
> HP
>
> On Saturday, October 3, 2015 at 11:59:40 AM UTC-4, Gary Verhaegen wrote:
>>
>> I'm on Windows at the moment, so I can't test, but I think you can 
>> filter on isFile: 
>>
>> (for [file (file-seq dir) 
>>   :where (.isFile file)] 
>>   (.getName file)) 
>>
>> should work, I think. 
>>
>> On 3 October 2015 at 07:36,   wrote: 
>> > Under linux, I have a tree of directories like this: 
>> > 
>> > /path/top 
>> > 
>> > /path/top/dir1  (in here there are two files f1, f2) 
>> > 
>> > /path/top/dir2 -> /another-path/dir2 (a symbolic link) 
>> > 
>> >  and under /another-path/dir2 there are two files g1, 
>> g2 
>> > 
>> > If I use below code, I would get a list of files f1, f2, and g1, g2 
>> > 
>> > (def directory (clojure.java.io/file "/path/top")) 
>> > (def files 
>> > (for [file (file-seq directory)] (.getName file))) 
>> > (files) 
>> > 
>> > BUT I want to skip traversing the directory dir2 since it is a symbolic 
>> > link. 
>> > 
>> > i.e. the list of files that I want to get is f1, f2 only. 
>> > 
>> > Could you please suggest a way to do this ? 
>> > 
>> > THanks 
>> > 
>> > HP 
>> > 
>> > -- 
>> > 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 
>> > --- 
>> > You received this message because you are subscribed to the Google 
>> Groups 
>> > "Clojure" group. 
>> > To unsubscribe from this group and stop receiving emails from it, send 
>> an 
>> > email to clojure+u...@googlegroups.com. 
>> > For more options, visit https://groups.google.com/d/optout. 
>>
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: newbie Q: how to tweak file-seq (original: how to selectively iterate through a tree of directories) ?

2015-10-03 Thread Gary Verhaegen
Sorry, I meant :when, not :where. Though it won't change the problem.

Here's the rub: first file-seq produces its whole seq, then for comes along
and filters out some of it. So (ignoring lazyness) file-seq ha already
traversed symbolic links (at least for folders) by the time for sees it.

I fear you'll have to write your own file traversal function. Fortunately,
it's not that hard. You can probably start from one of the examples here:

https://clojuredocs.org/clojure.core/tree-seq

On Saturday, 3 October 2015, > wrote:

> The directory structure is
>/path/top/dir1
>/path/top/dir1/f1
>/path/top/dir1/f2
>/path/top/dir2 -> /another/path/dir2
> --
>/another/path/dir2/g1
>/another/path/dir2/g2
>
> I tried this (following suggestion):
>
>   (for [file (file-seq dir) :while (.isFile file)] (.getPath file))
>
> It prints out a list with following items:
>   /path/top/dir1/f1
>   /path/top/dir1/f2
>   /path/top/dir2/g1
>   /path/top/dir2/g2
>
> BUT the last two items are the ones that I do NOT want.
> So, I guess I will need to tweak  file-seq so that it will NOT traverse
> /path/top/dir2  since it is a symbolic link.
>
> Is there anyway to tweak file-seq ??
>
> THanks
> HP
>
> On Saturday, October 3, 2015 at 11:59:40 AM UTC-4, Gary Verhaegen wrote:
>>
>> I'm on Windows at the moment, so I can't test, but I think you can
>> filter on isFile:
>>
>> (for [file (file-seq dir)
>>   :where (.isFile file)]
>>   (.getName file))
>>
>> should work, I think.
>>
>> On 3 October 2015 at 07:36,   wrote:
>> > Under linux, I have a tree of directories like this:
>> >
>> > /path/top
>> >
>> > /path/top/dir1  (in here there are two files f1, f2)
>> >
>> > /path/top/dir2 -> /another-path/dir2 (a symbolic link)
>> >
>> >  and under /another-path/dir2 there are two files g1,
>> g2
>> >
>> > If I use below code, I would get a list of files f1, f2, and g1, g2
>> >
>> > (def directory (clojure.java.io/file "/path/top"))
>> > (def files
>> > (for [file (file-seq directory)] (.getName file)))
>> > (files)
>> >
>> > BUT I want to skip traversing the directory dir2 since it is a symbolic
>> > link.
>> >
>> > i.e. the list of files that I want to get is f1, f2 only.
>> >
>> > Could you please suggest a way to do this ?
>> >
>> > THanks
>> >
>> > HP
>> >
>> > --
>> > 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
>> > ---
>> > You received this message because you are subscribed to the Google
>> Groups
>> > "Clojure" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> an
>> > email to clojure+u...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: newbie Q: how to tweak file-seq (original: how to selectively iterate through a tree of directories) ?

2015-10-03 Thread hpwei01
The directory structure is
   /path/top/dir1
   /path/top/dir1/f1
   /path/top/dir1/f2
   /path/top/dir2 -> /another/path/dir2 
--
   /another/path/dir2/g1
   /another/path/dir2/g2

I tried this (following suggestion):
   
  (for [file (file-seq dir) :while (.isFile file)] (.getPath file))

It prints out a list with following items:
  /path/top/dir1/f1
  /path/top/dir1/f2
  /path/top/dir2/g1
  /path/top/dir2/g2

BUT the last two items are the ones that I do NOT want.
So, I guess I will need to tweak  file-seq so that it will NOT traverse
/path/top/dir2  since it is a symbolic link.

Is there anyway to tweak file-seq ??

THanks
HP

On Saturday, October 3, 2015 at 11:59:40 AM UTC-4, Gary Verhaegen wrote:
>
> I'm on Windows at the moment, so I can't test, but I think you can 
> filter on isFile: 
>
> (for [file (file-seq dir) 
>   :where (.isFile file)] 
>   (.getName file)) 
>
> should work, I think. 
>
> On 3 October 2015 at 07:36,  > wrote: 
> > Under linux, I have a tree of directories like this: 
> > 
> > /path/top 
> > 
> > /path/top/dir1  (in here there are two files f1, f2) 
> > 
> > /path/top/dir2 -> /another-path/dir2 (a symbolic link) 
> > 
> >  and under /another-path/dir2 there are two files g1, g2 
> > 
> > If I use below code, I would get a list of files f1, f2, and g1, g2 
> > 
> > (def directory (clojure.java.io/file "/path/top")) 
> > (def files 
> > (for [file (file-seq directory)] (.getName file))) 
> > (files) 
> > 
> > BUT I want to skip traversing the directory dir2 since it is a symbolic 
> > link. 
> > 
> > i.e. the list of files that I want to get is f1, f2 only. 
> > 
> > Could you please suggest a way to do this ? 
> > 
> > THanks 
> > 
> > HP 
> > 
> > -- 
> > 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 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "Clojure" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to clojure+u...@googlegroups.com . 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: newbie question: how to selectively iterate through a tree of directories ?

2015-10-03 Thread Gary Verhaegen
I'm on Windows at the moment, so I can't test, but I think you can
filter on isFile:

(for [file (file-seq dir)
  :where (.isFile file)]
  (.getName file))

should work, I think.

On 3 October 2015 at 07:36,   wrote:
> Under linux, I have a tree of directories like this:
>
> /path/top
>
> /path/top/dir1  (in here there are two files f1, f2)
>
> /path/top/dir2 -> /another-path/dir2 (a symbolic link)
>
>  and under /another-path/dir2 there are two files g1, g2
>
> If I use below code, I would get a list of files f1, f2, and g1, g2
>
> (def directory (clojure.java.io/file "/path/top"))
> (def files
> (for [file (file-seq directory)] (.getName file)))
> (files)
>
> BUT I want to skip traversing the directory dir2 since it is a symbolic
> link.
>
> i.e. the list of files that I want to get is f1, f2 only.
>
> Could you please suggest a way to do this ?
>
> THanks
>
> HP
>
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


newbie question: how to selectively iterate through a tree of directories ?

2015-10-03 Thread hpwei01


Under linux, I have a tree of directories like this:

/path/top

/path/top/dir1  (in here there are two files f1, f2)

/path/top/dir2 -> /another-path/dir2 (a symbolic link) 

 and under /another-path/dir2 there are two files g1, g2

If I use below code, I would get a list of files f1, f2, and g1, g2

(def directory (clojure.java.io/file "/path/top"))(def files 
(for [file (file-seq directory)] (.getName file)))(files)

BUT I want to skip traversing the directory dir2 since it is a symbolic link.

i.e. the list of files that I want to get is f1, f2 only.

Could you please suggest a way to do this ?

THanks

HP

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: A generalization of iterate

2015-09-28 Thread Yoshinori Kohyama
Hi nchurch and all.

Your idea to generalize `iterate` looks very cool for me.
I wrote another implementation not to consume stack.

(defn generate' [f h & r]
  (cons h
(lazy-seq
  (apply generate' f
(reverse (cons (apply f h r) (reverse r)))

will work, if it is o.k. that we can assume at least one argument.

Yoshinori Kohyama

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: A generalization of iterate

2015-09-23 Thread nchurch
A, right.  Silly me.  This is something I have trouble remembering.  For 
some reason when you use regular concat, the error message is helpful 
(can't recur from non-tail position); with lazy-cat it shows a mismatch of 
argument numbers.

On Wednesday, September 23, 2015 at 2:10:47 PM UTC-7, Max Countryman wrote:
>
> Your problem with recur is because you can only recur from the tail 
> position. 
>
> On Sep 23, 2015, at 12:28, nchurch > 
> wrote:
>
> Yeah, it consumes stack just like the Clojurescript version of Iterate. 
>  I'm curious if anyone knows how to avoid this.  The Clojure version of 
> Iterate is Java; and for some reason 'recur' can't be used inside of 
> lazy-cat (not really documented, but apparently true). 
>
> On Tuesday, September 22, 2015 at 9:21:57 PM UTC-7, Max Countryman wrote:
>>
>> I wonder if something like this is a little easier to read?
>>
>> (defn generate
>>   ([f coll]
>>(generate f coll (reverse coll)))
>>   ([f coll args]
>>(let [next-val (apply f args)]
>>  (lazy-cat coll (generate f [next-val] (conj (butlast args) 
>> next-val))
>>
>> Where your Fibonacci example becomes:
>>
>> (take 100 (generate +’ [1 1]))
>>
>> As for your other questions, I can’t be of much help. But isn’t there 
>> nontrivial memory overhead to consider with this approach?
>>
>>
>> On Sep 22, 2015, at 18:19, nchurch  wrote:
>>
>> I was going through 4clojure (highly recommended!) and doing the 
>> Fibonacci exercise.  It occurred to me that the iterate function could be 
>> generalized in a reasonable way, so that the next value is generated by 
>> applying the function to the last N items so far, where N is the number of 
>> initial arguments beyond the function.  Thus:
>>
>> (generate inc 1) 
>>
>> works just like iterate, while
>>
>> (generate + 1 1)
>>
>> generates the Fibonacci sequence.
>>
>> Here's the code:
>>
>> (defn generate
>>   [f & more]
>>   (letfn [(recurse
>> [coll args]
>> (let [next-val (apply f args)]
>>   (lazy-cat coll (recurse [next-val] (conj (butlast args) 
>> next-val)]
>> (recurse more (reverse more
>>
>>
>> Code is also on Github <https://github.com/nchurch/generate>.
>>
>> I see this as part of a larger class of generalized sequence functions: for 
>> instance, extra arguments in a function given to *reduce* could refer to N 
>> arguments back in the sequence (might be useful, for instance, in smoothing 
>> a sequence of values using an average).
>>
>> Questions: is there an existing home for this kind of functionality?  And 
>> could the above code be improved?  (I just used the definition of *iterate* 
>> as a starting point.)
>>
>>
>>
>>
>> -- 
>> 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
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+u...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> -- 
> 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
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+u...@googlegroups.com .
> For more options, visit https://groups.google.com/d/optout.
>
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: A generalization of iterate

2015-09-23 Thread Max Countryman
Your problem with recur is because you can only recur from the tail position. 

> On Sep 23, 2015, at 12:28, nchurch  wrote:
> 
> Yeah, it consumes stack just like the Clojurescript version of Iterate.  I'm 
> curious if anyone knows how to avoid this.  The Clojure version of Iterate is 
> Java; and for some reason 'recur' can't be used inside of lazy-cat (not 
> really documented, but apparently true). 
> 
>> On Tuesday, September 22, 2015 at 9:21:57 PM UTC-7, Max Countryman wrote:
>> I wonder if something like this is a little easier to read?
>> 
>> (defn generate
>>   ([f coll]
>>(generate f coll (reverse coll)))
>>   ([f coll args]
>>(let [next-val (apply f args)]
>>  (lazy-cat coll (generate f [next-val] (conj (butlast args) 
>> next-val))
>> 
>> Where your Fibonacci example becomes:
>> 
>> (take 100 (generate +’ [1 1]))
>> 
>> As for your other questions, I can’t be of much help. But isn’t there 
>> nontrivial memory overhead to consider with this approach?
>> 
>> 
>>> On Sep 22, 2015, at 18:19, nchurch  wrote:
>>> 
>>> I was going through 4clojure (highly recommended!) and doing the Fibonacci 
>>> exercise.  It occurred to me that the iterate function could be generalized 
>>> in a reasonable way, so that the next value is generated by applying the 
>>> function to the last N items so far, where N is the number of initial 
>>> arguments beyond the function.  Thus:
>>> 
>>> (generate inc 1) 
>>> 
>>> works just like iterate, while
>>> 
>>> (generate + 1 1)
>>> 
>>> generates the Fibonacci sequence.
>>> 
>>> Here's the code:
>>> 
>>> (defn generate
>>>   [f & more]
>>>   (letfn [(recurse
>>> [coll args]
>>> (let [next-val (apply f args)]
>>>   (lazy-cat coll (recurse [next-val] (conj (butlast args) 
>>> next-val)]
>>> (recurse more (reverse more
>>> 
>>> Code is also on Github.
>>> 
>>> I see this as part of a larger class of generalized sequence functions: for 
>>> instance, extra arguments in a function given to reduce could refer to N 
>>> arguments back in the sequence (might be useful, for instance, in smoothing 
>>> a sequence of values using an average).
>>> 
>>> Questions: is there an existing home for this kind of functionality?  And 
>>> could the above code be improved?  (I just used the definition of iterate 
>>> as a starting point.)
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> 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
>>> --- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Clojure" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to clojure+u...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> 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
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: A generalization of iterate

2015-09-23 Thread nchurch
Yeah, it consumes stack just like the Clojurescript version of Iterate. 
 I'm curious if anyone knows how to avoid this.  The Clojure version of 
Iterate is Java; and for some reason 'recur' can't be used inside of 
lazy-cat (not really documented, but apparently true). 

On Tuesday, September 22, 2015 at 9:21:57 PM UTC-7, Max Countryman wrote:
>
> I wonder if something like this is a little easier to read?
>
> (defn generate
>   ([f coll]
>(generate f coll (reverse coll)))
>   ([f coll args]
>(let [next-val (apply f args)]
>  (lazy-cat coll (generate f [next-val] (conj (butlast args) 
> next-val))
>
> Where your Fibonacci example becomes:
>
> (take 100 (generate +’ [1 1]))
>
> As for your other questions, I can’t be of much help. But isn’t there 
> nontrivial memory overhead to consider with this approach?
>
>
> On Sep 22, 2015, at 18:19, nchurch > 
> wrote:
>
> I was going through 4clojure (highly recommended!) and doing the Fibonacci 
> exercise.  It occurred to me that the iterate function could be generalized 
> in a reasonable way, so that the next value is generated by applying the 
> function to the last N items so far, where N is the number of initial 
> arguments beyond the function.  Thus:
>
> (generate inc 1) 
>
> works just like iterate, while
>
> (generate + 1 1)
>
> generates the Fibonacci sequence.
>
> Here's the code:
>
> (defn generate
>   [f & more]
>   (letfn [(recurse
> [coll args]
> (let [next-val (apply f args)]
>   (lazy-cat coll (recurse [next-val] (conj (butlast args) 
> next-val)]
> (recurse more (reverse more
>
>
> Code is also on Github <https://github.com/nchurch/generate>.
>
> I see this as part of a larger class of generalized sequence functions: for 
> instance, extra arguments in a function given to *reduce* could refer to N 
> arguments back in the sequence (might be useful, for instance, in smoothing a 
> sequence of values using an average).
>
> Questions: is there an existing home for this kind of functionality?  And 
> could the above code be improved?  (I just used the definition of *iterate* 
> as a starting point.)
>
>
>
>
> -- 
> 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
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+u...@googlegroups.com .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: A generalization of iterate

2015-09-22 Thread Max Countryman
I wonder if something like this is a little easier to read?

(defn generate
  ([f coll]
   (generate f coll (reverse coll)))
  ([f coll args]
   (let [next-val (apply f args)]
 (lazy-cat coll (generate f [next-val] (conj (butlast args) next-val))

 Where your Fibonacci example becomes:

(take 100 (generate +’ [1 1]))

As for your other questions, I can’t be of much help. But isn’t there 
nontrivial memory overhead to consider with this approach?


> On Sep 22, 2015, at 18:19, nchurch  wrote:
> 
> I was going through 4clojure (highly recommended!) and doing the Fibonacci 
> exercise.  It occurred to me that the iterate function could be generalized 
> in a reasonable way, so that the next value is generated by applying the 
> function to the last N items so far, where N is the number of initial 
> arguments beyond the function.  Thus:
> 
> (generate inc 1) 
> 
> works just like iterate, while
> 
> (generate + 1 1)
> 
> generates the Fibonacci sequence.
> 
> Here's the code:
> 
> (defn generate
>   [f & more]
>   (letfn [(recurse
> [coll args]
> (let [next-val (apply f args)]
>   (lazy-cat coll (recurse [next-val] (conj (butlast args) 
> next-val)]
> (recurse more (reverse more
> 
> Code is also on Github <https://github.com/nchurch/generate>.
> 
> I see this as part of a larger class of generalized sequence functions: for 
> instance, extra arguments in a function given to reduce could refer to N 
> arguments back in the sequence (might be useful, for instance, in smoothing a 
> sequence of values using an average).
> 
> Questions: is there an existing home for this kind of functionality?  And 
> could the above code be improved?  (I just used the definition of iterate as 
> a starting point.)
> 
> 
> 
> 
> -- 
> 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 
> <http://groups.google.com/group/clojure?hl=en>
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com 
> <mailto:clojure+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


A generalization of iterate

2015-09-22 Thread nchurch
I was going through 4clojure (highly recommended!) and doing the Fibonacci 
exercise.  It occurred to me that the iterate function could be generalized 
in a reasonable way, so that the next value is generated by applying the 
function to the last N items so far, where N is the number of initial 
arguments beyond the function.  Thus:

(generate inc 1) 

works just like iterate, while

(generate + 1 1)

generates the Fibonacci sequence.

Here's the code:

(defn generate
  [f & more]
  (letfn [(recurse
[coll args]
(let [next-val (apply f args)]
  (lazy-cat coll (recurse [next-val] (conj (butlast args) 
next-val)]
(recurse more (reverse more


Code is also on Github <https://github.com/nchurch/generate>.

I see this as part of a larger class of generalized sequence functions: for 
instance, extra arguments in a function given to *reduce* could refer to N 
arguments back in the sequence (might be useful, for instance, in smoothing a 
sequence of values using an average).

Questions: is there an existing home for this kind of functionality?  And could 
the above code be improved?  (I just used the definition of *iterate* as a 
starting point.)



-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to iterate over maps and drop one specific element each time?

2014-06-11 Thread Jonathan Winandy
Hi !

If you want to keep certain entries, there is also select-keys :

(map #(select-keys % [:v :b :z])  [{:op :e :v 1} {:op :n :b 2} {:op :m :z
2.3}])





On Wed, Jun 11, 2014 at 3:12 PM, Di Xu  wrote:

> (map #(dissoc % :op) [{:op :e :v 1} {:op :n :b 2} {:op :m :z 2.3}])
>
> 2014-06-11 21:09 GMT+08:00 Hussein B. :
>
> Hi,
>>
>> I have a seq of maps:
>>
>> [ {:op :e :v 1} {:op :n :b 2} {:op :m :z 2.3} ]
>>
>> How to iterate over the sequence and extracting only the non-op entries?
>>
>> Desired result is:
>>
>> [ {:v 1} {:b 2} {:z 2.3} ]
>>
>> Thanks for help and time.
>>
>> --
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to iterate over maps and drop one specific element each time?

2014-06-11 Thread Di Xu
(map #(dissoc % :op) [{:op :e :v 1} {:op :n :b 2} {:op :m :z 2.3}])

2014-06-11 21:09 GMT+08:00 Hussein B. :

> Hi,
>
> I have a seq of maps:
>
> [ {:op :e :v 1} {:op :n :b 2} {:op :m :z 2.3} ]
>
> How to iterate over the sequence and extracting only the non-op entries?
>
> Desired result is:
>
> [ {:v 1} {:b 2} {:z 2.3} ]
>
> Thanks for help and time.
>
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to iterate over maps and drop one specific element each time?

2014-06-11 Thread Hussein B.
Hi,

I have a seq of maps:

[ {:op :e :v 1} {:op :n :b 2} {:op :m :z 2.3} ]

How to iterate over the sequence and extracting only the non-op entries?

Desired result is:

[ {:v 1} {:b 2} {:z 2.3} ]

Thanks for help and time.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-12-02 Thread Christophe Grand
Hi maclo,

Your code assume there is two "primes" sequences: the result and the one
being constructed. But there's only one and which is infinite.
You define your sequence as being (cons 2 something) and computing
"something" requires evaluating (every? #(pos? (mod 3 %)) (cons 2
something))
since (pos? (mod 3 2)) is true, every? continues processing the seq and
tries to evaluate (every? #(pos? (mod 3 %)) something).
So computing "something" requires knowing "something"!

The take-while is useful in that it reduces the scope of every to primes
already computed.

Your code works only because of a bug/unspecified behaviour which makes the
recursive reference of the sequence to be considered nil.

hth,

Christophe


On Sun, Dec 2, 2012 at 4:30 PM, maclo  wrote:

> Hi
>
>
>  user=> (def primes
>
>>   (cons 2
>> (filter
>>   (fn isprime[n]
>>
>> (every?
>>   #(pos? (mod n %))
>>   (take-while #(<=(* % %)n) primes)))
>>   (iterate inc 3
>> user=> (take 50 primes)
>> (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
>> 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193
>> 197 199 211 223 227 229)
>>
>
>  Is it really necessary to call 'take-while' ? The version without it
>
>
> (def primes
>   (cons 2
> (filter
>   (fn isprime? [n]
> (every? #(pos? (mod n %)) primes))
>   (iterate inc 3
>
> works for me as well. Is it safe to use this version or is using
> 'take-while' for some reason necessary ?
>
> maclo
>



-- 
On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

-- 
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: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-12-02 Thread maclo
Hi

 user=> (def primes

>   (cons 2
> (filter
>   (fn isprime[n]
>
> (every?
>   #(pos? (mod n %))
>   (take-while #(<=(* % %)n) primes)))
>   (iterate inc 3
> user=> (take 50 primes)
> (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
> 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 
> 197 199 211 223 227 229)
>
 
 Is it really necessary to call 'take-while' ? The version without it

(def primes
  (cons 2
(filter
  (fn isprime? [n]
(every? #(pos? (mod n %)) primes))
  (iterate inc 3

works for me as well. Is it safe to use this version or is using 
'take-while' for some reason necessary ?

maclo

-- 
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: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-30 Thread Christophe Grand
Hallo,

On Thu, Nov 29, 2012 at 11:20 PM, Ulrich  wrote:

> Now should we consider this a clojure bug?
>

It is, see http://dev.clojure.org/jira/browse/CLJ-457

What clojure version are you using btw to get the exceptions?


master from Github with my patch to CLJ-457 applied :-)

Now, that's clear, anyway, the main point was and still is,
> that the code (the version corrected with a base case)
>
> (def primes
>   (cons
> 2
> (filter
>   (fn[n]
> (every?
>   #(pos? (mod n %))
>   (take-while #(<=(*%%)n) primes)
>   )
> )
>   (drop 3 (range))
>   )
> )
>   )
>
>
> SHOULD imho properly work in a proper clojure implementation,
> n'est ce pas?
>

Genau and it certainly worked in Clojure 1.0 (chunked sequences were
introduced in 1.1).

So, with my patch applied, this code still does not work but at least we
get an exception rather than an incorrect result. Not perfect but, in my
opinion, a step towards correctness (and it should also break some rare
code ("recursive" chunked seqs only) which happens to work by luck).

Christophe

-- 
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: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-29 Thread Ulrich

You are right demanding a base case. I don't deny that any more. As stated 
above, me as a beginner, experimenting for experience, took the well-known 
Fibonacci corecursion (with base case) as guidance in setting my first 
prime corecursion code with range, and then reduced the base case from '(2 
3 5) to '(2 3) realizing that it didn't work any longer. Then finding that 
with "iterate" it did. Then further reduced to '(2) and '(), finding that 
it still worked. What clojure version are you using btw to get the 
exceptions? I never got those exceptions but the correct result (tried with 
1.2, 1.3 and 1.4). So thought with '() everything is still fine, so thought 
I can omit the whole concat. This was seconded from a mathematical point of 
view: the only subset S from X=[2,3,4,...] where each n is included, if it 
has no divisor in {m in S: m*m<=n} is THE sequence of primes. But of course 
clojure won't be able (yet :) ) to logically deduce this relation simply by 
evaluating the corecursion code. And simple calculation run into problems 
while evaluating "take-while". With a base case "take-while" will return 
properly.

Now, that's clear, anyway, the main point was and still is,
that the code (the version corrected with a base case)

(def primes
  (cons
2
(filter
  (fn[n]
(every?
  #(pos? (mod n %))
  (take-while #(<=(*%%)n) primes)
  )
)
  (drop 3 (range))
  )
)
  )


SHOULD imho properly work in a proper clojure implementation,
n'est ce pas?

If it doesn't because of internal!!! representation
of the involved sequences,
be it chunked or something else,
than this is a huge pitfall,
which might be hard to detect
in more complicated structures.

Now should we consider this a clojure bug?

Ulrich.

-- 
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: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-29 Thread Christophe Grand
On Wed, Nov 28, 2012 at 5:24 PM, Ulrich  wrote:

> Then I found the solution with "iterate" and then further reduced starting
> values to '(2) and then to '() what worked as well,
> as every? returns true for empty sequences, what is right from a logical
> point of view too. And with '() as start even the concat wasn't necessary
> any more. So this was no luck, but intention :)
>

It was your intention then but based on a false premise: a seq is immutable
and as such can't be both empty and non-empty depending how/when you access
it. There is no fix-point semantics to justify this behaviour. So to me
it's a bug.

With '(2 3 5) it worked because it was enough to remove all non primes from
the first chunk (< 32).

With the patch I propose I get:
* without a base case for recursion, I get an exception, chunked or not.
user=> (def primes
(filter
  (fn isprime[n]

(every?
  #(pos? (mod n %))
  (take-while #(<=(* % %)n) primes)))
  (drop 2 (range

#'user/primes
user=> (take 50 primes)
RuntimeException Recursive seq realization  clojure.lang.LazySeq.sval
(LazySeq.java:64)
user=> (def primes
(filter
  (fn isprime[n]

(every?
  #(pos? (mod n %))
  (take-while #(<=(* % %)n) primes)))
  (iterate inc 2)))
#'user/primes
user=> (take 50 primes)
RuntimeException Recursive seq realization  clojure.lang.LazySeq.sval
(LazySeq.java:64)

* With a base case however I get either the right result or an exception.
Even if the chunkiness of the seq still changes the outcome, it's an
improvement over the current situation where one gets either the right
result or an incorrect result. Better to fail than to err.
user=> (def primes
  (cons 2
(filter
  (fn isprime[n]

    (every?
  #(pos? (mod n %))
  (take-while #(<=(* % %)n) primes)))
  (iterate inc 3
user=> (take 50 primes)
(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101
103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
199 211 223 227 229)
user=> (def primes
  (cons 2
(filter
  (fn isprime[n]

(every?
  #(pos? (mod n %))
  (take-while #(<=(* % %)n) primes)))
  (drop 3 (range)
#'user/primes
user=> (take 50 primes)
RuntimeException Recursive seq realization  clojure.lang.LazySeq.sval
(LazySeq.java:64)


On Thu, Nov 29, 2012 at 1:11 AM, Ulrich  wrote:

> Has the afore mentioned interface for forced single-item consumption
> meanwhile been created?


Afaik, no.


Christophe

-- 
On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

-- 
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: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-28 Thread Ulrich

... so the main problem seems to be chunked vs. unchunked sequences in 
corecursion.
I read a bit into "chunked sequences" and so far it seems that a sequence 
is either chunked with size 32 or unchunked. So a "rechunk" of variable 
size seems of no use. Instead I found an "unchunk" at stackoverflow:

(def unchunk #(lazy-seq(when(seq%)(cons(first%)(unchunk(rest%))

Now the original code posted at top also works with "(drop 2 
(unchunk(range)))".
Further I found following remark by RichHickey in changes.txt for Clojure 
1.1 
(https://github.com/richhickey/clojure/blob/68aa96d832703f98f80b18cecc877e3b93bc5d26/changes.txt#L85)

Consumption of chunked-seqs as normal seqs should be completely transparent. 
However,  
note that some sequence processing will occur up to 32 elements at a time. This 
could  
matter to you if you are relying on full laziness to preclude the generation of 
any  
non-consumed results. An interface to force single-item consumption of chunked 
seqs is  
still being designed. Please share any use cases where chunked processing has 
resulted  
in behavioral differences that matter to you on the Clojure google group. 

Now, this is a good such example, where chunking leads to behavioral error. 
Has the afore mentioned interface for forced single-item consumption 
meanwhile been created?

Ulrich

-- 
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: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-28 Thread Ulrich


Am Mittwoch, 28. November 2012 16:42:32 UTC+1 schrieb Christophe Grand:
>
> Hi Ulrich,
>
> Wow, you got me scratching my head because your code should not even work.
> The problem I see with it is that your recursion has no base case. You 
> have to know that 2 is prime to boot the algorithm. Eg
>
> ... 

Salut Christophe,

thanks first for pointing out to the clojure source for tracking down 
problems, I hadn't thought about that yet.
Considering base case, in fact, the first code I started off with, was 
something similiar with starting values 2,3,5 :

(def primes
  (concat
'(2 3 5)
(filter
  (fn[n]
(every?
  #(pos? (mod n %))
  (take-while #(<=(*%%)n) primes)
  )
)
  (drop 6 (range))
  )
)   
  ) 

What works. But with '(2,3) (or '(2) like you suggested) this doesn't work 
any more, as 25 is included in the first "batch" (of the so called chunked 
sequence) which is only tested against the starting value of primes.

Then I found the solution with "iterate" and then further reduced starting 
values to '(2) and then to '() what worked as well,
as every? returns true for empty sequences, what is right from a logical 
point of view too. And with '() as start even the concat wasn't necessary 
any more. So this was no luck, but intention :)

Anyway, the question that still remains is, how one could "rechunk" a 
chunked sequence?

Ulrich.
 
 

>
>

-- 
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: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-28 Thread Christophe Grand
A more succinct test case:
user=> (def escher-seq (lazy-seq (lazy-seq (if (seq escher-seq) nil (list
42)
#'user/escher-seq
user=> escher-seq
(42)

thus escher-seq is not empty because it is empty :-)


On Wed, Nov 28, 2012 at 4:42 PM, Christophe Grand wrote:

> Hi Ulrich,
>
> Wow, you got me scratching my head because your code should not even work.
> The problem I see with it is that your recursion has no base case. You
> have to know that 2 is prime to boot the algorithm. Eg
>
> (def primes
>   (cons 2
>
> (filter
>   (fn isprime[n]
> (every?
>   #(pos? (mod n %))
>       (take-while #(<=(*%%)n) primes)))
>   (iterate inc 3
>
>
> So now we have two behaviours to explain:
> 1/ why it works with iterate
> 2/ why it doesn't work with range
> The difference comes from range returning a chunked seq (which explain the
> batched processing you rightly identified)
>
> Both  have a definitive reentrant smell.
>
> 1/ Let's look at the code for LazySeq
> https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LazySeq.java#L59especially
>  the seq and sval methods.
> So calling seq (and thus realizing the first prime) on a yet unrealized
> primes sequence causes at some points to evaluate
> (every?
>   #(pos? (mod n %))
>   (take-while #(<=(*%%)n) primes))
>
> which by the nature of every? is going to realize at least one item of
> primes. But we are exactly trying to compute it!
> From the implementation point of view, we are in the first call to seq, in
> the while loop. f has been cleared bu the first call to sval (which set sv)
> and sv cleared before entering the while and s has still its default value:
> null.
> Thus a recursive call to seq causes is going to see sv as null and returns
> s which is null!
> So the primes into the take-while is considered empty! Thus every? returns
> true and 2 is considered prime! By luck.
>
> A sentinel or a flag could solve the problem.
>
> 2/ This is the same problem exacerbated by the fact that we are producing
> a chunked seq so the recursive call sees an empty prime for the first 30
> items.
>
> However if you swap iterate by range in my version (which is correct,
> having a base case) you still have a reentrance bug
>
> ser=> (def primes
>   (cons 2
>
> (filter
>   (fn isprime[n]
> (every?
>   #(pos? (mod n %))
>   (take-while #(<=(* % %)n) primes)))
>   (drop 3 (range)
>
> #'user/primes
> user=> (take 50 primes)
> (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 37 41 43 47 53 59 61 67 71 73
> 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179
> 181 191 193 197)
>
> Christophe
>
>
>
>
> On Wed, Nov 28, 2012 at 2:29 PM, Ulrich  wrote:
>
>> Hello,
>>
>> I tried to define the sequence of primes via corecursion,
>> with trial divisions by primes p with p*p<=n,
>> finally arriving at following code.
>>
>> (def primes
>>   (filter
>> (fn isprime[n]
>>   (every?
>> #(pos? (mod n %))
>> (take-while #(<=(*%%)n) primes)
>> )
>>   )
>> (iterate inc 2)
>> )
>>   )
>>
>> Seems there's nothing wrong with that,
>> it's just the algorithm straightly set down
>> in a functional way. And it works correctly
>> as one can quickly confirm by "(take 50 primes)".
>>
>> But if replacing "(iterate inc 2)" with "(drop 2 (range))",
>> we get strange behaviour, "primes" then consists of all!!
>> numbers from 2 until 30 followed by primes only from 31 on.
>>
>> Being relatively new to clojure, I find this
>> very irritating and a potential cause of bad bugs.
>> And hope that this is not a bug in clojure itself
>> or even bad language design, but rather
>> some basic misunderstanding by me.
>>
>> So I analyzed it a bit, and it seems, that
>> "range" triggers? execution of "drop" and "filter"
>> in batches of 32 numbers, and "primes" in "take-while"
>> is not updated during such a batch. While using
>> "iterate" instead updates "primes" each step.
>>
>> Can someone more into clojure than me please correct and
>> better explain internal reasons of this strange behaviour.
>> How could one know the "batch size" of more complicated
>> expressions? And how could "range" be wrapped to yield
>> numbers one by one?
>>
>> Thanks, Ulrich.
>>
>>
&g

Re: (iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-28 Thread Christophe Grand
Hi Ulrich,

Wow, you got me scratching my head because your code should not even work.
The problem I see with it is that your recursion has no base case. You have
to know that 2 is prime to boot the algorithm. Eg

(def primes
  (cons 2
(filter
  (fn isprime[n]
(every?
  #(pos? (mod n %))
  (take-while #(<=(*%%)n) primes)))
  (iterate inc 3


So now we have two behaviours to explain:
1/ why it works with iterate
2/ why it doesn't work with range
The difference comes from range returning a chunked seq (which explain the
batched processing you rightly identified)

Both  have a definitive reentrant smell.

1/ Let's look at the code for LazySeq
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LazySeq.java#L59especially
the seq and sval methods.
So calling seq (and thus realizing the first prime) on a yet unrealized
primes sequence causes at some points to evaluate
(every?
  #(pos? (mod n %))
  (take-while #(<=(*%%)n) primes))

which by the nature of every? is going to realize at least one item of
primes. But we are exactly trying to compute it!
>From the implementation point of view, we are in the first call to seq, in
the while loop. f has been cleared bu the first call to sval (which set sv)
and sv cleared before entering the while and s has still its default value:
null.
Thus a recursive call to seq causes is going to see sv as null and returns
s which is null!
So the primes into the take-while is considered empty! Thus every? returns
true and 2 is considered prime! By luck.

A sentinel or a flag could solve the problem.

2/ This is the same problem exacerbated by the fact that we are producing a
chunked seq so the recursive call sees an empty prime for the first 30
items.

However if you swap iterate by range in my version (which is correct,
having a base case) you still have a reentrance bug

ser=> (def primes
  (cons 2
(filter
  (fn isprime[n]
(every?
  #(pos? (mod n %))
  (take-while #(<=(* % %)n) primes)))
  (drop 3 (range)

#'user/primes
user=> (take 50 primes)
(2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 37 41 43 47 53 59 61 67 71 73
79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179
181 191 193 197)

Christophe




On Wed, Nov 28, 2012 at 2:29 PM, Ulrich  wrote:

> Hello,
>
> I tried to define the sequence of primes via corecursion,
> with trial divisions by primes p with p*p<=n,
> finally arriving at following code.
>
> (def primes
>   (filter
> (fn isprime[n]
>   (every?
> #(pos? (mod n %))
> (take-while #(<=(*%%)n) primes)
> )
>   )
> (iterate inc 2)
> )
>   )
>
> Seems there's nothing wrong with that,
> it's just the algorithm straightly set down
> in a functional way. And it works correctly
> as one can quickly confirm by "(take 50 primes)".
>
> But if replacing "(iterate inc 2)" with "(drop 2 (range))",
> we get strange behaviour, "primes" then consists of all!!
> numbers from 2 until 30 followed by primes only from 31 on.
>
> Being relatively new to clojure, I find this
> very irritating and a potential cause of bad bugs.
> And hope that this is not a bug in clojure itself
> or even bad language design, but rather
> some basic misunderstanding by me.
>
> So I analyzed it a bit, and it seems, that
> "range" triggers? execution of "drop" and "filter"
> in batches of 32 numbers, and "primes" in "take-while"
> is not updated during such a batch. While using
> "iterate" instead updates "primes" each step.
>
> Can someone more into clojure than me please correct and
> better explain internal reasons of this strange behaviour.
> How could one know the "batch size" of more complicated
> expressions? And how could "range" be wrapped to yield
> numbers one by one?
>
> Thanks, Ulrich.
>
>
>  --
> 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




-- 
On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

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

(iterate inc 2) vs (drop 2 (range)) in corecursion

2012-11-28 Thread Ulrich
Hello,

I tried to define the sequence of primes via corecursion,
with trial divisions by primes p with p*p<=n,
finally arriving at following code.

(def primes
  (filter
(fn isprime[n]
  (every?
#(pos? (mod n %))
(take-while #(<=(*%%)n) primes)
)
  )
(iterate inc 2)
)
  )

Seems there's nothing wrong with that,
it's just the algorithm straightly set down
in a functional way. And it works correctly
as one can quickly confirm by "(take 50 primes)".

But if replacing "(iterate inc 2)" with "(drop 2 (range))",
we get strange behaviour, "primes" then consists of all!!
numbers from 2 until 30 followed by primes only from 31 on.

Being relatively new to clojure, I find this
very irritating and a potential cause of bad bugs.
And hope that this is not a bug in clojure itself
or even bad language design, but rather  
some basic misunderstanding by me.

So I analyzed it a bit, and it seems, that 
"range" triggers? execution of "drop" and "filter"
in batches of 32 numbers, and "primes" in "take-while"  
is not updated during such a batch. While using 
"iterate" instead updates "primes" each step.

Can someone more into clojure than me please correct and
better explain internal reasons of this strange behaviour.
How could one know the "batch size" of more complicated
expressions? And how could "range" be wrapped to yield
numbers one by one?

Thanks, Ulrich.


-- 
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: idiomatic use of iterate + cycle ?

2012-09-16 Thread Jim - FooBar();
exactly!!! this is great stuff indeed! I spent all morning trying to 
find more info than the actual docstring but I can't find anything! It 
seems to work as advertised though...very handy but it only works with 
1.5 :-)


(reduce #(if (= 3 %2) (reduced %) (conj % %2)) [] (range 5))

=>[0 1 2]


Jim


On 16/09/12 18:15, Sean Corfield wrote:
On Sun, Sep 16, 2012 at 5:15 AM, Jim - FooBar(); > wrote:


It turns out that reduce is exactly what I need...I didn't know
this but there is a handy 'reduced' fn that makes it easy to
terminate from within a reduce at any given time. At least this is
what i understand from the docs...so the final thing looks like this:


Wow, that is a super nice addition to 1.5 - I don't remember reading 
about that anywhere but it went in back at the end of April:


https://github.com/clojure/clojure/commit/96e8596cfdd29a2bb245d958683ee5fc1353b87a
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
--
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 


--
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: idiomatic use of iterate + cycle ?

2012-09-16 Thread Sean Corfield
On Sun, Sep 16, 2012 at 5:15 AM, Jim - FooBar(); wrote:

> It turns out that reduce is exactly what I need...I didn't know this but
> there is a handy 'reduced' fn that makes it easy to terminate from within a
> reduce at any given time. At least this is what i understand from the
> docs...so the final thing looks like this:
>

Wow, that is a super nice addition to 1.5 - I don't remember reading about
that anywhere but it went in back at the end of April:

https://github.com/clojure/clojure/commit/96e8596cfdd29a2bb245d958683ee5fc1353b87a
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

-- 
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: idiomatic use of iterate + cycle ?

2012-09-16 Thread Jim - FooBar();

Hi again,

It turns out that reduce is exactly what I need...I didn't know this but 
there is a handy 'reduced' fn that makes it easy to terminate from 
within a reduce at any given time. At least this is what i understand 
from the docs...so the final thing looks like this:


(defn tournament
"Starts a tournament between the 2 players (-1 1). If there is no 
winner, returns the entire history (vector) of
 the tournament after 100 moves. If there is a winner, a map will be 
returned containing the :winner and the :history."

[sb depth]
(reduce
  (fn [history dir]
  (let [cb (peek history)
win-dir (jit-referee cb)] ;;just-in-time referee looks for 
missing kings
(if win-dir (reduced {:winner win-dir :history history}) 
;;terminate with this map

(conj history (-> (chess-best-move dir cb depth)
  (:move)
  (try-move))
 [sb] (take 100 (cycle '(-1 1) ;;50 moves each should be enough

Ok, it's certainly not a one-liner but it's pretty clear and readable so 
I'm happy :-)


Jim



On 16/09/12 11:59, Jim - FooBar(); wrote:

Hi all,

I'm trying to come up with a way to create a 'tournament' fn that 
basically alternates between players (in turns) and calls soem 'move' 
fn on each. Now, obviously this can be done with loop/recur no 
problem, however perhaps a combination of cycle & iterate is more 
appropriate...so I'm, thinking something along these lines :


(defn tournament [board] ;;no need for players - directions are standard
(vec ;;return a vector of the entire history of this tournament
  (iterate #(chess-best-move (cycle [-1 1]) ) board))) 
;;chess-best-move returns new board



This is obviously not good enough...cycle is in the wrong place! I 
usually use cycle in combination with map rather than iterate and now 
I'm at a loss...I can't exactly use map in this case cos i need the 
calls to 'move' to be chained (passing the new board to the next 
one)...The truth is I can use reduce like this:


(defn tournament [b]
(reduce
  (fn [history dir]
(conj history (chess-best-move dir (peek history
  [b] (take 100 (cycle '(-1 1)  ;;100 moves maximum

I just thought iterate is a better fit...can anyone think how this can 
be done in a one-liner using iterate+cycle or am I at a good path with 
reduce?


Jim


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


idiomatic use of iterate + cycle ?

2012-09-16 Thread Jim - FooBar();

Hi all,

I'm trying to come up with a way to create a 'tournament' fn that 
basically alternates between players (in turns) and calls soem 'move' fn 
on each. Now, obviously this can be done with loop/recur no problem, 
however perhaps a combination of cycle & iterate is more 
appropriate...so I'm, thinking something along these lines :


(defn tournament [board] ;;no need for players - directions are standard
(vec ;;return a vector of the entire history of this tournament
  (iterate #(chess-best-move (cycle [-1 1]) ) board))) 
;;chess-best-move returns new board



This is obviously not good enough...cycle is in the wrong place! I 
usually use cycle in combination with map rather than iterate and now 
I'm at a loss...I can't exactly use map in this case cos i need the 
calls to 'move' to be chained (passing the new board to the next 
one)...The truth is I can use reduce like this:


(defn tournament [b]
(reduce
  (fn [history dir]
(conj history (chess-best-move dir (peek history
  [b] (take 100 (cycle '(-1 1)  ;;100 moves maximum

I just thought iterate is a better fit...can anyone think how this can 
be done in a one-liner using iterate+cycle or am I at a good path with 
reduce?


Jim

--
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: Iterate and stack overflow

2012-06-14 Thread vmargioulas
Thanks, this explains the stack overflow

On Thursday, June 14, 2012 4:12:28 PM UTC+3, Dave Sann wrote:
>
> also
>
>  (first (drop 100000 (iterate #(doall (map inc %)) (range 10
>
> so the better answer is probably - because map is lazy
>
>
> On Thursday, 14 June 2012 23:06:27 UTC+10, Dave Sann wrote:
>>
>> I suspect that the answer is the use of partial and the implementation of 
>> iterate
>>
>> with (comp vec... you force realisation of the vector
>>
>> without this I think you get (partial map (partial map (partial ) as 
>> f is repeatedly applied
>>
>> if you do this,
>>
>> (first (drop 10 (iterate #(apply list (map inc %)) (range 10
>>
>> which forces the list to be realised, it wont overflow
>>
>> D
>>
>>
>> On Thursday, 14 June 2012 23:00:15 UTC+10, Dave Sann wrote:
>>>
>>> ah...but does for 1
>>>
>>>
>>> On Thursday, 14 June 2012 22:58:58 UTC+10, Dave Sann wrote:
>>>>
>>>> It doesn't overflow for me.
>>>>
>>>>
>>>> user=> (first (drop 1000 (iterate (partial map inc) (range 10
>>>> (1000 1001 1002 1003 1004 1005 1006 1007 1008 1009)
>>>>
>>>>
>>>> On Thursday, 14 June 2012 22:52:33 UTC+10, vmargioulas wrote:
>>>>>
>>>>> Can someone explain why 
>>>>> ... iterating over a sequence cause a stack overflow 
>>>>> (first (drop 1000 (iterate (partial map inc) (range 10 -> 
>>>>> java.lang.StackOverflowError 
>>>>>
>>>>> ...but iterating over a vector works ok? 
>>>>> (first (drop 1000 (iterate (comp vec (partial map inc)) (range 10 
>>>>> - 
>>>>> > [1000 1001 1002 1003 1004 1005 1006 1007 1008 1009] 
>>>>>
>>>>

-- 
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: Iterate and stack overflow

2012-06-14 Thread Dave Sann
also

 (first (drop 10 (iterate #(doall (map inc %)) (range 10

so the better answer is probably - because map is lazy


On Thursday, 14 June 2012 23:06:27 UTC+10, Dave Sann wrote:
>
> I suspect that the answer is the use of partial and the implementation of 
> iterate
>
> with (comp vec... you force realisation of the vector
>
> without this I think you get (partial map (partial map (partial ) as f 
> is repeatedly applied
>
> if you do this,
>
> (first (drop 10 (iterate #(apply list (map inc %)) (range 10
>
> which forces the list to be realised, it wont overflow
>
> D
>
>
> On Thursday, 14 June 2012 23:00:15 UTC+10, Dave Sann wrote:
>>
>> ah...but does for 1
>>
>>
>> On Thursday, 14 June 2012 22:58:58 UTC+10, Dave Sann wrote:
>>>
>>> It doesn't overflow for me.
>>>
>>>
>>> user=> (first (drop 1000 (iterate (partial map inc) (range 10
>>> (1000 1001 1002 1003 1004 1005 1006 1007 1008 1009)
>>>
>>>
>>> On Thursday, 14 June 2012 22:52:33 UTC+10, vmargioulas wrote:
>>>>
>>>> Can someone explain why 
>>>> ... iterating over a sequence cause a stack overflow 
>>>> (first (drop 1000 (iterate (partial map inc) (range 10 -> 
>>>> java.lang.StackOverflowError 
>>>>
>>>> ...but iterating over a vector works ok? 
>>>> (first (drop 1000 (iterate (comp vec (partial map inc)) (range 10 - 
>>>> > [1000 1001 1002 1003 1004 1005 1006 1007 1008 1009] 
>>>>
>>>

-- 
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: Iterate and stack overflow

2012-06-14 Thread Dave Sann
I suspect that the answer is the use of partial and the implementation of 
iterate

with (comp vec... you force realisation of the vector

without this I think you get (partial map (partial map (partial ) as f 
is repeatedly applied

if you do this,

(first (drop 10 (iterate #(apply list (map inc %)) (range 10

which forces the list to be realised, it wont overflow

D


On Thursday, 14 June 2012 23:00:15 UTC+10, Dave Sann wrote:
>
> ah...but does for 1
>
>
> On Thursday, 14 June 2012 22:58:58 UTC+10, Dave Sann wrote:
>>
>> It doesn't overflow for me.
>>
>>
>> user=> (first (drop 1000 (iterate (partial map inc) (range 10
>> (1000 1001 1002 1003 1004 1005 1006 1007 1008 1009)
>>
>>
>> On Thursday, 14 June 2012 22:52:33 UTC+10, vmargioulas wrote:
>>>
>>> Can someone explain why 
>>> ... iterating over a sequence cause a stack overflow 
>>> (first (drop 1000 (iterate (partial map inc) (range 10)))) -> 
>>> java.lang.StackOverflowError 
>>>
>>> ...but iterating over a vector works ok? 
>>> (first (drop 1000 (iterate (comp vec (partial map inc)) (range 10 - 
>>> > [1000 1001 1002 1003 1004 1005 1006 1007 1008 1009] 
>>>
>>

-- 
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: Iterate and stack overflow

2012-06-14 Thread Dave Sann
ah...but does for 1


On Thursday, 14 June 2012 22:58:58 UTC+10, Dave Sann wrote:
>
> It doesn't overflow for me.
>
>
> user=> (first (drop 1000 (iterate (partial map inc) (range 10
> (1000 1001 1002 1003 1004 1005 1006 1007 1008 1009)
>
>
> On Thursday, 14 June 2012 22:52:33 UTC+10, vmargioulas wrote:
>>
>> Can someone explain why 
>> ... iterating over a sequence cause a stack overflow 
>> (first (drop 1000 (iterate (partial map inc) (range 10 -> 
>> java.lang.StackOverflowError 
>>
>> ...but iterating over a vector works ok? 
>> (first (drop 1000 (iterate (comp vec (partial map inc)) (range 10 - 
>> > [1000 1001 1002 1003 1004 1005 1006 1007 1008 1009] 
>>
>

-- 
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: Iterate and stack overflow

2012-06-14 Thread Dave Sann
It doesn't overflow for me.


user=> (first (drop 1000 (iterate (partial map inc) (range 10
(1000 1001 1002 1003 1004 1005 1006 1007 1008 1009)


On Thursday, 14 June 2012 22:52:33 UTC+10, vmargioulas wrote:
>
> Can someone explain why 
> ... iterating over a sequence cause a stack overflow 
> (first (drop 1000 (iterate (partial map inc) (range 10 -> 
> java.lang.StackOverflowError 
>
> ...but iterating over a vector works ok? 
> (first (drop 1000 (iterate (comp vec (partial map inc)) (range 10 - 
> > [1000 1001 1002 1003 1004 1005 1006 1007 1008 1009] 
>

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

Iterate and stack overflow

2012-06-14 Thread vmargioulas
Can someone explain why
... iterating over a sequence cause a stack overflow
(first (drop 1000 (iterate (partial map inc) (range 10 ->
java.lang.StackOverflowError

...but iterating over a vector works ok?
(first (drop 1000 (iterate (comp vec (partial map inc)) (range 10 -
> [1000 1001 1002 1003 1004 1005 1006 1007 1008 1009]

-- 
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: iterate with side-effect function?

2011-09-28 Thread siyu798
Thanks everyone.

-- 
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: iterate with side-effect function?

2011-09-28 Thread Meikel Brandmeyer (kotarak)
Hi,

there is ye olde loop/recur, which handles side effects (and this case in 
particular) quite nicely. The only advantage of iterate is that the sequence 
of intermediate values is available to an outside observer. However, as 
stated before: mixing lazy sequences with side effects is asking for 
trouble.

Sincerely
Meikel

-- 
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: iterate with side-effect function?

2011-09-28 Thread siyu798
Odyssomay,
  While does not work in this case as ctx will be "updated" on each 
iteration and fed to the next iteration.

Thanks,
siyu

-- 
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: iterate with side-effect function?

2011-09-28 Thread Jonathan Fischer Friberg
How about while?

(while not-finished
  (do stuff ...))

On Wed, Sep 28, 2011 at 4:23 AM, Nathan Sorenson  wrote:

> Quite often I convince myself I need state or some effectful trigger, but
> further thought reveals a simpler stateless approach.
>
> That being said--if you absolutely need to be doing something based on
> effects, something that absolutely can't be tracked via values in a purely
> functional way--like polling a queue once per second (functional-reactive
> programming notwithstanding), I personally prefer straight loop/recur to the
> list processing functions. In my mind, usings seq/filter/map suggests you
> are doing something lazy, referentially transparent, and composable. If you
> are not doing that, a loop recur signals to me you are manipulating the
> execution flow in a precise way.
>
> But again, I always try to find a way to avoid dealing with the messy
> stateful world until the last possible moment. Lots of application logic can
> be completely pure with one small "write to file"-type operation at the end.
>
>  --
> 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
>

-- 
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: iterate with side-effect function?

2011-09-27 Thread Nathan Sorenson
Quite often I convince myself I need state or some effectful trigger, but 
further thought reveals a simpler stateless approach.

That being said--if you absolutely need to be doing something based on 
effects, something that absolutely can't be tracked via values in a purely 
functional way--like polling a queue once per second (functional-reactive 
programming notwithstanding), I personally prefer straight loop/recur to the 
list processing functions. In my mind, usings seq/filter/map suggests you 
are doing something lazy, referentially transparent, and composable. If you 
are not doing that, a loop recur signals to me you are manipulating the 
execution flow in a precise way. 

But again, I always try to find a way to avoid dealing with the messy 
stateful world until the last possible moment. Lots of application logic can 
be completely pure with one small "write to file"-type operation at the end.

-- 
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: iterate with side-effect function?

2011-09-27 Thread siyu798
Nathan,
Thanks for the explanation, what i'm trying to do is to "repeatedly" run 
a side-effect function until the return value of it meets certain criteria. 
 It could have been done using loop/recur as shown below, and wondering if 
there's alternatives.  Is a there general approach to accomplish this?

(loop [ctx (init-ctx)]
  (let [ctx (side-effect-operations)]
 (if (ctx :done?) 
   ctx
   (recur ctx)))


siyu


-- 
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: iterate with side-effect function?

2011-09-27 Thread Nathan Sorenson
Because the fn is wrapped in a lazy sequence, the effects won't run if you 
ask for the same value again-- 

For instance:

(def a (iterate (fn [cnt]
(prn cnt)
;; save data to csv
(inc cnt)) 0))

(nth a 5) ... all the effects are fired.
(nth a 5) ... simply returns 5-- no effects are fired because the lazy 
sequence has already been realized up to this point. This may not be what 
you expect.

Also, you shouldn't in general trust effects to be run the 'correct' amount 
of times. For instance, if we were operating on a chunked-sequence, if you 
asked for the 3rd element, you would get the side effects for the next 32 
elements, which may not be what you'd expect. That doesn't happen to be the 
case here but it's something to keep in mind as well.

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

iterate with side-effect function?

2011-09-27 Thread siyu798
Hi,
   From clojure doc, it states iterate takes a side-effect free function, 
and what's the implication if the function has side-effect as follow?

(->> 0
  (iterate (fn [cnt]
(prn cnt)
;; save data to csv
(Thread/sleep 6000)
(inc cnt)))
  (take-while (partial > 10))
  last)

Thanks,
siyu

-- 
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: iterate?

2011-07-04 Thread Alan Malloy
Or, to defeat the purpose of iterate entirely:

(= (repeat 100 :foo)
   (take 100 (iterate (constantly :foo) :foo)))

On Jul 4, 3:51 am, Christophe Grand  wrote:
> => (= (repeat 100 :foo)
>         (take 100 (iterate identity :foo)))
> true
>
> hth,
>
> Christophe
>
>
>
>
>
>
>
>
>
> On Mon, Jul 4, 2011 at 12:39 PM, Antonio Recio  wrote:
> > Do you know how to solve this?
>
> > (= (repeat 100 :foo)
> >      (take 100 (iterate ___ :foo)))
>
> > --
> > 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
>
> --
> Professional:http://cgrand.net/(fr)
> On Clojure:http://clj-me.cgrand.net/(en)

-- 
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: iterate?

2011-07-04 Thread Christophe Grand
=> (= (repeat 100 :foo)
(take 100 (iterate identity :foo)))
true

hth,

Christophe

On Mon, Jul 4, 2011 at 12:39 PM, Antonio Recio  wrote:

> Do you know how to solve this?
>
> (= (repeat 100 :foo)
>  (take 100 (iterate ___ :foo)))
>
> --
> 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




-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

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

iterate?

2011-07-04 Thread Antonio Recio
Do you know how to solve this?

(= (repeat 100 :foo)
 (take 100 (iterate ___ :foo)))

-- 
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: clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-06 Thread hoeck


Hi Daniel,

I too like the iter macro so much that I've written my own version for
clojure (http://github.com/hoeck/clojurebox2d/blob/master/hoeck/
iterate.clj :).
I use it to make it easier to work with the JBox2D physics engine,
which is a a Java port from Box2D (written in C++) and thus often uses
C++ Idioms like plain Objects with .getNext to traverse a list and
more arrays than iterators.
Also, traversing an array and picking/collecting/modifying some of its
contents is way simpler in an iter clause than writing a clojure loop.
So, I'm not really using it as a replacement for map, filter, reduce
and friends, but as a more convenient replacement to loop/recur and
areduce. Maybe, if there is some interest in it, we could make a
unified iterate implementation?

Erik



On 6 Nov., 01:03, Daniel Janus  wrote:
> Dear all,
>
> I am happy to announce the public availability of clj-iter, an Iterate-
> like iteration macro. It is free (available under the terms of MIT
> license) and can be found on GitHub:http://github.com/nathell/clj-iter
>
> The design goal was to keep it as simple as possible, and make it
> blend well with the rest of Clojure.  In
> contrast to cl-loop, which uses mutable bindings, clj-iter has a
> functional flavour, and macroexpands to the kind of code you would
> write manually using loop/recur.  It is also very simple, having a
> fraction of Iterate's functionality, but I hope even the little there
> is will be sufficient in many cases.
>
> To avoid citing the entire README blurb, I'll just give you some
> examples:
>
>     (iter (for x in [31 41 59 26])
>           (for y from 1)
>           (collect (+ x y)))
>     ==> (32 43 62 30)
>
>     (iter (for s on [1 2 3 4 5])
>         (for q initially () then (cons (first s) q))
>         (collect (cons (first s) (concat (take 2 (rest s)) (take 2
> q)
>     ==> ((1 2 3) (2 3 4 1) (3 4 5 2 1) (4 5 3 2) (5 4 3))
>
> Please let me know whether it is of any use to you.  Feedback, and
> especially patches, are more than welcome.
--~--~-~--~~~---~--~~
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: clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-05 Thread Timothy Pratley


On Nov 6, 12:51 pm, Daniel Janus  wrote:
> As another example, consider multiplying the first 42 elements of a
> list of numbers by 42, and leaving the rest unchanged. It's much more
> straightforward for me to write (and then read)
>
> (iter (for x in lst)
>      (for i from 0)
>      (collect (if (< i 42) (* x 42) x)))

How about this version?
user=> (def lst [1 2 3 4 5 6 7 8])
user=> (concat (map #(* % 42) (take 4 lst)) (drop 4 lst))
(42 84 126 168 5 6 7 8)

Don't get me wrong - I like the macro - just pointing out how I'd
write this particular use case.


--~--~-~--~~~---~--~~
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: clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-05 Thread Daniel Janus

On 6 Lis, 02:02, John Harrop  wrote:
> On Thu, Nov 5, 2009 at 7:03 PM, Daniel Janus  wrote:
> > To avoid citing the entire README blurb, I'll just give you some
> > examples:
>
> >    (iter (for x in [31 41 59 26])
> >          (for y from 1)
> >          (collect (+ x y)))
> >    ==> (32 43 62 30)
>
> >    (iter (for s on [1 2 3 4 5])
> >        (for q initially () then (cons (first s) q))
> >        (collect (cons (first s) (concat (take 2 (rest s)) (take 2
> > q)
> >    ==> ((1 2 3) (2 3 4 1) (3 4 5 2 1) (4 5 3 2) (5 4 3))
>
> I hate to be the party-pooper in this bunch

On the contrary, criticism is most welcome too.  Especially when the
comments show a way of solving a problem (as in the case of the
sliding window) I would never have thought of. Thank you (and also to
Kyle for offering up another variant)!

> but what's the advantage over:
>
> (map + [31 41 59 26] (iterate inc 1))
>
> and
>
> (let [s (take-while identity (iterate next [1 2 3 4 5]))]
>   (map #(concat (cons (first %1) (concat (take 2 (rest %1)) (take 2 %2
>     s
>     (reductions #(cons (first %2) %1) () s)))

One possible answer is clarity.  It's mentally easier for me to parse
the clj-iter version than either alternative versions proposed.
Certainly, it is a matter of personal taste, but then again, you don't
have to use it if you don't want to.

As another example, consider multiplying the first 42 elements of a
list of numbers by 42, and leaving the rest unchanged. It's much more
straightforward for me to write (and then read)

(iter (for x in lst)
 (for i from 0)
 (collect (if (< i 42) (* x 42) x)))

than something along the lines of

(map (fn [[i x]] (if (< i 42) (* x 42) x))
   (map vector (iterate inc 0) lst))

Thanks again,
--Daniel
--~--~-~--~~~---~--~~
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: [ANN] clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-05 Thread John Harrop
On Thu, Nov 5, 2009 at 7:03 PM, Daniel Janus  wrote:

> To avoid citing the entire README blurb, I'll just give you some
> examples:
>
>(iter (for x in [31 41 59 26])
>  (for y from 1)
>  (collect (+ x y)))
>==> (32 43 62 30)
>
>(iter (for s on [1 2 3 4 5])
>(for q initially () then (cons (first s) q))
>(collect (cons (first s) (concat (take 2 (rest s)) (take 2
> q)
>==> ((1 2 3) (2 3 4 1) (3 4 5 2 1) (4 5 3 2) (5 4 3))


I hate to be the party-pooper in this bunch, but what's the advantage over:

(map + [31 41 59 26] (iterate inc 1))

and

(let [s (take-while identity (iterate next [1 2 3 4 5]))]
  (map #(concat (cons (first %1) (concat (take 2 (rest %1)) (take 2 %2
s
(reductions #(cons (first %2) %1) () s)))

?

--~--~-~--~~~---~--~~
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: clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-05 Thread kyle smith

What's wrong with (map + [31 41 59 26] (iterate inc 1))  ?

(use 'clojure.contrib.seq-utils)

(defn sliding-window [coll size]
  (let [idx (into {} (indexed coll))]
(map #(vals (select-keys idx (range (- % size) (+ % size 1
 (range (count coll)

This is the same as your second example if order isn't important.
Someone else might be able to come up with something better.  Do you
have any more/better use cases?
--~--~-~--~~~---~--~~
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: [ANN] clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-05 Thread Stefan Arentz


On 2009-11-05, at 7:03 PM, Daniel Janus wrote:

>
> Dear all,
>
> I am happy to announce the public availability of clj-iter, an  
> Iterate-
> like iteration macro. It is free (available under the terms of MIT
> license) and can be found on GitHub: http://github.com/nathell/clj- 
> iter
>
> The design goal was to keep it as simple as possible, and make it
> blend well with the rest of Clojure.  In
> contrast to cl-loop, which uses mutable bindings, clj-iter has a
> functional flavour, and macroexpands to the kind of code you would
> write manually using loop/recur.  It is also very simple, having a
> fraction of Iterate's functionality, but I hope even the little there
> is will be sufficient in many cases.
>
> To avoid citing the entire README blurb, I'll just give you some
> examples:
>
>(iter (for x in [31 41 59 26])
>  (for y from 1)
>  (collect (+ x y)))
>==> (32 43 62 30)
>
>(iter (for s on [1 2 3 4 5])
>(for q initially () then (cons (first s) q))
>(collect (cons (first s) (concat (take 2 (rest s)) (take 2
> q)
>==> ((1 2 3) (2 3 4 1) (3 4 5 2 1) (4 5 3 2) (5 4 3))
>
> Please let me know whether it is of any use to you.  Feedback, and
> especially patches, are more than welcome.

This is awesome. Can this be made part of contrib?

  S.


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



[ANN] clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-05 Thread Daniel Janus

Dear all,

I am happy to announce the public availability of clj-iter, an Iterate-
like iteration macro. It is free (available under the terms of MIT
license) and can be found on GitHub: http://github.com/nathell/clj-iter

The design goal was to keep it as simple as possible, and make it
blend well with the rest of Clojure.  In
contrast to cl-loop, which uses mutable bindings, clj-iter has a
functional flavour, and macroexpands to the kind of code you would
write manually using loop/recur.  It is also very simple, having a
fraction of Iterate's functionality, but I hope even the little there
is will be sufficient in many cases.

To avoid citing the entire README blurb, I'll just give you some
examples:

(iter (for x in [31 41 59 26])
  (for y from 1)
  (collect (+ x y)))
==> (32 43 62 30)

(iter (for s on [1 2 3 4 5])
(for q initially () then (cons (first s) q))
(collect (cons (first s) (concat (take 2 (rest s)) (take 2
q)
==> ((1 2 3) (2 3 4 1) (3 4 5 2 1) (4 5 3 2) (5 4 3))

Please let me know whether it is of any use to you.  Feedback, and
especially patches, are more than welcome.
--~--~-~--~~~---~--~~
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: iterate

2008-11-13 Thread Parth Malwankar



On Nov 13, 12:25 pm, notallama <[EMAIL PROTECTED]> wrote:
> i put this at the end of my boot.clj for added fun:
>
> (defn iterate
>   "returns a lazy seq of arg1, arg2 ... argn, (f arg1 ... argn), (f
> arg2 ... argn (f arg1 ... argn)), etc."
>   [f & [x & rest :as all]]
>   (lazy-cons x (apply iterate f (concat rest [(apply f all)]
>
> user=> (take 10 (iterate + 1 1))
> (1 1 2 3 5 8 13 21 34 55)
>
> user=> (take 4 (iterate #(* % %) 2))
> (2 4 16 256)
>
> is this good as a contrib? is there a better way to implement this?


There is already an "iterate" in Clojure.
Your implementation much have shadowed it.

user=> (doc iterate)
-
clojure/iterate
([f x])
  Returns a lazy seq of x, (f x), (f (f x)) etc. f must be free of
side-effects
nil
user=> (iterate #(+ 1 %) 1)
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 3
0 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ...)

But it doesn't take multiple arguments like your
implementation. Thats a nice enhancement.

Parth


> also, this language is delightful.
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



iterate

2008-11-12 Thread notallama

i put this at the end of my boot.clj for added fun:

(defn iterate
  "returns a lazy seq of arg1, arg2 ... argn, (f arg1 ... argn), (f
arg2 ... argn (f arg1 ... argn)), etc."
  [f & [x & rest :as all]]
  (lazy-cons x (apply iterate f (concat rest [(apply f all)]


user=> (take 10 (iterate + 1 1))
(1 1 2 3 5 8 13 21 34 55)

user=> (take 4 (iterate #(* % %) 2))
(2 4 16 256)

is this good as a contrib? is there a better way to implement this?
also, this language is delightful.
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---