Skip to content

Instantly share code, notes, and snippets.

View Jeweller-Tsai's full-sized avatar

nate Jeweller-Tsai

View GitHub Profile
defmodule Node do
defstruct value: "", children: []
end
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
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
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
# iterate through a date range, and print the dates
date_range |> Enum.each(fn date -> date |> inspect |> IO.puts end)
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]}
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
d1 = Date.new 2016, 7, 1
d2 = Date.new 2016, 7, 31
(d1 .. d2).each { |d| puts d unless d.sunday? or d.saturday? }
body = %{"title": "Creating an awesome worklog client using Elixir"} |> Poison.encode!
HTTPoison.post! @url, body, %{"Authorization" => "token your_access_token"}
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