Skip to content

Instantly share code, notes, and snippets.

View thelissimus's full-sized avatar
💢

kei thelissimus

💢
View GitHub Profile
@thelissimus
thelissimus / request.ts
Created March 14, 2024 12:30
Effect-TS wrapper for common HTTP method for axios.
import axios, { AxiosRequestConfig } from "axios";
import { Effect as IO, Option, pipe } from "effect";
import { credentials } from "@store/global";
import { BackendPath, UnauthenticatedError, RequestFailedError } from "@common/index";
const Headers = {
Authorization: "Authorization",
RefreshToken: "x-refresh-token",
} as const;
@thelissimus
thelissimus / list.rs
Last active March 14, 2024 17:03
Inductive implementation of Linked List in Rust.
use std::fmt;
// List definition
#[derive(Debug, Clone)]
enum List<A> {
Nil,
Cons(A, Box<List<A>>),
}
use List::{Cons, Nil};
@thelissimus
thelissimus / RedBlackTree.lean
Created April 2, 2024 21:51
Red-Black Tree implementation in Lean 4 with type level height.
inductive Color : Type where
| red | black
inductive Node (α : Type) : Color → Nat → Type where
| leaf : Node α .black 0
| nodeB : Node α lc h → α → Node α rc h → Node α .black (h + 1)
| nodeR : Node α .black h → α → Node α .black h → Node α .red h
inductive RedBlackTree (α : Type) : Type where
| tree : Node α .black h → RedBlackTree α
@thelissimus
thelissimus / lc.js
Created April 4, 2024 10:02
Untyped Lambda Calculus combinators in JavaScript.
zero = f => a => a
once = f => a => f(a)
twice = f => a => f(f(a))
thrice = f => a => f(f(f(a)))
succ = n => f => a => f(n(f)(a))
succB = n => f => B(f)(n(f))
num = n => n(x => x + 1)(0)
n0 = zero
n1 = succ(n0)
@thelissimus
thelissimus / RBTree.lean
Last active April 5, 2024 06:23
Lean 4 implementation of "Red-Black Trees in a Functional Setting" by Chris Okasaki.
universe u
inductive Color where
| red
| black
deriving Repr
inductive RBTree (α : Type u) where
| leaf
| node (c : Color) (l : RBTree α) (v : α) (r : RBTree α)
@thelissimus
thelissimus / FunctorApplicativeMonad.ts
Created April 5, 2024 21:08
An implementation of an Option with Functor, Applicative and Monad functions in TypeScript.
// NOTE: Provided Haskell signatures were simplified for learning purposes.
// Such as: renaming `<*>` as `apply`. Operators may unnecessarily confuse the
// reader of this file. Check out the documentation if you want to apply this
// knowledge in real Haskell.
type Option<A> =
| { _tag: "None" }
| { _tag: "Some", value: A }
const none = <A>(): Option<A> => ({ _tag: "None" });
@thelissimus
thelissimus / ListParser.hs
Created April 6, 2024 19:07
A practical application of parser combinators.
module ListParser (module ListParser) where
import Data.Void (Void)
import Text.Megaparsec (Parsec, optional, parseTest, sepBy, (<|>))
import Text.Megaparsec.Char (char, space, string)
import Text.Megaparsec.Char.Lexer (decimal)
type Parser = Parsec Void String
parseInt :: Parser Int
@thelissimus
thelissimus / Ethers.res
Created April 8, 2024 18:27
ReScript FFI example bindings for ethers.
@module("ethers") external decodeBase58Unsafe: string => bigint = "decodeBase58"
@get external code: Js.Exn.t => string = "code"
@get external argument: Js.Exn.t => string = "argument"
@get external value: Js.Exn.t => string = "value"
@get external shortMessage: Js.Exn.t => string = "shortMessage"
type decodeBase58Error =
| UnknownError
| InputError({
@thelissimus
thelissimus / lint.pl
Created April 15, 2024 12:38
LInt implemented in Prolog.
exp(E) --> int(E).
exp(E) --> binop(E).
int(I) --> [I], { integer(I) }.
binop(add(A, B)) --> "+", exp(A), exp(B).
binop(sub(A, B)) --> "-", exp(A), exp(B).
binop(mul(A, B)) --> "*", exp(A), exp(B).
binop(div(A, B)) --> "/", exp(A), exp(B).
@thelissimus
thelissimus / main.go
Last active April 16, 2024 11:51
Better resource management in Go using "Bracket pattern".
package main
import (
"io/fs"
"os"
)
// Manual
func bracket[R any](acquire func() (R, error), release func(R) error, use func(R) error) error {
r, err := acquire()