Skip to content

Instantly share code, notes, and snippets.

@bendisposto
Created February 18, 2015 15:28
Show Gist options
  • Save bendisposto/aadd0f87f61a45ecbb75 to your computer and use it in GitHub Desktop.
Save bendisposto/aadd0f87f61a45ecbb75 to your computer and use it in GitHub Desktop.
vl_5_2_2015 Teil3: Contracts/Schema
(ns dbc.core
(:require [schema.core :as s]))
(comment
(cons 3 [4 5])
(conj 3 [4 5])
;; ClassCastException ?!?
(defn conj' [coll ele]
(assert (instance? Iterable coll) "First argument must be a collection")
(conj coll ele))
(conj' 3 [4 5])
(defn conj' [coll ele]
{:pre [(instance? Iterable coll)]}
(conj coll ele))
(conj' 3 [4 5])
(defn interval-sqrt
([n] (interval-sqrt n 0 n 1e-5))
([n a b eps]
(let [m (/ (+ a b) 2)]
(cond (< (- b a) eps) a
(> (* m m) n) (recur n a m eps)
:else (recur n m b eps)))))
(interval-sqrt 4)
(interval-sqrt 0)
(interval-sqrt -4)
(defn interval-sqrt
([n]
{:pre [(>= n 0)]
:post [(not (neg? (- n (* % %)))) (< (- n (* % %)) 1e5)]} (interval-sqrt n 0 n 1e-5))
([n a b eps]
(let [m (double (/ (+ a b) 2))]
(cond (< (- b a) eps) a
(> (* m m) n) (recur n a m eps)
:else (recur n m b eps)))))
(interval-sqrt 2)
(interval-sqrt 4)
(defn foo [x])
;; Eingabe soll eine Map sein, die Keywords auf Vektoren von Maps
;; abbildet. Diese Maps bilden Zahlen auf Strings oder Booleans ab.
;; Und nun als Precondition schreiben!
;; Prismatic Schema
(s/validate s/Int 4)
(s/validate s/Str 4)
(s/validate s/Str "5")
(s/validate [s/Int] [2 3 4])
(s/validate [s/Int] [2 3 "s"])
(s/validate {s/Keyword
[{s/Int (s/either s/Str s/Bool)}]}
{:foo [{1 true} {2 "2" 3 false}]})
(s/validate {s/Keyword [{s/Int (s/either s/Str s/Bool)}]} {:foo [{1 true} {2 "2" :a false}]})
(def PersonS {:name s/Str :alter s/Int})
(s/validate PersonS {:name "Jens"})
(s/validate PersonS {:name "Jens" :alter 29})
(s/validate PersonS {:name "Jens" :alter 29 :luege true})
(def PersonS {:name s/Str, :alter s/Int, s/Keyword s/Any})
(s/validate PersonS {:name "Jens" :alter 29 :luege true :tralala [1 2 3]})
(def Foo {:person PersonS})
(s/defn printperson :- s/Bool
[p :- PersonS]
(println (str (:name p) ", " (:alter p)))
:ok)
(printperson {:name "Jens" :alter 29})
(s/with-fn-validation
(printperson {:name "Jens" :alter 29}))
;; Komplizierte Strukturen
;; Eine gerade ganze Zahl
;; Prädikate & logisches und
(def EvenInt (s/both s/Int (s/pred even? :gerade)))
(s/validate EvenInt 2)
(s/validate EvenInt 3)
(s/validate EvenInt 2.0)
;; Composition!
;; Schemas sind nur Daten
(def EVecS [EvenInt])
(s/validate EVecS [2 4 5 6])
(s/validate EVecS [2 4 6])
;; Das war nur eine Übersicht! Es gibt noch eine ganze Menge mehr
;; Typen und Funktionen. Einfach einmal reinschauen!
;; https://github.com/Prismatic/schema
)
(defproject dbc "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[prismatic/schema "0.3.4"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment