Comment

Comment by nivertech

Of course in Elixir[1] reduce is more efficient (at least memory-wise) than map/flat_map, but in case of scripting it usually doesn't matter (unless this JSON file is huge, but it's fully loaded into memory and parsed there anyway).

I would rewrite the example from the post like this (NOTE: I didn't run it, so it might be slightly incorrect):

    #/usr/bin/env elixir

    "file.json"
    |> File.read!()
    |> Jason.decode!()
    |> Enum.flat_map(& &1["children"])
    |> Enum.map(&{&1["id"], &1})
    |> Enum.into(%{})
---

1. although map is much easier to parallelize/vectorize (e.g. in Array PLs)

parent nivertech 3 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton