Skip to content

Instantly share code, notes, and snippets.

View clohr's full-sized avatar

Christian Lohr clohr

View GitHub Profile
@clohr
clohr / singly-linked-list.js
Created March 7, 2017 00:37
ES6 Singly Linked List
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class SinglyList {
constructor() {
this._length = 0;
@clohr
clohr / getDuplicates.js
Created March 4, 2017 19:56
Return duplicates within a JS array
const count = names => names.reduce((acc, name) => Object.assign(acc, {[name]: (acc[name] || 0) + 1}), {})
const duplicates = dict => Object.keys(dict).filter((a) => dict[a] > 1)
const getDuplicates = vals => duplicates(count(vals))
export default getDuplicates
@clohr
clohr / queue.js
Last active November 25, 2017 19:42
ES6 queue example
class Queue {
constructor() {
this._oldestIndex = 1
this._newestIndex = 1
this._storage = {}
}
getStorage() {
return this._storage
}
getSize() {
@clohr
clohr / stack.js
Last active March 4, 2017 19:15
Creating a stack type in JS
class Stack {
constructor() {
this._size = 0
this._storage = {}
}
getSize() {
return this._size
}
getStorage() {
return this._storage
@clohr
clohr / sets.js
Created February 26, 2017 19:09
ES6 Sets: Intersection, Difference, and Union
const a = new Set([1, 2, 3, 4, 4, 4])
const b = new Set([3, 4, 5, 6])
const intersect = (set1, set2) => [...set1].filter(num => set2.has(num))
const differ = (set1, set2) => [...set1].filter(num => !set2.has(num))
const joinSet = (set1, set2) => [...set1, ...set2]
const myIntersectedSet = new Set(intersect(a, b))
console.log('myIntersectedSet', myIntersectedSet)
@clohr
clohr / fmap.swift
Created February 5, 2017 03:35
fmap in Swift 3 to handle nil values
// fmap
precedencegroup ComparisonPrecedence {
associativity: left
}
infix operator <^> : ComparisonPrecedence
func <^><A, B>(f: (A) -> B, a: A?) -> B? {
switch a {
case .some(let x):
@clohr
clohr / lasso.js
Last active February 3, 2017 14:39
Capture mouse click and touch events
const head = ([h, ...tail]) => h
const defaultTo = (defaultValue, testValue) => Boolean(testValue) ? testValue : defaultValue
const capture = (evt) => {
const eventTarget = evt.target
const touchTarget = evt.touches && evt.touches.length > 0
const xPos = touchTarget ? head(touchTarget).clientX : evt.clientX
const yPos = touchTarget ? head(touchTarget).clientY : evt.clientY
@clohr
clohr / updating-deep-records.elm
Last active January 30, 2017 03:36
Updating deep records in Elm
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
main =
program
{ init = init
@clohr
clohr / nested-list-of-records.elm
Last active January 30, 2017 03:18
Updating nested records within lists
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import Array exposing (Array)
main =
program
{
init = init,
update = update,
@clohr
clohr / isomorphism.js
Last active December 17, 2016 18:58
Isomorphisms from Composable Functional JavaScript
// Either
const Right = x => ({
map: fn => Right(fn(x)),
fold: (f, g) => g(x),
inspect: () => `Right(${x})`
})
const Left = x => ({
map: fn => Left(x),
fold: (f, g) => f(x),