Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# sum two sequences by element

Tags:

f#

I am looking for a way to sum two sequences by element in F#.

For example, if I have these two sequences:

let first = seq [ 183.24; 170.15;174.17]
let second = seq [25.524;24.069;24.5]

I want to get the following result:

third list = [208.764;194.219;198.67]

What would be the simplest or the best way to achieve this?

like image 532
A191919 Avatar asked Dec 11 '22 09:12

A191919


1 Answers

You can use the zip function :

let third = Seq.zip first second |> Seq.map (fun (x, y) -> x + y)

It will create a new sequence with a tuple where the first element is from first and second form second, then you can map and apply the addition of both elements.

As pointed in the comments, map2 is another option, we could say that map2 is equivalent to zip followed by map.

like image 111
Gus Avatar answered Dec 15 '22 03:12

Gus