Skip to content

Instantly share code, notes, and snippets.

View dbrack's full-sized avatar

Dominik Brack dbrack

View GitHub Profile
@dbrack
dbrack / functor-definition.md
Last active July 19, 2017 13:26
Functors and higher order functions - definition

objects that implement map are functors. So, Array is a functor, and Array.filter is a higher order function

abstract

a functor is an object which acts as a container for a value, which then allows you to APPLY one or more functions to that value, and returns a new functor (in case of .map(), an Array) containing the transformed value.

See also here

#!/bin/sh
BOLD=$(tput BOLD)
NORMAL=$(tput sgr0)
RED='\033[0;31m'
LOCAL_SETTINGS_BASE=~/Library/Application\ Support/Code
LOCAL_SETTINGS="${LOCAL_SETTINGS_BASE}/User"
REMOTE_SETTINGS=~/Google\ Drive/App\ Sync/vscode/Settings
@swissmanu
swissmanu / functional.js
Last active December 7, 2017 14:55
Array juggling in JavaScript with functional programming approach
// precondition: xs is always an array
function head(xs) {
if (xs.length === 0) return null
return xs[0]
}
function tail(xs) {
return xs.slice(1)
}
@swissmanu
swissmanu / eventually.ts
Last active February 15, 2018 08:23
Typed eventually construct using TypeScript and Promises
// Example Usage:
eventually(20)(() => fetch('http://i.fail.often.com/data.json'))
.then(data => console.log(data))
.catch(e => console.log(e));
function eventually<T>(
giveUpAfterNumberOfRetries: number,
nextInterval: (numberOfRetries: number) => number = linearRetryInterval
) {
// Demo: converting a thenable to a Promise
const fulfilledThenable = {
then(reaction) {
reaction('hello');
}
};
const promise = Promise.resolve(fulfilledThenable);
console.log(promise instanceof Promise); // true
promise.then(x => console.log(x)); // hello
dialog {
position: fixed;
top: 50%;
left: 50%;
right: auto;
padding: 30px;
transform: perspective(500px) translate(-50%, -50%);
background: linear-gradient(to bottom, #FFF, #F4F4F4) #FFF;
border: none;
border-radius: 3px;
const handler = {
get(target, propKey, receiver) {
if (/^_[0-9]+$/.test(propKey)) {
const result = [];
const first = Number(receiver);
const last = Number(propKey.slice(1));
for (let i=first; i<=last; i++) {
result.push(i);
}
return result;
export type Tuple2<A, B> = [A, B];
export type Tuple3<A, B, C> = [A, B, C];
export type Tuple4<A, B, C, D> = [A, B, C, D];
export type Tuple5<A, B, C, D, E> = [A, B, C, D, E];
export type Tuple6<A, B, C, D, E, F> = [A, B, C, D, E, F];
export type Tuple7<A, B, C, D, E, F, G> = [A, B, C, D, E, F, G];
export type Tuple8<A, B, C, D, E, F, G, H> = [A, B, C, D, E, F, G, H];
export type Tuple9<A, B, C, D, E, F, G, H, I> = [A, B, C, D, E, F, G, H, I];
export type Tuple10<A, B, C, D, E, F, G, H, I, J> = [A, B, C, D, E, F, G, H, I, J];
@tejacques
tejacques / HOCBaseRender.tsx
Last active May 2, 2022 13:05
React Higher Order Components in TypeScript
import * as React from 'react';
import { Component } from 'react';
export default function HOCBaseRender<Props, State, ComponentState>(
Comp: new() => Component<Props & State, ComponentState>) {
return class HOCBase extends Component<Props, State> {
render() {
return <Comp {...this.props} {...this.state}/>;
}
}