Skip to content

Instantly share code, notes, and snippets.

@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
(ns com.snowtide.clojure.memoize)
(defn- mutable-memoize
[f #^java.util.Map map]
(fn [& args]
(if-let [e (find map args)]
(val e)
(let [ret (apply f args)]
(.put map args ret)
ret))))
type a = [ `A of int ]
type b = [ a | `B of int ]
let pair (a : a) (b : b) = ([a] :> b list) @ [b] (* works fine *)
module type Config = sig type t end
module type Box = sig
module C : Config
type t = { regions : C.t list }
@cemerick
cemerick / poly.ml
Last active February 20, 2019 20:11
(****** works ******)
type foo = [`A of int | `B of int]
module type K = sig
type t
val value: t -> int
end
module J (K: K) = struct
let value = K.value
end
module FooJ = J(struct