mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 13:37:57 +03:00
core: add result draft
This commit is contained in:
parent
4a84e36924
commit
ae62154afc
@ -1,6 +1,7 @@
|
||||
(load "Interfaces.carp")
|
||||
(load "Macros.carp")
|
||||
(load "Maybe.carp")
|
||||
(load "Result.carp")
|
||||
(load "Dynamic.carp")
|
||||
(load "Format.carp")
|
||||
(load "Int.carp")
|
||||
|
89
core/Result.carp
Normal file
89
core/Result.carp
Normal file
@ -0,0 +1,89 @@
|
||||
(deftype (Result a b)
|
||||
(Success [a])
|
||||
(Error [b]))
|
||||
|
||||
(defmodule Result
|
||||
(defn apply [a success-f error-f]
|
||||
(match a
|
||||
(Success x) (success-f x)
|
||||
(Error x) (error-f x)))
|
||||
|
||||
(defn map [a f]
|
||||
(match a
|
||||
(Success x) (Success (f x))
|
||||
(Error x) (Error x)))
|
||||
|
||||
(defn and-then [a f]
|
||||
(match a
|
||||
(Success x) (f x)
|
||||
(Error x) (Error x)))
|
||||
|
||||
(defn unwrap-or-zero [a]
|
||||
(match a
|
||||
(Success x) x
|
||||
(Error _) (zero)))
|
||||
|
||||
;(defn and [a b]
|
||||
; (match a
|
||||
; (Success _) b
|
||||
; (Error x) (Error x)))
|
||||
|
||||
;(defn or [a b]
|
||||
; (match a
|
||||
; (Success x) (Success x)
|
||||
; (Error _) b))
|
||||
|
||||
(defn or-else [a f]
|
||||
(match a
|
||||
(Success x) (Success x)
|
||||
(Error x) (f x)))
|
||||
|
||||
(defn unwrap-or-else [a f]
|
||||
(match a
|
||||
(Success x) x
|
||||
(Error x) (f x)))
|
||||
|
||||
(defn unsafe-from-success [a]
|
||||
(match a
|
||||
(Success x) x))
|
||||
|
||||
(defn from-success [a dflt]
|
||||
(match a
|
||||
(Error _) dflt
|
||||
(Success x) x))
|
||||
|
||||
(defn unsafe-from-error [a]
|
||||
(match a
|
||||
(Error x) x))
|
||||
|
||||
(defn from-error [a dflt]
|
||||
(match a
|
||||
(Success _) dflt
|
||||
(Error x) x))
|
||||
|
||||
(defn to-maybe [a]
|
||||
(match a
|
||||
(Success x) (Maybe.Just x)
|
||||
(Error _) (Maybe.Nothing)))
|
||||
|
||||
(defn success? [a]
|
||||
(match @a
|
||||
(Error _) false
|
||||
(Success _) true))
|
||||
|
||||
(defn error? [a]
|
||||
(match @a
|
||||
(Error _) true
|
||||
(Success _) false))
|
||||
|
||||
(defn = [a b]
|
||||
(match @a
|
||||
(Success x)
|
||||
(match @b
|
||||
(Error _) false
|
||||
(Success y) (= x y))
|
||||
(Error x)
|
||||
(match @b
|
||||
(Success _) false
|
||||
(Error y) (= x y))))
|
||||
)
|
110
test/result.carp
Normal file
110
test/result.carp
Normal file
@ -0,0 +1,110 @@
|
||||
(load "Test.carp")
|
||||
|
||||
(use-all Test Result)
|
||||
|
||||
(deftest test
|
||||
(assert-equal test
|
||||
1
|
||||
(apply (Success 0) Int.inc Int.dec)
|
||||
"apply works with Success"
|
||||
)
|
||||
(assert-equal test
|
||||
-1
|
||||
(apply (Error 0) Int.inc Int.dec)
|
||||
"apply works with Error"
|
||||
)
|
||||
(assert-true test
|
||||
(error? &(map (Error "hi") Int.inc))
|
||||
"map works with Error"
|
||||
)
|
||||
(assert-equal test
|
||||
&(Success 2)
|
||||
&(map (the (Result Int String) (Success 1)) Int.inc)
|
||||
"map works with Success"
|
||||
)
|
||||
(assert-true test
|
||||
(error? &(and-then (Error "hi") (fn [x] (Success (Int.inc x)))))
|
||||
"and-then works with Error"
|
||||
)
|
||||
(assert-equal test
|
||||
&(Success 2)
|
||||
&(and-then (the (Result Int String) (Success 1))
|
||||
(fn [x] (Success (Int.inc x))))
|
||||
"and-then works with Success"
|
||||
)
|
||||
)
|
||||
|
||||
; (defn and-then [a f]
|
||||
; (match a
|
||||
; (Success x) (f x)
|
||||
; (Error x) (Error x)))
|
||||
;
|
||||
; (defn unwrap-or-zero [a]
|
||||
; (match a
|
||||
; (Success x) x
|
||||
; (Error _) (zero)))
|
||||
;
|
||||
; (defn and [a b]
|
||||
; (match a
|
||||
; (Success _) b
|
||||
; (Error x) (Error x)))
|
||||
;
|
||||
; (defn or [a b]
|
||||
; (match a
|
||||
; (Success x) (Success x)
|
||||
; (Error _) b))
|
||||
;
|
||||
; (defn or-else [a f]
|
||||
; (match a
|
||||
; (Success x) (Success x)
|
||||
; (Error x) (f x)))
|
||||
;
|
||||
; (defn unwrap-or-else [a f]
|
||||
; (match a
|
||||
; (Success x) x
|
||||
; (Error x) (f x)))
|
||||
;
|
||||
; (defn unsafe-from-success [a]
|
||||
; (match a
|
||||
; (Success x) x))
|
||||
;
|
||||
; (defn from-success [a dflt]
|
||||
; (match a
|
||||
; (Error _) dflt
|
||||
; (Success x) x))
|
||||
;
|
||||
; (defn unsafe-from-error [a]
|
||||
; (match a
|
||||
; (Error x) x))
|
||||
;
|
||||
; (defn from-error [a dflt]
|
||||
; (match a
|
||||
; (Success _) dflt
|
||||
; (Error x) x))
|
||||
;
|
||||
; (defn to-maybe [a]
|
||||
; (match a
|
||||
; (Success x) (Maybe.Just x)
|
||||
; (Error _) (Maybe.Nothing)))
|
||||
;
|
||||
; (defn success? [a]
|
||||
; (match @a
|
||||
; (Error _) false
|
||||
; (Success _) true))
|
||||
;
|
||||
; (defn error? [a]
|
||||
; (match @a
|
||||
; (Error _) true
|
||||
; (Success _) false))
|
||||
;
|
||||
; (defn = [a b]
|
||||
; (match @a
|
||||
; (Success x)
|
||||
; (match @b
|
||||
; (Error _) false
|
||||
; (Success y) (= x y))
|
||||
; (Error x)
|
||||
; (match @b
|
||||
; (Success _) false
|
||||
; (Error y) (= x y))))
|
||||
;)
|
Loading…
Reference in New Issue
Block a user