Created
November 24, 2013 21:47
-
-
Save ggb/7632813 to your computer and use it in GitHub Desktop.
Kata Four: Data Munging. >> http://codekata.pragprog.com/2007/01/kata_four_data_.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule DataMuging do | |
def processLines [], result, _cols do | |
result | |
end | |
def processLines [head | tail], result, { fst, scd, thd } do | |
if Regex.match?(%r/^\s*\d+.?\s+/, head) do | |
s = String.split head | |
processLines tail, [ { Enum.at(s, fst), | |
Enum.at(s, scd) |> String.replace("*", ""), | |
Enum.at(s, thd) |> String.replace("*", "") } | result ], { fst, scd, thd } | |
else | |
processLines tail, result, { fst, scd, thd } | |
end | |
end | |
def evalTriple data do | |
data |> Enum.min_by (fn( { _x, y, z } ) -> abs(binary_to_integer(y) - binary_to_integer(z)) end) | |
end | |
def main file, cols do | |
case File.read file do | |
{ :ok, contents } -> String.split(contents, "\n") |> processLines([], cols) |> evalTriple | |
{ :error, reason } -> reason | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment