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.||Foreach|
|Apply an effectful operation to each element in a collection.||Foreach|
|Build a new collection from existing collections.||Foreach-Yield|
|Transform the elements of a collection.||Foreach-Yield|
|Convert a collection of one type into another type.||Foreach-Yield|
|Work with ||Monadic For-Yield|
|Work with ||Applicative For-Yield|
Note: Flix does not have traditional
for-loops. Instead, we recommend the use recursion and/or one of the above constructs.