alue, it generates a seq from a
value. (generate is sometimes a name for that, too)
I don't know if it is somewhere in core or contrib.
With this function, it is very simple:
(defn nil-coalesce [s1 s2]
(unfold #(and %1
(if (first %1)
[(first %1) [(ne
My take is almost identical but I used the same length arguments for
the collections which means that the difference in the if branch are
easier to spot, at the cost of more cryptic argument names. This
version will not blow the stack, and its easy to wrap in lazy-seq.
(defn nil-coalesce [s1 s2
used, nil will be returned:
>
> > e.g. user> (nil-coalesce (nil 1 2 3 nil nil) (4 5))
> > (4 1 2 3 5 nil)
_Returning_ nil is the requirement, which is what happens in the code
I posted (it behaves the same as other submissions as far as I can
tell). Replacing nil with nil in the re
ace nils when there are fewer substitutions than nil
positions" was one of the requirements.
>From the first post:
> If nil is encountered in the first sequence and the second sequence is
> exhaused, nil will be returned:
>
> e.g. user> (nil-coalesce (nil 1 2 3 nil nil)
xhausted. In your modified
version, nils replace nils when there are fewer substitutions than nil
positions, which is unnecessary. Unless I'm missing something...which
is entirely possible.
It does fail when subs is empty, though. So:
(defn nil-coalesce [v subs]
(if (empty? subs) v
placements]
(lazy-seq
(if (seq coll)
(let [[x & xs] coll
[y & ys] replacements]
(if (pred x)
(cons (f x y) (map-replace pred f xs ys))
(cons x (map-replace pred f xs replacements)))
(defn nil-coalesce
"Replace all occurren
s a vector:
>
> (use '[clojure.contrib.seq-utils :only [positions]])
>
> (defn nil-coalesce [v subs]
> (apply assoc v (interleave (positions nil? v) subs)))
>
> Justin
--
You received this message because you are subscribed to the Google
Groups "Clojure" grou
Oh, and, duh. Both lists are traversed at each iteration as Nicolas
stated.
On Aug 17, 3:56 pm, Rising_Phorce wrote:
> I posted because clojure *tends* to be so succinct and in this case
> the solution complexity seems disproportionate to the problem. In my
> first post I forgot to mention my s
an item from the
> second, until the first sequence is exhausted. If nil is encountered
> in the first sequence and the second sequence is exhaused, nil will be
> returned:
>
> e.g. user> (nil-coalesce (nil 1 2 3 nil nil) (4 5))
> (4 1 2 3 5 nil)
>
> I ended up with a sc
Neither particularly short nor particularly clever:
(defn nil-coalesce [coll subs]
(loop [[c & cs :as coll] coll
[s & ss :as subs] subs
acc []]
(if coll
(recur cs
(if (nil? c) ss subs)
(conj acc (if (nil? c) s c)))
acc)))
On
Another one that works with all collections. Short, but not
necessarily the most efficient:
(use '[clojure.contrib.seq-utils :only [positions]])
(defn nil-coalesce [coll subs]
(map-indexed (zipmap (positions nil? coll) subs) coll))
Justin
On Aug 17, 4:10 pm, Justin Kramer wrote:
>
Hi,
Am 17.08.2010 um 21:56 schrieb Rising_Phorce:
> I posted because clojure *tends* to be so succinct and in this case
> the solution complexity seems disproportionate to the problem.
I think the plain lazy-seq version is a pretty straight-forward translation. I
don't think, that it is overly
(= cnt-mn cnt-r)
> (map #(if (nil? %1) %2 %1) maybe-nil replacements)
> (map #(if (nil? %1) %2 %1) maybe-nil (concat replacements (repeat
> (- cnt-mn cnt-r) nil))
Ok, maybe I understand the task now?
(defn nil-coalesce [a b]
(->> a
(redu
With the precondition that the first collection is a vector:
(use '[clojure.contrib.seq-utils :only [positions]])
(defn nil-coalesce [v subs]
(apply assoc v (interleave (positions nil? v) subs)))
Justin
--
You received this message because you are subscribed to the Google
Groups &qu
I posted because clojure *tends* to be so succinct and in this case
the solution complexity seems disproportionate to the problem. In my
first post I forgot to mention my second attempt...
(map #(if (nil? %1) %2 %1) [nil 1 2 3 nil nil] [4 5])
but it got ugly because I needed to extend the replac
On Tue, Aug 17, 2010 at 1:38 PM, Nicolas Oury wrote:
>>
>> Clojure golf is the most fun golf!
>>
>> (defn nil-coalesce [a b]
>> (map #(or %1 %2) a (concat b (repeat nil
>>
>> Or if you really want to treat nil and false differently:
>>
>&
>
> Clojure golf is the most fun golf!
>
> (defn nil-coalesce [a b]
> (map #(or %1 %2) a (concat b (repeat nil
>
> Or if you really want to treat nil and false differently:
>
> (defn nil-coalesce [a b]
> (map #(if (nil? %1) %2 %1) a (concat b (repeat nil))
which takes items from the first
> sequence unless null in which case it will take an item from the
> second, until the first sequence is exhausted. If nil is encountered
> in the first sequence and the second sequence is exhaused, nil will be
> returned:
>
> e.g. user> (nil-coale
Here's a pass using reduce, keeping both collections in the return value,
conj-ing to one and taking from the other as necessary...
(defn nil-coalesce [coll subs]
(first (reduce (fn [[a b] x]
(if x
[(conj a x) b]
[(conj a (fi
Yeah! Golf!
user=> (defn nil-coalesce
[coll replacements]
(lazy-seq
(when-let [s (seq coll)]
(let [fst (first s)]
(if (nil? fst)
(cons (first replacements)
(nil-coalesce (rest s) (rest replaceme
m the
second, until the first sequence is exhausted. If nil is encountered
in the first sequence and the second sequence is exhaused, nil will be
returned:
e.g. user> (nil-coalesce (nil 1 2 3 nil nil) (4 5))
(4 1 2 3 5 nil)
I ended up with a scheme like natural recursion. I see two
drawbac
21 matches
Mail list logo