Skip to content

Instantly share code, notes, and snippets.

View trygvea's full-sized avatar

Trygve Matland Amundsen trygvea

View GitHub Profile
function * Numbers () {
let number = 0;
while (true) {
yield ++number;
}
}
function * take (numberToTake, iterable) {
const iterator = iterable[Symbol.iterator]();
import React from 'react'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import { Provider, connect } from 'react-redux'
const reducer = (state = {counter:0}, action = {}) => {
switch (action.type) {
case 'INCREMENT':
return {counter: state.counter+1}
default:
const curry = f => (...args) =>
args.length >= f.length
? f(...args)
: (...more) => curry(f)(...args, ...more)
// Test it. If
const f = (a,b,c) => a+b+c
const g = curry(f)
// then the following holds true:
@trygvea
trygvea / pureStatefulComputation.js
Last active June 29, 2016 08:04
Pure stateful computations in Javascript (ES6)
// push & pop as pure stateful computations
const push = elem => stack => [null, [elem, ...stack]]
const pop = ([head, ...tail]) => [head, tail]
// ----------------------------------------------------------
// Let's do a simple stack manipulation popping the first two
// elements and pushing their product back on to the stack.
// The boring way to do it: Note how we manually must lift
// the stack through computations.
// (compare that to stackManipM below)
@trygvea
trygvea / pureStatefulComputationReloaded.js
Last active October 20, 2016 21:24
Pure stateful computations in Javascript (ES6), reloaded
// pureStatefulComputation: (state) => [result, newState]
// push & pop as pure stateful computations
const push = elem => stack => [undefined, [elem, ...stack]]
const pop = ([head, ...tail]) => [head, tail]
// ------------------------------------------------------------
// stackManip, version 1.
// The passing of state through computations is tedious
@trygvea
trygvea / keybase.md
Created October 12, 2016 19:40
Keybase proof

Keybase proof

I hereby claim:

  • I am trygvea on github.
  • I am trygvea (https://keybase.io/trygvea) on keybase.
  • I have a public key whose fingerprint is 61DF 261E E636 2555 75C0 0143 EBC0 F71B D423 9905

To claim this, I am signing this object:

@trygvea
trygvea / gist:31125217cd7220359e8f0ea392712bef
Last active October 11, 2017 10:23
Face detection using dlib
# Cut and paste from:
# http://dlib.net/face_recognition.py.html
# https://github.com/ageitgey/face_recognition/blob/master/face_recognition/api.py
# https://medium.com/towards-data-science/facial-recognition-using-deep-learning-a74e9059a150
#
# Install dlib: See https://www.pyimagesearch.com/2017/03/27/how-to-install-dlib/
# Download dlib models: http://dlib.net/files/
import os
import dlib
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
type Minus<T, U> = {[P in Diff<keyof T, keyof U>]: T[P]};
interface Eo {type: string}
interface Meo extends Eo{ meoStuff: any}
const isMeo = (eo: Eo): eo is Meo => eo.type === 'MEO type'
const aMethod = (eo: Eo) => {
if (isMeo(eo)) {
eo.meoStuff
}
@trygvea
trygvea / debouncePromise.ts
Last active November 23, 2018 09:02
debouncePromise
import {debounce} from 'lodash'; // or whatever
const debouncePromise = <T>(fn: (...args) => Promise<T>, wait: number, options = {}): ((...args) => Promise<T>) => {
return (...args) =>
new Promise((resolve, reject) => {
const promisedFn = (...args) =>
fn(...args)
.then(resolve)
.catch(reject);
const debouncedPromisedFn = debounce(promisedFn, wait, options);
// Objects with only numeric properties that can be treated as a n-dimensional vector
export type Vector<T> = {
[P in keyof T]: number
};
const vextend = <T>(obj: Vector<T>, key: string, val: number): Vector<T> => Object.assign(obj, { [key]: val });
export const vreduce = <T, R>(obj: Vector<T>, reducer: (agg: R, [key, val]: [string, number]) => R, init: R): R =>