Skip to content

Instantly share code, notes, and snippets.

@cemerick
cemerick / generative.clj
Created June 27, 2012 15:03
"Integrating" clojure.test and test.generative
;; My good-enough glomming together of clojure.test and test.generative
(ns cemerick.generative
(:require [clojure.test.generative.generators :as gens]
[clojure.test.generative :as gen])
(:use clojure.test))
;; Too bad last-report isn't sent an action upon success as well.
;; Perhaps this should just be replaced with a try/catch/rethrow
;; around the body in defspectest
@cemerick
cemerick / browser_repl.clj
Last active August 12, 2024 17:33
browser-REPL refactoring
;; ## Changes from cljs.repl.browser
;;
;; * Multiple concurrent browser-REPLs can be safely used
;; * The browser-REPL's HTTP server is now always-on
;; * Each browser-REPL session supports a new top-level "entry" URL that
;; can be used to easily start the REPL in a browser or other JS runtime
;; (i.e. you don't need to have a separate webapp running to initiate the
;; browser-REPL connection)
;; * The entry (and REPL) URLs are available in slots on the browser-REPL's
;; environment, making it trivial to automate browser-REPL sessions
@cemerick
cemerick / gist:5457242
Last active April 20, 2024 19:51
Using language-detection from Clojure
; using https://code.google.com/p/language-detection/
; i.e. [com.cybozu.labs/langdetect "1.1-20120112"]
(require '[clojure.java.io :as io])
(->> #{"af" "ar" "bg" "bn" "cs" "da" "de" "el" "en" "es" "et" "fa" "fi" "fr" "gu"
"he" "hi" "hr" "hu" "id" "it" "ja" "kn" "ko" "lt" "lv" "mk" "ml" "mr" "ne"
"nl" "no" "pa" "pl" "pt" "ro" "ru" "sk" "sl" "so" "sq" "sv" "sw" "ta" "te"
"th" "tl" "tr" "uk" "ur" "vi" "zh-cn" "zh-tw"}
(map (partial str "profiles/"))
@cemerick
cemerick / defonce.clj
Created August 25, 2013 03:01
`defonce` for ClojureScript
(ns whatever.cljs
(:require [cljs.compiler :refer (munge)])
(:refer-clojure :exclude (munge defonce)))
(defmacro defonce
[vname expr]
(let [ns (-> &env :ns :name name munge)
mname (munge (str vname))]
`(when-not (.hasOwnProperty ~(symbol "js" ns) ~mname)
(def ~vname ~expr))))
let asprintf_nm x =
let open Format in
let fns = get_formatter_out_functions () in
try
set_formatter_out_functions {fns with out_newline = Fun.const () ; out_indent = Fun.const ()};
asprintf x
with exn ->
set_formatter_out_functions fns;
raise exn
@cemerick
cemerick / gist:6865761
Last active May 25, 2020 14:59
disabling λ, ƒ, and ∈ characters replacing fn and # without forking emacs-live
; in ~/.live-packs/$YOURNAME-pack/init.el
; not using `eval-after-load` because doing so appears to affect only the keywords associated
; with the first "type" of Clojure file you open (i.e. .clj or .cljs); this should knock out
; the special formatting on every buffer that ever has clojure-mode applied to it
(add-hook 'clojure-mode-hook
(lambda ()
(font-lock-remove-keywords
nil `(("(\\(fn\\)[\[[:space:]]"
(0 (progn (compose-region (match-beginning 1)
(match-end 1) "λ")
@cemerick
cemerick / gist:3750288
Created September 19, 2012 15:32
Extending defrecord types in Clojure
;; Records are just types that provide default implementations of certain
;; key interfaces that allow them to stand in for maps.
;; This set of interfaces/protocols is not closed though; you can certainly make them
;; useful in places where maps aren't, e.g. w/ sequential destructuring:
=> (defrecord Point [x y]
clojure.lang.Indexed
(nth [_ i] (case i 0 x 1 y
(throw (IndexOutOfBoundsException.))))
(nth [_ i default]
Fatal error: exception Vendor.Helpers.InvalidReference("unknown qualifier `(-'")
Raised at file "vendor/odoc_parser/helpers.ml", line 28, characters 14-72
Called from file "vendor/odoc_parser/helpers.ml", line 258, characters 28-47
Called from file "vendor/odoc_parser/syntax.ml", line 138, characters 47-71
Called from file "vendor/odoc_parser/syntax.ml", line 340, characters 20-66
Called from file "vendor/odoc_parser/syntax.ml", line 358, characters 16-34
Called from file "vendor/odoc_parser/syntax.ml", line 365, characters 17-54
Called from file "vendor/odoc_parser/syntax.ml", line 727, characters 18-33
Called from file "vendor/odoc_parser/syntax.ml", line 1103, characters 6-71
Called from file "vendor/odoc_parser/error.ml", line 62, characters 9-15
type k = [ `A of int | `B of string | `Ks of (string fn * k) list ]
and 'a fn = 'a -> k -> bool
(* Error: In the definition of k, type string fn should be 'a fn *)
module Foo : sig
type t = private [ `A | `B ]
end = struct
type t = [ `A ]
end