Skip to content
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())
}
4 changes: 4 additions & 0 deletions iter/iter.mbti
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package moonbitlang/core/iter

alias @moonbitlang/core/math as @math

// Values

// Types and methods
Expand All @@ -19,8 +21,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
8 changes: 8 additions & 0 deletions iter/iter_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,11 @@ 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")?
}
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"
]
}
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
32 changes: 32 additions & 0 deletions math/math.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ fn T::debug_write(T, Buffer) -> Unit
fn T::op_equal(T, T) -> Bool

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

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

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

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

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

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

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

pub trait Unity {
unity() -> Self
}

// Extension Methods

6 changes: 5 additions & 1 deletion math/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"moonbitlang/core/assertion",
"moonbitlang/core/result",
"moonbitlang/core/double",
"moonbitlang/core/string",
"moonbitlang/core/coverage"
],
"test_import": [
"moonbitlang/core/double",
"moonbitlang/core/int",
"moonbitlang/core/int64"
]
}
49 changes: 49 additions & 0 deletions math/num_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn test_num[T : 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 "int.num" {
let x = -5
let y = 7
test_num(x, y, x + y, x * y, x - y, x / y, -1)?
}

test "int64.num" {
let x = -500L
let y = 792L
test_num(x, y, x + y, x * y, x - y, x / y, -1L)?
}
6 changes: 0 additions & 6 deletions num/moon.pkg.json

This file was deleted.

Loading