patternMinor
Elixir pipes and anonymous functions
Viewed 0 times
pipesanonymouselixirandfunctions
Problem
I recently got started with Elixir. I'm used to F#'s pipes, and
How should this be done?
Seq.map and LINQ's .Select statements. Things are different in Elixir, and the code I have seems very ugly. Anon functions in anon functions.defrecord FileData, name: "", date: nil
def filedetails() do
files = File.ls!
datestr = fn {{year, month, day}, _time} -> "#{day}/#{month}/#{year}" end
filedates = files |> (Stream.map &(File.stat!(&1)))
|> (Stream.map &(&1.ctime))
|> Stream.map &(datestr.(&1))
Enum.zip(files,filedates)
|> Enum.map fn {n,d}->FileData[name: n, date: d] end
endHow should this be done?
Solution
I'm not sure why you'd need to pipe three different streams - one for every manipulation. I'd probably use one Stream to do all the manipulations together, something like:
or
filedates = files |> Stream.map &((&1 |> File.stat!).ctime |> datastr.())or
filedates = files |> Stream.map &(File.stat!(&1).ctime |> datastr.())Code Snippets
filedates = files |> Stream.map &((&1 |> File.stat!).ctime |> datastr.())filedates = files |> Stream.map &(File.stat!(&1).ctime |> datastr.())Context
StackExchange Code Review Q#39532, answer score: 5
Revisions (0)
No revisions yet.