-
-
Save lilactown/e924ec5dfe801a596ffd02477ddfd55f to your computer and use it in GitHub Desktop.
cljs-ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns demo | |
(:require | |
[town.lilac.cljs-ts :refer [deft]])) | |
;; | |
;; primitive types | |
;; | |
(defn add ^number [^number n ^number m] | |
(+ n m)) | |
;; | |
;; Collections | |
;; | |
(defn sum ^number [^(seqable number) xs] | |
,,,) | |
(defn sum ^number [^(seq number) xs] | |
,,,) | |
(defn last ^number [^(vec number) xs] | |
,,,) | |
(defn last ^number [^(list number) xs] | |
,,,) | |
(defn pick ^number [^(set number) xs] | |
,,,) | |
(defn get ^number [^(associative keyword number) m ^keyword k] | |
,,,) | |
;; | |
;; Arities | |
;; | |
(defn add | |
(^number [^number n] n) | |
(^number [^number n ^number m] (+ n m)) | |
(^number [^number n ^number m ^number o] (+ n m o))) | |
(defn foo | |
(^number [^number n] n) | |
(^string [^number n ^number m] (str n m)) | |
(^(vector number) [^number n ^number m ^number o] (vector n m o))) | |
;; | |
;; Anonymous functions | |
;; | |
(defn callback ^number [^(fn ^number [^number n]) f] | |
(f 1)) | |
;; | |
;; named type | |
;; | |
(deft input number) | |
(deft output number) | |
(defn add ^output [^input n ^input m] | |
(+ n m)) | |
;; | |
;; structural typing | |
;; | |
;; metadata on binding | |
(defn o [^(keys {:x string}) opts]) | |
;; destructuring | |
(defn o [{:keys [^string x]}]) | |
;; | |
;; Unions | |
;; | |
(defn o [^(or string (vec string) (fn ^string []) {:keys [^string s]}) x] | |
,,,) | |
;; | |
;; intersections | |
;; | |
(deft combined | |
(and (keys {:a string}) | |
(keys {:b number}))) | |
(deft conflicting | |
(and (keys {:a string}) | |
(keys {:a number}))) | |
;; | |
;; Generics | |
;; | |
(defn ^(gen T) last ^T [^(seq T) xs] | |
,,,) | |
(defn ^(gen T U) map ^(seq T) [^(fn ^T [^U x]) f ^(seqable U) xs] | |
,,,) | |
;; fully typed version of map | |
(deft reducer [Result Input] | |
(fn | |
(^Result []) | |
(^Result [^Result result]) | |
(^Result [^Result result ^Input input]))) | |
(deft transducer [T U] | |
(fn ^(gen V) xf ^(reducer T V) [^(reducer U V) rf])) | |
(defn ^(gen T U) map | |
(^(transducer T U) [^(fn ^T [^U x]) f] | |
(fn ^(gen V) xf [^(reducer V T) rf] | |
(fn | |
(^V [] (rf)) | |
(^V [result] (rf result)) | |
(^V [result input] (rf result (f input)))))) | |
(^(seq T) [^(fn ^T [^U x]) f ^(seqable U) xs] | |
(for [x xs] (f x))) | |
;; TODO variadic generics | |
(^(seq T) [^(fn ^T [^U x ^(seq U) & y]) f] | |
,,,)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bodacious