A solution based on unfold with the last element read (or initially
nil) and the rest of the list as a seed.
On Thu, Nov 11, 2010 at 12:12 AM, David Jacobs
wrote:
> Thanks for all of the options, and Ken, thanks for the detailed comparison.
> This will be extremely useful. (I'm trying to port so
Thanks for all of the options, and Ken, thanks for the detailed comparison.
This will be extremely useful. (I'm trying to port some statistical tests to
Clojure for work.)
Best,
David
On 10 Nov 2010, at 5:13 pm, Alan wrote:
> Juha's is my favorite by a mile. And if you're content with a list o
Juha's is my favorite by a mile. And if you're content with a list of
[element, count] pairs, you could do this:
(defn seq-ties [coll]
(->> coll (partition-by identity) (filter next) (map (juxt first
count)))
(seq-ties [1 1 1 1 2 3 3 4 5 7 7])
;==>([1 4] [3 2] [7 2])
On Nov 10, 1:27 pm, Juha A
On Nov 10, 10:28 pm, David Jacobs
wrote:
> I have a sorted list, and I'd like to derive from that a list
> containing all "ties" in the original.
One more solution assuming input is sorted:
(defn seq-ties [coll]
(mapcat #(cons (first %) %) (keep next (partition-by identity
coll
--
Juha Ar
On Wed, Nov 10, 2010 at 4:01 PM, Ken Wesson wrote:
> Oh, and another difference: the frequencies implementation handles nil
> in the input. The fully-lazy one also does. The loop/recur will screw
> up if the input starts with nil. Changing the literal nil in the loop
> initial bindings to (Object.
Oh, and another difference: the frequencies implementation handles nil
in the input. The fully-lazy one also does. The loop/recur will screw
up if the input starts with nil. Changing the literal nil in the loop
initial bindings to (Object.) will fix it though.
--
You received this message because
Wow, thanks for the quick replies, guys :) I can't decide which one I
like best.
On Nov 10, 3:40 pm, Ken Wesson wrote:
> And
>
> (let [x (map #(if (= %1 %2) [%1]) s (rest s))]
> (mapcat #(if (nil? %1) %2 %1) x (concat (rest x) [nil])))
>
> does it lazily. :)
--
You received this message becau
user=> (defn foo [the-input-seq] (loop [s the-input-seq o [] last nil]
(if (empty? s)
o
(let [n (first s)]
(if (or (= n (second s)) (= n last))
(recur (rest s) (conj o n) n)
(recur (rest s) o n))
#'user/foo
user=> (defn bar [coll]
(let [freqs (frequencies coll)]
(f
HI,
Am 10.11.2010 um 21:28 schrieb David Jacobs:
> That is, if I have (1 2 3 4 4 5 5 5 5 5 6 9 12 12), I want to get back
> the sequence (4 4 5 5 5 5 5 12 12).
Low-level solution using lazy-seq. As lazy as possible:
(defn ties
[coll]
(let [step (fn step [seen s]
(lazy-seq
And
(let [x (map #(if (= %1 %2) [%1]) s (rest s))]
(mapcat #(if (nil? %1) %2 %1) x (concat (rest x) [nil])))
does it lazily. :)
--
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
I don't know about a standard solution, but my take is that you can
use the built-in 'frequencies' function to build up a frequency table
for the list, then filter on anything that occurs more than once:
(defn get-ties [coll]
(let [freqs (frequencies coll)]
(filter #(> (freqs %) 1) coll)))
On Wed, Nov 10, 2010 at 3:28 PM, David Jacobs
wrote:
> I have a sorted list, and I'd like to derive from that a list
> containing all "ties" in the original.
>
> That is, if I have (1 2 3 4 4 5 5 5 5 5 6 9 12 12), I want to get back
> the sequence (4 4 5 5 5 5 5 12 12).
>
> My first thought was to
I have a sorted list, and I'd like to derive from that a list
containing all "ties" in the original.
That is, if I have (1 2 3 4 4 5 5 5 5 5 6 9 12 12), I want to get back
the sequence (4 4 5 5 5 5 5 12 12).
My first thought was to try to filter down the list, but filter takes
each element of a l
13 matches
Mail list logo