Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Iter::ascending() and Iter::descending() #327

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions double/double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,6 @@ pub fn is_neg_inf(self : Double) -> Bool {
self < -max_val
}

fn test_num[T : @num.Num + Debug + Default + Eq](
x : T,
y : T,
x_plus_y : T,
x_mul_y : T,
x_minus_y : T,
x_div_y : T,
x_signum : T
) -> Result[Unit, String] {
@assertion.assert_eq(x + y, x_plus_y)?
@assertion.assert_eq(x * y, x_mul_y)?
@assertion.assert_eq(x - y, x_minus_y)?
@assertion.assert_eq(x / y, x_div_y)?
@assertion.assert_eq(x.abs(), T::default() - x)?
@assertion.assert_eq(x.signum(), x_signum)?
Ok(())
}

test "double.num" {
let x = -500.0
let y = 792.0
test_num(x, y, x + y, x * y, x - y, x / y, -1.0)?
}

test "from_int" {
@assertion.assert_eq(Double::from_int(1), 1.0)?
@assertion.assert_eq(Double::from_int(0), 0.0)?
Expand Down
1 change: 0 additions & 1 deletion double/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/num",
"moonbitlang/core/assertion",
"moonbitlang/core/bool",
"moonbitlang/core/int64",
Expand Down
31 changes: 9 additions & 22 deletions int/int.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,17 @@ pub fn Int::min_value() -> Int {
min_val
}

fn test_num[T : @num.Num + Debug + Default + Eq](
x : T,
y : T,
x_plus_y : T,
x_mul_y : T,
x_minus_y : T,
x_div_y : T,
x_signum : T
) -> Result[Unit, String] {
@assertion.assert_eq(x + y, x_plus_y)?
@assertion.assert_eq(x * y, x_mul_y)?
@assertion.assert_eq(x - y, x_minus_y)?
@assertion.assert_eq(x / y, x_div_y)?
@assertion.assert_eq(x.abs(), T::default() - x)?
@assertion.assert_eq(x.signum(), x_signum)?
Ok(())
}

test "int.num" {
let x = -5
let y = 7
test_num(x, y, x + y, x * y, x - y, x / y, -1)?
pub fn Int::unity() -> Int {
1
}

pub fn hash(self : Int) -> Int {
self
}

test "misc" {
inspect(max_value(), content="2147483647")?
inspect(min_value(), content="-2147483648")?
inspect(unity(), content="1")?
inspect((42).hash(), content="42")?
}
1 change: 1 addition & 0 deletions int/int.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn Int::hash(Int) -> Int
fn Int::max_value() -> Int
fn Int::min_value() -> Int
fn Int::signum(Int) -> Int
fn Int::unity() -> Int

// Traits

Expand Down
1 change: 0 additions & 1 deletion int/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/num",
"moonbitlang/core/assertion",
"moonbitlang/core/coverage"
]
Expand Down
31 changes: 9 additions & 22 deletions int64/int64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,19 @@ pub fn Int64::min_value() -> Int64 {
min_val
}

fn test_num[T : @num.Num + Debug + Default + Eq](
x : T,
y : T,
x_plus_y : T,
x_mul_y : T,
x_minus_y : T,
x_div_y : T,
x_signum : T
) -> Result[Unit, String] {
@assertion.assert_eq(x + y, x_plus_y)?
@assertion.assert_eq(x * y, x_mul_y)?
@assertion.assert_eq(x - y, x_minus_y)?
@assertion.assert_eq(x / y, x_div_y)?
@assertion.assert_eq(x.abs(), T::default() - x)?
@assertion.assert_eq(x.signum(), x_signum)?
Ok(())
}

test "int64.num" {
let x = -500L
let y = 792L
test_num(x, y, x + y, x * y, x - y, x / y, -1L)?
pub fn Int64::unity() -> Int64 {
1L
}

pub fn hash(self : Int64) -> Int {
let lo = self.to_int()
let hi = self.lsr(32).to_int()
lo.lxor(hi)
}

test "misc" {
inspect(max_value(), content="9223372036854775807")?
inspect(min_value(), content="-9223372036854775808")?
inspect(unity(), content="1")?
inspect(42L.hash(), content="42")?
}
1 change: 1 addition & 0 deletions int64/int64.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn Int64::hash(Int64) -> Int
fn Int64::max_value() -> Int64
fn Int64::min_value() -> Int64
fn Int64::signum(Int64) -> Int64
fn Int64::unity() -> Int64

// Traits

Expand Down
1 change: 0 additions & 1 deletion int64/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/num",
"moonbitlang/core/assertion",
"moonbitlang/core/coverage"
]
Expand Down
40 changes: 40 additions & 0 deletions iter/consumers.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,43 @@ pub fn fold[T, B](self : Iter[T], f : (B, T) -> B, init : B) -> B {
pub fn count[T](self : Iter[T]) -> Int {
self.fold(fn { acc, _ => acc + 1 }, 0)
}

/// Calculates the sum of elements in the iterator.
///
/// Only available if the elements implements the `Default` trait and have the
/// `op_add()` function. The result is simply adding all elements sequentially
/// to the default value of the element type, which means the `sum()` of an
/// empty iterator is the default value itself.
///
/// # Examples
///
/// ```
/// let arr = [1, 2, 3, 4, 5]
/// print(arr.as_iter().sum()) // output: 15
///
/// let empty_arr: Array[Int] = []
/// print(empty_arr.as_iter().sum()) // output: 0
/// ```
pub fn sum[T : Default + @math.Add](self : Iter[T]) -> T {
self.fold(fn { acc, x => acc + x }, T::default())
}

/// Calculates the product of elements in the iterator.
///
/// Only available if the elements implements the `Unity` trait and have the
/// `op_mul()` function. The result is simply multiplying all elements
/// sequentially to the unity value of the element type, which means the
/// `product()` of an empty iterator is the unity value itself.
///
/// # Examples
///
/// ```
/// let arr = [1, 2, 3, 4, 5]
/// print(arr.as_iter().product()) // output: 120
///
/// let empty_arr: Array[Int] = []
/// print(empty_arr.as_iter().product()) // output: 1
/// ```
pub fn product[T : @math.Unity + @math.Multiply](self : Iter[T]) -> T {
self.fold(fn { acc, x => acc * x }, T::unity())
}
6 changes: 6 additions & 0 deletions iter/iter.mbti
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package moonbitlang/core/iter

alias @moonbitlang/core/math as @math

// Values

// Types and methods
type Iter
fn Iter::_unstable_internal_make[T](((T) -> Bool) -> Bool) -> Iter[T]
fn Iter::append[T](Iter[T], T) -> Iter[T]
fn Iter::ascending[T : Default + @math.Add + @math.Unity](T, T) -> Iter[T]
fn Iter::concat[T](Iter[T], Iter[T]) -> Iter[T]
fn Iter::count[T](Iter[T]) -> Int
fn Iter::descending[T : Default + @math.Subtract + @math.Unity](T, T) -> Iter[T]
fn Iter::drop[T](Iter[T], Int) -> Iter[T]
fn Iter::drop_while[T](Iter[T], (T) -> Bool) -> Iter[T]
fn Iter::empty[T]() -> Iter[T]
Expand All @@ -19,8 +23,10 @@ fn Iter::iter[T](Iter[T], (T) -> Unit) -> Unit
fn Iter::map[T, R](Iter[T], (T) -> R) -> Iter[R]
fn Iter::op_add[T](Iter[T], Iter[T]) -> Iter[T]
fn Iter::prepend[T](Iter[T], T) -> Iter[T]
fn Iter::product[T : @math.Unity + @math.Multiply](Iter[T]) -> T
fn Iter::repeat[T](T) -> Iter[T]
fn Iter::singleton[T](T) -> Iter[T]
fn Iter::sum[T : Default + @math.Add](Iter[T]) -> T
fn Iter::take[T](Iter[T], Int) -> Iter[T]
fn Iter::take_while[T](Iter[T], (T) -> Bool) -> Iter[T]
fn Iter::tap[T](Iter[T], (T) -> Unit) -> Iter[T]
Expand Down
26 changes: 26 additions & 0 deletions iter/iter_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,29 @@ fn from_array[T](arr : Array[T]) -> Iter[T] {
},
)
}

