fuzion
v0.090
請注意,此語言正在進行中。
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中的一個重要概念:一切都是一個功能。功能是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
這個示例實現了一種簡單的算法變體,該變體找到了兩個數字中最大的常見除數。但是,它還展示了Fuzion的著名功能之一:通過合同設計。通過指定功能前和後條件,可以使正確的檢查成為可能。
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
Fuzion的另一個主要概念是代數效應- 一種以安全方式封裝代碼的新方法。
在上面的示例中,已使用自定義效果來實現具有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
您現在應該有一個名為“構建”的文件夾。
cd build
export PATH=$PWD/bin:$PATH
cd tests/rosettacode_factors_of_an_integer
fz factors
為了編譯相同的示例(需要Clang C Compiler):
fz -c factors
./factors
玩得開心!