โปรดทราบว่าภาษานี้กำลังดำเนินการอยู่
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: การออกแบบตามสัญญา โดยการระบุคุณสมบัติก่อนและ postconditions สำหรับคุณสมบัติการตรวจสอบความถูกต้องจะเกิดขึ้นได้
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
คุณควรมีโฟลเดอร์ที่เรียกว่า Build ตอนนี้
cd build
export PATH=$PWD/bin:$PATH
cd tests/rosettacode_factors_of_an_integer
fz factors
ในการรวบรวมตัวอย่างเดียวกัน (ต้องใช้คอมไพเลอร์ clang c):
fz -c factors
./factors
มีความสุข!