Skip to content

Instantly share code, notes, and snippets.

@luxbock
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luxbock/9351ceaf9f4512e6885b to your computer and use it in GitHub Desktop.
Save luxbock/9351ceaf9f4512e6885b to your computer and use it in GitHub Desktop.
(ns rdp-214.core
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.core.typed :as t
:refer [ann U Seq Str Vec Sequential]]))
;;;; Boilerplate ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(t/tc-ignore
(defn- annotate
[[type expr]]
(if (symbol? expr)
[(list 'clojure.core.typed/ann expr type) nil]
(let [_ (assert (sequential? expr) "T received a bad type annotation.")
[ann-type name] expr]
(case ann-type
defn [(list 'clojure.core.typed/ann name type) expr]
defprotocol [(list 'clojure.core.typed/ann-protocol name type) expr]
defrecord [(list 'clojure.core.typed/ann-record name type) expr]
[(list 'clojure.core.typed/ann-form expr type) nil])))))
(defmacro T [& forms]
`(do ~@(keep identity (mapcat annotate (partition 2 forms)))))
(T
[java.io.BufferedReader -> (Seq Str)]
clojure.core/line-seq
[(U Str java.net.URL) -> java.io.File]
clojure.java.io/file
[Str -> java.net.URL]
clojure.java.io/resource
[java.io.File -> java.io.BufferedReader]
clojure.java.io/reader
[color :- Long, x :- Long, y :- Long, width :- Long, height :- Long]
(defrecord Paper
[^long color
^long x
^long y
^long width
^long height])
[Str -> (Seq Str)]
(defn resource-as-lines
[f-name]
(-> f-name io/resource io/file io/reader line-seq))
[Str -> (Seq (Seq Paper))]
(defn prepare
[f-name]
;; doesn't actually work because of apply
(map (comp (T [(Seq (Seq Long)) -> (Seq Paper)]
(fn [ls]
(assert (= (count ls) 5))
(apply ->Paper ls) )
[Str -> (Seq (Seq Long))]
(fn [s]
(map (T [Str -> Long] #(Long/parseLong %))
(str/split s #" ")))))
(resource-as-lines f-name)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment