@@ -130,12 +130,10 @@ pub fn combination(
130
130
n : Int ,
131
131
k : Int ,
132
132
mode : option . Option ( CombinatoricsMode ) ,
133
- ) -> Result ( Int , String ) {
133
+ ) -> Result ( Int , Nil ) {
134
134
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 )
139
137
_ , _ -> {
140
138
case mode {
141
139
option . Some ( WithRepetitions ) -> combination_with_repetitions ( n , k )
@@ -145,12 +143,11 @@ pub fn combination(
145
143
}
146
144
}
147
145
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 )
151
148
}
152
149
153
- fn combination_without_repetitions ( n : Int , k : Int ) -> Result ( Int , String ) {
150
+ fn combination_without_repetitions ( n : Int , k : Int ) -> Result ( Int , Nil ) {
154
151
case n , k {
155
152
_ , _ if k == 0 || k == n -> {
156
153
1 |> Ok
@@ -202,17 +199,11 @@ fn combination_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
202
199
/// </a>
203
200
/// </div>
204
201
///
205
- pub fn factorial ( n ) -> Result ( Int , String ) {
202
+ pub fn factorial ( n ) -> Result ( Int , Nil ) {
206
203
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 )
216
207
_ ->
217
208
list . range ( 1 , n )
218
209
|> list . fold ( 1 , fn ( acc , x ) { acc * x } )
@@ -300,22 +291,16 @@ pub fn permutation(
300
291
n : Int ,
301
292
k : Int ,
302
293
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 )
315
300
}
316
301
}
317
302
318
- fn permutation_without_repetitions ( n : Int , k : Int ) -> Result ( Int , String ) {
303
+ fn permutation_without_repetitions ( n : Int , k : Int ) -> Result ( Int , Nil ) {
319
304
case n , k {
320
305
_ , _ if k < 0 || k > n -> {
321
306
0 |> Ok
@@ -330,7 +315,7 @@ fn permutation_without_repetitions(n: Int, k: Int) -> Result(Int, String) {
330
315
}
331
316
}
332
317
333
- fn permutation_with_repetitions ( n : Int , k : Int ) -> Result ( Int , String ) {
318
+ fn permutation_with_repetitions ( n : Int , k : Int ) -> Result ( Int , Nil ) {
334
319
let n_float = conversion . int_to_float ( n )
335
320
let k_float = conversion . int_to_float ( k )
336
321
// 'n' ank 'k' are positive integers, so no errors here...
@@ -388,29 +373,20 @@ pub fn list_combination(
388
373
arr : List ( a) ,
389
374
k : Int ,
390
375
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 )
402
381
}
403
382
}
404
383
405
384
fn list_combination_without_repetitions (
406
385
arr : List ( a) ,
407
386
k : Int ,
408
- ) -> Result ( iterator . Iterator ( List ( a) ) , String ) {
387
+ ) -> Result ( iterator . Iterator ( List ( a) ) , Nil ) {
409
388
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 )
414
390
// Special case: When k = n, then the entire list is the only valid combination
415
391
_ , arr_length if k == arr_length -> {
416
392
iterator . single ( arr ) |> Ok
@@ -446,7 +422,7 @@ fn do_list_combination_without_repetitions(
446
422
fn list_combination_with_repetitions (
447
423
arr : List ( a) ,
448
424
k : Int ,
449
- ) -> Result ( iterator . Iterator ( List ( a) ) , String ) {
425
+ ) -> Result ( iterator . Iterator ( List ( a) ) , Nil ) {
450
426
Ok ( do_list_combination_with_repetitions ( iterator . from_list ( arr ) , k , [ ] ) )
451
427
}
452
428
@@ -528,17 +504,12 @@ pub fn list_permutation(
528
504
arr : List ( a) ,
529
505
k : Int ,
530
506
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 )
542
513
}
543
514
}
544
515
@@ -558,12 +529,9 @@ fn remove_first_by_index(
558
529
fn list_permutation_without_repetitions (
559
530
arr : List ( a) ,
560
531
k : Int ,
561
- ) -> Result ( iterator . Iterator ( List ( a) ) , String ) {
532
+ ) -> Result ( iterator . Iterator ( List ( a) ) , Nil ) {
562
533
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 )
567
535
_ , _ -> {
568
536
let indexed_arr = list . index_map ( arr , fn ( x , i ) { # ( i , x ) } )
569
537
Ok ( do_list_permutation_without_repetitions (
@@ -594,9 +562,9 @@ fn do_list_permutation_without_repetitions(
594
562
fn list_permutation_with_repetitions (
595
563
arr : List ( a) ,
596
564
k : Int ,
597
- ) -> Result ( iterator . Iterator ( List ( a) ) , String ) {
565
+ ) -> iterator . Iterator ( List ( a) ) {
598
566
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 )
600
568
}
601
569
602
570
fn do_list_permutation_with_repetitions (
0 commit comments