この言語は進行中の作業であることに注意してください。
hello_world is
# first we define a custom mutate effect.
# we will need this for buffered reading from stdin
#
lm : mutate is
# calling `lm` creates an instance of our mutate effect,
# `instate_self` is then used to instate this instance and
# run code in the context of the instated effect.
#
lm ! ()->
# read someone's name from standard input
#
get_name =>
(io.stdin lm) ! ()->
io.buffered.read_line lm ? str String => str | io.end_of_file => ""
# greet someone with the name given
#
greet(name String) is
say "Hello, {name}!"
# greet the user
#
x := greet get_name
# you can access any feature - even argument features of other features
# from outside
#
say "How are you, {x.name}?"
このhello_world例は、Fuzionの1つの重要な概念を非常によく示しています。すべてが機能です。機能は、クラス、方法、インターフェイス、および他のプログラミング言語の他のさまざまな概念によって作成される混乱に対するFuzionの応答です。すべてが機能であるため、プログラマーは気にする必要はなく、コンパイラはこの作業を行います。ご覧のとおり、外部からいくつかの機能の引数機能にアクセスすることも可能です。
ex_gcd is
# return common divisors of a and b
#
common_divisors_of(a, b i32) =>
max := max a.abs b.abs
(1..max).flat_map i32 i->
if (a % i = 0) && (b % i = 0)
[-i, i]
else
[]
# find the greatest common divisor of a and b
#
gcd(a, b i32)
pre
safety: (a != 0 || b != 0)
post
safety: a % result = 0
safety: b % result = 0
pedantic: (common_divisors_of a b).reduce bool true (acc,cur -> acc && (result % cur = 0))
=>
if b = 0 then a else gcd b (a % b)
say <| gcd 8 12
say <| gcd -8 12
say <| gcd 28 0
この例は、2つの数字の最大の共通除数を見つけるアルゴリズムの単純なバリアントを実装しています。ただし、Fuzionの注目すべき機能の1つである契約による設計も示しています。機能の前後の条件を指定することにより、正確なチェックが可能になります。
generator_effect is
# define a generator effect with a yield operation
#
gen(T type,
yield T->unit # yield is called by code to yield values
) : effect is
# traverse a list and yield the elements
#
list.traverse unit =>
match list.this
c Cons => (generator_effect.gen A).env.yield c.head; c.tail.traverse
nil =>
# bind the yield operation dynamically
#
(gen i32 (i -> say "yielded $i")) ! ()->
[0,8,15].as_list.traverse
フジオンのもう1つの主要な概念は、代数効果の概念です。これは、副作用を伴うコードを安全な方法でカプセル化するための新しいアプローチです。
上記の例では、 yield操作を備えたジェネレーターを実装するためにカスタム効果が使用されています。他のいくつかの言語では、これには言語によってキーワードのyieldを提供する必要がありますが、Fuzionでは言語サポートなしで実装できます。
Fuzionで遊んでみたい場合は、インタラクティブなチュートリアルをお試しください。
言語と実装の設計については、fuzion-lang.devを確認してください。
現在のディレクトリにはスペースが含まれていないことに注意してください。
git clone https://github.com/tokiwa-software/fuzion
Debianベースのシステムの場合、このコマンドはすべての要件をインストールする必要があります。
sudo apt-get install make clang libgc1 libgc-dev openjdk-21-jdk
このコマンドはすべての要件をインストールする必要があります。
brew install bdw-gc gnu-sed make temurin llvmさらに、パス環境変数を更新する必要がある場合があります。
export PATH:"/usr/local/opt/gnu-sed/libexec/gnubin:/usr/local/opt/gnu-make/libexec/gnubin:$PATH"
PowerShell/CMDからの建物はまだ機能していないことに注意してください。
Java/JavacとClangがあなたの$パスにあることを確認してください。
cd fuzion
make
今すぐBuildというフォルダーが必要です。
cd build
export PATH=$PWD/bin:$PATH
cd tests/rosettacode_factors_of_an_integer
fz factors
同じ例をコンパイルするには(Clang Cコンパイラが必要です)。
fz -c factors
./factors
楽しむ!