Skip to content

Instantly share code, notes, and snippets.

@nmn
nmn / $HOC.ts
Created March 4, 2017 01:30
A generalized type-def for Higher-Order Components in React.
type HOC<InjectedProps, ExtraProps, Args: $ReadOnlyArray<*>> = <D, P, C: React$Component<D, P, any>>(
component: Class<C>,
...rest: Args
) => Class<React$Component<D, $Diff<P, InjectedProps> & ExtraProps, any>>;
declare var injectName: HOC<{name: string}, {bla: Array<number>}, [string, number]>;
type Props = {
name: string,
age: number,
@nmn
nmn / HaskellySkewHeap.swift
Last active December 22, 2016 02:06
Swift Skew Heap
// https://en.wikipedia.org/wiki/Skew_heap#Implementation
import Foundation
indirect enum SkewHeap<T> where T: Equatable, T: Comparable {
case Empty
case Node(T, SkewHeap<T>, SkewHeap<T>)
}
func singleton<T>(_ a: T) -> SkewHeap<T> where T: Equatable, T: Comparable {
return .Node(a, .Empty, .Empty)
@nmn
nmn / $Value.ts
Last active July 9, 2016 21:49
[BROKEN] Magic Flow Type Part 2: Trying to create a magic type $Value that gets the value type of an object and key.
/* eslint-disable */
// @flow
type $ObjectPair<K, V> = {[key: K]: V} | Object
type _$Value<Key: string, V, O: $ObjectPair<Key, V>> = V
type $Value<O: Object, K: $Keys<O>> = _$Value<K, *, O>
type Obj = {
c: number,
b: boolean,
@nmn
nmn / $NonNull.ts
Created July 9, 2016 04:50
MagicFlowType 3: Type to convert nullable to non-nullable types
/* @flow */
type _NonNull<T, N: ?T> = T
type $NonNull<N> = _NonNull<*, N>
type Num = $NonNull<?number>
;(45: Num)
// $ExpectError
;(undefined: Num)
@nmn
nmn / .zshrc
Created April 12, 2016 18:23
My Shell
# PREREQUISITES:
# install docker
# git clone antigen into your home folder. Should be able to find $HOME/antigen.zsh
# install rvm
# git clone zsh-autosuggestions into ~/.zsh/
# so `source ~/.zsh/zsh-autosuggestions/autosuggestions.zsh` works.
# Switch terminal to zsh instead of bash
export BROWSER_SYNC_HOSTNAME=<USERNAMEHERE>.dev.agkn.net
export GOPATH=$HOME/go-packages/
@nmn
nmn / VerExBabelPlugin.js
Last active January 21, 2016 03:40
A work in progress to make a Babel plugin that compiles VerbalExpression (nice library to create RegEx) into proper RegEx's at compile time
import VerEx from 'verbal-expressions';
const eventualCallIs = name => {
// FOR PERF
const boundCheck = node =>
node.type === 'Identifier' && node.name === name ||
node.type === 'MemberExpression' && boundCheck(node.object) ||
node.type === 'CallExpression' && boundCheck(node.callee)
return boundCheck;
@nmn
nmn / pureFunctions.js
Last active August 29, 2015 14:26
Quick and dirty hack to make pure functions work in React:
function component(fn){
class C extends React.Component {
render(){
return fn(this.props, this.context)
}
}
C.displayName = fn.displayName || fn.name
C.propTypes = fn.propTypes
C.contextTypes = fn.contextTypes
return C
@nmn
nmn / csp-distribute.js
Last active August 29, 2015 14:18
CSP - distribute expensive tasks to workers - A contrived example to show the strength and flexibility of CSP when compared to FRP.
import {go, chan, put, take, buffers, putAsync, CLOSED} from 'js-csp'
const BUFFER_LIMIT = 100
const NUM_OF_WORKERS = 4
var inputChannel = csp.chan(buffers.dropping(BUFFER_LIMIT))
function fakeDataGenerator(){
putAsync(Math.random())
setTimeout(fakeDataGenerator, Math.random() * 2000)
@nmn
nmn / Enhancer.js
Created April 7, 2015 10:40
Observe API with JS-CSP Mults
import React, { Component } from 'react'
import {operations, chan, go, take, CLOSED} from 'js-csp'
const {mult} = operations
export default function polyfillObserve(ComposedComponent, observe) {
observe = observe || ComposedComponent.observe
if(!observe){
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >
<form>
<record name="recording" beep="true" dtmfterm="true" maxtime="100s">
<prompt>
Please start your recording at the sound of the beep.
</prompt>
<prompt>
After you are finished, you may press any DTMF key to indicate that you are done recording.
</prompt>