Skip to content

Instantly share code, notes, and snippets.

View ElectricCoffee's full-sized avatar

Niko Lepka ElectricCoffee

View GitHub Profile
@ElectricCoffee
ElectricCoffee / bool.ts
Created September 20, 2022 08:03
utility functions for the boolean type
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}`);
⎕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
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.
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 =
@ElectricCoffee
ElectricCoffee / arrow.ts
Last active June 9, 2022 09:16
An implementation of Haskell's arrow functions in Typescript. Originally used to deal with key-value pairs, hence the specific names, but the functions are general enough to be used for any two-element arrays.
/**
* 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
#!/usr/bin/env perl
use v5.30;
use warnings;
use Getopt::Long;
use autodie;
# Define command-line arguments
GetOptions (
'output=s' => \my $out_file,
);
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);
@ElectricCoffee
ElectricCoffee / chess960.ml
Last active January 25, 2022 23:20
Fischer Random Chess setup generator written in OCaml
(** 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 *)
@ElectricCoffee
ElectricCoffee / Chess960.js
Last active January 25, 2022 23:20
Fischer Random Chess setup generator
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])
=head1 Updater
A class that houses the data and processes required to correctly deal with the updater yaml file.
=over 4
=cut
package Updater;