Transform Elixir

In “Grading (with) Elixir” I complained that using Enum.reduce/3 was a bit cumbersome for a simple transform of map values. Funny enough, the last problem I’ve been playing with, the Transform part of ETL kind of hints why Enum.reduce/3 is just the right tool for the job after all. Let see why.

The exercise describes a problem in which you need to take old scrabble scoring system and convert it to the new one. The old system groups letters by their value (point) while the new one requires letters to be the keys and point their respective values, to make scoring a more enjoyable process.

So, basically, we need to take old scoring data and transform it to the new one. Let’s see how:

def transform(input) do
  input |> Enum.reduce(%{}, fn({value, keys}, acc) ->
    keys |>{String.downcase(&1), value})) |> Enum.into(acc)

So this time, we just take the input stream, and transform the input’s values into value => key tuple list, and pack all of that into a single map.

And vuala, the reduce now makes much more sense 🙂

As always, you can find the complete example @ Github.

And as a parting quote, here’s a “deja vu” situation described from that same exercise:

Extract-Transform-Load (ETL) is a fancy way of saying, “We have some crufty, legacy data over in this system, and now we need it in this shiny new system over here, so we’re going to migrate this.”

(Typically, this is followed by, “We’re only going to need to run this once.” That’s then typically followed by much forehead slapping and moaning about how stupid we could possibly be.)

If I had a dime for every time it actually happened! 🙂


Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: