Skip to content

Instantly share code, notes, and snippets.

View reiddraper's full-sized avatar
🍹

Reid Draper reiddraper

🍹
View GitHub Profile
@gfredericks
gfredericks / gen_subset.clj
Created April 1, 2014 17:19
Example use of the hypothetical gen/for for test.check.
(defn gen-subset
"Generates an even-cardinality subset of the given elements"
[elements]
(gen/for [bools (apply gen/tuple (repeat (count elements) gen/boolean))
:let [true-count (->> bools (filter identity) (count))]
:when (even? true-count)]
(->> (map list bools elements)
(filter first)
(map second)
(set))))
@tekacs
tekacs / show
Created April 20, 2011 17:55
This does something essentially equivalent to showoff.io if you have a publicly facing server...
# Usage: show <local-port> <subdomain>
function show() {
DOMAIN=".tekacs.com"
REMOTE="$2$DOMAIN"
ssh -tR 1080:127.0.0.1:$1 vps "sudo ssh -Nl \$USER -L $REMOTE:80:127.0.0.1:1080 localhost"
}

KV Operations

The following list illustrates how the Riak KV operations would be specified using different apis. The first sub-entry for each list member represents how the operation is specified in the existing api. The second sub-entry shows the operation in the new proposed api and the final sub-entry uses a shortened syntax of b for buckets and k for keys.

  1. List Buckets
@josephg
josephg / gist:1022202
Created June 13, 2011 01:34
Love coffeescript
# A synchronous processing queue. The queue calls process on the arguments,
# ensuring that process() is only executing once at a time.
#
# process(data, callback) _MUST_ eventually call its callback.
#
# Example:
#
# queue = require 'syncqueue'
#
# fn = queue (data, callback) ->
@amtal
amtal / rpn.erl
Created September 25, 2011 04:10
Simple interpreter using a monad for a "mutable" environment.
-module(rpn).
-export([eval/1, hypotenuse/2]).
-compile({parse_transform,do}).
-spec eval([Op]) -> stack_m(ok).
%% Interpreter for a simple stack-based language.
%%
%% Uses a custom stack_m monad, which is a trivial wrapper around state_m[1].
%% It exports:
%% -spec pop() -> stack_m(A).
@gfredericks
gfredericks / flex-config.clj
Created October 17, 2011 18:32
Flexible config via defn-macro
(def *config* nil)
(defn wrap-configged-fn
[f]
(fn [& [maybe-config :as args]]
(if (and *config* (not (identical? *config* maybe-config)))
(apply f *config* args)
(apply f args))))
(defmacro defn-with-config
@CampingScorpion
CampingScorpion / gist:1614509
Created January 15, 2012 05:36
Midje `formula`
;; This first swipe at generative-style testing in Midje was super easy:
;; first a use of it
(defn make-string []
(rand-nth ["a" "b" "c" "d" "e" "f" "g" "i"]))
(formula [a (make-string) b (make-string)]
(str a b) => #(.startsWith % a))
@Pet3ris
Pet3ris / fsmparse.clj
Created January 27, 2012 12:45
Finite State Machines: jumps, flexible transitions and parsing
(ns fsmparse
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
;; We will encode a state machine that accepts lists containing '(w h y) as a sublist
;; Moreover, instead of a recognizer, we will implement a parser, that returns a list
;; of visited states in order
;;
;; +----#-----+----#-----+ +--?--+
;; v | | v |
@CampingScorpion
CampingScorpion / gist:1861626
Created February 19, 2012 02:15
Mutable objects!
(defn make-foo [n]
(let [foo (atom n)]
{:update (fn [n] (reset! foo n))
:inc (fn [] (swap! foo inc))
:status (fn [] (print @foo))} ))
(defmacro defobject
"doesn't work yet"
[name field-vec & methods]
(let [methods (partition 3 methods)
@Pet3ris
Pet3ris / fst.clj
Created February 25, 2012 13:02
Finite State Transducers: English pluralization
(ns fst
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
;; A finite state transducer is essentially a translator between
;; two tapes of symbols. It is normally a translator from an input
;; tape to an output tape, but since we are using core.logic,
;; we hope to relax this restriction :).
;; The main idea is that every transition accepts two symbols