aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/any.gleam120
-rw-r--r--src/result.gleam12
2 files changed, 63 insertions, 69 deletions
diff --git a/src/any.gleam b/src/any.gleam
index 41abed3..22dd0d5 100644
--- a/src/any.gleam
+++ b/src/any.gleam
@@ -1,4 +1,5 @@
import list
+import atom
import result
import expect
@@ -230,74 +231,67 @@ pub external fn tuple(Any) -> Result({Any, Any}, String)
test tuple {
{1, []}
- |> from
- |> tuple
- |> expect:equal(_, Ok({from(1), from([])}))
+ |> from
+ |> tuple
+ |> expect:equal(_, Ok({from(1), from([])}))
{"ok", "ok"}
- |> from
- |> tuple
- |> expect:equal(_, Ok({from("ok"), from("ok")}))
+ |> from
+ |> tuple
+ |> expect:equal(_, Ok({from("ok"), from("ok")}))
{1}
- |> from
- |> tuple
- |> expect:is_error
+ |> from
+ |> tuple
+ |> expect:is_error
{1, 2, 3}
- |> from
- |> tuple
- |> expect:is_error
-
-// {1, 2.0}
-// |> from
-// |> tuple
-// |> result:map(_, fn(x) {
-// let {a, b} = x
-// a |> int |> result:map(_, fn(x) { {x, b} })
-// })
-// // |> result:then(_, fn(x) {
-// // let {a, b} = x
-// // b |> float |> result:map(_, fn(x) { {a, x} })
-// // })
-// // |> expect:equal(_, Ok({1, 2.0}))
+ |> from
+ |> tuple
+ |> expect:is_error
+
+ {1, 2.0}
+ |> from
+ |> tuple
+ |> result:then(_, fn(x) {
+ let {a, b} = x
+ a |> int |> result:map(_, fn(i) { {i, b} })
+ })
+ |> result:then(_, fn(x) {
+ let {a, b} = x
+ b |> float |> result:map(_, fn(f) { {a, f} })
+ })
+ |> expect:equal(_, Ok({1, 2.0}))
}
-////// TODO: FIXME: This doesn't work anymore because atoms are no longer a thing.
-////// "Decode a field from a map, extracting a specified field.
-//////
-////// Multiple fields can be extracted and stored in a new record like so:
-//////
-////// Ok(name) <- decode:field(raw_data, \"name\") |> result:then(_, decode:string)
-////// Ok(size) <- decode:field(raw_data, \"size\") |> result:then(_, decode:int)
-////// Ok({ name = name, size = size })
-//////
-////pub external fn field(Any, a) -> Result(String, Any)
-//// = "gleam__stdlib" "field"
-
-////test field {
-//// let _ = {ok = 1}
-//// |> from
-//// |> field("ok")
-//// |> expect:equal(from(1))
-
-//// let _ = {earlier = 2, ok = 3}
-//// |> from
-//// |> field("ok")
-//// |> expect:equal(from(3))
-
-//// let _ = {}
-//// |> from
-//// |> field("ok")
-//// |> expect:is_error
-
-//// let _ = 1
-//// |> from
-//// |> field("ok")
-//// |> expect:is_error
-
-//// []
-//// |> from
-//// |> field("ok")
-//// |> expect:is_error
-////}
+pub external fn field(Any, a) -> Result(Any, String)
+ = "gleam__stdlib" "decode_field"
+
+test field {
+ let Ok(ok_atom) = atom:from_string("ok")
+
+ {ok = 1}
+ |> from
+ |> field(_, ok_atom)
+ |> expect:equal(_, Ok(from(1)))
+
+ {earlier = 2, ok = 3}
+ |> from
+ |> field(_, ok_atom)
+ |> expect:equal(_, Ok(from(3)))
+
+ {}
+ |> from
+ |> field(_, ok_atom)
+ |> expect:is_error
+
+ 1
+ |> from
+ |> field(_, ok_atom)
+ |> expect:is_error
+
+ []
+ |> from
+ |> field(_, [])
+ |> expect:is_error
+}
diff --git a/src/result.gleam b/src/result.gleam
index 18503ec..a2bd330 100644
--- a/src/result.gleam
+++ b/src/result.gleam
@@ -91,12 +91,8 @@ test flatten {
pub fn then(result, fun) {
case result {
- | Ok(x) ->
- case fun(x) {
- | Ok(y) -> Ok(y)
- | Error(y) -> Error(y)
- }
- | Error(_) -> result
+ | Ok(x) -> fun(x)
+ | Error(e) -> Error(e)
}
}
@@ -110,6 +106,10 @@ test then {
|> expect:equal(_, Ok(2))
Ok(1)
+ |> then(_, fn(_) { Ok("type change") })
+ |> expect:equal(_, Ok("type change"))
+
+ Ok(1)
|> then(_, fn(_) { Error(1) })
|> expect:equal(_, Error(1))
}