This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {AbortSignal} signal | |
* @param {(ev: AbortSignalEventMap['abort']) => void} onCancel | |
* @returns {() => void} | |
*/ | |
function createSignalCancelListener(signal, onCancel) { | |
signal.addEventListener('abort', onCancel); | |
return () => signal.removeEventListener('abort', onCancel); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Fun<A, B> = (a: A) => B; | |
class Just<A> { | |
constructor(private readonly _value: A) {} | |
valueOf(): A { | |
return this._value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
NewerOlder