Menu

Goose: Push into an Array, Pass It to a Function
📰

Goose: Push into an Array, Pass It to a Function

DEV Community: docker·flisk·about 4 hours ago
#0UgJ5MBC
#devtodocker#devto#dev#grow#fullscreen
Reading 0:00
15s threshold

Part 3 of Goose, then any language on Flisk. This is the program part 4 will compile into an image. The function is the one from part 2.

shop.goose :

fn total(xs: i64[:]) -> i64 { var t = 0; for x in xs { t += x; } return t; }

fn main() { var grow: i64[>..] = []; for i in 5 { grow.push(i * i); } print(grow); print(total(grow)); print(total(grow[1..4])); }

Enter fullscreen mode

Exit fullscreen mode

goose shop.goose

Enter fullscreen mode

Exit fullscreen mode

for i in 5 is 0, 1, 2, 3, 4 . push appends. i64[>..] is grow-only: references into it stay valid across push . That is the array you want until you have a reason to pick another spelling.

grow[1..4] is a slice, exclusive at the end. total accepts the whole array and the slice, because both become i64[:] . grow[^2..] is the last two elements.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.