Skip to content

Instantly share code, notes, and snippets.

(ns hash-set-bench
"A Benchmark I modified to Clojure from:
https://github.com/c-cube/hashset_benchs")
(defn iterNeighbors [f [i j]]
(f [(dec i) j])
(f [(inc i) j])
(f [i (dec j)])
(f [i (inc j)]))
@didibus
didibus / virtual-time.clj
Created April 11, 2017 01:09
My take on the google group question found here: https://groups.google.com/forum/#!topic/clojure/oe1Ch1oSlLk
(ns spec-test.virtual-time
(:require [clojure.spec :as s]
[clojure.spec.test :as st]))
(s/def ::virtual-time
(s/or
:positive-infinity #{:positive-infinity}
:negative-infinity #{:negative-infinity}
:number number?))
(s/fdef vt-lt
@didibus
didibus / virtual-time.clj
Created April 11, 2017 04:56
My deftype based take on the google group question found here: https://groups.google.com/forum/#!topic/clojure/oe1Ch1oSlLk
(ns spec-test.virtual-time
(:require [clojure.spec :as s]
[clojure.spec.test :as st]
[clojure.spec.gen :as sgen]))
(deftype VirtualTime [time]
Object
(hashCode [_]
(hash time))
(equals [this that]
@didibus
didibus / abstract-factory.clj
Created April 13, 2017 09:54
My attempt at what the abstract factory pattern would look like in Clojure
(defn draw-ui [{:keys [label casing content]}]
(let [pad (Math/floor (/ (- (Math/max (count label) (count (or (:label content) "")))
(+ 2 (Math/min (count label) (count (or (:label content) "")))))
2))]
(println (str "|" (if (= :upper casing)
(.toUpperCase label)
(.toLowerCase label)) "|" \newline
"|" (reduce (fn [a b] (str a " ")) "" label) "|" \newline
(when content
(str "|" (apply str (repeat pad \space))
@didibus
didibus / #clojurescript-counter
Last active October 30, 2020 00:31
Example of a simple ClojureScript implementation of a counter, using no dependencies.
#clojurescript-counter
@didibus
didibus / reagent-counter.cljs
Last active July 16, 2018 01:41
A simple counter implemented in ClojureScript using reagent as the only dependency.
(ns counter.core
(:require [reagent.core :as r]))
;;;; Utils
(defn ratom? [a]
(= reagent.ratom/RAtom (type a)))
(defn make-state [state]
@didibus
didibus / defnk.clj
Last active November 3, 2018 09:22
Clojure macro to define strict keyword argument functions
(defmacro defnk
[name & fdecl]
(let [doc-string? (when (string? (first fdecl))
(first fdecl))
fdecl (if (string? (first fdecl))
(next fdecl)
fdecl)
attr-map? (when (map? (first fdecl))
(first fdecl))
fdecl (if (map? (first fdecl))
@didibus
didibus / #datafy-nav-example
Last active January 5, 2023 05:09
Clojure datafy/nav example
#datafy-nav-example
@didibus
didibus / user.clj
Created January 29, 2020 04:20
A Clojure user.clj file which lets you lazy load certain namespace at the REPL
(def safe-requires
"List of namespaces to require and refer when inside user ns at load time.
Can be given an initialization body to execute after having been required.
To do so, wrap the lib spec in a vector, and all elements after the lib
spec vector will be evaled after the lib spec has been required."
'[[clojure.repl :as repl :refer (source apropos dir pst doc find-doc)]
[clojure.java.javadoc :as javadoc :refer (javadoc)]
[clojure.pprint :as pprint :refer (pp pprint)]
[clojure.stacktrace :as stacktrace :refer (e)]
@didibus
didibus / #letnoshadow.clj
Last active October 30, 2020 00:27
A Clojure macro which gives you a let that does not allow shadowing of symbols already declared.
#letnoshadow.clj