Skip to content

Instantly share code, notes, and snippets.

View kraftdorian's full-sized avatar
🔍
Open to new opportunities

Damian Kajzer kraftdorian

🔍
Open to new opportunities
View GitHub Profile
@kraftdorian
kraftdorian / maybe.ts
Last active March 9, 2023 14:50
Maybe typeclass utils in TypeScript
type Fun<A, B> = (a: A) => B;
class Just<A> {
constructor(private readonly _value: A) {}
valueOf(): A {
return this._value;
}
}
@kraftdorian
kraftdorian / promiseFunctor.ts
Created November 14, 2021 23:33
TypeScript Promise functor
function PromiseFunctor<In, Out>(f: (input: In) => Out, value: Promise<In>): Promise<Out> {
return value.then<Out>(
a => f(a)
);
}
// ==== TEST ====
const x = 1;
const p = Promise.resolve(x);
@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
};