diff options
-rw-r--r-- | 063array.mu | 141 | ||||
-rw-r--r-- | 064list.mu | 28 |
2 files changed, 169 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 + ] +] diff --git a/064list.mu b/064list.mu index 9216ce9b..13fc581e 100644 --- a/064list.mu +++ b/064list.mu @@ -258,6 +258,34 @@ scenario removing-from-singleton-list [ ] ] +# reverse the elements of a list +# (contributed by Caleb Couch) +def reverse list:&:list:_elem temp:&:list:_elem -> result:&:list:_elem [ + local-scope + load-ingredients + reply-unless list, temp + object:_elem <- first, list + list <- rest list + temp <- push object, temp + result <- reverse list, temp +] + +scenario reverse-list [ + run [ + local-scope + list:&:list:number <- push 1, 0 + list <- push 2, list + list <- push 3, list + stash [list:], list + list <- reverse list, 0 + stash [reversed:], list + ] + trace-should-contain [ + app: list: 3 -> 2 -> 1 + app: reversed: 1 -> 2 -> 3 + ] +] + def to-text in:&:list:_elem -> result:text [ local-scope load-ingredients |