Skip to content

Instantly share code, notes, and snippets.

@abp
abp / Actor.java
Created May 2, 2012 22:04 — forked from viktorklang/Actor.java
Minimalist Java Actors
// ©2012 Viktor Klang
// 6,016 bytes jarred.
package java.klang;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class Actor {
public static interface Fun<T, R> { public R apply(T t); }
public static interface Effect extends Fun<Behavior, Behavior> { };
public static interface Behavior extends Fun<Object, Effect> { };
public static interface Address { Address tell(Object msg); };
@abp
abp / gist:2645634
Created May 9, 2012 15:37 — forked from stuarthalloway/gist:2645453
Datomic queries against collections
;; Datomic example code
(use '[datomic.api :only (db q) :as d])
;; ?answer binds a scalar
(q '[:find ?answer :in ?answer]
42)
;; of course you can bind more than one of anything
(q '[:find ?last ?first :in ?last ?first]
"Doe" "John")
@abp
abp / gist:2721724
Created May 17, 2012 21:38 — forked from swannodette/gist:2719676
unify_datums.clj
(ns datomic-play.core
(:use [datomic.api :only [db q] :as d])
(:require [clojure.core.logic :as l]
[clojure.pprint :as pp]))
(def uri "datomic:dev://localhost:4334/hello")
(defprotocol IUnifyWithDatum
(unify-with-datum [u v s]))
@abp
abp / part1.md
Created May 18, 2012 09:40 — forked from fogus/part1.md

Inheritance is a key concept in most object-oriented languages, but applying it skillfully can be challenging in practice. Back in 1989, M. Sakkinen wrote a paper called Disciplined inheritance that addresses these problems and offers some useful criteria for working around them. Despite being more than two decades old, this paper is extremely relevant to the modern Ruby programmer.

Sakkinen's central point seems to be that most traditional uses of inheritance lead to poor encapsulation, bloated object contracts, and accidental namespace collisions. He provides two patterns for disciplined inheritance and suggests that by normalizing the way that we model things, we can apply these two patterns to a very wide range of scenarios. He goes on to show that code that conforms to these design rules can easily be modeled as ordinary object composition, exposing a solid alternative to tradi

@abp
abp / restrict-map.clj
Created May 29, 2012 15:14 — forked from fogus/restrict-map.clj
Restricting nested maps to keys of interest
;; I could have used a closed dispatch (aka cond) but you may find this version more enjoyable
;; the spec format is the one provided by BG
(defprotocol Selector
(-select [s m]))
(defn select [m selectors-coll]
(reduce conj {} (map #(-select % m) selectors-coll)))
(extend-protocol Selector
@abp
abp / lens.clj
Created May 30, 2012 11:25 — forked from fogus/lens.clj
(def users [{:name "Brian" :age 22} {:name "Ben" :age 19}])
;; Takes a "path", returns a function that takes an "object" and
;; returns a "costate"
(defn lens [p]
(fn [o]
{:get (get-in o p)
:set #(assoc-in o p %1)}))
(def myAgeLens (lens [0 :age]))
@abp
abp / clojure-match.clj
Created June 16, 2012 21:37 — forked from ckirkendall/clojure-match.clj
Language Compare F#, Ocaml, Scala, Clojure and Haskell
(use '[clojure.core.match :only [match]])
(defn evaluate [env exp]
(match [exp]
[(['Number a] :seq)] a
[(['Add x y] :seq)] (+ (evaluate env x) (evaluate env y))
[(['Multiply x y] :seq)] (* (evaluate env x) (evaluate env y))
[(['Variable i] :seq)] (env i)))
(def environment {"a" 3, "b" 4, "c" 5})
@abp
abp / twip.c
Created June 27, 2012 21:32 — forked from fogus/twip.c
/* The whole point of the twIP stack is to respond to pings. This is
done by reading one IP packet at a time, hoping that it is an IP
ping packet (no check is made!), changing the packet type to a ping
reply packet, updating the ICMP checksum, swapping the IP source
and destination addresses, and sending the packet back. That's
it.
*/
/* This is the packet buffer. I chose the size of the array so that
the maximum packet size that twIP would support would be the same
@abp
abp / automath.clj
Created June 29, 2012 06:18 — forked from fogus/automath.clj
Namespace of functions that are proxies for methods on java.lang.Math for maximum developer convenience.
(ns automath
"Namespace of functions that are proxies for methods on
java.lang.Math for maximum developer convenience."
(:use [clojure.string :only (lower-case)]))
(def ^{:private true
:doc "Set of method names not to generate a proxy function for."}
exclusions #{"min" "max"})
(defn- methods-in
@abp
abp / two-useful-macros.clj
Created June 30, 2012 05:38 — forked from rplevy/two-useful-macros.clj
with and within
(defmacro with
"do things with the first expression passed,
and produce the result"
[expr & body]
`(let [~'% ~expr] ~@body))
(defmacro within
"do things with the first expression passed (for side effects),
but produce the value of the first expression"
[expr & body]