flan/test/programs/match-bool.flan

59 lines
1.5 KiB
Plaintext

;;;; = and != on bools, a match over a bool, and keyword arms over a dyn.
(defstruct Flag [on bool])
(defn truth [n i32] bool (> n 0))
(defn same? [a bool b bool] bool (= a b))
;; Exhaustive without a _ arm: true and false are every bool.
(defn word [b bool] string
(match b
true "yes"
false "no"))
(defn flipped [b bool] i32
(match b
false 0
true 1))
(defn only-true [b bool] i32
(match b
true 1
_ 0))
;; Over a dyn :north is the arm (= d :north), beside numbers, strings and
;; bools, which are dyn values too.
(defn heading [d dyn] string
(match d
:north "up"
:south "down"
1 "one"
"west" "left"
true "true"
_ "other"))
(defn main [] i32
(let [f (Flag {.on true})
g (Flag {.on false})
flags [true false true]]
(print (same? true true)) (print " ")
(print (same? true false)) (print " ")
(print (!= true false)) (print " ")
(print (= (.on f) (truth 3))) (print " ")
(print (= (.on g) (truth 3))) (print " ")
(print (!= (.on g) (at flags 1))) (print " ")
(print (= true (at flags 0) (at flags 2))) (println "")
(println (word true))
(println (word (truth -1)))
(print (flipped true)) (print (flipped false))
(print (only-true true)) (print (only-true false)) (println "")
(println (heading :north))
(println (heading :south))
(println (heading :east))
(println (heading 1.0))
(println (heading "west"))
(println (heading true))
(println (heading false))
0))