aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2018-02-24 23:39:16 +0000
committerLouis Pilfold <louis@lpil.uk>2018-02-24 23:48:21 +0000
commit29481d823686ead1214b273e9e39e63472681bcc (patch)
tree6c47aff4a827d65727201f27378f71038ff76bc5
downloadgleam_stdlib-29481d823686ead1214b273e9e39e63472681bcc.tar.gz
gleam_stdlib-29481d823686ead1214b273e9e39e63472681bcc.zip
Restructure
-rw-r--r--.gitignore18
-rw-r--r--LICENSE191
-rw-r--r--README.md9
-rw-r--r--rebar.config2
-rw-r--r--src/Bool.gleam62
-rw-r--r--src/Foreign.gleam15
-rw-r--r--src/List.gleam166
-rw-r--r--src/Maybe.gleam63
-rw-r--r--src/Order.gleam84
-rw-r--r--src/Result.gleam97
-rw-r--r--src/gleam_stdlib.app.src15
11 files changed, 722 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..40ca652
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,18 @@
+.rebar3
+_*
+.eunit
+*.o
+*.beam
+*.plt
+*.swp
+*.swo
+.erlang.cookie
+ebin
+log
+erl_crash.dump
+.rebar
+logs
+_build
+.idea
+*.iml
+rebar3.crashdump
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..c1dabd0
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,191 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ Copyright 2018, Louis Pilfold <louis@lpil.uk>.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..20330eb
--- /dev/null
+++ b/README.md
@@ -0,0 +1,9 @@
+gleam_stdlib
+=====
+
+An OTP library
+
+Build
+-----
+
+ $ rebar3 compile
diff --git a/rebar.config b/rebar.config
new file mode 100644
index 0000000..f618f3e
--- /dev/null
+++ b/rebar.config
@@ -0,0 +1,2 @@
+{erl_opts, [debug_info]}.
+{deps, []}. \ No newline at end of file
diff --git a/src/Bool.gleam b/src/Bool.gleam
new file mode 100644
index 0000000..ef39179
--- /dev/null
+++ b/src/Bool.gleam
@@ -0,0 +1,62 @@
+module Bool
+
+export Bool(..), not/1, compare/2, max/2, min/2
+
+import Order exposing Order(_)
+
+type Bool
+ = True
+ | False
+
+fn not(bool) =
+ case bool
+ | True => False
+ | False => True
+
+test not =
+ not(True) |> Assert.false
+ not(False) |> Assert.true
+
+fn compare(a, b) =
+ case (a, b)
+ | (True, True) => EQ
+ | (True, False) => GT
+ | (False, False) => LT
+ | (False, True) => GT
+
+test compare =
+ compare(True, True) |> Assert.equal(_, EQ)
+ compare(True, False) |> Assert.equal(_, GT)
+ compare(False, False) |> Assert.equal(_, LT)
+ compare(False, True) |> Assert.equal(_, GT)
+
+fn max(a, b) =
+ case a
+ | True => True
+ | False => b
+
+test max =
+ max(True, True) |> Assert.equal(_, True)
+ max(True, False) |> Assert.equal(_, True)
+ max(False, False) |> Assert.equal(_, False)
+ max(False, True) |> Assert.equal(_, True)
+
+fn min(a, b) =
+ case a
+ | False => False
+ | True => b
+
+test min =
+ min(True, True) |> Assert.equal(_, True)
+ min(True, False) |> Assert.equal(_, False)
+ min(False, False) |> Assert.equal(_, False)
+ min(False, True) |> Assert.equal(_, False)
+
+fn to_int(bool) =
+ case bool
+ | False => 0
+ | True => 1
+
+test to_int =
+ to_int(True) |> Assert.equal(_, 1)
+ to_int(False) |> Assert.equal(_, 0)
diff --git a/src/Foreign.gleam b/src/Foreign.gleam
new file mode 100644
index 0000000..66cbd9f
--- /dev/null
+++ b/src/Foreign.gleam
@@ -0,0 +1,15 @@
+module Foreign
+
+export Foreign, new/1
+
+doc """
+Foreign 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.
+"""
+foreign type Foreign
+
+doc """
+Convert any Gleam data into Foreign data.
+"""
+foreign new :gleam_native :identity :: a -> Foreign
diff --git a/src/List.gleam b/src/List.gleam
new file mode 100644
index 0000000..67b713e
--- /dev/null
+++ b/src/List.gleam
@@ -0,0 +1,166 @@
+module List
+
+export length/1, reverse/1, empty/1, member/2, head/1, tail/1, filter/2,
+ foldl/3, foldr/3, map/2, flatten/1, drop/2, take/2, of/1, new/0
+
+import Maybe exposing Maybe(..)
+
+// Using the Erlang C BIF implementation.
+//
+foreign length :erlang :length :: |List(a)| -> Int
+
+test length =
+ length([]) |> Assert.equal(_, 0)
+ length([1]) |> Assert.equal(_, 1)
+ length([1, 1]) |> Assert.equal(_, 2)
+ length([1, 1, 1]) |> Assert.equal(_, 3)
+
+// Using the Erlang C BIF implementation.
+//
+foreign reverse :erlang :reverse :: |List(a)| -> List(a)
+
+test reverse =
+ length([]) |> Assert.equal(_, [])
+ length([1, 2, 3, 4, 5]) |> Assert.equal(_, [5, 4, 3, 2, 1])
+
+fn is_empty(list) =
+ list == []
+
+test is_empty =
+ is_empty([]) |> Assert.true
+ is_empty([1]) |> Assert.false
+
+fn member(list, elem) =
+ case list
+ | [] => False
+ | elem :: _ => True
+ | _ :: rest => member(rest, elem)
+
+test is_member =
+ is_member([0, 4, 5, 1], 1) |> Assert.true
+ is_member([0, 4, 5, 7], 1) |> Assert.false
+ is_member([], 1) |> Assert.false
+
+fn head(list) =
+ case list
+ | [] => Nothing
+ | elem :: _ => Just(elem)
+
+test head =
+ head([0, 4, 5, 7]) |> Assert.equal(_, Just(0))
+ head([]) |> Assert.equal(_, Nothing)
+
+fn tail(list) =
+ case list
+ | [] => Nothing
+ | _ :: rest => Just(rest)
+
+test tail =
+ tail([0, 4, 5, 7]) |> Assert.equal(_, Just([4, 5, 7]))
+ tail([0]) |> Assert.equal(_, Just([]))
+ tail([]) |> Assert.equal(_, Nothing)
+
+fn filter(list, fun) =
+ filter(list, fun, [])
+
+test filter =
+ filter([], |x| True) |> Assert.equal(_, [])
+ filter([0, 4, 5, 7, 3], |x| True) |> Assert.equal(_, [0, 4, 5, 7, 3])
+ filter([0, 4, 5, 7, 3], |x| x > 4) |> Assert.equal(_, [5, 7])
+ filter([0, 4, 5, 7, 3], |x| x < 4) |> Assert.equal(_, [0, 3])
+
+fn filter(list, fun, acc) =
+ case list
+ | [] => reverse(acc)
+ | x :: xs => (
+ new_acc = case fun(x)
+ | True => x :: acc
+ | False => acc
+ filter(xs, fun, new_acc)
+ )
+
+fn map(list, fun) =
+ map(list, fun, [])
+
+test map =
+ map([], |x| * 2) |> Assert.equal(_, [])
+ map([0, 4, 5, 7, 3], |x| x * 2) |> Assert.equal(_, [0, 8, 10, 14, 6])
+
+fn map(list, fun, acc) =
+ case list
+ | [] => reverse(acc)
+ | x :: xs => map(xs, fun, fun(x) :: acc)
+
+fn drop(list, n) =
+ case n <= 0
+ | True => list
+ | False => (
+ case list
+ | [] => []
+ | _ :: xs => drop(xs, n - 1)
+ )
+
+test drop/2 =
+ drop([], 5) |> Assert.equal(_, [])
+ drop([1, 2, 3, 4, 5, 6, 7, 8], 5) |> Assert.equal(_, [6, 7, 8])
+
+fn take(list, n) =
+ take(list, n, [])
+
+fn take(list, n, acc) =
+ case n <= 0
+ | True => reverse(acc)
+ | False => (
+ case list
+ | [] => reverse(acc)
+ | x :: xs => take(xs, n - 1, x :: acc)
+ )
+
+test take =
+ take([], 5) |> Assert.equal(_, [])
+ take([1, 2, 3, 4, 5, 6, 7, 8], 5) |> Assert.equal(_, [1, 2, 3, 4, 5])
+
+fn of(x) =
+ [x]
+
+test of() =
+ of([]) |> Assert.equal(_, [[]])
+ of(1) |> Assert.equal(_, [1])
+
+fn new() =
+ []
+
+test new() =
+ new() |> Assert.equal(_, [])
+
+fn flatten(lists) =
+ flatten(lists, [])
+
+test flatten() =
+ flatten([]) |> Assert.equal(_, [])
+ flatten([[]]) |> Assert.equal(_, [])
+ flatten([[], [], []]) |> Assert.equal(_, [])
+ flatten([[1, 2], [], [3, 4]]) |> Assert.equal(_, [1, 2, 3, 4])
+
+fn flatten(lists, acc) =
+ case lists
+ | [] => acc
+ | l :: rest => flatten(rest, acc ++ l)
+
+fn foldl(list, acc, fun) =
+ case list
+ | [] => acc
+ | x :: rest => foldl(rest, fun(x, acc), fun)
+
+test foldl() =
+ foldl([1, 2, 3], [], |x, acc| x :: acc)
+ |> Assert.equal(_, [3, 2, 1])
+
+fn foldr(list, acc, fun) =
+ case list
+ | [] => acc
+ | x :: rest => fun(x, foldl(rest, acc, fun))
+
+test foldr() =
+ foldr([1, 2, 3], [], |x, acc| x :: acc)
+ |> Assert.equal(_, [1, 2, 3])
diff --git a/src/Maybe.gleam b/src/Maybe.gleam
new file mode 100644
index 0000000..2805a4d
--- /dev/null
+++ b/src/Maybe.gleam
@@ -0,0 +1,63 @@
+module Maybe
+
+export Maybe(..), is_just/1, is_nothing/1, map/2, flatten/1, flat_map/2,
+ unwrap/2
+
+type Maybe(x)
+ = Just(x)
+ | Nothing
+
+fn is_just(maybe) =
+ case maybe
+ | Just(_) => True
+ | Nothing => False
+
+test is_just() =
+ is_just(Just(1)) |> Assert.true
+ is_just(Nothing) |> Assert.false
+
+fn is_nothing(maybe) =
+ case maybe
+ | Just(_) => False
+ | Nothing => True
+
+test is_nothing() =
+ is_nothing(Just(1)) |> Assert.false
+ is_nothing(Nothing) |> Assert.true
+
+fn map(maybe, fun) =
+ case maybe
+ | Just(x) => fun(x)
+ | Nothing => Nothing
+
+test map() =
+ map(Just(1), |x| x + 1) |> Assert.equal(_, Just(2))
+ map(Nothing, |x| x + 1) |> Assert.equal(Nothing)
+
+fn flatten(maybe) =
+ maybe
+ |> unwrap(_, Nothing)
+
+test flatten() =
+ flatten(Just(Just(1))) |> Assert.equal(Just(1))
+ flatten(Just(Nothing)) |> Assert.equal(Nothing)
+ flatten(Nothing) |> Assert.equal(Nothing)
+
+fn flat_map(maybe, fun) =
+ maybe
+ |> map(_, fun)
+ |> flatten
+
+test flat_map() =
+ flat_map(Nothing, |x| Just(x + 1)) |> Assert.equal(Nothing)
+ flat_map(Just(1), |x| Just(x + 1)) |> Assert.equal(Just(2))
+ flat_map(Just(1), |_| Nothing) |> Assert.equal(Nothing)
+
+fn unwrap(maybe, fallback) =
+ case maybe
+ | Just(v) => v
+ | Nothing => fallback
+
+test unwrap() =
+ unwrap(Just(1), 50) |> Assert.equal(1)
+ unwrap(Nothing, 50) |> Assert.equal(50)
diff --git a/src/Order.gleam b/src/Order.gleam
new file mode 100644
index 0000000..76ed92b
--- /dev/null
+++ b/src/Order.gleam
@@ -0,0 +1,84 @@
+module Order
+
+export Order(..), reverse/1, to_int/1, compare/2, max/2, min/2
+
+type Order
+ = LT
+ | EQ
+ | GT
+
+fn reverse(order) =
+ case order
+ | LT => GT
+ | EQ => EQ
+ | GT => LT
+
+test reverse =
+ reverse(LT) |> Assert.equal(_, GT)
+ reverse(EQ) |> Assert.equal(_, EQ)
+ reverse(GT) |> Assert.equal(_, LT)
+
+fn to_int(order) =
+ case order
+ | LT => -1
+ | EQ => 0
+ | GT => 1
+
+test to_int =
+ to_int(LT) |> Assert.equal(_, -1)
+ to_int(EQ) |> Assert.equal(_, 0)
+ to_int(GT) |> Assert.equal(_, 1)
+
+fn compare(a, b) =
+ case (a, b)
+ | (LT, LT) => EQ
+ | (LT, _) => LT
+ | (EQ, EQ) => EQ
+ | (GT, GT) => EQ
+ | (EQ, GT) => LT
+ | _ => GT
+
+test compare =
+ compare(LT, LT) |> Assert.equal(_, EQ)
+ compare(LT, EQ) |> Assert.equal(_, LT)
+ compare(LT, GT) |> Assert.equal(_, LT)
+ compare(EQ, LT) |> Assert.equal(_, GT)
+ compare(EQ, EQ) |> Assert.equal(_, EQ)
+ compare(EQ, GT) |> Assert.equal(_, LT)
+ compare(GT, LT) |> Assert.equal(_, GT)
+ compare(GT, EQ) |> Assert.equal(_, GT)
+ compare(GT, GT) |> Assert.equal(_, EQ)
+
+fn max(a, b) =
+ case (a, b)
+ | (GT, _) => GT
+ | (EQ, LT) => EQ
+ | _ => b
+
+test max =
+ max(LT, LT) |> Assert.equal(_, LT)
+ max(LT, EQ) |> Assert.equal(_, EQ)
+ max(LT, GT) |> Assert.equal(_, GT)
+ max(EQ, LT) |> Assert.equal(_, EQ)
+ max(EQ, EQ) |> Assert.equal(_, EQ)
+ max(EQ, GT) |> Assert.equal(_, GT)
+ max(GT, LT) |> Assert.equal(_, GT)
+ max(GT, EQ) |> Assert.equal(_, GT)
+ max(GT, GT) |> Assert.equal(_, GT)
+
+fn min(a, b) =
+ case (a, b)
+ | (LT, _) => LT
+ | (EQ, GT) => EQ
+ | _ => b
+
+test min =
+ min(LT, LT) |> Assert.equal(_, LT)
+ min(LT, EQ) |> Assert.equal(_, LT)
+ min(LT, GT) |> Assert.equal(_, LT)
+ min(EQ, LT) |> Assert.equal(_, LT)
+ min(EQ, EQ) |> Assert.equal(_, EQ)
+ min(EQ, GT) |> Assert.equal(_, EQ)
+ min(GT, LT) |> Assert.equal(_, LT)
+ min(GT, EQ) |> Assert.equal(_, EQ)
+ min(GT, GT) |> Assert.equal(_, GT)
diff --git a/src/Result.gleam b/src/Result.gleam
new file mode 100644
index 0000000..ff916e8
--- /dev/null
+++ b/src/Result.gleam
@@ -0,0 +1,97 @@
+module Result
+
+export Result(..), is_ok/1, is_none/1, map/2, map_error/2, flatten/1, flat_map/2,
+ unwrap/2, to_maybe/1, from_maybe/1
+
+import Maybe exposing Maybe(..)
+
+doc """
+Result represents the result of something that may succeed or fail.
+`Ok` means it was successful, `Error` means it failed.
+"""
+type Result(error, value)
+ = Ok(value)
+ | Error(error)
+
+fn is_ok(result) =
+ case result
+ | Error(_) => False
+ | Ok(_) => True
+
+test is_ok =
+ is_ok(Ok(1)) |> Assert.true
+ is_ok(Error(1)) |> Assert.false
+
+fn is_error(result) =
+ case result
+ | Ok(_) => False
+ | Error(_) => True
+
+test is_error =
+ is_error(Ok(1)) |> Assert.false
+ is_error(Error(1)) |> Assert.true
+
+fn map(result, fun) =
+ case result
+ | Ok(x) => fun(x)
+ | Error(_) => result
+
+test map =
+ map(Ok(1), |x| x + 1) |> Assert.equal(_, Ok(2))
+ map(Error(1), |x| x + 1) |> Assert.equal(Error(1))
+
+fn map_error(result, fun) =
+ case result
+ | Ok(_) => result
+ | Error(error) => error |> fun |> Error
+
+test map_error =
+ map_error(Ok(1), |x| x + 1) |> Assert.equal(_, Ok(1))
+ map_error(Error(1), |x| x + 1) |> Assert.equal(Error(2))
+
+fn flatten(result) =
+ case result
+ | Ok(x) => x
+ | Error(_) => result
+
+test flatten =
+ flatten(Ok(Ok(1))) |> Assert.equal(_, Ok(1))
+ flatten(Ok(Error(1))) |> Assert.equal(_, Error(1))
+ flatten(Error(1)) |> Assert.equal(_, Error(1))
+
+fn flat_map(result, fun) =
+ result
+ |> unwrap(_, fun)
+ |> flatten
+
+test flat_map =
+ flat_map(Error(1), |x| Ok(x + 1)) |> Assert.equal(_, Error(1))
+ flat_map(Ok(1), |x| Ok(x + 1)) |> Assert.equal(_, Ok(2))
+ flat_map(Ok(1), |_| Error(1)) |> Assert.equal(_, Error(1))
+
+fn unwrap(result, default) =
+ case result
+ | Ok(v) => v
+ | Error(_) => default
+
+test unwrap =
+ unwrap(Ok(1), 50) |> Assert.equal(_, 1)
+ unwrap(Error("nope"), 50) |> Assert.equal(_, 50)
+
+fn to_maybe(result) =
+ case result
+ | Ok(v) => Just(v)
+ | Error(_) => Nothing
+
+test to_maybe =
+ to_maybe(Ok(1)) |> Assert.equal(_, Just(_, 1))
+ to_maybe(Error(1)) |> Assert.equal(_, Nothing)
+
+fn from_maybe(maybe, error_reason) =
+ case maybe
+ | Just(v) => Ok(v)
+ | Nothing => Error(error_reason)
+
+test from_maybe =
+ to_maybe(Just(1), :ok) |> Assert.equal(_, Ok(1))
+ to_maybe(Nothing, :ok) |> Assert.equal(_, Error(:ok))
diff --git a/src/gleam_stdlib.app.src b/src/gleam_stdlib.app.src
new file mode 100644
index 0000000..a7f5a66
--- /dev/null
+++ b/src/gleam_stdlib.app.src
@@ -0,0 +1,15 @@
+{application, gleam_stdlib,
+ [{description, "An OTP library"},
+ {vsn, "0.1.0"},
+ {registered, []},
+ {applications,
+ [kernel,
+ stdlib
+ ]},
+ {env,[]},
+ {modules, []},
+
+ {maintainers, []},
+ {licenses, ["Apache 2.0"]},
+ {links, []}
+ ]}.