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:
>
>
(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
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
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
(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
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
> (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
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]
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
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
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
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]
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
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
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
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
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
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
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
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
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
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
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)
(
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
24 matches
Mail list logo