Skip to content

Instantly share code, notes, and snippets.

View thelissimus's full-sized avatar
💢

kei thelissimus

💢
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / BitPack.hs
Last active March 1, 2024 06:48
Bit-packing Word32 and Word8 into Word64 in Haskell.
import Data.Bits (Bits (shiftL, shiftR, (.&.), (.|.)))
import Data.Word (Word32, Word64, Word8)
word64 :: (Word32, Word8) -> Word64
word64 (w32, w8) = ((fromIntegral w32 :: Word64) `shiftL` 8) .|. (fromIntegral w8 :: Word64)
{-# INLINE [0] word64 #-}
unword64Fst :: Word64 -> Word32
unword64Fst w = fromIntegral (w `shiftR` 8)
{-# INLINE [0] unword64Fst #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
module Main (main) where
import Control.DeepSeq (NFData, force)
import Control.Exception (evaluate)