Skip to content

Instantly share code, notes, and snippets.

View spencerwi's full-sized avatar

Spencer Williams spencerwi

View GitHub Profile
@spencerwi
spencerwi / Mappable.java
Created May 6, 2016 15:23
HigherKindedTypes blog post code
public interface Mappable<A> {
public <B> Mappable<B> map(Function<A, B> f);
}
@spencerwi
spencerwi / Main.elm
Last active February 24, 2016 22:36
Simple Elm FRP square demo
import Html exposing (div, text)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
import StartApp as StartApp
import Signal as Signal
import Mouse as Mouse
import Effects as Effects
app = StartApp.start
{ init = (init, Effects.none)
@spencerwi
spencerwi / ips.ml
Last active September 9, 2015 02:11
Get IPs in OCaml (using only stdlib)
module StringSet = Set.Make(String)
let addr_to_string = function
| Unix.ADDR_UNIX s -> s
| Unix.ADDR_INET (iaddr, _) -> Unix.string_of_inet_addr iaddr
let get_local_ips () =
Unix.getaddrinfo "localhost" "" []
|> List.map (fun x -> x.Unix.ai_addr)
@spencerwi
spencerwi / tmux.conf
Last active August 29, 2015 14:16
tmux.conf
#Colors
BACKGROUND=black
FOREGROUND=white
setw -g status-bg $BACKGROUND
setw -g status-fg $FOREGROUND
setw -g window-status-current-bg $FOREGROUND
setw -g window-status-current-fg $BACKGROUND
set -g pane-active-border-bg default
set -g pane-active-border-fg $FOREGROUND
@spencerwi
spencerwi / FoldMapAndFilter.clj
Last active May 16, 2020 03:30
Any place I have first-class functions and recursion, I can build foldLeft (reduce), with which I can build map and filter.
(defn fold_ [f initial [head & the-rest :as arr]]
(cond
(empty? arr) initial
:else (recur f (f initial head) the-rest)))
(defn map_ [f arr]
(let [applyF (fn [previous current] (conj previous (f current)))]
(fold_ applyF [] arr)))
(defn filter_ [predicate arr]
@spencerwi
spencerwi / propsToEnvVars.hs
Last active August 29, 2015 14:14
Convert Java properties to environment variables (naively)
#!/usr/bin/env runhaskell
module JavaPropsToEnvVars where
import Data.List (break, stripPrefix)
type PropertyName = String
type PropertyValue = String
type Property = (PropertyName, PropertyValue)
@spencerwi
spencerwi / ips.cl
Created January 19, 2015 02:55
Quick util to print out the IP addresses on a machine. Requires Quicklisp.
(ql:quickload :ip-interfaces)
(defun ip-as-string (interface)
(format nil "~{~A~^~.~}" (map 'list (lambda (x) x) (ip-interfaces:ip-interface-address interface))))
(defun print-interface-ip (interface)
(format t "~A: ~A~%" (ip-interfaces:ip-interface-name interface) (ip-as-string interface)))
(loop for interface in (ip-interfaces:get-ip-interfaces)
do (print-interface-ip interface))
@spencerwi
spencerwi / Java8Demo.java
Created February 21, 2014 15:21
Quick demo playing with Java 8 streams, lambdas, and aggregate operations.
import java.util.List;
import java.util.ArrayList;
import java.util.stream.*;
import java.util.Comparator;
import java.util.Collections;
import java.util.Optional;
import java.util.Random;
class Java8Demo {
private static final String[] poolOfFirstNames = { "Joe", "Paul", "John", "Samantha", "Amanda", "Karen" };
@spencerwi
spencerwi / displayNone.js
Created November 13, 2013 20:21
Poor-man's DocumentFragment using display:none
/* Again, the assumption: arrayOfElements is an array of DOM nodes created from a template or otherwise. */
/* We can set our targetNode to "display:none", change it, and then reset it, making it into a poor man's DocumentFragment */
var targetNode = document.getElementById('target');
var previousDisplayValue = targetNode.style.display;
targetNode.style.display="none"; /* 1 reflow */
var appendToNode = targetNode.appendChild.bind(targetNode);
arrayOfElements.forEach(appendtoTargetNode); /* 0 reflows */
@spencerwi
spencerwi / docfrag.js
Last active December 28, 2015 06:09
Buffering DOM updates using DocumentFragments
/* Assumption: arrayOfElements is an array of DOM nodes created from a template or otherwise. */
/* Don't do this! This reflows the DOM a *lot* */
var targetNode = document.getElementById('target');
var appendToTargetNode = targetNode.appendChild.bind(targetNode);
arrayOfElements.forEach(appendToTargetNode);
/* Instead, use a DocumentFragment */