Adding numbers inside the list

I have a list like [2,10,3,5]. Now i want to add numbers inside list i.e 2+10+3+5 = 20. I should get 20 as the result, how can i do that??

Enum.sum/1 doesn’t do what you want?

1 Like

thank you :slight_smile:

… or more generically:

Enum.reduce [2,10,3,5], &Kernel.+/2

Enum.reduce/3

List.foldl [2,10,3,5], 0, &Kernel.+/2

List.foldl/3

2 Likes

You can just do &+/2.

2 Likes

Sure, it may be harder to look up / Google though. :wink:
I’ll try in the future to include mentioning it for completeness.

1 Like

https://www.google.com/search?q=elixir+%26%2B/2 :stuck_out_tongue: