Skip to content

Instantly share code, notes, and snippets.

@fredyr
fredyr / kvmap.clj
Last active December 25, 2015 16:49
(defn kvmap [fun hash]
(into {} (map (fn [[k v]] [k (fun k v)])
(seq hash))))
(kvmap (fn [k v] (str v "/" k)) {"host" "http://localhost" "most" "umph"})
;; => {"most" "umph/most", "host" "http://localhost/host"}
(defn kvmpa [fun hs]
(reduce-kv (fn [all k v]
@fredyr
fredyr / joy.clj
Last active December 25, 2015 23:59
A little Joy in Clojure
;; Playing around with Joy in Clojure
;; http://en.wikipedia.org/wiki/Joy_%28programming_language%29
;; The code is partly based on Joy in Scheme by John Cowan
;; http://home.ccil.org/~cowan/
(defn get-var [var env] (@env var))
(defn set-var! [var val env] (swap! env conj {var val}))
(def global-env (atom {}))
(def joy-stack (atom []))
rows = 3
glasses = [
0.0,
0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0 ]
def index(i, j):
"ith glass in the jth row"
return i + sum(range(1, j+1))
def dist(v, i, j):
(loop [foo (seq [])
bar [1 2 3]]
(if (empty? bar)
foo
(recur (conj foo (first bar)) (next bar))))
(defn book [state owner]
(prn state)
(om/component
(dom/div nil (dom/h2 nil (:author state)))))
;;=>
{:author "Richard K. Morgan", :book "Altered Carbon"} core.cljs:55
Uncaught TypeError: Cannot read property 'string' of undefined core.cljs:110
;; Manual creation of a cursor giving the same behaviour
;; Inserted this snippet inside an om/component
f x = (42-x)^2
impr = [(x, f x) | x <- [0, 0.1 ..]]
opt goal = head $ dropWhile (\(x, err) -> err >= goal) impr
;; Updated tutorial code for Om 0.1.6, 2014-01-16
;; http://www.lexicallyscoped.com/2013/12/25/slice-of-reactjs-and-cljs.html
;; See comments below for details on the changes.
(def app-state
(atom {:comments [{:author "Pete Hunt" :text "This is a comment."}
{:author "Jordan Walke" :text "This is *another* coment"}]}))
(defn comment [{:keys [author text]} owner]
(om/component
@fredyr
fredyr / om-table.clj
Created January 21, 2014 09:43
Removing a table row failed in om/react when tbody was missing.
(ns om-table.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(enable-console-print!)
(extend-type string
ICloneable
(-clone [n] (js/String. n)))
(def app-state (atom {:table ["very" "undefined" "such" "parentNode"]}))
-- Arne Andersson - Balanced Search Trees Made Simple
-- Original article: http://user.it.uu.se/~arnea/ps/simp.pdf
-- Wikipedia entry: http://en.wikipedia.org/wiki/AA_tree
data AATree a = E | T a (AATree a) (AATree a) Int deriving (Show, Eq)
insert E x = T x E E 1
insert (T y left right level) x
| x < y = balance $ T y (insert left x) right level
| otherwise = balance $ T y left (insert right x) level
@fredyr
fredyr / comments.js
Created February 27, 2014 19:32
React javascript version
var comments = [
{author: "Pete Hunt", text: "This is a text"},
{author: "Suvash", text: "This is *another* comment"}
];
// Data is local to components,
// passed down from the parent
// this.props - immutable