Dear Clojure Group,
I am trying to adjust the implementation of the 'partition' function
so that the last element of a provided padding such as ["a" "b"] will
be repeated if necessary to ensure the length of the last list
returned by 'padding' is the same as the other lists.
This is the original implementation of 'padding':
(defn partition [n coll]
(partition n n coll))
([n step coll]
(lazy-seq
(when-let [s (seq coll)]
(let [p (take n s)]
(when (= n (count p))
(cons p (partition n step (drop step s))))))))
([n step pad coll]
(lazy-seq
(when-let [s (seq coll)]
(let [p (take n s)]
(if (= n (count p))
(cons p (partition n step pad (drop step s)))
(list (take n (concat p pad)))))))))
And here is my implementation of 'my-partition':
(defn my-partition
([n coll]
(partition n n coll))
([n step coll]
(lazy-seq
(when-let [s (seq coll)]
(let [p (take n s)]
(when (= n (count p)) ;; 1)
(cons p (partition n step (drop step s))))))))
([n step pad coll]
(lazy-seq
(when-let [s (seq coll)]
(let [p (take n s)]
(if (= n (count p))
(cons p (partition n step pad (drop step s)))
;; Changed from here on:
(let [padding (take n (concat p pad))]
(if (= n (count padding))
(list padding)
(list (take n (concat padding (repeat (last
padding)))))))))))))
Unfortunately this doesn't work as 'partition' and 'my-partition' both
return the same result when 'my-partition' should add additional
padding instead:
(partition 3 3 ["a"] [1 2 3 4 5 6 7])
;; ((1 2 3) (4 5 6) (7 "a"))
(my-partition 3 3 ["a"] [1 2 3 4 5 6 7])
;; ((1 2 3) (4 5 6) (7 "a"))
;; Should be: ((1 2 3) (4 5 6) (7 "a" "a" "a"))
I believe that the last step is OK as it returns the expected result:
(def pad ["a" "b"])
(take 10 (concat [1 2 "a"] (repeat (last pad))))
;; (1 2 "a" "b" "b" "b" "b" "b" "b" "b")
Does anybody know where I made a mistake? Any suggestions are highly
appreciated!
Stefan
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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