2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,42 @@
$ cat simple-moving-avg.exs
#!/usr/bin/env elixir
defmodule Math do
def average([]), do: nil
def average(enum) do
Enum.sum(enum) / length(enum)
end
end
defmodule SMA do
def sma(l, p \\ 10) do
IO.puts("\nSimple moving average(period=#{p}):")
Enum.chunk(l, p, 1)
|> Enum.map(&(%{"input": &1, "avg": Float.round(Math.average(&1), 3)}))
end
defmacro gen_func(p) do
quote do
fn l -> SMA.sma(l, unquote(p)) end
end
end
def read_numeric_input do
IO.stream(:stdio, :line)
|> Enum.map(&(String.split(&1, ~r{\s+})))
|> List.flatten()
|> Enum.reject(&(is_nil(&1) || String.length(&1) == 0))
|> Enum.map(&(Integer.parse(&1) |> elem(0)))
end
def run do
sma_func_10 = gen_func(10)
sma_func_15 = gen_func(15)
numbers = read_numeric_input
sma_func_10.(numbers) |> IO.inspect
sma_func_15.(numbers) |> IO.inspect
end
end
SMA.run

View file

@ -0,0 +1,5 @@
#!/bin/bash
elixir ./simple-moving-avg.exs <<EOF
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
2 4 6 8 10 12 14 12 10 8 6 4 2
EOF