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:
Consuming a Stream
Or we can create a stream from an integer interval and consume it
Filtering a Stream
When using a stream, we can simplify the code by applying a filter:
Printing a Stream
Since a stream provides its own print function, we can use it:
Streams using Pipe operators
The pipe operator
infix | provides an concise syntax for
streams. Filters can be applied using
Converting Stream to a String
When a stream is concatenated to a String, it is converted to a string consisting of its elements:
Embedding code in a String
Alternatively, we can embedd the stream in a string using
Finally, here is the version using streams to find the factors of all integers up to 42:
Adding features to existing features
factors feature to
i32 does not work yet: