Skip to content

Instantly share code, notes, and snippets.

@lynaghk
lynaghk / gist:1334086
Created November 2, 2011 16:20
Counting occurrences of items in a collection
;;Three ways to count the number of occurrences in a collection
;; ("orange" "bottle" "coke" "bottle") => [("bottle" "coke" "orange") (2 1 1)]
(let [c '("orange" "bottle" "coke" "bottle")]
(let [counts (reduce #(conj %1 (hash-map %2 (inc (get %1 %2 0))))
{} c)]
[(keys counts)
(vals counts)])
@lynaghk
lynaghk / gist:1334952
Created November 2, 2011 21:17
<canvas> gradient generator
# print to console for debugging
p = (x) ->
console.log x
x
# generate HSL object from normalized arguments
hsl = (hue, saturation, lightness) ->
h: hue
s: saturation
l: lightness
$("#marketList tbody.yui-dt-data")
.find('tr')
.map(function(_,row){
var $r = $(row);
var data = {
"name": $r.find(".yui-dt-col-market").text()
, "sell": parseFloat($r.find(".yui-dt-col-sell").text())
, "buy": parseFloat($r.find(".yui-dt-col-buy").text())
};
return data;
class Cl.errors.Error
description: -> "An error has occurred in Cassowary Coffee"
toString: -> @description()
class Cl.errors.ConstraintNotFound extends Cl.errors.Error
description: -> "Tried to remove a constraint never added to the tableau"
class Cl.errors.NonlinearExpression extends Cl.errors.Error
description: -> "The resulting expression would be nonlinear"
@lynaghk
lynaghk / gist:1551222
Created January 2, 2012 16:06
ClojureScript multimethod namespace reference problem
;;ClojureScript compiler 329708 (2011.12.14)
;;Based on RangeNum protocol by Tassilo Horn:
;; http://groups.google.com/group/clojure/browse_thread/thread/f3192b351b0ab2b4/ec6c106dfdcf0cf9?lnk=gst&q=ranged#msg_ed6de8b7a5cabf41
(ns c2.main
(:refer-clojure :exclude [+])
(:use-macros [c2.util :only [typeof]]))
(defprotocol RangeNumProtocol
@lynaghk
lynaghk / main.clj
Created January 6, 2012 15:26
Cassowary constraint solver in ClojureScript
;;Using the Cassowary constraint solver from ClojureScript
;;This demo shows using multimethods for readable constraint syntax using +, -, and =.
;;Output is a row of circles with random radii spaced so that the space between their boundaries is uniform.
(ns c2.main
;;refer-clojure :exclude is currently broken in ClojureScript master
;;Ticket open: http://dev.clojure.org/jira/browse/CLJS-114
;;Fix applied here: https://github.com/lynaghk/clojurescript/tree/114-refer-clojure-exclude
(:refer-clojure :exclude [+ - =])
@lynaghk
lynaghk / gist:2206272
Created March 26, 2012 16:17 — forked from jonase/gist:2205346
A more advanced rule system?
(ns termite.core
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
(defn- meta-guard [key]
#(do
(println % ":\t" (meta %))
(-> % meta key (= true))))
;;remove anything that has {:clj true} metadata, including on var declarations.
@lynaghk
lynaghk / gist:2271894
Created April 1, 2012 06:03
core.logic questions
(def form '(defmacro nom [x] (some (delicious)) forms))
;;this works nicely
(let [x (lvar)
rest (lvar)]
(run* [q]
(== form (llist 'defmacro x rest))
(== q x)))
;;=> (nom)
.event.scientific
color: gray
padding-left: 20px
//Add a blue CSS triangle
//could use any kind of icon or sprite in here too, of course
&:before
width: 0px
height: 0px
content: ""
position: absolute
@lynaghk
lynaghk / gist:2504215
Created April 26, 2012 23:43
Reactive wishlist
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Sketches of code I want to be able to write in ClojureScript
;;
;;See
;;
;; lampwww.epfl.ch/~imaier/pub/DeprecatingObserversTR2010.pdf
;; http://www.flapjax-lang.org/
;; https://parasol.tamu.edu/groups/pttlgroup/property-models/index.html
;;
;; perf consideration (thanks dnolen): https://github.com/clojure/core.logic/blob/master/src/main/clojure/cljs/core/logic.cljs#L111