clojure
Mönster Matchning med core.match
Sök…
Anmärkningar
core.match
biblioteket implementerar en mönster matchning algoritm som använder begreppet "nödvändighet" från lata mönster matchning.
Matchande bokstäver
(let [x true
y true
z true]
(match [x y z]
[_ false true] 1
[false true _ ] 2
[_ _ false] 3
[_ _ true] 4))
;=> 4
Matchar en vektor
(let [v [1 2 3]]
(match [v]
[[1 1 1]] :a0
[[1 _ 1]] :a1
[[1 2 _]] :a2)) ;; _ is used for wildcard matching
;=> :a2
Matcha en karta
(let [x {:a 1 :b 1}]
(match [x]
[{:a _ :b 2}] :a0
[{:a 1 :b _}] :a1
[{:x 3 :y _ :z 4}] :a2))
;=> :a1
Matchar en bokstavlig symbol
(match [['asymbol]]
[['asymbol]] :success)
;=> :success
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow