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
玩得开心!