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 intrinsic annotations to array functions #190

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/// ```
/// [1, 2, 3, 4, 5].iter(fn(x){ print("\(x) ") }) //output: 1 2 3 4 5
/// ```
/// @intrinsic %array.iter
pub fn iter[T](self : Array[T], f : (T) -> Unit) -> Unit {
for i = 0; i < self.length(); i = i + 1 {
f(self[i])
Expand Down Expand Up @@ -58,6 +59,7 @@ test "iter" {
/// print("(\(index),\(elem)) ")
/// }) //output: (0,1) (1,2) (2,3) (3,4) (4,5)
/// ```
/// @intrinsic %array.iteri
pub fn iteri[T](self : Array[T], f : (Int, T) -> Unit) -> Unit {
for i = 0; i < self.length(); i = i + 1 {
f(i, self[i])
Expand Down Expand Up @@ -156,6 +158,7 @@ test "iter_revi" {
/// let doubled = arr.map(fn(x){ x * 2 })
/// debug(doubled) //output: [2, 4, 6, 8, 10]
/// ```
/// @intrinsic %array.map
pub fn map[T, U](self : Array[T], f : (T) -> U) -> Array[U] {
if self.length() == 0 {
return []
Expand Down Expand Up @@ -267,6 +270,7 @@ test "from_array" {
/// let sum = [1, 2, 3, 4, 5].fold_left(~init=0, fn { sum, elem => sum + elem })
/// sum // 15
/// ```
/// @intrinsic %array.fold_left
pub fn fold_left[T, U](self : Array[T], f : (U, T) -> U, ~init : U) -> U {
for i = 0, acc = init; i < self.length(); {
continue i + 1, f(acc, self[i])
Expand Down
Loading