Lutz Hühnken's Flix Examples ↩
Directory lutz-examples\ contains Flix code examples presented in Lutz Hühnken's article "Flix for Java programmers" (June 2022).
|
🔎 Visit the Codeberg repository
2022-06-24-flix-for-java-programmers
to get the original Flix code examples.
We made the following changes resp. additions in comparison to the original Flix code examples :
- The source files have been updated 1 to successfully compile with the latest version of the Flix compiler.
- Each example has the same directory layout as the Flix code examples presented in document
examples\README.md
. - Each example includes a basic test suite (directory
src\test\
). - We provide several build scripts to build, run and test the Flix code examples.
Currently we provide four ways to build, run and test the Flix code examples :
Build tool | Build file | Parent file | Environment(s) |
---|---|---|---|
cmd.exe |
build.bat |
MS Windows | |
sh.exe |
build.sh |
Any a) | |
gradle.exe |
build.gradle |
common.gradle |
Any |
make.exe |
Makefile |
Makefile.inc |
Any |
channels
Example ▴
This project has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | build.sh | build.xml | flix.toml | gradle.properties | Makefile \---src +---main | Main.flix \---test TestMain.flix
Command build
-verbose run
generates and executes the target file channels.jar
:
> build -verbose run Initialize Flix project directory "target\channels" Copy 1 Flix source file to directory "target\channels\src\" Compile 1 Flix source file into directory "target\channels\build\" Create archive file "target\channels\channels.jar" Extract Flix runtime from archive file "%FLIX_HOME%\flix.jar" Add Flix runtime to archive file "target\channels\channels.jar" Execute Flix program "target\channels\channels.jar" 29 39 [...] 72 35 done
Command build -verbose test
generates the target file target\channels\channels.jar
from the Flix sources files src\main\channels.flix
and src\test\channelsTest.flix
and runs the test suite :
> build -verbose test Copy 1 Flix source file to directory "target\channels\src\" Copy 1 Flix test source file to directory "target\channels\test\" Compile 1 Flix source file and 1 Flix test source file Create archive file "target\channels\channels.jar" Extract Flix runtime from archive file "C:\opt\flix-0.43.0\flix.jar" Add Flix runtime to archive file "target\channels\channels.jar" Execute tests for Flix program "target\channels\channels.jar" Running 1 tests... PASS test01 384,1us Passed: 1, Failed: 0. Skipped: 0. Elapsed: 6,9ms.
🔎 Command
build.sh
is the counterpart of the above batch filebuild.bat
for Unix-like environments; for instance :> bash -c "./build.sh -verbose run" Initialize directory "target/channels" Copy Flix source files to directory "target/channels/src" Copy Flix test source files to directory "target/channels/test" Compile 1 Flix source files to directory "target/channels/build" Create archive file "target/channels/channels.jar" Extract class files from archive file "C:/opt/flix-0.43.0/flix.jar" Update archive file "target/channels/channels.jar" Execute the JAR file "target/channels/channels.jar" 20 ... 7 done > bash --version | findstr version GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu) License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
datalog
Example ▴
Command make run
(Makefile
/Makefile.inc
) generates the target file target\datalog\datalog.jar
from the Flix source file src\main\datalog.flix
and executes the target file :
> make run [ -d "target/datalog" ] || "%GIT_HOME%/usr/bin/mkdir.exe" -p "target/datalog" cd "target/datalog"; \ [ -d "build" ] || "%JAVA_HOME%/bin/java.exe" -jar "%FLIX_HOME%/flix.jar" init && \ "%GIT_HOME%/usr/bin/rm.exe" -f "src/Main.flix" && \ "%GIT_HOME%/usr/bin/cp.exe" -r "F:/lutz-examples/datalog/src/main/." src && \ "%JAVA_HOME%/bin/java.exe" -jar "%FLIX_HOME%/flix.jar" build && \ "%JAVA_HOME%/bin/java.exe" -jar "%FLIX_HOME%/flix.jar" build-jar "%JAVA_HOME%/bin/java.exe" -jar "target/datalog/datalog.jar" Children of Zeus = Apollo :: Ares :: Nil Ancestors of Apollo = Kronos :: Leto :: Rhea :: Zeus :: Nil 0
This example has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | build.sh | build.xml | gradle.properties | Makefile \---src +---main | effects.flix \---test effectsTest.flix
Command gradle
-q run
– given the configuration build.gradle
(resp. common.gradle
) – generates and executes the target file target\effects\effects.jar
:
> gradle -q run 2 1 2 3 4 2 4 6 8 0
interop
Example ▴
This example has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | build.sh | build.xml | gradle.properties | Makefile \---src +---main | interop.flix \---test interopTest.flix
Command build run
generates the target file target\interop\interop.jar
from the Flix source file src\main\interop.flix
and executes the target file :
> build run false 0
This example has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | build.sh | build.xml | gradle.properties | Makefile \---src +---main | mutualrecursion.flix | MutualRecursion.java \---test mutualrecursionTest.flix
Command make
-s run
– given the configuration Makefile
(resp. Makefile.inc
) – generates and executes the target file target\mutualrecursion\mutualrecursion.jar
:
> make -s run true 0
🔎 Command
build.bat
features option-java
to compile/run the Java version of this code example.> build -java -verbose run Compile Java source files to directory "target\classes" Execute Java program "MutualRecursion" true
random
Example ▴
This example has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | build.sh | build.xml | gradle.properties | Makefile \---src +---main | random.flix \---test randomTest.flix
Command gradle
-q run
– given the configuration build.gradle
(resp. common.gradle
) – generates and executes the target file target\random\random.jar
:
> gradle -q run 109 169 [...] 192 124 0
This example has the following directory structure :
> tree /f /a . | findstr /v /b [A-Z] | build.bat | build.gradle | build.sh | build.xml | gradle.properties | Makefile \---src +---main | tailcalls.flix | tailcalls.java \---test tailcallsTest.flix
Command build.bat
run
builds and generates the target file target\tailcalls\tailcalls.jar
:
> build run 312512500 0
🔎 The same example written in Java (
src\main\tailcalls.java
) fails with a runtime error :> build -java run Exception in thread "main" java.lang.StackOverflowError at Tailcalls.tailsum(tailcalls.java:12) at Tailcalls.tailsum(tailcalls.java:12) [...] at Tailcalls.tailsum(tailcalls.java:12) at Tailcalls.tailsum(tailcalls.java:12) Error: Failed to execute Java program "Tailcalls"
Footnotes▴
[1] Updated source files ↩
-
Here are our modifications of the original Flix code examples from Lutz Hühnken's article "Flix for Java programmers" (June 2022) :
Source file(s) Source code change *.flix
Old syntax: & Impure
, new syntax:\ IO
.channels.flix
random.flix
Changed return type Unit & Impure
toUnit \ NonDet
for functionsdoSomething
andmain
(see definition ofRandom.nextInt64
).