Skip to content

Commit 77e13b9

Browse files
authored
Merge pull request #28 from gleam-community/refactor/nil-errors
♻️ Replace `String` errors with `Nil`
2 parents 3254597 + eb1b743 commit 77e13b9

9 files changed

+178
-353
lines changed

src/gleam_community/maths/arithmetics.gleam

+11-17
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ pub fn int_sum(arr: List(Int)) -> Int {
442442
pub fn float_product(
443443
arr: List(Float),
444444
weights: option.Option(List(Float)),
445-
) -> Result(Float, String) {
445+
) -> Result(Float, Nil) {
446446
case arr, weights {
447447
[], _ ->
448448
1.0
@@ -452,22 +452,16 @@ pub fn float_product(
452452
|> list.fold(1.0, fn(acc, a) { a *. acc })
453453
|> Ok
454454
_, option.Some(warr) -> {
455-
let results =
456-
list.zip(arr, warr)
457-
|> list.map(fn(a) {
458-
pair.first(a)
459-
|> elementary.power(pair.second(a))
460-
})
461-
|> result.all
462-
case results {
463-
Ok(prods) ->
464-
prods
465-
|> list.fold(1.0, fn(acc, a) { a *. acc })
466-
|> Ok
467-
Error(msg) ->
468-
msg
469-
|> Error
470-
}
455+
list.zip(arr, warr)
456+
|> list.map(fn(a: #(Float, Float)) -> Result(Float, Nil) {
457+
pair.first(a)
458+
|> elementary.power(pair.second(a))
459+
})
460+
|> result.all
461+
|> result.map(fn(prods) {
462+
prods
463+
|> list.fold(1.0, fn(acc: Float, a: Float) -> Float { a *. acc })
464+
})
471465
}
472466
}
473467
}

src/gleam_community/maths/combinatorics.gleam

+36-68
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,10 @@ pub fn combination(
130130
n: Int,
131131
k: Int,
132132
mode: option.Option(CombinatoricsMode),
133-
) -> Result(Int, String) {
133+
) -> Result(Int, Nil) {
134134
case n, k {
135-
_, _ if n < 0 ->
136-
"Invalid input argument: n < 0. Valid input is n >= 0." |> Error
137-
_, _ if k < 0 ->
138-
"Invalid input argument: k < 0. Valid input is k >= 0." |> Error
135+
_, _ if n < 0 -> Error(Nil)
136+
_, _ if k < 0 -> Error(Nil)
139137
_, _ -> {
140138
case mode {
141139
option.Some(WithRepetitions) -> combination_with_repetitions(n, k)
@@ -145,12 +143,11 @@ pub fn combination(
145143
}
146144
}
147145

148-
fn combination_with_repetitions(n: Int, k: Int) -> Result(Int, String) {
149-
{ n + k - 1 }
150-
|> combination_without_repetitions(k)
146+
fn combination_with_repetitions(n: Int, k: Int) -> Result(Int, Nil) {
147+
combination_without_repetitions(n + k - 1, k)
151148
}
152149

153-
fn combination_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
150+
fn combination_without_repetitions(n: Int, k: Int) -> Result(Int, Nil) {
154151
case n, k {
155152
_, _ if k == 0 || k == n -> {
156153
1 |> Ok
@@ -202,17 +199,11 @@ fn combination_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
202199
/// </a>
203200
/// </div>
204201
///
205-
pub fn factorial(n) -> Result(Int, String) {
202+
pub fn factorial(n) -> Result(Int, Nil) {
206203
case n {
207-
_ if n < 0 ->
208-
"Invalid input argument: n < 0. Valid input is n >= 0."
209-
|> Error
210-
0 ->
211-
1
212-
|> Ok
213-
1 ->
214-
1
215-
|> Ok
204+
_ if n < 0 -> Error(Nil)
205+
0 -> Ok(1)
206+
1 -> Ok(1)
216207
_ ->
217208
list.range(1, n)
218209
|> list.fold(1, fn(acc, x) { acc * x })
@@ -300,22 +291,16 @@ pub fn permutation(
300291
n: Int,
301292
k: Int,
302293
mode: option.Option(CombinatoricsMode),
303-
) -> Result(Int, String) {
304-
case n, k {
305-
_, _ if n < 0 ->
306-
"Invalid input argument: n < 0. Valid input is n >= 0." |> Error
307-
_, _ if k < 0 ->
308-
"Invalid input argument: k < 0. Valid input is k >= 0." |> Error
309-
_, _ -> {
310-
case mode {
311-
option.Some(WithRepetitions) -> permutation_with_repetitions(n, k)
312-
_ -> permutation_without_repetitions(n, k)
313-
}
314-
}
294+
) -> Result(Int, Nil) {
295+
case n, k, mode {
296+
_, _, _ if n < 0 -> Error(Nil)
297+
_, _, _ if k < 0 -> Error(Nil)
298+
_, _, option.Some(WithRepetitions) -> permutation_with_repetitions(n, k)
299+
_, _, _ -> permutation_without_repetitions(n, k)
315300
}
316301
}
317302

318-
fn permutation_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
303+
fn permutation_without_repetitions(n: Int, k: Int) -> Result(Int, Nil) {
319304
case n, k {
320305
_, _ if k < 0 || k > n -> {
321306
0 |> Ok
@@ -330,7 +315,7 @@ fn permutation_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
330315
}
331316
}
332317

333-
fn permutation_with_repetitions(n: Int, k: Int) -> Result(Int, String) {
318+
fn permutation_with_repetitions(n: Int, k: Int) -> Result(Int, Nil) {
334319
let n_float = conversion.int_to_float(n)
335320
let k_float = conversion.int_to_float(k)
336321
// 'n' ank 'k' are positive integers, so no errors here...
@@ -388,29 +373,20 @@ pub fn list_combination(
388373
arr: List(a),
389374
k: Int,
390375
mode: option.Option(CombinatoricsMode),
391-
) -> Result(iterator.Iterator(List(a)), String) {
392-
case k {
393-
_ if k < 0 ->
394-
"Invalid input argument: k < 0. Valid input is k >= 0."
395-
|> Error
396-
_ ->
397-
case mode {
398-
option.Some(WithRepetitions) ->
399-
list_combination_with_repetitions(arr, k)
400-
_ -> list_combination_without_repetitions(arr, k)
401-
}
376+
) -> Result(iterator.Iterator(List(a)), Nil) {
377+
case k, mode {
378+
_, _ if k < 0 -> Error(Nil)
379+
_, option.Some(WithRepetitions) -> list_combination_with_repetitions(arr, k)
380+
_, _ -> list_combination_without_repetitions(arr, k)
402381
}
403382
}
404383

405384
fn list_combination_without_repetitions(
406385
arr: List(a),
407386
k: Int,
408-
) -> Result(iterator.Iterator(List(a)), String) {
387+
) -> Result(iterator.Iterator(List(a)), Nil) {
409388
case k, list.length(arr) {
410-
_, arr_length if k > arr_length -> {
411-
"Invalid input argument: k > length(arr). Valid input is 0 <= k <= length(arr)."
412-
|> Error
413-
}
389+
_, arr_length if k > arr_length -> Error(Nil)
414390
// Special case: When k = n, then the entire list is the only valid combination
415391
_, arr_length if k == arr_length -> {
416392
iterator.single(arr) |> Ok
@@ -446,7 +422,7 @@ fn do_list_combination_without_repetitions(
446422
fn list_combination_with_repetitions(
447423
arr: List(a),
448424
k: Int,
449-
) -> Result(iterator.Iterator(List(a)), String) {
425+
) -> Result(iterator.Iterator(List(a)), Nil) {
450426
Ok(do_list_combination_with_repetitions(iterator.from_list(arr), k, []))
451427
}
452428

@@ -528,17 +504,12 @@ pub fn list_permutation(
528504
arr: List(a),
529505
k: Int,
530506
mode: option.Option(CombinatoricsMode),
531-
) -> Result(iterator.Iterator(List(a)), String) {
532-
case k {
533-
_ if k < 0 ->
534-
"Invalid input argument: k < 0. Valid input is k >= 0."
535-
|> Error
536-
_ ->
537-
case mode {
538-
option.Some(WithRepetitions) ->
539-
list_permutation_with_repetitions(arr, k)
540-
_ -> list_permutation_without_repetitions(arr, k)
541-
}
507+
) -> Result(iterator.Iterator(List(a)), Nil) {
508+
case k, mode {
509+
_, _ if k < 0 -> Error(Nil)
510+
_, option.Some(WithRepetitions) ->
511+
Ok(list_permutation_with_repetitions(arr, k))
512+
_, _ -> list_permutation_without_repetitions(arr, k)
542513
}
543514
}
544515

@@ -558,12 +529,9 @@ fn remove_first_by_index(
558529
fn list_permutation_without_repetitions(
559530
arr: List(a),
560531
k: Int,
561-
) -> Result(iterator.Iterator(List(a)), String) {
532+
) -> Result(iterator.Iterator(List(a)), Nil) {
562533
case k, list.length(arr) {
563-
_, arr_length if k > arr_length -> {
564-
"Invalid input argument: k > length(arr). Valid input is 0 <= k <= length(arr)."
565-
|> Error
566-
}
534+
_, arr_length if k > arr_length -> Error(Nil)
567535
_, _ -> {
568536
let indexed_arr = list.index_map(arr, fn(x, i) { #(i, x) })
569537
Ok(do_list_permutation_without_repetitions(
@@ -594,9 +562,9 @@ fn do_list_permutation_without_repetitions(
594562
fn list_permutation_with_repetitions(
595563
arr: List(a),
596564
k: Int,
597-
) -> Result(iterator.Iterator(List(a)), String) {
565+
) -> iterator.Iterator(List(a)) {
598566
let indexed_arr = list.index_map(arr, fn(x, i) { #(i, x) })
599-
Ok(do_list_permutation_with_repetitions(indexed_arr, k))
567+
do_list_permutation_with_repetitions(indexed_arr, k)
600568
}
601569

602570
fn do_list_permutation_with_repetitions(

0 commit comments

Comments
 (0)