Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a two argument function to a list?

Tags:

clojure

I have a list of elements with some values in it.

I want to map it to a function and produce a list that contains, values computed by applying this two argument function to consecutive values in the first list.

This list will have one less element.

mapping function should take two arguments at a time.

EDIT

Example

the two argument function I am using is quite complex, so for simplicity lets assume it to be a function that calculated average of two numbers.

if I have a list: [3 8 11 14 19 20 88].

Is it possible for me two write a function that maps my average function which for (average 3 8) will give 5.5

for (average 8 11) will give 9.5

and (average 11 14) will give 12.5

and so on...

on applying average to two consecutive values of the list at a time should give me.

[5.5 9.5 12.5 16.5 19.5 54.0]

as result.

map applies a single argument function to whole list and produces a new list with exactly same number of elements.

what I want is a way to apply my function which takes two arguments to take two consecutive at a time, apply my function to it and add the result to a new list.

like image 531
Amogh Talpallikar Avatar asked Jul 17 '13 11:07

Amogh Talpallikar


People also ask

How do you pass multiple arguments to a list in Python?

We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.

Can map take multiple arguments?

You can pass as many iterable as you like to map() function in Python.

How many arguments can be passed to map () function?

The map function has two arguments (1) a function, and (2) an iterable. Applies the function to each element of the iterable and returns a map object.

Can a function argument be a list?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.


1 Answers

You can do it with map:

(map f coll (rest coll))
like image 88
Leonid Beschastny Avatar answered Sep 18 '22 00:09

Leonid Beschastny