From 568b87527c798766998373042352c9fb7335088f Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 15 Apr 2021 22:10:17 -0500 Subject: [PATCH] added some more array functionality --- core/array.onyx | 50 ++++++++++++++++++++---------- examples/17_operator_overload.onyx | 44 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 examples/17_operator_overload.onyx diff --git a/core/array.onyx b/core/array.onyx index da196732..20f5ac38 100644 --- a/core/array.onyx +++ b/core/array.onyx @@ -127,16 +127,39 @@ fast_delete :: (arr: ^[..] $T, idx: u32) { arr.count -= 1; } -contains :: (arr: ^[..] $T, x: T) -> bool { - for it: *arr do if it == x do return true; - return false; -} - pop :: (arr: ^[..] $T) -> T { arr.count -= 1; return arr.data[arr.count]; } +// Uses '==' to compare for equality. +contains :: proc { + (arr: ^[..] $T, x: T) -> bool { + for it: *arr do if it == x do return true; + return false; + }, + + (arr: [] $T, x: T) -> bool { + for it: arr do if it == x do return true; + return false; + } +} + +// Uses '+' to sum. +sum :: proc { + (arr: ^[..] $T, start: T = 0) -> T { + sum := start; + for it: *arr do sum += it; + return sum; + }, + + (arr: [] $T, start: T = 0) -> T { + sum := start; + for it: arr do sum += it; + return sum; + } +} + average :: (arr: ^[..] $T) -> T { sum := cast(T) 0; for it: *arr do sum += it; @@ -181,17 +204,12 @@ fold :: proc { } map :: proc { - (arr: ^[..] $T, f: (T) -> T) { - for ^it: *arr do *it = f(*it); - }, - - (arr: ^[..] $T, f: (^T) -> void) { - for ^it: *arr do f(it); - }, - - (arr: ^[..] $T, data: $R, f: (T, R) -> T) { - for ^it: *arr do *it = f(*it, data); - }, + (arr: ^[..] $T, f: (^T) -> void) do for ^it: *arr do f(it);, + (arr: ^[..] $T, f: (T) -> T) do for ^it: *arr do *it = f(*it);, + (arr: ^[..] $T, data: $R, f: (T, R) -> T) do for ^it: *arr do *it = f(*it, data);, + (arr: [] $T, f: (^T) -> void) do for ^it: arr do f(it);, + (arr: [] $T, f: (T) -> T) do for ^it: arr do *it = f(*it);, + (arr: [] $T, data: $R, f: (T, R) -> T) do for ^it: arr do *it = f(*it, data);, } #private_file diff --git a/examples/17_operator_overload.onyx b/examples/17_operator_overload.onyx new file mode 100644 index 00000000..49f5e303 --- /dev/null +++ b/examples/17_operator_overload.onyx @@ -0,0 +1,44 @@ +#load "core/std" +use package core + +// Operator overloading allows you to define what it means to perform +// a binary operation between two types. In Onyx, they are defined in +// the following way: +// +// #operator +// +// where is the binary operator, and is an expression +// that should be a procedure that will be used for the overload. +// + +// Take this example. Note, DO NOT actually use this exact implementation +// because it leaks memory all over the place. Especially because with +// a binary operator, you cannot pass a custom allocator. + +#operator + (x: str, y: str) -> str { + return string.concat(x, y); +} + +main :: (args: [] cstr) { + // Now we can say "+" between two strings. Also, we can use it + // to give us access to more features like array.sum which adds + // everything in the array together using the '+' operator. + // Since '+' is concatenate, array.sum will join all the strings + // into one, in one of the most inefficient ways possible. + + test := "Hello, " + "World!"; + println(test); + + strings := str.[ "This ", "is ", "a ", "test." ]; + result := array.sum(cast([] str) strings, start=""); + println(result); + + // As a side note, '==' is already overloaded for strings in the + // standard library, so you can do this: + + if array.contains(cast([] str) strings, "is ") { + println("The array contains 'is '."); + } else { + println("The array does NOT contain 'is '."); + } +} -- 2.25.1