Skip to content

Instantly share code, notes, and snippets.

View fogus's full-sized avatar
💭
attempting to learn how to better learn

Fogus fogus

💭
attempting to learn how to better learn
View GitHub Profile
(defn comp+
([] identity)
([f] f)
([f g]
(fn
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
(defn comp+
([] identity)
([f] f)
([f g]
(fn
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
(defn comp+
([] identity)
([f] f)
([f g]
(fn
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
(def sqr #(*% %))
(def csqr (with-constraints sqr
(contract number-cnstr
"constrains the act of squaring"
[n] number? (not= 0 n) => number? pos?))
(csqr 10)
;=> 100
;; returns a map
(def numbered-elements (partial zipmap (iterate inc 0)))
(numbered-elements '[a b c d])
;=> {3 d, 2 c, 1 b, 0 a}
;; returns a seq of pairs
(def numbered-elements #(->> % (interleave (iterate inc 0)) (partition 2)))
(numbered-elements '[a b c d])
(def mvec #(vec (map % %&)))
(mvec #(* % %) 1 2 3 4 5)
(mvec #(.toUpperCase %) "test")
;; The original doesn't return vectors -- it returns lazy seqs
(ns fogus
(:refer-clojure :exclude [or +]))
(defprotocol ReadOps
(+ [this rhs])
(or [this rhs]))
(extend-type String
ReadOps
(+ [this rhs]
(def sqr-8bit-contract
(contract atari-constraints
"Defines the constraints for Atari 2600 squaring"
[n] [(< n 16) integer? pos? => (< % 256)]))
(def sqr-8bit
(with-constraints
constrained-sqr
sqr-8bit-contract))
@fogus
fogus / j.c
Created August 27, 2010 01:33
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef char C;typedef long I;
typedef struct a{I t,r,d[3],p[2];}*A;
#define P printf
#define R return
#define V1(f) A f(w)A w;
#define V2(f) A f(a,w)A a,w;
(defmacro defunits-of [quantity base-unit & units]
(let [magnitude (gensym)
unit (gensym)
conversions (apply hash-map base-unit 1 units)]
`(defmacro ~(symbol (str "unit-of-" quantity))
[~magnitude ~unit]
`(* ~~magnitude
~(relative-units ~unit ~conversions)))))