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 Node do | |
defstruct value: "", children: [] | |
end |
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 DateRange do | |
defstruct first: nil, last: nil, first_gregorian_days: nil, last_greogorian_days: nil | |
defmodule Operator do | |
def first <~> last do | |
DateRange.new first, last | |
end | |
end |
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 ExclusiveDateRange do | |
defstruct first: nil, last: nil | |
def new(first, last) do | |
%ExclusiveDateRange{first: first, last: last} | |
end | |
defimpl Enumerable, for: ExclusiveDateRange do | |
def reduce _dr, {:halt, acc}, _fun do |
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
def next %Date{year: y, month: m, day: d} do | |
case Date.new(y, m, d + 1) do | |
{:ok, nd} -> nd | |
{:error, _} -> | |
case Date.new(y, m + 1, 1) do | |
{:ok, nd} -> nd | |
{:error, _} -> | |
{:ok, nd} = Date.new(y + 1, 1, 1) | |
nd | |
end |
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
# iterate through a date range, and print the dates | |
date_range |> Enum.each(fn date -> date |> inspect |> IO.puts end) |
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 DateRange do | |
defstruct first: nil, last: nil | |
end | |
d1 = ~D[2016-07-01] | |
d2 = ~D[2016-07-31] | |
%DateRange{first: d1, last: d2} # => %DateRange{first: ~D[2016-07-01], last: ~D[2016-07-31]} |
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
def july do | |
1..31 | |
|> Enum.map(fn i -> | |
{:ok, d} = Date.new 2016, 7, i | |
d | |
end) | |
|> Enum.filter(fn d -> | |
!weekend?(d) | |
end) | |
end |
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
d1 = Date.new 2016, 7, 1 | |
d2 = Date.new 2016, 7, 31 | |
(d1 .. d2).each { |d| puts d unless d.sunday? or d.saturday? } |
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
body = %{"title": "Creating an awesome worklog client using Elixir"} |> Poison.encode! | |
HTTPoison.post! @url, body, %{"Authorization" => "token your_access_token"} |
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 ElixirTail do | |
def tail path do | |
Stream.resource(fn -> File.open!(path) end, | |
fn file -> | |
case IO.read(file, :line) do | |
:eof -> | |
:timer.sleep 10 | |
{[], file} | |
data -> | |
IO.write data |
NewerOlder