test "sum" {
inspect(repeat(1).take(5).sum(), content="5")?
}

test "product" {
inspect(repeat(2).take(8).product(), content="256")?
}

test "ascending" {
let it : Iter[Int] = ascending()
inspect(it.take(5).sum(), content="10")?
inspect(it.take(10).sum(), content="45")?
inspect(ascending(step=2).take(5).sum(), content="20")?
inspect(ascending(init=5).take(5).sum(), content="35")?
inspect(ascending(init=5, step=3).take(5).sum(), content="55")?
}

test "descending" {
let it : Iter[Int] = descending()
inspect(it.take(5).sum(), content="-10")?
inspect(it.take(10).sum(), content="-45")?
inspect(descending(step=2).take(5).sum(), content="-20")?
inspect(descending(init=5).take(5).sum(), content="15")?
inspect(descending(init=5, step=3).take(5).sum(), content="-5")?
}
2 changes: 2 additions & 0 deletions iter/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/assertion",
"moonbitlang/core/math",
"moonbitlang/core/coverage"
],
"test_import": [
"moonbitlang/core/int",
"moonbitlang/core/char"
]
}
60 changes: 60 additions & 0 deletions iter/producers.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,63 @@ pub fn Iter::repeat[T](a : T) -> Iter[T] {
},
)
}

