Skip to content

Instantly share code, notes, and snippets.

@slikts
slikts / mapUntil.ts
Last active February 23, 2018 10:17
const mapUntil = function*<T, K>(
fn: (a: T) => K,
p: (a: K) => boolean,
xs: T[]
): IterableIterator<K> {
for (const x of xs) {
const result = fn(x)
if (!p(result)) {
break
}
@slikts
slikts / pipe.ts
Last active August 8, 2018 13:19
const pipe = <A, B>(
iterable: IterableIterator<A>,
seed: B,
fn: (a: A) => B
): A => {
const iterator = iterable[Symbol.iterator]()
if (iterable instanceof GeneratorFunction) {
iterator.next()
}
let result = iterator.next(seed)
const makeArray = (length, f) => Array.from({ length }, f);
const range = (a, b) => makeArray(Math.abs(b - a) + 1, (_, i) => a + (i * Math.sign(b - a)));
const compose = (f, g) => a => f(g(a));
const takeUntil = (f, xs) => xs.slice(0, xs.findIndex(f));
const id = a => a;
const not = a => !a;
const takeWhile = (f, xs) => takeUntil(compose(not, f), xs);
const takeTruthy = xs => takeWhile(id, xs);

Keybase proof

I hereby claim:

  • I am slikts on github.
  • I am slikts (https://keybase.io/slikts) on keybase.
  • I have a public key ASADBAyX3ySALSbFLJAaG-CR7gVARLIAhfqxVgSX1RDCuwo

To claim this, I am signing this object:

Verifying my Blockstack ID is secured with the address 1GypFJtCV2dG5kH8JWuCNNxE6ENLpRcEVu https://explorer.blockstack.org/address/1GypFJtCV2dG5kH8JWuCNNxE6ENLpRcEVu
const numberToRawBinary = n => {
const data = new DataView(new ArrayBuffer(8));
data.setFloat64(0, n);
return Array.from(new Uint8Array(data.buffer), x =>
x.toString(2).padStart(8, 0)
).join("");
};
@slikts
slikts / settle.js
Last active June 2, 2018 10:45
Promise.settle()
const settle = ps =>
Promise.all(
ps.map(async p => {
let [value, error, ok] = [null, null, false];
try {
value = await p;
ok = true;
} catch (e) {
error = e;
}
const zip = (xs, ys) => xs.slice(0, Math.min(xs.length, ys.length)).map((x, i) => [x, ys[i]]);
const Delay = duration => new Promise(resolve => setTimeout(resolve, duration))