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.
We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.
You can pass as many iterable as you like to map() function in Python.
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.
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.
You can do it with map:
(map f coll (rest coll))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With