/// Creates an iterator that produces an ascending sequence.
///
/// By default, the sequence starts from the `default()` value of the yielded
/// element type, and increases (`op_add()`) by the `unity()` value of the
/// element type. Therefore, the element type must implement the `Default`
/// trait, as well as the `@math.Add` and `@math.Unity` traits.
///
/// # Example
///
/// ```moonbit
/// let sum: Int = @iter.ascending().take(100).sum()
/// print(sum) // outputs: 4950
/// ```
pub fn Iter::ascending[T : Default + @math.Add + @math.Unity](
~init : T = T::default(),
~step : T = T::unity()
) -> Iter[T] {
Iter(
fn(yield) {
for v = init {
if yield(v) {
continue v + step
} else {
break false
}
}
},
)
}

/// Creates an iterator that produces an descending sequence.
///
/// By default, the sequence starts from the `default()` value of the yielded
/// element type, and decreases (`op_sub()`) by the `unity()` value of the
/// element type. Therefore, the element type must implement the `Default`
/// trait, as well as the `@math.Subtract` and `@math.Unity` traits.
///
/// # Example
///
/// ```moonbit
/// let sum: Int = @iter.descending().take(100).sum()
/// print(sum) // outputs: -4950
/// ```
pub fn Iter::descending[T : Default + @math.Subtract + @math.Unity](
~init : T = T::default(),
~step : T = T::unity()
) -> Iter[T] {
Iter(
fn(yield) {
for v = init {
if yield(v) {
continue v - step
} else {
break false
}
}
},
)
}
6 changes: 6 additions & 0 deletions math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Moonbit/Core Math

## Overview

A collection of common arithmetic/trigonometric functions and traits for
numerical computing.
33 changes: 33 additions & 0 deletions math/algebraic.mbt → math/arithmetic.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ test "minimum.ref" {
@assertion.assert_is(minimum(x2t, x2), x2t)?
}

pub trait Add {
op_add(Self, Self) -> Self
}

pub trait Subtract {
op_sub(Self, Self) -> Self
}

pub trait Multiply {
op_mul(Self, Self) -> Self
}

pub trait Divide {
op_div(Self, Self) -> Self
}

pub trait Negate {
op_neg(Self) -> Self
}

pub trait FromInt {
from_int(Int) -> Self
}

pub trait Num: FromInt + Add + Subtract + Multiply + Divide + Negate {
abs(Self) -> Self
signum(Self) -> Self
}

pub trait Unity {
unity() -> Self
}

// For testing purposes
struct T {
mut x : Int
Expand Down
Loading
Loading