This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Maybe<A> { | |
readonly kind: "Just" | "Nothing"; | |
value?: A; | |
private constructor(kind: "Just" | "Nothing", value?: A) { | |
this.kind = kind; | |
this.value = value; | |
} | |
static readonly just = <A>(value: A): Maybe<A> => new Maybe("Just", value); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Bool { | |
static readonly not = (x: boolean) => !x; | |
static readonly and = (x: boolean, y: boolean) => x && y; | |
static readonly or = (x: boolean, y: boolean) => x || y; | |
static readonly implies = (x: boolean, y: boolean) => !(x && !y); | |
static readonly fromBinary = (x: number) => { | |
if (x === 1) return true; | |
if (x === 0) return false; | |
throw new Error(`Expected an x of either 0 or 1, found ${x}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⎕IO ← 1 | |
⍝ Roll ⍵ d6 | |
Roll←{?⍵/6} | |
⍝ Counts all instances of ⍺ in roll list ⍵. 1s are wild | |
CountRolls ← {+/⍵∊1 ⍺} | |
⍝ (count value) (⍺⍺ _Guess) rolls | |
⍝ applies ⍺⍺ operand to the guess and returns | |
⍝ 1 if the actual count ⍺⍺ count is true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Fn { | |
/** | |
* Identiy function. Returns its own input | |
* @param a | |
* @returns | |
*/ | |
static id = <A>(a: A) => a; | |
/** | |
* Takes two arguments and returns the first. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from "lodash"; | |
type TransformFn = (str: string) => string; | |
type CompareFn = (haystack: string, needle: string) => boolean; | |
/// Helper Functions (not exported) /// | |
// First transforms the needle and the haystack with preProcess | |
// then compares with comparator | |
const cmpHelper = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Turns a single value into a 2 element array, with the same item in both cells | |
* @param a | |
* @returns | |
*/ | |
export const split = <A>(a: A) => [a, a]; | |
/** | |
* Gets the key in a key-value pair | |
* @param param0 a key-value pair. A 2 element array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use v5.30; | |
use warnings; | |
use Getopt::Long; | |
use autodie; | |
# Define command-line arguments | |
GetOptions ( | |
'output=s' => \my $out_file, | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Monoid<T>{ | |
mzero: T; | |
mplus: (a: T, b: T) => T; | |
constructor(mzero: T, mplus: (a: T, b: T) => T) { | |
this.mzero = mzero; | |
this.mplus = mplus; | |
} | |
} | |
const add = new Monoid<number>(0, (a, b) => a + b); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(** s combinator *) | |
let (<*>) f g x = g (f x) x;; | |
(** b combinator, i.e. function composition *) | |
let (<$>) f g x = x |> f |> g;; | |
(** replaces a single element in a list at index `pos` *) | |
let amend a pos lst = List.mapi (fun i x -> if i = pos then a else x) lst;; | |
(** picks a random item from a list *) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from "lodash"; | |
// [0, 1, 2, 3, 4, 5, 6, 7] | |
// b, w, b, w, b, w, b, w | |
// gets all indices of `chr` in `str` | |
const getIndices = (chr) => (str) => | |
str | |
.split("") | |
.map((n, i) => [n, i]) |