diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-18 01:12:09 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-18 01:12:09 +0000 |
commit | 2cbe6bd721ae2be68c29e4de71db51bafc3478f8 (patch) | |
tree | 8bc1a3646d662fe047f45af4ba6a4c55e2ab6f09 /src | |
parent | 336614d8fc815d5eec7461ed271ae45697d9216c (diff) | |
download | gleam_stdlib-2cbe6bd721ae2be68c29e4de71db51bafc3478f8.tar.gz gleam_stdlib-2cbe6bd721ae2be68c29e4de71db51bafc3478f8.zip |
tuple module, decode lists
Diffstat (limited to 'src')
-rw-r--r-- | src/any.gleam | 201 | ||||
-rw-r--r-- | src/gleam__stdlib.erl | 5 | ||||
-rw-r--r-- | src/tuple.gleam | 23 |
3 files changed, 135 insertions, 94 deletions
diff --git a/src/any.gleam b/src/any.gleam index 5deaeb8..262d39c 100644 --- a/src/any.gleam +++ b/src/any.gleam @@ -1,8 +1,17 @@ import list import atom +import tuple import result import expect +fn list_module() { + list +} + +fn tuple_module() { + tuple +} + // `Any` data is data that we don"t know the type of yet. // We likely get data like this from interop with Erlang, or from // IO with the outside world. @@ -95,32 +104,31 @@ test float { |> expect:equal(_, Error("Expected a Float, got `[]`")) } +// TODO // pub external fn atom(Any) -> Result(Atom, String) // = "gleam__stdlib" "decode_atom" -//// test atom { -//// // TODO -//// // make an atom here -//// // |> from -//// // |> atom -//// // |> expect:equal(_, Ok("")) - -//// // TODO -//// // make an atom here -//// // |> from -//// // |> atom -//// // |> expect:equal(_, Ok("ok")) - -//// let _ = 1 -//// |> from -//// |> atom -//// |> expect:is_error - -//// [] -//// |> from -//// |> atom -//// |> expect:is_error -//// } +// test atom { +// make an atom here +// |> from +// |> atom +// |> expect:equal(_, Ok("")) + +// make an atom here +// |> from +// |> atom +// |> expect:equal(_, Ok("ok")) + +// 1 +// |> from +// |> atom +// |> expect:is_error + +// [] +// |> from +// |> atom +// |> expect:is_error +// } pub external fn bool(Any) -> Result(Bool, String) = "gleam__stdlib" "decode_bool" @@ -148,83 +156,86 @@ test bool { } pub external fn thunk(Any) -> Result(fn() -> Any, String) - = "gleam__stdlib" "thunk" + = "gleam__stdlib" "decode_thunk" -//// test thunk { -//// let _ = fn() { 1 } -//// |> from -//// |> thunk -//// |> expect:is_ok - -//// let _ = fn(x) { x } -//// |> from -//// |> thunk -//// |> expect:is_error +test thunk { + fn() { 1 } + |> from + |> thunk + |> expect:is_ok -//// let _ = 1 -//// |> from -//// |> thunk -//// |> expect:is_error + fn() { 1 } + |> from + |> thunk + |> result:map(_, fn(f) { f() }) + |> expect:equal(_, Ok(from(1))) -//// [] -//// |> from -//// |> thunk -//// |> expect:is_error -//// } + fn(x) { x } + |> from + |> thunk + |> expect:is_error -external fn list_any(Any) -> Result(List(Any), String) = "gleam__stdlib" "decode_list" + 1 + |> from + |> thunk + |> expect:is_error -fn list_module() { - list + [] + |> from + |> thunk + |> expect:is_error } +external fn list_any(Any) -> Result(List(Any), String) = + "gleam__stdlib" "decode_list" + pub fn list(any, decode) { any - |> list_any - |> result:then(_, fn(x) { list_module():traverse(x, decode) }) + |> list_any + |> result:then(_, list_module():traverse(_, decode)) } -//// test list { -//// let _ = [] -//// |> from -//// |> list(string) -//// |> expect:equal(_, Ok([])) - -//// let _ = [] -//// |> from -//// |> list(atom) -//// |> expect:equal(_, Ok([])) - -//// let _ = [1, 2, 3] -//// |> from -//// |> list(int) -//// |> expect:equal(_, Ok([1, 2, 3])) - -//// let _ = [[1], [2], [3]] -//// |> from -//// |> list(list(int)) -//// |> expect:equal(_, Ok([1, 2, 3])) - -//// let _ = 1 -//// |> from -//// |> list(string) -//// |> expect:is_error - -//// let _ = 1.0 -//// |> from() -//// |> list(int) -//// |> expect:is_error - -//// let _ = [""] -//// |> from() -//// |> list(int) -//// |> expect:is_error - -//// [from(1), any:from("not an int")] -//// |> from -//// |> list(int) -//// |> expect:is_error -//// } +test list { + [] + |> from + |> list(_, string) + |> expect:equal(_, Ok([])) + + [] + |> from + |> list(_, int) + |> expect:equal(_, Ok([])) + + [1, 2, 3] + |> from + |> list(_, int) + |> expect:equal(_, Ok([1, 2, 3])) + + [[1], [2], [3]] + |> from + |> list(_, list(_, int)) + |> expect:equal(_, Ok([[1], [2], [3]])) + + 1 + |> from + |> list(_, string) + |> expect:is_error + + 1.0 + |> from + |> list(_, int) + |> expect:is_error + + [""] + |> from + |> list(_, int) + |> expect:is_error + + [from(1), from("not an int")] + |> from + |> list(_, int) + |> expect:is_error +} pub external fn tuple(Any) -> Result({Any, Any}, String) = "gleam__stdlib" "decode_tuple" @@ -254,12 +265,16 @@ test tuple { |> from |> tuple |> result:then(_, fn(x) { - let {a, b} = x - a |> int |> result:map(_, fn(i) { {i, b} }) + x + |> tuple_module():first + |> int + |> result:map(_, fn(f) { {f, tuple_module():second(x)} }) }) |> result:then(_, fn(x) { - let {a, b} = x - b |> float |> result:map(_, fn(f) { {a, f} }) + x + |> tuple_module():second + |> float + |> result:map(_, fn(f) { {tuple_module():first(x), f} }) }) |> expect:equal(_, Ok({1, 2.0})) } diff --git a/src/gleam__stdlib.erl b/src/gleam__stdlib.erl index f377e3f..3ab9fd6 100644 --- a/src/gleam__stdlib.erl +++ b/src/gleam__stdlib.erl @@ -6,7 +6,7 @@ atom_create_from_string/1, atom_to_string/1, map_fetch/2, iodata_append/2, iodata_prepend/2, identity/1, decode_int/1, decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, - decode_tuple/1, decode_field/2]). + decode_tuple/1, decode_list/1, decode_field/2]). expect_equal(A, Expected) -> ?assertEqual(Expected, A). expect_not_equal(A, Expected) -> ?assertNotEqual(Expected, A). @@ -58,6 +58,9 @@ decode_thunk(Data) -> decode_error_msg("a zero arity function", Data). decode_tuple(Data = {_, _}) -> {ok, Data}; decode_tuple(Data) -> decode_error_msg("a 2 element tuple", Data). +decode_list(Data) when is_list(Data) -> {ok, Data}; +decode_list(Data) -> decode_error_msg("a List", Data). + decode_field(Data, Key) -> case Data of #{Key := Value} -> diff --git a/src/tuple.gleam b/src/tuple.gleam new file mode 100644 index 0000000..d437a9f --- /dev/null +++ b/src/tuple.gleam @@ -0,0 +1,23 @@ +import expect + +pub fn first(tup) { + let {a, _} = tup + a +} + +test first { + {1, 2} + |> first + |> expect:equal(_, 1) +} + +pub fn second(tup) { + let {_, a} = tup + a +} + +test second { + {1, 2} + |> second + |> expect:equal(_, 2) +} |