Skip to content

Instantly share code, notes, and snippets.

@zipcode
zipcode / lift.js
Created May 29, 2014 22:07
Lifting in Javascript
/* Came up with this today */
Function.prototype.lift = function(f) {
return function() {
return this.apply(this, Array.prototype.map.call(arguments, f));
}.bind(this)
}
/* Why!? Well, suppose you've got some function that takes a bunch of ints... */
// cmp :: Int -> Int -> Int
function cmp (a, b) {
function id(x) { return x; }
function constant(v) {
return function () {
return v;
}
}
Function.prototype.memoize = function() {
var f = this;
scala> implicit class InputStreamAsString(is: InputStream) {
| def asString = {
| val buff = new Array[Byte](256)
| new String(Iterator.continually({ val c = is.read(buff); buff.take(c)}).takeWhile(_.size>0).flatten.toArray)
| }
| }
defined class InputStreamAsString
scala> var s = new ByteArrayInputStream("asdfasdfasdfasdfasdf".getBytes("UTF-8"))
s: java.io.ByteArrayInputStream = java.io.ByteArrayInputStream@72c454a
@zipcode
zipcode / yuck.js
Last active August 29, 2015 13:56
Object.lenses = function () {
var lens = null;
for (arg in arguments) {
var next = Object.lens(arguments[arg]);
lens = lens ? lens.compose(next) : next;
}
return lens
}
var l = Object.lenses("one", "five")
@zipcode
zipcode / lens.js
Created February 6, 2014 19:59
A quick and dirty jQuery implementation of Lenses. If you're into that sort of thing.
(function ($) {
$.lens = {};
$.lens.of = function (get, set) {
var lens = function (d) { return get(d); }
lens.set = set;
lens.modify = function (d, f) { return set(d, f(get(d))); }
lens.compose = function (other_lens) {
return $.lens.of(
function (d) { return other_lens(get(d)); },
function setps1 {
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
# Hostname (if SSH_TTY is set)
# Directory
# Git branch
# Last command exit status
# $
export PS1='\
@zipcode
zipcode / Brainfuck.scala
Created March 6, 2012 22:32
A quick, dirty and *untested* brainfuck interpreter.
import scala.collection.mutable.Queue
object Brainfuck {
def main(args: Array[String]) {
val code = io.Source.fromFile("src.txt") filter ("[]<>.,+-" contains _) toSeq
exec(code)
}
def exec(code: Seq[Char]) {