Skip to content

Instantly share code, notes, and snippets.

@dbramucci
dbramucci / polymorphic_plus.ml
Created October 21, 2020 01:44
Example of using OCaml's module system for adhoc polymorphic infix operators.
(*
If you want, you can replace all
+?
with
+
and this will still work.
It's just that the default (+) for `int` will be shadowed after the
let Open Num in
line
*)
@dbramucci
dbramucci / gist:a0d84a701bf40c28e006f5ff2c207132
Created March 13, 2020 00:25
generators in Haskell without conduits/pipes
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
# fib n = go 0 1 n
# where go a _ 0 = a
# go a b n = go b (a + b) (n - 1)
@dbramucci
dbramucci / hailstone.hs
Created March 12, 2020 23:46
A cool demonstration of complex lazy evaluation and recursive list definitions
hailstone :: [[Int]]
hailstone = [] : [1] : map (\n -> let n' = collatz n in n : (hailstone !! n')) [2..]
collatz n = if even then half else 3 * n + 1
where
(half, r) = n `quotRem` 2
even = r == 0
{- GHCI output, _ means that part hasn't been evaluated yet
Prelude> hailstone !! 3
# Forgets results in between calls
# Also, it is nowhere close to as efficient as the Haskell version
# i.e. I think this is exponential in time
def fibs_gen():
yield 0
yield 1
fib = fibs_gen()
fib_tail = fibs_gen()
@dbramucci
dbramucci / jump.py
Last active February 6, 2020 08:06
Code to run benchmarks
from typing import List
from pyrsistent import pvector
from pyrsistent.typing import PVector
def benchmark_jump(n: int):
string = 'xx.' * n
for i in range(0, n, 3):
string = jump(string, i, 1)
@dbramucci
dbramucci / sillyfib.py
Last active February 3, 2020 23:42
Fibonacci implemented with barely any use of anything that isn't a function
def Zero(zero_val):
return lambda rec_func: zero_val
def Succ(nat):
return lambda zero_val: lambda rec_func: rec_func(nat)
def add(a):
return (lambda b:
@dbramucci
dbramucci / result.cs
Created February 3, 2020 02:34
C# Result Proof of Concept
using System;
using System.Threading;
// S is the type stored for successful computations, E for errors
public struct Result<S, E>
{
private bool isSuccessful;
private S successValue;
private E errorValue;
from timeit import timeit
a = list(range(6))
num = 1_000_000
print("toons's: ", timeit("""
i = iter(islice(iter(a), 0, len(a), 2))
j = iter(islice(iter(a), 1, len(a), 2))
try:
@dbramucci
dbramucci / HaskellCheatsheet.hs
Last active April 22, 2024 03:57
A cheatsheet for Haskell syntax.
-- save as HaskellCheatsheet.hs
module HaskellCheatsheet where
-- On Windows, I sometimes have to remove the module line above
-- in order to get GHC to compile the file as an executable instead
-- of a library.
-- Single line comment
{-
  • Protocol
  • One JSON Dictionary transmitted as a string through ws or wss
  • All Packets
    • Fields
      • "PacketType" - string
        • Describes what type of packet this is
      • "PacketID" - string
        • Uniquely identifies the string transmitted
      • "ProtocolVersion" - string
  • Identifies the current version of the protocol being used