Skip to content

Instantly share code, notes, and snippets.

View airled's full-sized avatar

Uladzimir airled

  • Minsk, Belarus
View GitHub Profile
@airled
airled / naive_promise.ex
Created July 21, 2018 10:06
Naive promise implementation for Elixir
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
@airled
airled / naive_promise.rb
Last active July 21, 2018 09:42
Naive promise implementation
class Promise
def initialize(&blk)
@functions = [blk]
end
def then(&blk)
@functions << blk
end
def run
@airled
airled / prewalk.ex
Created May 19, 2018 17:30
simple Macro.prewalk implementation
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
@airled
airled / header_parser.ex
Created May 5, 2018 14:44
Simple HTTP header parser
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
@airled
airled / list.cr
Last active May 5, 2018 19:00
Linked List in crystal
class List(T)
class Node(T)
property :value
property :next
def initialize(@value : T, @next : List::Node(T) | List::EmptyNode)
end
end
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