Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Sgeo / lens.rkt
Created February 17, 2014 06:29
#lang racket/base
(require racket/function
racket/list
racket/dict)
(provide (struct-out lens)
lens-get
lens-mod
lens-set
#lang racket/base
(require racket/control)
(struct monad (bind return) #:transparent)
(define-syntax-rule (with-monad ([val m]) body ...)
(let* ([m-once m]
[val
(lambda (ma)
@Sgeo
Sgeo / demethodize
Created November 20, 2014 08:06
Corrected version of @HeadDZombie demethodize based on spread syntax
var demethodize = function(func) {
"use strict";
return function(...args) {
func.apply(args[0], args.slice(1))
};
};
@Sgeo
Sgeo / pure <-> mut
Last active August 29, 2015 14:13
Functions that convert between pure-style (A -> A) and mutating (&mut A -> ()) functions.
fn mut_to_pure<'f, A, FI>(mut f: FI) -> Box<FnMut(A) -> A+'f>
where FI: FnMut(&mut A)+'f {
Box::new(move |mut a| {f(&mut a); a})
}
fn pure_to_mut<'f, A, FI>(mut f: FI) -> Box<FnMut(&mut A)+'f>
where FI: FnMut(A) -> A+'f {
Box::new(move |a: &mut A| {
unsafe {
let mut temp: A = std::mem::uninitialized();
@Sgeo
Sgeo / hlist_with_working_contains.rs
Last active August 29, 2015 14:19
HList implementation with macro that allows production of Contains<T>
#![allow(dead_code)]
struct HNil;
struct HCons<H, T> {
head: H,
tail: T
}
trait Contains<A> {
@Sgeo
Sgeo / split_mut.rs
Created September 17, 2015 05:01
split_mut.rs. This is almost certainly a bad idea
#![allow(dead_code)]
use std::fmt;
use std::error;
#[derive(Debug)]
enum SplitMutError {
AliasedMut
}
@Sgeo
Sgeo / pry crash
Created August 19, 2011 09:39
Pry crash
pry(main)> Object.methods
C:/Ruby192/lib/ruby/gems/1.9.1/gems/pry-0.9.3-x86-mingw32/lib/pry/helpers/base_h
elpers.rb:92:in `block in simple_pager': undefined local variable or method `out
put' for Pry::Helpers::BaseHelpers:Module (NameError)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/pry-0.9.3-x86-mingw32/lib/pry/h
elpers/base_helpers.rb:91:in `each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/pry-0.9.3-x86-mingw32/lib/pry/h
elpers/base_helpers.rb:91:in `each_slice'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/pry-0.9.3-x86-mingw32/lib/pry/h
elpers/base_helpers.rb:91:in `simple_pager'
Question Answer given Answer expected
Is your character an adult man? Probably not Probably
Has your character really existed? Probably not Probably
Does your character have legs? Probably not No
Does your character use a cane? Probably not No
Is your character on an album cover? Probably not No
Does your character wield electricity/lightning as a weapon? Probably not No
Does your character have claws? Probably not No
Is your character Muslim? Probably not No
Is your character hired to kill people? Probably not No
@Sgeo
Sgeo / gist:3846768
Created October 7, 2012 01:30
delimonads
(ns delimonads.core
(:require [monads.core :as m])
(:use delimc.core))
(reset
(defn bind-cont
"Binds its monadic value argument to the continuation.
Easy to understand explanation: (f (bind-cont x) y) becomes do {x' <- x; f x' y}"
[monadic-value]
@Sgeo
Sgeo / gist:4005862
Created November 3, 2012 04:18
Some hypothetical test.
#_(deftest maybe-plus
(is (= :bogus
@(m/plus [m/maybe-zero-val
(m/do m/maybe
[_ (m/maybe 1)]
(throw (Exception. "Should not be thrown")))]))))