Rosetta Code Factors of an Integer ExampleFrom rosettacode.org:
[The following task description was taken from rosettacode.org:]
Compute the factors of a positive integer.
These factors are the positive integers by which the number being factored can be divided to yield a positive integer result.
(Though the concepts function correctly for zero and negative integers, the set of factors of zero has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty; this task does not require handling of either of these cases).
Note that every prime number has two factors: 1 and itself.
Using a loop
Code to find the factors using a loop in Fuzion would look like this:
Iterating over an Interval
Very similarly, we can iterate over an interval using
a lambda expression:
When iterating over an interval, we can appy a filter to extract the factors we want to print:
Printing a Sequence
Since Sequence provides its own print function, we can use it:
Iterating Sequences using Pipe operators
The pipe operator
infix | provides an concise syntax to iterate
over a Sequence.
Filters can be applied using
Converting Sequence to a string
When a Sequence can be concatenated to a string, it is converted to a string consisting of its elements:
Embedding code in a String
Alternatively, we can embedd the expression in a string using
Finally, here is the version using a filter embedded in a string to find the factors of all integers up to 42:
Creating a routine
We can put the code into a routine
factors and then call it
given the argument
val is a field of type
i32, so this gives a means to access
i32 value. One might think that
i32.this could be
used as well, but this is not possible since the type of
i32, but it could be any subtype of
i32 in a
feature inheriting from
Adding features to library feature i32
Alternatively, we can extend the standard library and add a feature
factors as inner feature of