Skip to content

Instantly share code, notes, and snippets.

View airled's full-sized avatar

Uladzimir airled

  • Minsk, Belarus
View GitHub Profile
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
@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
@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 / 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 / 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 / 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
# Carrierwave works incorrectly with file formatting. If you do not override
# path and url methods it will return you incorrect path and url to webp file
# respectively. Also you should add removing callback because Carrierwave could
# not delete webp file by itself because of incorrect path.
# Remove this hacks if this issue will be fixed.
class ItemGalleryUploader < ApplicationUploader
WEBP_VERSION_NAME = :webpver
def filename
splitted = original_filename.split('.')
# Carrierwave works incorrectly with file formatting. If you do not override
# path and url methods it will return you incorrect path and url to webp file
# respectively. Also you should add removing callback because Carrierwave could
# not delete webp file by itself because of incorrect path.
# Remove this hacks if this issue will be fixed.
class ItemGalleryUploader < ApplicationUploader
WEBP_VERSION_NAME = :webpver
WEBP_CONV_OPTIONS = {quality: 80, method: 5}.freeze
before :remove, :clear_webp_file
@airled
airled / pytorch_linear_regression.py
Created December 11, 2019 07:28
Simple linear regression in pytorch
import torch
from torch import nn
import numpy as np
net = nn.Sequential(
torch.nn.Linear(2, 1)
)
def fun(x1, x2):
return 2 * x1 + 3 * x2 + 5
pipeline = []
define_method :use, -> middleware do
pipeline.unshift(middleware)
end
class One
def initialize(app)
@app = app
end