Re: Missing fns: rotate & rotate-while

2020-02-26 Thread kanishk kumar
Testing with this, gives the wrong result (rotate 6 [1 2 3 4 5]) Expected Output: [2 3 4 5 1] Actual Output: [1 2 3 4 5] On Wednesday, 21 April 2010 21:07:40 UTC+5:30, Michał Marczyk wrote: > > On 21 April 2010 17:23, Sean Devlin > > wrote: > > I've had to code these guys up a few times: > >

Re: Missing fns: rotate & rotate-while

2019-03-23 Thread Nathan Rogers
(defn rotate [a n] (let [l (count a) off (mod (+ (mod n l) l) l)] (concat (drop off a) (take off a (rotate '((1 2) (3 4) (5 6) (7 8) (9 10)) 3) => ((7 8) (9 10) (1 2) (3 4) (5 6)) On Saturday, March 23, 2

Re: Missing fns: rotate & rotate-while

2019-03-23 Thread Nathan Rogers
Concat (defn rotate [a n] (let [l (count a) off (-> (mod n l) (+ (mod n l) l) l)] (concat (drop off a) (take off a (rotate '((1 2) (3 4) (5 6) (7 8) (9 10)) 3) => ((7 8) (9 10) (1 2) (3 4) (5 6)) On Saturd

Re: Missing fns: rotate & rotate-while

2019-03-23 Thread Matching Socks
Could it finish off the result in some way other than flatten? flatten would go too far if the input data were a collection of collections. Indeed, I would support adding (some form of) rotate to the standard library *on the condition that flatten be removed.* rotate is an amusement, but fl

Re: Missing fns: rotate & rotate-while

2019-03-22 Thread Nathan Rogers
(defn rotate [a n] (let [l (count a) off (mod (+ (mod n l) l) l)] (flatten (list (drop off a) (take off a) (rotate '(1 2 3 4 5) -1) => (5 1 2 3 4) (rotate '(1 2 3 4 5) -6) => (5 1 2

Re: Missing fns: rotate & rotate-while

2010-04-22 Thread Sean Devlin
Oh wow... totally would have :) On Apr 21, 8:16 pm, Harvey Hirst wrote: > > (defn rotate [n s] > >  (let [[front back] (split-at (mod n (count s)) s)] > >    (concat back front))) > > Don't forget (mod n 0) is an ArithmeticException. > > Harvey > > -- > You received this message because you are s

Re: Missing fns: rotate & rotate-while

2010-04-22 Thread Harvey Hirst
> (defn rotate [n s] >  (let [[front back] (split-at (mod n (count s)) s)] >    (concat back front))) Don't forget (mod n 0) is an ArithmeticException. Harvey -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to cloju

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Per Vognsen
By the way, once you start looking for conjugation in common code patterns, you see it everywhere. A less trivial example is the Schwartzian transform for caching sorting keys: (defn schwartz [key-fn] #(map (fn [x] [(key-fn x) x]) %)) (def unschwartz #(map second %)) (defn schwartz-sort [key-fn]

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Michał Marczyk
On 22 April 2010 03:51, Per Vognsen wrote: > Yet another variation: > > [...] > > Food for thought. :) This is absolutely beautiful. I feel a tremendous joy now. :-) Sincerely, Michał -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Per Vognsen
Yet another variation: (defn conjugate [f g & [g-inv]] (comp (or g-inv g) f g)) (defn composite [f f-inv n x] (nth (iterate (if (pos? n) f f-inv) x) (abs n))) (defn rotate-left [xs] (when (seq xs) (concat (rest xs) [(first xs)]))) (def rotate-right (conjugate rotate-left reverse)) (defn

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Michał Marczyk
On 21 April 2010 20:09, Mark Engelberg wrote: > In some languages, split-at is more performant than doing take and > drop separately.  But in Clojure, split-at is simply defined as: > (defn split-at >  "Returns a vector of [(take n coll) (drop n coll)]" >  [n coll] >    [(take n coll) (drop n coll

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Michał Marczyk
On 21 April 2010 19:57, Sean Devlin wrote: > If you're gonna over engineer use a protocol :-p You're absolutely right, of course; I stand corrected! Here's my new attempt at overengineering then: (defprotocol Rotator (rotate [self s])) (extend-protocol Rotator Number (rotate [self s]

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Sean Devlin
Hmmm... we could talk about what's faster or measure it. Time to eat my own damn dog food, I guess :) Traveling now, I'll run the experiments in a few days when I get back to my normal setup. Sean On Apr 21, 2:09 pm, Mark Engelberg wrote: > In some languages, split-at is more performant than

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Mark Engelberg
In some languages, split-at is more performant than doing take and drop separately. But in Clojure, split-at is simply defined as: (defn split-at "Returns a vector of [(take n coll) (drop n coll)]" [n coll] [(take n coll) (drop n coll)]) So by using split-at, you gain nothing other than t

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Sean Devlin
If you're gonna over engineer use a protocol :-p On Apr 21, 1:43 pm, Michał Marczyk wrote: > On 21 April 2010 19:35, Sean Devlin wrote: > > > I like that version :) > > :-) > > In this case, rotate-while could be rewritten like so: > > (defn rotate-with [pred s] >   (let [[front back] (split-wit

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Michał Marczyk
On 21 April 2010 19:35, Sean Devlin wrote: > I like that version :) :-) In this case, rotate-while could be rewritten like so: (defn rotate-with [pred s] (let [[front back] (split-with pred s)] (concat back front))) And to round it off with ridiculous overengineering ;-) -- (defn rotate

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Sean Devlin
I like that version :) On Apr 21, 1:11 pm, Michał Marczyk wrote: > One could also do > > (defn rotate [n s] >   (let [[front back] (split-at (mod n (count s)) s)] >     (concat back front))) > > I was hoping for a moment to avoid calculating (count s) up front, but > that definitely does interfer

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Michał Marczyk
One could also do (defn rotate [n s] (let [[front back] (split-at (mod n (count s)) s)] (concat back front))) I was hoping for a moment to avoid calculating (count s) up front, but that definitely does interfere with negative / large shifts, so never mind that. Sincerely, Michał -- You r

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Sean Devlin
Hmmm... good point. Now that I think about it, cycle is an artifact of an early implementation. On Apr 21, 1:01 pm, Mark Engelberg wrote: > On Wed, Apr 21, 2010 at 8:46 AM, Sean Devlin wrote: > > You're right about changing the docstring to read ""sequence" > > > I think the lazy-cat version l

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Mark Engelberg
On Wed, Apr 21, 2010 at 8:46 AM, Sean Devlin wrote: > You're right about changing the docstring to read ""sequence" > > I think the lazy-cat version looses the ability to rotate in reverse, > which I've come to love from Ruby.  Also, I have found use cases where > I want to rotate a period longer

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Michał Marczyk
On 21 April 2010 17:46, Sean Devlin wrote: > I think the lazy-cat version looses the ability to rotate in reverse, > which I've come to love from Ruby.  Also, I have found use cases where > I want to rotate a period longer than the sequence length. Right. I don't mind rotate being strict, actuall

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Sean Devlin
You're right about changing the docstring to read ""sequence" I think the lazy-cat version looses the ability to rotate in reverse, which I've come to love from Ruby. Also, I have found use cases where I want to rotate a period longer than the sequence length. Thanks for the feedback, though. Se

Re: Missing fns: rotate & rotate-while

2010-04-21 Thread Michał Marczyk
On 21 April 2010 17:23, Sean Devlin wrote: > I've had to code these guys up a few times: Nice functions! I'd replace "collection" with "sequence" in the docstrings, though. (And rename the args accordingly.) You can also rewrite rotate as (defn rotate [n s] (lazy-cat (drop n s) (

Missing fns: rotate & rotate-while

2010-04-21 Thread Sean Devlin
Hey everyone, I've had to code these guys up a few times: (defn rotate "Take a collection and left rotates it n steps. If n is negative, the collection is rotated right. Executes in O(n) time." [n coll] (let [c (count coll)] (take c (drop (mod n c) (cycle coll) (defn rotate-while