Skip to content

Instantly share code, notes, and snippets.

View airled's full-sized avatar

Uladzimir airled

  • Minsk, Belarus
View GitHub Profile
@airled
airled / contract_call.js
Created April 11, 2024 12:55
ETH contract call
const { ethers } = require("ethers");
const rpcUrl = ''
const contractAddress = ''
const abi = []
async function main() {
const provider = new ethers.JsonRpcProvider(rpcUrl);
const signer = new ethers.Wallet("", provider)
const contract = new ethers.Contract(contractAddress, abi, signer);
@airled
airled / subs.rb
Created February 5, 2024 05:55
Check substring in string
def subs(str1, str2)
return nil if str2.size > str1.size
str1.each_char.with_index do |char, index|
next if char != str2[0]
match = true
str2[1..].each_char.with_index do |_, str2_slice_index|
str2_index = str2_slice_index + 1
next if str2[str2_index] == str1[index + str2_index]
@airled
airled / curry.js
Last active March 2, 2023 15:08
Simple currying example in JS
class Curry {
constructor(fn) {
return this.curry(fn, fn.length, [])
}
curry(fn, argRestCounter, args) {
if (argRestCounter === 0) return fn(…args);
return arg => this.curry(fn, argRestCounter 1, […args, arg]);
}
}
pipeline = []
define_method :use, -> middleware do
pipeline.unshift(middleware)
end
class One
def initialize(app)
@app = app
end
@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
# 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
# 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('.')
@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