Skip to content

Instantly share code, notes, and snippets.

@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)); },
@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")
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
function id(x) { return x; }
function constant(v) {
return function () {
return v;
}
}
Function.prototype.memoize = function() {
var f = this;
@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) {
@zipcode
zipcode / BetterBooly.scala
Created June 4, 2014 18:26
How Scalaz uses implicits to augment objects.
import scala.language.implicitConversions
/*
So earlier this week I was reading about how scalaz implements the "enhance my library"
pattern. It's a pretty nifty way to add methods to existing objects in a type-safe way.
However, this is @zip here, so of course I'm gonna use it for something gross.
So, I'm a fan of Javascript, inexplicably. Javascript has a… looser… notion of types.
In particular, all sorts of things can be true or false! Empty lists are false, full
ones are true. Who wouldn't want the following capability?
@zipcode
zipcode / Monads.scala
Created June 5, 2014 22:52
A shitty explanation of Monads in Scala
import scalaz._
import Scalaz._
object HowMonadsWork {
/*
You may have found yourself asking, what is a Monad, anyway?
Some kinda burrito-spacesuit-magical-wavy thing, if you ask the internet.
The internet isn't very helpful. This probably isn't, either.
function setps1 {
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
# "(root) " in red if root
# Hostname (if SSH_TTY is set)
# Directory
# Git branch, if in git and available
# Last command exit status
# $ times shlvl, or # if root
from functools import wraps
class instancemethod(object):
def __init__(self, f):
self.f = f
def __get__(self, obj, cls=None):
if obj is None:
obj = cls.instance()
@wraps(self.f)
javascript:(function(){var g="#gamergate";var f=function(t){return t.split(' ').map(function(w){return w.toLowerCase()==g?w:'butt'}).join(' ')};var es=document.getElementsByClassName("tweet-text");for(var i=0;i<es.length;i++){if(es[i].innerText.toLowerCase().indexOf(g)>0)es[i].innerText=f(es[i].innerText)}})()