On Sat, May 8, 2010 at 1:01 PM, Meikel Brandmeyer wrote:
> Hi,
>
> if you want to go low-level you can use lazy-seq.
>
> (defn replace-first
> [needle replacement coll]
> (lazy-seq
>(when-let [s (seq coll)]
> (let [fst (first s)]
>(if (= fst needle)
> (cons replacement
Hi,
if you want to go low-level you can use lazy-seq.
(defn replace-first
[needle replacement coll]
(lazy-seq
(when-let [s (seq coll)]
(let [fst (first s)]
(if (= fst needle)
(cons replacement (rest s))
(cons fst (replace-first needle replacement (rest s)
I would use reductions(i.e. Haskell's scanl) given my understanding that
clojure sequence is just as lazy as Haskell.
(rest (map first (reductions (fn [ [v a b] x ] (if (= a x) [b nil b] [x a
b]))
[nil 2 3] '(1 1 2 2 3 4
I use nil here as 'something that would not appear in the list', I am su
On Sat May 08 2010 at 08:38 am, Mark J. Reed wrote:
> But there are no doubt better ways to do it, probably built in or in
> clojure.core.
Using 'split-with' from core seems handy here:
clojure.core/split-with
([pred coll])
Returns a vector of [(take-while pred coll) (drop-while pred coll)]
S
> Hi,there!
>
> I need a function that replaces a first found element of list.
> like that,
>
>>(replace-first :a :b [:c :c :a :c :a]
> [:c :c :b :c :a]
> ~~
> ;replace first :a to :b
An interesting problem for a Sunday morning when I ought to be
cleaning the house. :) Here are my (admittedl
You're scanning the list twice, first to find the element position, and then
to do the split; it's better to do it all at once. Here's a simple version:
(defn replace-first [from to in]
(cond (empty? in) in
(= (first in) from) (cons to (rest in))
:else (cons (first in) (re
Hi,there!
I need a function that replaces a first found element of list.
like that,
>(replace-first :a :b [:c :c :a :c :a]
[:c :c :b :c :a]
~~
;replace first :a to :b
and my code is as follows.
---code begin--
(use '[clojure.contrib.seq-utils])
(de