(partition 2 coll) will give you the sequence two at a time.
To map your function, you'd do (map #(apply myfcn %) (partition 2 '(1
2 3 4 5 6 7 8)))
On Jul 21, 10:20 pm, Glen Rubin wrote:
> Hi! I want to process a collection 2 elements at a time using map.
>
> My function accepts 2 parameters (a,
Here's my take:
(defn mmap [f coll]
(->> coll
(partition 2)
(map (fn [[x y]] (f x y)
For instance:
user> (mmap + (range 8))
(1 5 9 13)
user> (mmap * (range 8))
(0 6 20 42)
You probably want to think about whether you'll see input sequences
with an odd number of terms, and how
you have to partition it first.
user=> (partition 2 [1 2 3 4 5 6 7 8])
((1 2) (3 4) (5 6) (7 8))
let's say we want to add the numbers.
user=> (map #(apply + %) (partition 2 [1 2 3 4 5 6 7 8]))
(3 7 11 15)
On Wed, Jul 21, 2010 at 10:20 PM, Glen Rubin wrote:
> Hi! I want to process a collect
Use partition:
(map (apply myfcn) (partition 2 [1 2 3 4 5 6 7 8]))
Or something like that. Not at a REPL so that's from memory.
Experiment with partition at the repl and it'll become clear.
Jim
On Jul 21, 9:20 pm, Glen Rubin wrote:
> Hi! I want to process a collection 2 elements at a time usi
Hi! I want to process a collection 2 elements at a time using map.
My function accepts 2 parameters (a,b) and returns a result (c):
(myfcn [a b])
=> c
so I want to iterate myfcn over a collection and create a new sequence
for example let's say myfcn sums a and b, then i would like to do
some