Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread .Bill Smith
I want a concise function that, given an arbitrary length sequence, determines whether the sequence is of consecutive integers starting with one. So: (f [1 2 3]) returns true (f [1 2 4]) returns false (f [0 1 2]) returns false My first try, which I am not proud of, follows: (defn f

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread Ken Wesson
On Fri, Jul 1, 2011 at 3:28 PM, .Bill Smith william.m.sm...@gmail.com wrote: I want a concise function that, given an arbitrary length sequence, determines whether the sequence is of consecutive integers starting with one.  So:  (f [1 2 3]) returns true  (f [1 2 4]) returns false  (f [0 1

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread Ken Wesson
The other way around, actually -- the second and third will hang (in count) on infinite seqs, the first stopping on any non-matching item. Sorry, in a hurry here. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread David Nolen
On Fri, Jul 1, 2011 at 3:28 PM, .Bill Smith william.m.sm...@gmail.comwrote: I want a concise function that, given an arbitrary length sequence, determines whether the sequence is of consecutive integers starting with one. So: (f [1 2 3]) returns true (f [1 2 4]) returns false (f [0 1

RE: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread Bhinderwala, Shoeb
Here is another way: (defn f [xs] (and (= 1 (first xs)) (apply = (map - (rest xs) xs From: clojure@googlegroups.com [mailto:clojure@googlegroups.com] On Behalf Of David Nolen Sent: Friday, July 01, 2011 3:51 PM To:

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread Chouser
On Fri, Jul 1, 2011 at 3:51 PM, David Nolen dnolen.li...@gmail.com wrote: On Fri, Jul 1, 2011 at 3:28 PM, .Bill Smith william.m.sm...@gmail.com wrote: I want a concise function that, given an arbitrary length sequence, determines whether the sequence is of consecutive integers starting with

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread David Nolen
On Fri, Jul 1, 2011 at 4:04 PM, Chouser chou...@gmail.com wrote: (defn f [xs] (every? true? (map = xs (iterate inc 1 --Chouser Hrm, shoulda thought 'o that :) David -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread Ken Wesson
I see some more clever ways have been posted. Perhaps a more interesting, general problem: detect if the input is an arithmetic sequence at all. Here's a few implementations to get you started. Straightforward: (let [diffs (map - xs (rest xs))] (every? identity (map = diffs (rest diffs

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread David Nolen
On Fri, Jul 1, 2011 at 4:04 PM, Chouser chou...@gmail.com wrote: (defn f [xs] (every? true? (map = xs (iterate inc 1 --Chouser Also, (defn f [xs] (every? #{1} (map - xs (iterate inc 0 -- You received this message because you are subscribed to the Google Groups Clojure group. To

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread .Bill Smith
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,

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread Alan Malloy
On Jul 1, 1:41 pm, David Nolen dnolen.li...@gmail.com wrote: On Fri, Jul 1, 2011 at 4:04 PM, Chouser chou...@gmail.com wrote: (defn f [xs] (every? true? (map = xs (iterate inc 1 --Chouser Also, (defn f [xs] (every? #{1} (map - xs (iterate inc 0 This has problems before 1.3 if

Re: Most concise way to determine that a sequence is consecutive integers starting with one?

2011-07-01 Thread Rob Lachlan
Using map-indexed: (defn f [xs] (every? true? (map-indexed #(= (inc %1) %2) xs))) On Jul 1, 12:28 pm, .Bill Smith william.m.sm...@gmail.com wrote: I want a concise function that, given an arbitrary length sequence, determines whether the sequence is of consecutive integers starting with one.