Re: Why take-last of empty collection is nil?

2011-02-03 Thread Ken Wesson
On Thu, Feb 3, 2011 at 5:22 PM, Alan wrote: > You don't need an extra element in the vector to distinguish between > empty and full. You can store (start, size) instead of (start, end). I considered that, but it made the arithmetic much messier in various places. Count gets simpler, obviously, bu

Re: Why take-last of empty collection is nil?

2011-02-03 Thread Alan
You don't need an extra element in the vector to distinguish between empty and full. You can store (start, size) instead of (start, end). On Feb 3, 12:57 pm, Ken Wesson wrote: > On Thu, Feb 3, 2011 at 2:03 PM, Alan wrote: > > Or use a fixed-size vector as a ring, with start and end "pointers", >

Re: Why take-last of empty collection is nil?

2011-02-03 Thread Ken Wesson
On Thu, Feb 3, 2011 at 2:03 PM, Alan wrote: > Or use a fixed-size vector as a ring, with start and end "pointers", > and a couple functions to abstract over the handling thereof. But > unless you really really care about performance, this is likely to be > overkill, and Ken's suggestion to just us

Re: Why take-last of empty collection is nil?

2011-02-03 Thread Alan
Or use a fixed-size vector as a ring, with start and end "pointers", and a couple functions to abstract over the handling thereof. But unless you really really care about performance, this is likely to be overkill, and Ken's suggestion to just use a vector or a seq and accept the O(n) behavior. On

Re: Why take-last of empty collection is nil?

2011-02-03 Thread Ken Wesson
On Thu, Feb 3, 2011 at 3:11 AM, Petr Gladkikh wrote: > On Thu, Feb 3, 2011 at 1:31 PM, Meikel Brandmeyer wrote: >> Hi, >> >> On 3 Feb., 08:04, Petr Gladkikh wrote: >> >>> Should not it be empty colection instead? >>> It seems odd to me since it is inconsistent and forces to consider one >>> more

Re: Why take-last of empty collection is nil?

2011-02-03 Thread Bill James
On Feb 3, 1:04 am, Petr Gladkikh wrote: > > And another question. I have written this function > (defn index-by >   "Make map (f x) -> x" >   [f coll] >   (reduce #(assoc %1 (f %2) %2) {} coll)) > > I wonder, is there already such function somewhere in Clojure libraries? Somewhat shorter: (defn

Re: Why take-last of empty collection is nil?

2011-02-03 Thread Meikel Brandmeyer
Hi, On 3 Feb., 09:11, Petr Gladkikh wrote: > I have a vector that holds some history. I conj new items to it and to > save space I'd like to retain not more than n last items. > To do that I used (take-last n history). So: [] -> (take-last n []) -> > nil -> (conj nil newItem) -> '(newItem) > > B

Re: Why take-last of empty collection is nil?

2011-02-03 Thread Petr Gladkikh
On Thu, Feb 3, 2011 at 1:31 PM, Meikel Brandmeyer wrote: > Hi, > > On 3 Feb., 08:04, Petr Gladkikh wrote: > >> Should not it be empty colection instead? >> It seems odd to me since it is inconsistent and forces to consider one >> more case (nil or collection). > > It is consistent. There is a dif

Re: Why take-last of empty collection is nil?

2011-02-02 Thread Ken Wesson
On Thu, Feb 3, 2011 at 2:31 AM, Meikel Brandmeyer wrote: > Hi, > > On 3 Feb., 08:04, Petr Gladkikh wrote: > >> Should not it be empty colection instead? >> It seems odd to me since it is inconsistent and forces to consider one >> more case (nil or collection). > > It is consistent. There is a dif

Re: Why take-last of empty collection is nil?

2011-02-02 Thread Meikel Brandmeyer
Hi, On 3 Feb., 08:04, Petr Gladkikh wrote: > Should not it be empty colection instead? > It seems odd to me since it is inconsistent and forces to consider one > more case (nil or collection). It is consistent. There is a difference between () and nil. () is the empty list. However there is no

Why take-last of empty collection is nil?

2011-02-02 Thread Petr Gladkikh
Should not it be empty colection instead? It seems odd to me since it is inconsistent and forces to consider one more case (nil or collection). And another question. I have written this function (defn index-by "Make map (f x) -> x" [f coll] (reduce #(assoc %1 (f %2) %2) {} coll)) I wonder,