Skip to content

Instantly share code, notes, and snippets.

View miner's full-sized avatar

Steve Miner miner

View GitHub Profile
@reborg
reborg / clojure-irc-pdf-links
Last active July 16, 2017 15:18
PDF Links #Clojure IRC
(defn double-map [f]
(let [^clojure.lang.IFn$DD f f]
(fn [f1]
(if (instance? clojure.lang.IFn$DDD f1)
(let [^clojure.lang.IFn$DDD f1 f1]
(fn
([] (f1))
(^double [^double result] (.invokePrim ^clojure.lang.IFn$DD f1 result))
(^double [^double result ^double input]
(.invokePrim f1 result (.invokePrim f input)))
@ghadishayban
ghadishayban / weighted_rand.clj
Last active September 23, 2022 08:15
Vose's alias method for weighted randoms
(ns weighted-rand
(:import clojure.lang.PersistentQueue))
(defprotocol Rand
(nextr [_ rng]))
;; Vose's alias method
;; http://www.keithschwarz.com/darts-dice-coins/
(deftype Vose [n ^ints alias ^doubles prob]
(require '[clojure.core.async :as a])
(def xform (comp (map inc)
(filter even?)
(dedupe)
(flatmap range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(flatmap flatten)
(random-sample 1.0)
(in-ns 'eclj.core)
(defprotocol Fn
:on-interface clojure.lang.Fn
"Marker interface indicating invokeables that are explictly functions")
(defprotocol IFn
:on-interface clojure.lang.IFn
(^{:on :invoke} -invoke
[this]
@gfredericks
gfredericks / schema->gen.clj
Created March 26, 2014 16:49
Trying to generate test.check generators from prismatic schemas.
(ns schema->gen
"Functions for generating test data from schemas."
(:require [four.stateful :as four]
[re-rand :refer [re-rand]]
[schema.core :as sch]
[simple-check.generators :as gen]))
(defn ^:private re-randify-regex
"schema requires ^$ while re-rand forbids them"
[re]
@cgrand
cgrand / comprehensions.clj
Created May 24, 2013 14:06
Comprehension framework, upon which are (re)implemented, for, doseq, reducible/foldable for and reduce-based doseq
;; I wrote this in the Eurostar on my way back from the last lambdanext.eu clojure course.
(ns comprehensions
(:refer-clojure :exclude [for doseq])
(:require [clojure.core.reducers :as r]))
;; borrowed from clojure.core
(defmacro ^{:private true} assert-args
[& pairs]
`(do (when-not ~(first pairs)
@z5h
z5h / ycombinator.clj
Created March 6, 2013 20:25
Applicative-Order Y Combinator (Clojure Version)
; Stumbling towards Y (Clojure Version)
;
; (this tutorial can be cut & pasted into your IDE / editor)
;
; The applicative-order Y combinator is a function that allows one to create a
; recursive function without using define.
; This may seem strange, because usually a recursive function has to call
; itself, and thus relies on itself having been defined.
;
; Regardless, here we will stumble towards the implementation of the Y combinator.
(ns net.cgrand.decay
"Exponentially decaying lists. See http://awelonblue.wordpress.com/2013/01/24/exponential-decay-of-history-improved/
for background and http://clj-me.cgrand.net/2013/02/12/decaying-lists-log-scale-for-lists/ for documentation")
;; PRNG, formulas straight from java.util.Random javadoc
(defn- seed ^long [^long n]
(bit-and (unchecked-multiply n 0x5DEECE66D)
(unchecked-dec (bit-shift-left 1 48))))
(defn- next-seed ^long [^long seed]
@semperos
semperos / clojure-deftype-scaffolding.clj
Created October 4, 2012 18:16
Clojure Scaffolding for deftype (Christophe Grand) - Show which methods a class implements and for which interfaces
;; Big thanks to Christophe Grand - https://groups.google.com/d/msg/clojure/L1GiqSyQVVg/m-WJogaqU8sJ
(defn scaffold [iface]
(doseq [[iface methods] (->> iface .getMethods
(map #(vector (.getName (.getDeclaringClass %))
(symbol (.getName %))
(count (.getParameterTypes %))))
(group-by first))]
(println (str " " iface))
(doseq [[_ name argcount] methods]
(println