diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2016-09-27 09:07:49 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2016-09-27 09:07:53 -0700 |
commit | 56e00e88789f685493002266773bad25e7740874 (patch) | |
tree | fae611e8333a9f3c71e1934b8d353d83c003a2b0 /063array.mu | |
parent | fb782b6380eb02d947851350e67747ce6126879b (diff) | |
download | mu-56e00e88789f685493002266773bad25e7740874.tar.gz |
3418 - some functions contributed by Caleb Couch
Diffstat (limited to '063array.mu')
-rw-r--r-- | 063array.mu | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/063array.mu b/063array.mu index 205e03e9..c4f732d3 100644 --- a/063array.mu +++ b/063array.mu @@ -39,3 +39,144 @@ def new-array -> result:&:@:char [ } return result ] + +# fill an existing array with a set of numbers +# (contributed by Caleb Couch) +recipe fill array:&:@:num -> array:&:@:num [ + local-scope + load-ingredients + loopn:num <- copy 0 + length:num <- length *array + { + length?:bool <- equal loopn, length + break-if length? + object:num, arg-received?:bool <- next-ingredient + break-unless arg-received? + *array <- put-index *array, loopn, object + loopn <- add loopn, 1 + loop + } +] + +scenario fill-on-an-empty-array [ + run [ + local-scope + array:&:@:num <- new number:type, 3 + array <- fill array, 1 2 3 + 10:@:num/raw <- copy *array + ] + memory-should-contain [ + 10 <- 3 + 11 <- 1 + 12 <- 2 + 13 <- 3 + ] +] + +scenario fill-overwrites-existing-values [ + run [ + local-scope + array:&:@:num <- new number:type, 3 + *array <- put-index *array, 0, 4 + array <- fill array, 1 2 3 + 10:@:num/raw <- copy *array + ] + memory-should-contain [ + 10 <- 3 + 11 <- 1 + 12 <- 2 + 13 <- 3 + ] +] + +scenario fill-exits-gracefully-when-given-no-ingredients [ + run [ + local-scope + array:&:@:num <- new number:type, 3 + array <- fill array + 10:@:num/raw <- copy *array + ] + memory-should-contain [ + 10 <- 3 + 11 <- 0 + 12 <- 0 + 13 <- 0 + ] +] + +# swap two elements of an array +# (contributed by Caleb Couch) +recipe swap array:&:@:num, index1:num, index2:num -> array:&:@:num [ + local-scope + load-ingredients + object1:num <- index *array, index1 + object2:num <- index *array, index2 + *array <- put-index *array, index1, object2 + *array <- put-index *array, index2, object1 +] + +scenario swap-works [ + run [ + local-scope + array:&:@:num <- new number:type, 4 + array <- fill array, 4 3 2 1 + array <- swap array, 0, 2 + 10:num/raw <- index *array, 0 + 11:num/raw <- index *array, 2 + ] + memory-should-contain [ + 10 <- 2 + 11 <- 4 + ] +] + +# reverse the elements of an array +# (contributed by Caleb Couch) +def reverse array:&:@:_elem -> array:&:@:_elem [ + local-scope + load-ingredients + start:num <- copy 0 + length:num <- length *array + end:num <- subtract length, 1 + { + done?:bool <- greater-or-equal start, end + break-if done? + array <- swap array, start, end + start <- add start, 1 + end <- subtract end, 1 + loop + } +] + +scenario reverse-array-odd-length [ + run [ + local-scope + array:&:@:num <- new number:type, 3 + array <- fill array, 3 2 1 + array <- reverse array + 10:@:num/raw <- copy *array + ] + memory-should-contain [ + 10 <- 3 + 11 <- 1 + 12 <- 2 + 13 <- 3 + ] +] + +scenario reverse-array-even-length [ + run [ + local-scope + array:&:@:num <- new number:type, 4 + array <- fill array, 4 3 2 1 + array <- reverse array + 10:@:num/raw <- copy *array + ] + memory-should-contain [ + 10 <- 4 + 11 <- 1 + 12 <- 2 + 13 <- 3 + 14 <- 4 + ] +] |