Idiom # 284: Create a zeroed list of integers
See programming-idioms.org :
Code
Runnable Example
Fuzion has lazy evaluation for lists, so if only a small part of the list is used, the following definition is more efficient than an array, for example.
Code input
ex284 is
n := 5
a := [0].cycle.take n
say a
Here are some more ways to achieve the same as well as definition using an array.
Code input
ex284 is
n := 5
# Define a list of one element, cycle it then take n elements.
a := (0 : nil).cycle.take n
say a
# First define a list with one element and a generator function for successive elements. Then take n elements.
b := (0 :: x->x).take n
say b
# Recursive definition of the infinite list with only 0 as element.
zeros => 0 : zeros
c := zeros.take n
say c
# The standard list type in Fuzion is Sequence, array inherits from Sequence,
# so the following does also work, but it doesn't have lazy evaluation
# so performance can be worse
d := array n i->0
say d
last changed: 2025-05-13