On 12 Dec 2008, at 23:10, Mark Fredrickson wrote:
>> For (2), say I want to insert 3 before every item less than or equal
>> to 5 in a seq:
>
> Again reduce to the rescue:
>
> (reduce into (map (fn [i] (if (<= i 5) [3 i] [i])) [24 6 5 5 7 5 8
> 2]))
Could use mapcat:
(def bar [24 6 5 5 7 5 8 2])
(defn insert-before-if [pred ins lst]
(mapcat #(if (pred %) [ins %] [%]) lst))
(insert-before-if #(<= 5 %) 3 bar)
=> (3 24 3 6 3 5 3 5 3 7 3 5 3 8 2)
stephen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---