Flix — being a functional programming language — has few control-structures. Most control is simply function application. The Flix control structures are:
- If-Then-Else: A traditional if-then-else expression.
- Pattern Matching: A functional construct for taking apart algebraic data types.
- Foreach: An imperative construct for iteration through collections.
- Foreach-Yield: An imperative construct for building new collections from existing collections.
- Monadic For-Yield: A functional construct for
monadic operations, similar to Scala's
for-comprehensions and Haskell's
- Applicative For-Yield: A functional construct
for applicative operations, similar to Haskell's applicative
What's the difference between
forM, and applicative
The following table gives some uses cases for each construct:
|Print all elements in a collection.
|Apply an effectful operation to each element in a collection.
|Build a new collection from existing collections.
|Transform the elements of a collection.
|Convert a collection of one type into another type.
flatMap through a
Note: Flix does not have traditional
for-loops. Instead, we recommend the use recursion and/or one of the above constructs.