Re: Nil Coalesce

2010-08-18 Thread Nicolas Oury
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

Re: Nil Coalesce

2010-08-18 Thread GrumpyLittleTed
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

Re: Nil Coalesce

2010-08-18 Thread Justin Kramer
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

Re: Nil Coalesce

2010-08-17 Thread Michael Wood
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)

Re: Nil Coalesce

2010-08-17 Thread Justin Kramer
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

Re: Nil Coalesce

2010-08-17 Thread Brian Goslinga
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

Re: Nil Coalesce

2010-08-17 Thread Alan
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

Re: Nil Coalesce

2010-08-17 Thread Rising_Phorce
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

Re: Nil Coalesce

2010-08-17 Thread ataggart
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

Re: Nil Coalesce

2010-08-17 Thread Dave Jack
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

Re: Nil Coalesce

2010-08-17 Thread Justin Kramer
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: >

Re: Nil Coalesce

2010-08-17 Thread Meikel Brandmeyer
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

Re: Nil Coalesce

2010-08-17 Thread Chouser
(= 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

Re: Nil Coalesce

2010-08-17 Thread Justin Kramer
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

Re: Nil Coalesce

2010-08-17 Thread Rising_Phorce
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

Re: Nil Coalesce

2010-08-17 Thread Chouser
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: >> >&

Re: Nil Coalesce

2010-08-17 Thread Nicolas Oury
> > 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))

Re: Nil Coalesce

2010-08-17 Thread Chouser
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

Re: Nil Coalesce

2010-08-17 Thread Jeff Valk
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

Re: Nil Coalesce

2010-08-17 Thread Meikel Brandmeyer
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

Nil Coalesce

2010-08-17 Thread Rising_Phorce
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