Skip to content

Instantly share code, notes, and snippets.

View kraftdorian's full-sized avatar

Damian kraftdorian

View GitHub Profile
@kraftdorian
kraftdorian / reverseString.ts
Created February 20, 2024 18:01
TypeScript utility type to reverse a string
type Reverse<Input extends string, Acc extends string = ''> =
Input extends `${infer Head}${infer Tail}`
? Reverse<Tail, `${Head}${Acc}`>
: Acc;
// const o1: Reverse<'SATOR'> = 'ROTAS';
// const o2: Reverse<'AREPO'> = 'OPERA';
// const o3: Reverse<'TENET'> = 'TENET';
// const o4: Reverse<'OPERA'> = 'AREPO';
// const o5: Reverse<'ROTAS'> = 'SATOR';
@kraftdorian
kraftdorian / cancelablePromise.js
Last active October 15, 2023 14:21
Cancelable Promise - experiment
/**
* @param {AbortSignal} signal
* @param {(ev: AbortSignalEventMap['abort']) => void} onCancel
* @returns {() => void}
*/
function createSignalCancelListener(signal, onCancel) {
signal.addEventListener('abort', onCancel);
return () => signal.removeEventListener('abort', onCancel);
}
@kraftdorian
kraftdorian / paginate.js
Last active July 9, 2023 19:07
Static-size items pagination
// inspired by https://www.zacfukuda.com/blog/pagination-algorithm
// especialy, the left/right surrounding items idea
const PaginationItem = {
PreviousPage: Symbol('PaginationItem.PreviousPage'),
NextPage: Symbol('PaginationItem.NextPage'),
};
function paginate(
currentPageNumber,
@kraftdorian
kraftdorian / flatten-2d-list.hs
Last active March 12, 2023 17:34
Transform 2-D array to 1-D array
flatten2DList :: [[a]] -> [a] -> [a]
flatten2DList (x:[]) acc = acc ++ x
flatten2DList (x:xs) acc = flatten2DList xs (acc ++ x)
-- or just use concat
flatten2DList' :: [[a]] -> [a]
flatten2DList' x = concat x
/**
* @param {Array<number>} arr Numeric array
* @param {number} needle Value to search for
* @return {number} -1 if value was not found, positive value index otherwise
*/
function binarySearch(arr, needle) {
if (!Array.isArray(arr) || needle == null) {
return -1;
}
@kraftdorian
kraftdorian / genElCombObj.ts
Last active August 4, 2022 16:33
Generating Elementary Combinatorial Object
// Implementation inspired by:
// https://www.site.uottawa.ca/~lucia/courses/5165-09/GenCombObj.pdf
// https://docs.microsoft.com/en-us/previous-versions/visualstudio/aa289166(v=vs.70)
function subsetLexCharacteristicVector(n: number, T: Set<number>): Array<0|1> {
const TX: Array<0|1> = Array.from({ length: n });
let i = 0;
for (i; i < n; ++i) {
TX[i] = T.has(i) ? 1 : 0;
}
@kraftdorian
kraftdorian / positionedTree.ts
Created June 28, 2022 12:54
Algorithm to map the input tree so it receives depth and position information, as well as the flat version of its nodes
interface ITreeNode<Data=unknown> {
id: string|null;
data: Data;
children: ITreeNode<Data>[];
}
interface IPositionedTreeNode<Data=unknown> extends ITreeNode<Data> {
depth: number;
parentId: ITreeNode<Data>['id'];
}
@kraftdorian
kraftdorian / enumComposition.ts
Last active April 11, 2022 14:08
TypeScript enum composition experiments
// based on:
// https://github.com/microsoft/TypeScript/issues/17592#issuecomment-320805415
// https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums
enum GlobalEnum {}
// example enum 1
enum Example1 {
A = 'A',
B = 'B'
@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);