Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can reduce with list comprehensions in Elixir, but it's uncommon to see:

    sum_list = fn list ->
      for item <- list,
          reduce: 0,
          do: (sum -> sum + item)
    end

    sum_list.([1,2,3])
Other comments are correct that `Enum.reduce/2` is probably better:

    Enum.reduce(list, &(&1 + &2))
Obviously you don't have the flexibility / mutability you refer to in other comments with either of these, but you can always put more in your accumulator:

    get_sum_and_update_count = fn list, map ->
      for item <- list,
          reduce: {0, map},
          do: ({sum, map} -> {sum + item, Map.update(map, item, 1, &(&1 + 1))})
    end
(A contrived function that returns a list sum and an updated map with the count of the values found in the list)


Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: