This file contains hidden or 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 Promise do | |
defstruct functions: [] | |
def new(), do: %Promise{} | |
def new(func), do: %Promise{functions: [func]} | |
def then(%Promise{functions: functions} = promise, func) do | |
Map.put promise, :functions, [func | functions] | |
end |
This file contains hidden or 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
class Promise | |
def initialize(&blk) | |
@functions = [blk] | |
end | |
def then(&blk) | |
@functions << blk | |
end | |
def run |
This file contains hidden or 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 Tr do | |
def tr(ast, fun) do | |
case fun.(ast) do | |
{x1, x2, [x3, x4]} -> {x1, x2, [tr(x3, fun), tr(x4, fun)]} | |
{x1, x2, x3} -> {x1, x2, tr(x3, fun)} | |
x -> x | |
end | |
end | |
end |
This file contains hidden or 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 HeaderReader do | |
def read(conn) do | |
{:ok,headers} = Agent.start_link(fn -> %{} end) | |
init_line = :gen_tcp.recv(conn, 0) | |
do_read(headers, conn, :gen_tcp.recv(conn, 0)) | |
end | |
defp do_read(headers, conn, {:ok, "\r\n"}) do | |
IO.inspect Agent.get headers, fn state -> state end | |
:gen_tcp.close conn |
This file contains hidden or 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
class List(T) | |
class Node(T) | |
property :value | |
property :next | |
def initialize(@value : T, @next : List::Node(T) | List::EmptyNode) | |
end | |
end | |
This file contains hidden or 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 s(l, start, stop, accuracy = 1000000) | |
range = stop - start | |
dx = range / accuracy.to_f | |
sum = 0 | |
accuracy.times do | |
sum += 0.5 * (l.(start) + l.(start + dx)) * dx | |
start += dx | |
stop += dx | |
end | |
sum |
NewerOlder