aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/gleam/option.gleam23
-rw-r--r--src/gleam/uri.gleam9
-rw-r--r--test/gleam/option_test.gleam22
4 files changed, 46 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 08acb07..913175d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,7 +15,8 @@
- The `dynamic.tuple2_of` function has been renamed to `dynamic.typed_tuple2`.
- The `list.traverse` function has been renamed to `list.try_map`.
- The `list.traverse` first argument gains the label `over`.
-- The `option` module gains the the `map`, `flatten` and `then` functions.
+- The `option` module gains the the `map`, `flatten`, `then` and `or`
+ functions.
## 0.9.0 - 2020-05-26
diff --git a/src/gleam/option.gleam b/src/gleam/option.gleam
index 2c01b4a..c1a456e 100644
--- a/src/gleam/option.gleam
+++ b/src/gleam/option.gleam
@@ -157,3 +157,26 @@ pub fn then(option: Option(a), apply fun: fn(a) -> Option(b)) -> Option(b) {
None -> None
}
}
+
+/// Return the first value if it is Some, otherwise return the second value.
+///
+/// ## Examples
+///
+/// > or(Some(1), Some(2))
+/// Some(1)
+///
+/// > or(Some(1), None)
+/// Some(1)
+///
+/// > or(None, Some(2))
+/// Some(2)
+///
+/// > or(None, None)
+/// None
+///
+pub fn or(first: Option(a), second: Option(a)) -> Option(a) {
+ case first {
+ Some(_) -> first
+ None -> second
+ }
+}
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam
index fbd95ec..d3cf2e6 100644
--- a/src/gleam/uri.gleam
+++ b/src/gleam/uri.gleam
@@ -187,13 +187,6 @@ pub fn origin(uri: Uri) -> Result(String, Nil) {
}
}
-fn option_or(first: Option(a), second: Option(a)) -> Option(a) {
- case first {
- Some(_) -> first
- None -> second
- }
-}
-
fn drop_last(elements) {
let tuple(keep, _last) = list.split(elements, list.length(elements) - 1)
keep
@@ -250,7 +243,7 @@ pub fn merge(base: Uri, relative: Uri) -> Result(Uri, Nil) {
base.host,
base.port,
base.path,
- option_or(relative.query, base.query),
+ option.or(relative.query, base.query),
relative.fragment,
)
Ok(resolved)
diff --git a/test/gleam/option_test.gleam b/test/gleam/option_test.gleam
index 7271769..0db3adf 100644
--- a/test/gleam/option_test.gleam
+++ b/test/gleam/option_test.gleam
@@ -47,7 +47,7 @@ pub fn map_option_test() {
|> should.equal(Some(2))
Some(1)
- |> option.map(fn(x) { "2" })
+ |> option.map(fn(_) { "2" })
|> should.equal(Some("2"))
None
@@ -75,10 +75,28 @@ pub fn then_option_test() {
|> should.equal(Some(2))
Some(1)
- |> option.then(fn(x) { Some("2") })
+ |> option.then(fn(_) { Some("2") })
|> should.equal(Some("2"))
None
|> option.then(fn(x) { Some(x + 1) })
|> should.equal(None)
}
+
+pub fn or_option_test() {
+ Some(1)
+ |> option.or(Some(2))
+ |> should.equal(Some(1))
+
+ Some(1)
+ |> option.or(None)
+ |> should.equal(Some(1))
+
+ None
+ |> option.or(Some(2))
+ |> should.equal(Some(2))
+
+ None
+ |> option.or(None)
+ |> should.equal(None)
+}