aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam3
-rw-r--r--src/gleam/int.gleam3
-rw-r--r--src/gleam/list.gleam29
-rw-r--r--src/gleam/map.gleam5
-rw-r--r--src/gleam/result.gleam24
-rw-r--r--src/gleam/string.gleam6
-rw-r--r--src/gleam/uri.gleam56
7 files changed, 50 insertions, 76 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index 6556204..953a267 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -1,6 +1,5 @@
import gleam/iodata
import gleam/order.{Order}
-import gleam/result.{Option}
pub type Float =
Float
@@ -15,7 +14,7 @@ pub type Float =
/// > parse("ABC")
/// None
///
-pub external fn parse(String) -> Option(Float) =
+pub external fn parse(String) -> Result(Float, Nil) =
"gleam_stdlib" "parse_float"
/// Return the string representation of the provided float.
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 7399aac..1e9f17c 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -1,5 +1,4 @@
import gleam/order.{Order}
-import gleam/result.{Option}
pub type Int =
Int
@@ -14,7 +13,7 @@ pub type Int =
/// > parse("ABC")
/// Error(Nil)
///
-pub external fn parse(String) -> Option(Int) =
+pub external fn parse(String) -> Result(Int, Nil) =
"gleam_stdlib" "parse_int"
/// Print a given int to a string.
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 4e10c15..687eb61 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -21,7 +21,6 @@
import gleam/int
import gleam/pair
import gleam/order.{Order}
-import gleam/result.{Option}
pub type List(elements) =
List(elements)
@@ -138,9 +137,9 @@ pub fn contains(list: List(a), has elem: a) -> Bool {
/// > head([1, 2])
/// Ok(1)
///
-pub fn head(list: List(a)) -> Option(a) {
+pub fn head(list: List(a)) -> Result(a, Nil) {
case list {
- [] -> result.none()
+ [] -> Error(Nil)
[x, ..] -> Ok(x)
}
}
@@ -161,9 +160,9 @@ pub fn head(list: List(a)) -> Option(a) {
/// > tail([1, 2])
/// Ok([2])
///
-pub fn tail(list: List(a)) -> Option(List(a)) {
+pub fn tail(list: List(a)) -> Result(List(a), Nil) {
case list {
- [] -> result.none()
+ [] -> Error(Nil)
[_, ..xs] -> Ok(xs)
}
}
@@ -476,9 +475,9 @@ pub fn fold_right(
pub fn find(
in haystack: List(a),
one_that is_desired: fn(a) -> Bool,
-) -> Option(a) {
+) -> Result(a, Nil) {
case haystack {
- [] -> result.none()
+ [] -> Error(Nil)
[x, ..rest] -> case is_desired(x) {
True -> Ok(x)
_ -> find(in: rest, one_that: is_desired)
@@ -505,10 +504,10 @@ pub fn find(
///
pub fn find_map(
in haystack: List(a),
- with fun: fn(a) -> Option(b),
-) -> Option(b) {
+ with fun: fn(a) -> Result(b, Nil),
+) -> Result(b, Nil) {
case haystack {
- [] -> result.none()
+ [] -> Error(Nil)
[x, ..rest] -> case fun(x) {
Ok(x) -> Ok(x)
_ -> find_map(in: rest, with: fun)
@@ -656,11 +655,11 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) {
/// > at([1, 2, 3], 5)
/// Error(Nil)
///
-pub fn at(in list: List(a), get index: Int) -> Option(a) {
+pub fn at(in list: List(a), get index: Int) -> Result(a, Nil) {
case index < 0 {
- True -> result.none()
+ True -> Error(Nil)
False -> case list {
- [] -> result.none()
+ [] -> Error(Nil)
[x, ..rest] -> case index == 0 {
True -> Ok(x)
False -> at(rest, index - 1)
@@ -858,14 +857,14 @@ pub fn split_while(
pub fn key_find(
in keyword_list: List(tuple(k, v)),
find desired_key: k,
-) -> Option(v) {
+) -> Result(v, Nil) {
find_map(
keyword_list,
fn(keyword) {
let tuple(key, value) = keyword
case key == desired_key {
True -> Ok(value)
- False -> result.none()
+ False -> Error(Nil)
}
},
)
diff --git a/src/gleam/map.gleam b/src/gleam/map.gleam
index dff9e90..76b6300 100644
--- a/src/gleam/map.gleam
+++ b/src/gleam/map.gleam
@@ -1,6 +1,5 @@
import gleam/result
import gleam/list
-import gleam/result.{Option}
/// A dictionary of keys and values.
///
@@ -92,7 +91,7 @@ pub external fn new() -> Map(key, value) =
/// > new() |> insert("a", 0) |> get("b")
/// Error(Nil)
///
-pub external fn get(from: Map(key, value), get: key) -> Option(value) =
+pub external fn get(from: Map(key, value), get: key) -> Result(value, Nil) =
"gleam_stdlib" "map_get"
external fn erl_insert(key, value, Map(key, value)) -> Map(key, value) =
@@ -279,7 +278,7 @@ pub fn drop(from map: Map(k, v), drop disallowed_keys: List(k)) -> Map(k, v) {
pub fn update(
in map: Map(k, v),
update key: k,
- with fun: fn(Option(v)) -> v,
+ with fun: fn(Result(v, Nil)) -> v,
) -> Map(k, v) {
map
|> get(key)
diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam
index 7861827..b686939 100644
--- a/src/gleam/result.gleam
+++ b/src/gleam/result.gleam
@@ -7,22 +7,11 @@ pub type Result(success, error) =
/// Nil is a type used to represent the absence of something, similar to null
/// or undefined in other languages.
///
-/// Unlike some other languages values cannot be implicitly nil, value that may
-/// be absent is typically represented using `Result(TheType, Nil)`. This is
-/// such a common type that offer the `Option(TheType)` alias.
+/// Unlike some other languages values cannot be implicitly nil.
///
pub type Nil =
Nil
-/// A value that is either there or not there.
-///
-/// Some other languages have a dedicated Option type that is not related to
-/// Result for this, however this tends to have all the same functions as
-/// Result so in Gleam we combine the two.
-///
-pub type Option(value) =
- Result(value, Nil)
-
/// Check whether the result is an Ok value.
///
/// ## Examples
@@ -174,17 +163,6 @@ pub fn unwrap(result: Result(a, e), or default: a) -> a {
}
}
-/// Another way of writing `Error(Nil)`.
-///
-/// ## Examples
-///
-/// > none()
-/// Error(Nil)
-///
-pub fn none() -> Option(a) {
- Error(Nil)
-}
-
/// Transforms any error into Error(Nil)
///
/// ## Examples
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index a6c7944..7c2f188 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -5,7 +5,7 @@ import gleam/iodata
import gleam/dynamic.{Dynamic}
import gleam/list
import gleam/order
-import gleam/result.{Option}
+import gleam/result
pub type String =
String
@@ -410,7 +410,9 @@ pub fn trim_right(string: String) -> String {
/// > pop_grapheme("")
/// Error(Nil)
///
-pub external fn pop_grapheme(string: String) -> Option(tuple(String, String)) =
+pub external fn pop_grapheme(
+ string: String,
+) -> Result(tuple(String, String), Nil) =
"gleam_stdlib" "string_pop_grapheme"
/// Convert a string to a list of Graphemes.
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam
index 19feb27..ed63292 100644
--- a/src/gleam/uri.gleam
+++ b/src/gleam/uri.gleam
@@ -8,7 +8,7 @@
//// https://www.w3.org/TR/html52/sec-forms.html#urlencoded-form-data
import gleam/list
-import gleam/result.{Option}
+import gleam/result
import gleam/string
import gleam/dynamic.{Dynamic}
import gleam/map.{Map}
@@ -18,13 +18,13 @@ import gleam/map.{Map}
///
pub type Uri {
Uri(
- scheme: Option(String),
- userinfo: Option(String),
- host: Option(String),
- port: Option(Int),
+ scheme: Result(String, Nil),
+ userinfo: Result(String, Nil),
+ host: Result(String, Nil),
+ port: Result(Int, Nil),
path: String,
- query: Option(String),
- fragment: Option(String),
+ query: Result(String, Nil),
+ fragment: Result(String, Nil),
)
}
@@ -47,27 +47,25 @@ type UriKey {
/// The opposite operation is `uri.to_string`
///
pub fn parse(string: String) -> Result(Uri, Nil) {
- case dynamic.map(erl_parse(string)) {
- Error(_) -> Error(Nil)
- Ok(uri_map) -> {
- let get = fn(k, decoder: dynamic.Decoder(t)) {
- uri_map
- |> map.get(dynamic.from(k))
- |> result.then(fn(x) { result.map_error(decoder(x), fn(_) { Nil }) })
- }
-
- let uri = Uri(
- scheme: get(Scheme, dynamic.string),
- userinfo: get(Userinfo, dynamic.string),
- host: get(Host, dynamic.string),
- port: get(Port, dynamic.int),
- path: result.unwrap(get(Path, dynamic.string), ""),
- query: get(Query, dynamic.string),
- fragment: get(Fragment, dynamic.string),
- )
- Ok(uri)
- }
+ try uri_map = dynamic.map(erl_parse(string))
+ |> result.nil_error
+ let get = fn(k, decoder: dynamic.Decoder(t)) {
+ try value = map.get(uri_map, dynamic.from(k))
+ value
+ |> decoder
+ |> result.nil_error
}
+
+ let uri = Uri(
+ scheme: get(Scheme, dynamic.string),
+ userinfo: get(Userinfo, dynamic.string),
+ host: get(Host, dynamic.string),
+ port: get(Port, dynamic.int),
+ path: result.unwrap(get(Path, dynamic.string), ""),
+ query: get(Query, dynamic.string),
+ fragment: get(Fragment, dynamic.string),
+ )
+ Ok(uri)
}
external fn erl_parse_query(String) -> Dynamic =
@@ -78,7 +76,7 @@ external fn erl_parse_query(String) -> Dynamic =
///
/// The opposite operation is `uri.query_to_string`.
///
-pub fn parse_query(query: String) -> Option(List(tuple(String, String))) {
+pub fn parse_query(query: String) -> Result(List(tuple(String, String)), Nil) {
query
|> erl_parse_query
|> dynamic.list(dynamic.tuple2_of(_, dynamic.string, dynamic.string))
@@ -143,7 +141,7 @@ external fn erl_to_string(Map(UriKey, Dynamic)) -> Dynamic =
/// The opposite operation is `uri.parse`.
///
pub fn to_string(uri: Uri) -> String {
- let field = fn(key: UriKey, value: Option(anything)) {
+ let field = fn(key: UriKey, value: Result(anything, Nil)) {
result.map(value, fn(value) { tuple(key, dynamic.from(value)) })
}