Skip to content

Instantly share code, notes, and snippets.

@steveh
Created April 6, 2016 20:46
Show Gist options
  • Save steveh/1337fdf9d8b4c8dc896cd5ddf110e4db to your computer and use it in GitHub Desktop.
Save steveh/1337fdf9d8b4c8dc896cd5ddf110e4db to your computer and use it in GitHub Desktop.
defmodule Coercer do
@date_format "{YYYY}-{0M}-{0D}"
def coerce({value, idx}) do
case idx do
1 -> Decimal.new(value)
2 -> Timex.parse(value, @date_format) |> elem(1)
_ -> value
end
end
end
records = File.stream!("input.csv")
|> CSV.decode
|> Enum.drop(1)
|> Enum.map(fn row ->
Stream.with_index(row)
|> Enum.map(fn cell ->
Coercer.coerce(cell)
end)
end)
IO.inspect records
defmodule DecSum do
def sum(enum) do
enum |> Enum.reduce(Decimal.new(0), &Decimal.add/2)
end
end
vals = Enum.map(records, fn(row) ->
Enum.fetch!(row, 1)
end)
IO.inspect DecSum.sum(vals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment