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 / autoupdate.pl
Last active June 13, 2022 22:17
A script that automates updating the various packages on my system
#!/usr/bin/env perl
# autoupdate.pl
# copyright 2021 Nikolaj Lepka <slench102+git@gmail.com>
use v5.28;
use strict;
use warnings;
use autodie;
use Term::ANSIColor qw(:constants);
use Carp;
use Getopt::Long;
@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,
);
@ElectricCoffee
ElectricCoffee / SimpleHashGenerator.scala
Last active March 9, 2022 10:39
A very simple checksum generator written in scala, the following checksum types have been tested: MD2 MD5 SHA SHA-256 SHA-512
package your.pkg.here
import java.security.MessageDigest
import java.nio.file.{Files, Paths}
object Generator {
implicit class Helper(val sc: StringContext) extends AnyVal {
def md5(): String = generate("MD5", sc.parts(0))
def sha(): String = generate("SHA", sc.parts(0))
def sha256(): String = generate("SHA-256", sc.parts(0))
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 *)