From 5cf4bb1d95e22a46ded620e6d40cdbb14bfb1a28 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 27 Sep 2016 09:24:36 -0700 Subject: 3420 --- html/063array.mu.html | 155 +++++++++++++++++++++++++++++++++++++++++++++++--- html/064list.mu.html | 28 +++++++++ 2 files changed, 176 insertions(+), 7 deletions(-) diff --git a/html/063array.mu.html b/html/063array.mu.html index 9b32f88d..48d3301a 100644 --- a/html/063array.mu.html +++ b/html/063array.mu.html @@ -34,8 +34,8 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color scenario array-from-args [ run [ local-scope - x:text <- new-array 0, 1, 2 - 10:@:char/raw <- copy *x + x:&:@:num <- new-array 0, 1, 2 + 10:@:num/raw <- copy *x ] memory-should-contain [ 10 <- 3 # array length @@ -45,25 +45,25 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color ] ] -# create an array out of a list of scalar args -def new-array -> result:text [ +# create an array out of a list of args +def new-array -> result:&:@:_elem [ local-scope capacity:num <- copy 0 { # while read curr-value - curr-value:char, exists?:bool <- next-ingredient + curr-value:_elem, exists?:bool <- next-ingredient break-unless exists? capacity <- add capacity, 1 loop } - result <- new character:type, capacity + result <- new _elem:type, capacity rewind-ingredients i:num <- copy 0 { # while read curr-value done?:bool <- greater-or-equal i, capacity break-if done? - curr-value:char, exists?:bool <- next-ingredient + curr-value:_elem, exists?:bool <- next-ingredient assert exists?, [error in rewinding ingredients to new-array] *result <- put-index *result, i, curr-value i <- add i, 1 @@ -71,6 +71,147 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color } 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/html/064list.mu.html b/html/064list.mu.html index b9e0e18d..236bf533 100644 --- a/html/064list.mu.html +++ b/html/064list.mu.html @@ -292,6 +292,34 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color ] ] +# 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 -- cgit 1.4.1-2-gfad0