typed clojure in practice
play

Typed Clojure in Practice Ambrose Bonnaire-Sergeant Indiana - PowerPoint PPT Presentation

Typed Clojure in Practice Ambrose Bonnaire-Sergeant Indiana University @ambrosebs Lets write some Clojure code ... a hashing function Heres how you call it (summarise [1 2 3]) ;=> <Int> (summarise nil) ;=> <Int>


  1. Need a better test (defn summarise ([nseq] (summarise nseq 0)) ([nseq acc] (if nseq (* (summarise (next nseq) (inc acc)) (first nseq)) 42))) => (t/check-ns) Type mismatch: Expected: Number Actual: (U nil Int) in: (first nseq)

  2. Need a better test (defn summarise ([nseq] (summarise nseq 0)) [ ] [ ] ([nseq acc] (if nseq [ ] (* (summarise (next nseq) (inc acc)) [ ] (first nseq)) 42))) => (t/check-ns) Type mismatch: Expected: Number Actual: (U nil Int) in: (first nseq)

  3. (seq [1 2]) (seq nil) ;=> (1 2) ;=> nil (seq []) ;=> nil

  4. Update implementation (ann summarise (IFn [NInts -> Int] [NInts Int -> Int])) (defn summarise ([nseq] (summarise nseq 0)) ([nseq acc] (if nseq (* (summarise (next nseq) (inc acc)) (first nseq)) 42)))

  5. Update implementation (ann summarise (IFn [NInts -> Int] [NInts Int -> Int])) (defn summarise ([nseq] (summarise nseq 0)) ([nseq acc] (if (seq nseq) (* (summarise (next nseq) (inc acc)) (first nseq)) 42))) => (t/check-ns) : ok

  6. => (summarise [42 33 32]) ;=> 1280664 => (summarise nil) ;=> 42 => (summarise []) ;=> 42

  7. Typed Clojure is an Optional Type System that catches type errors in real Clojure code

  8. Where have we come from?

  9. 2011

  10. 2012

  11. 2012

  12. Sam Ambrose Tobin-Hochstadt Bonnaire-Sergeant 2014

  13. Where are we now?

  14. Colin Fleming

  15. } Function application error

  16. Francesco Bellomi CrossClj.info

  17. CrossClj.info

  18. TypedClojure.vim

  19. Minori Di Xu Yamashita

  20. Typed Clojure 101

  21. Occurrence Typing

  22. (ns gen-vec) (defn gen-vec [n-or-v] (if (number? n-or-v) (vec (range n-or-v)) n-or-v))

  23. (ns gen-vec) (defn gen-vec [n-or-v] (if (number? n-or-v) (vec (range n-or-v)) n-or-v)) (gen-vec 5) ;=> [0 1 2 3 4] (gen-vec [1 2 3 4]) ;=> [1 2 3 4]

  24. (gen-vec )

  25. 5 (gen-vec ) 5 (if (number? ) true 5 (vec (range )) 5 )) 5 (vec (range )) [0 1 2 3 4]

  26. (gen-vec )

  27. [1 2 3] (gen-vec ) [1 2 3] false (if (number? ) [1 2 3] (vec (range )) [1 2 3] )) [1 2 3]

  28. (ns gen-vec (:require [clojure.core.typed :refer [U Num Vec Int ann] :as t])) (ann gen-vec [(U Num (Vec Int)) -> (Vec Int)]) (defn gen-vec [n-or-v] (if (number? n-or-v) (vec (range n-or-v)) n-or-v))

  29. (ns gen-vec (:require [clojure.core.typed :refer [U Num Vec Int ann] :as t])) (ann gen-vec [(U Num (Vec Int)) -> (Vec Int)]) (defn gen-vec [n-or-v] (if (number? n-or-v) (vec (range n-or-v)) n-or-v)) (ann clojure.core/number? (Pred Num)) => (t/check-ns) : ok

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend