Skip to content

Instantly share code, notes, and snippets.

View kraftdorian's full-sized avatar

Damian kraftdorian

View GitHub Profile
@kraftdorian
kraftdorian / flip.ts
Created November 14, 2021 23:17
TypeScript flip function
function flip<A, B, C>(f: (a0: A, a1: B) => C): (b0: B, b1: A) => C {
return (b0: B, b1: A) => {
return f(b1, b0);
}
}
// ==== TEST ====
const x: string = 'test';
const y: number = 1;
@kraftdorian
kraftdorian / monad.ts
Last active November 25, 2021 19:29
TypeScript Monad implementation attempt
//
// Created with help of the great contents from:
// http://learnyouahaskell.com/a-fistful-of-monads
//
// Just a helper type to define exact type of a basic function with one input and output.
type BasicFn<A, B> = (input: A) => B;
// Wrapper interface for Monad valueOf return value, to differentiate from other return types.
interface IMonadValueOf<A> {
@kraftdorian
kraftdorian / User.hs
Last active November 14, 2021 21:23
Some basic experiments in Haskell
-- http://learnyouahaskell.com/modules#making-our-own-modules
module User (
Account,
AccountRole,
accounts,
isAccountRoleEqual,
isAccountRoleSet,
filterAccountsWithRoleSet,
filterAccountsByRole,
filterAccountsByAdminRole
@kraftdorian
kraftdorian / monads.js
Created June 27, 2021 14:12
My first attempt to understand Monads in JavaScript
// Heavily based on this great article by Eric Elliott:
// https://medium.com/javascript-scene/javascript-monads-made-simple-7856be57bfe8
const main1 = () => {
const compose = (f, g) => (x) => f(g(x));
// g :: Integer -> [Integer]
const g = (a) => Array.of(a);
// f :: [Integer] -> Bool
@kraftdorian
kraftdorian / flatten.js
Created June 26, 2021 15:58
Generic flatten implementation in JavaScript
const PRIMITIVE_TYPES = {
string: true,
number: true,
bigint: true,
boolean: true,
symbol: true,
undefined: true
};
function isPrimitive(value) {
@kraftdorian
kraftdorian / haskellStyleAssocListValueByKeyFind.js
Created June 26, 2021 15:39
Assoc list value search by key, inspired with Haskell language style
const Nothing = Symbol('Nothing');
const Just = (value) => Symbol("Just " + JSON.stringify(value));
const isAssocList = (value) => {
return Array.isArray(value) && value.length === 2;
};
const findAssocListValueByKey = (list, key) => {
const [assocItem, ...items] = list;
if (undefined === assocItem) {
@kraftdorian
kraftdorian / scalaStyleTuple.js
Last active April 24, 2021 10:42
Tuple in JavaScript written in Scala style
const tuple = (...atoms) => Object.freeze(
atoms.reduce((partial, atom, index) => ({
...partial,
['_' + ++index]: atom
}), {})
);
@kraftdorian
kraftdorian / api.mjs
Last active April 24, 2021 13:41
Case match pattern in JavaScript written in Erlang style
import {case_spec, match_spec} from './lib.mjs';
const _case = (expression) => {
return {
of: (...predicates) => {
return case_spec(expression, () => predicates.reduce((partial, expr) => {
return {
...partial,
[match_spec(expression, expr.matchSpec)]: expr.matchFn
};
@kraftdorian
kraftdorian / recursiveErlangStyleStringSplit.js
Created April 15, 2021 19:20
Recursive string split in JavaScript written in Erlang style
const usePrologStyleList = (array) => {
const [head, ...tail] = array;
return [head, tail];
};
const useErlangStyleString = (string, resultString) => {
const length = string.length;
if (!length) {
return resultString;
}
@kraftdorian
kraftdorian / recursivePrologStyleListReverse.js
Created April 5, 2021 18:53
Recursive list reverse in JavaScript written in Prolog style
const usePrologStyleList = (array) => {
const [head, ...tail] = array;
return [head, tail];
};
const listReverseState = (list, reversedList, acc) => {
const [head, tail] = usePrologStyleList(list);
if (head === undefined) {
return reversedList = acc;