Skip to content

Instantly share code, notes, and snippets.

View c-spencer's full-sized avatar

Chris Spencer c-spencer

View GitHub Profile
(ns typingfun.core
(:require [clojure.core.typed :as t]))
(t/ann-protocol AddProtoc
adder [AddProtoc Number -> AddProtoc])
(t/defprotocol> AddProtoc
(adder [this amount]))
(t/ann-datatype Accumulator [t :- Number])
(defn Buffer [string]
(set! this.string string)
(set! this.pos 0)
undefined)
(defn Buffer.prototype.read1 []
(cond
(= this.pos this.string.length)
(do
(set! this.pos (+ this.pos 1))
@c-spencer
c-spencer / gist:6528482
Last active December 22, 2015 20:48
core.typed messings about
; trying to work around no update-in type
(defmacro typed-update-in [m [v] f]
`((ann-form update-in
(~'All [~'x ~'y]
[ (~'HMap :mandatory {~v ~'x}) (~'Vector* Keyword) [~'x ~'-> ~'y]
~'->
(~'HMap :mandatory {~v ~'y}) ]))
~m [~v] ~f))
; still causes an error because of the inner un-typed update-in
@c-spencer
c-spencer / datomic-annotations.clj
Last active December 24, 2016 21:45
A first pas at Datomic Types
(ns typed-datomic.types
(:use [clojure.core.typed])
(:import [clojure.lang Keyword Symbol]
[java.util Date]))
;; Type aliases
; Marker protocol to distinguish historical databases
(ann-protocol HistoryDB)
(defprotocol> HistoryDB)
@c-spencer
c-spencer / core.clj
Last active December 23, 2015 02:49
Simple demo of type construction from Datomic with core.typed and a little glue
(defn create-type
"Extract a type from provided field idents stored in Datomic database at uri."
[uri type-name overrides]
(let [c (d/connect uri)
d (d/db c)
datomic-type-map {:db.type/string 'String
:db.type/ref 'Any}
mt (dt/q> :- [EntityID]
'[:find ?e
:in $ ?t-name
(cf (fn [m] (conj [0] (if m 1 2))))
; [(Fn [Any -> [(Value 0) (U (Value 1) (Value 2))]]) {:then tt, :else ff}]
(cf (fn [{:keys [a] :as m} c]
(merge m {:b (+ a c) :c c}))
['{:a Number} Number -> '{:a Number :b Number :c Number}])
; (Fn [(HMap :mandatory {:a Number}) Number -> (HMap :mandatory {:a Number, :b Number, :c Number})])
(cf (fn [m] (dissoc {:a 1 :b 2} (if m :a :b))))
; [(Fn [Any -> (U (HMap :mandatory {:a (Value 1)} :complete? true)
@c-spencer
c-spencer / sysdea.clj
Last active December 24, 2015 01:59
Simple experiment in ClojureScript approach to Sysdea documents with undo/redo
; adapted from clojure.core.incubator
(defn dissoc-in
"Dissociates a number of entries from the object at the given path,
removing empty maps on the path.
e.g. (dissoc-in {:a {:b {:e 6} :c 6}} [:a :b] [:e]) -> {:a {:c 6}}"
[m [k & ks :as keys] rems]
(if k
(if-let [nextmap (get m k)]
(let [newmap (dissoc-in nextmap ks rems)]
(if (seq newmap)
@c-spencer
c-spencer / message_format.ex
Last active August 29, 2015 14:10
Simple and incomplete MessageFormat compiler for Elixir
defmodule Plural do
require Record
Record.defrecordp :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecordp :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
defmacro __using__(_env) do
quote do
@before_compile Plural
Module.register_attribute(__MODULE__, :plurals, accumulate: true)
LANG=en
This is a comment that precedes the strings.
@test message
Hello from the translator.
-- and this is a comment inbetween strings
@test message 2
# current
("You came in " <> case(plural("en", :ordinal, args[:place])) do
"few" ->
to_string(args[:place]) <> "rd"
"one" ->
to_string(args[:place]) <> "st"
"other" ->
to_string(args[:place]) <> "th"
"two" ->
to_string(args[:place]) <> "nd"