@@ -502,7 +502,10 @@ pub fn random(boundary_a: Int, boundary_b: Int) -> Int {
502
502
|> float . round ( )
503
503
}
504
504
505
- /// Returns division of the inputs as a `Result`.
505
+ /// Performs a truncated integer division.
506
+ ///
507
+ /// Returns division of the inputs as a `Result`: If the given divisor equals
508
+ /// `0`, this function returns an `Error`.
506
509
///
507
510
/// ## Examples
508
511
///
@@ -512,12 +515,105 @@ pub fn random(boundary_a: Int, boundary_b: Int) -> Int {
512
515
///
513
516
/// > divide(1, 0)
514
517
/// Error(Nil)
518
+ ///
519
+ /// > divide(5, 2)
520
+ /// Ok(2)
521
+ ///
522
+ /// > divide(-99, 2)
523
+ /// Ok(-49)
515
524
/// ```
516
525
///
517
- pub fn divide ( a : Int , by b : Int ) -> Result ( Int , Nil ) {
518
- case b {
526
+ pub fn divide ( dividend : Int , by divisor : Int ) -> Result ( Int , Nil ) {
527
+ case divisor {
519
528
0 -> Error ( Nil )
520
- b -> Ok ( a / b )
529
+ divisor -> Ok ( dividend / divisor )
530
+ }
531
+ }
532
+
533
+ /// Computes the remainder of an integer division of inputs as a `Result`.
534
+ ///
535
+ /// This functions mimicks modulo operation of following languages:
536
+ /// C, C#, C++, Go, Java, JavaScript, Kotlin, Nim, PHP, Rust,
537
+ /// Scala, Swift, Crystal as well as Erlang's and Elixir's rem operator.
538
+ ///
539
+ /// Returns division of the inputs as a `Result`: If the given divisor equals
540
+ /// `0`, this function returns an `Error`.
541
+ ///
542
+ /// ## Examples
543
+ ///
544
+ /// ```gleam
545
+ /// > int.remainder(3, 2)
546
+ /// Ok(1)
547
+ ///
548
+ /// > int.remainder(1, 0)
549
+ /// Error(Nil)
550
+ ///
551
+ /// > int.remainder(10, -1)
552
+ /// Ok(0)
553
+ ///
554
+ /// > int.remainder(13, by: 3)
555
+ /// Ok(1)
556
+ ///
557
+ /// > int.remainder(-13, by: 3)
558
+ /// Ok(-1)
559
+ ///
560
+ /// > int.remainder(13, by: -3)
561
+ /// Ok(1)
562
+ ///
563
+ /// > int.remainder(-13, by: -3)
564
+ /// Ok(-1)
565
+ /// ```
566
+ ///
567
+ pub fn remainder ( dividend : Int , by divisor : Int ) -> Result ( Int , Nil ) {
568
+ case divisor {
569
+ 0 -> Error ( Nil )
570
+ divisor -> Ok ( dividend % divisor )
571
+ }
572
+ }
573
+
574
+ /// Computes the modulo of an integer division of inputs as a `Result`.
575
+ ///
576
+ /// This functions mimicks modulo operation on following languages:
577
+ /// Haskell, Lua, Python, Ruby, as well as Elixir's Integer.mod().
578
+ ///
579
+ /// Returns division of the inputs as a `Result`: If the given divisor equals
580
+ /// `0`, this function returns an `Error`.
581
+ ///
582
+ /// ## Examples
583
+ ///
584
+ /// ```gleam
585
+ /// > int.modulo(3, 2)
586
+ /// Ok(1)
587
+ ///
588
+ /// > int.modulo(1, 0)
589
+ /// Error(Nil)
590
+ ///
591
+ /// > int.modulo(10, -1)
592
+ /// Ok(0)
593
+ ///
594
+ /// > int.modulo(13, by: 3)
595
+ /// Ok(1)
596
+ ///
597
+ /// > int.modulo(-13, by: 3)
598
+ /// Ok(2)
599
+ ///
600
+ /// > int.modulo(13, by: -3)
601
+ /// Ok(-2)
602
+ ///
603
+ /// > int.modulo(-13, by: -3)
604
+ /// Ok(-1)
605
+ /// ```
606
+ ///
607
+ pub fn modulo ( dividend : Int , by divisor : Int ) -> Result ( Int , Nil ) {
608
+ case divisor {
609
+ 0 -> Error ( Nil )
610
+ _ -> {
611
+ let remainder = dividend % divisor
612
+ case remainder * divisor < 0 {
613
+ True -> Ok ( remainder + divisor )
614
+ False -> Ok ( remainder )
615
+ }
616
+ }
521
617
}
522
618
}
523
619
0 commit comments