正文
模式解构
:支持递归模式匹配
无循环结构
:通过递归和集合操作替代
类型后缀
:变量类型通过后缀声明(如
arr@list
)
智能下标
:
a[自动取中位数,
a[^n]
倒数索引
二、语法示例
1. 快速排序
qsort: (list@list) => {
[] -> []
[pivot, ...rest] -> {
rest
<> filter(_ <= pivot@)
<> qsort
<> merge (
[pivot],
rest
<> filter(_ > pivot@)
<> qsort
)
}
}
[9,3,7,5] <> qsort <> print
2. 二分查找
bsearch: (target@num, arr@list) => {
(low@num, high@num) -> {
low > high@ => -1
_ -> {
mid = (low + high)
arr[mid@] -> {
_ == target@ => mid
_ < target@ => (mid+1, high@) <> recur
_ > target@ => (low@, mid-1) <> recur
}
}
} @ (0, arr<>len-1)
}
[2,4,6,8,10] <> bsearch(6) <> print
3. KMP字符串匹配
kmp: (pattern@str, text@str) => {
pmt = pattern
<> chars@
<> scan (0, [0]@list) {
(i@num, (len, table)@tuple) -> {
len > 0 and pmt[i@] != pmt[len@]
=> (table[len-1@], table)
pmt[i@] == pmt[len@]
=> (len+1, table<>append(len+1))
_ => (0, table<>append(0))
}
} <> last
text <> chars@
<> scan (0, 0) {
(i@num, (j, pos)@tuple) -> {
j == pattern<>len => break pos - j@
chars[i@] == pmt[j@]
=> (i+1, j+1)
j > 0 => (i, pmt[j-1@])
_ => (i+1, 0)
}
} <> or(-1)
}
"ABABDABACD" <> kmp("ABAC") <> print
三、语言创新点
双向数据流:
// 传统方式
process3(process2(process1(data)))
// FlowScript方式
data <> process1 <> process2 <> process3
// 或反向执行
process3 < process2 < process1 < data
智能索引系统
:
arr = [1,3,5,7,9]
arr[// 取中位数5
arr[^1]
arr[2..// 从索引2到中间:[5,7]
递归模式匹配
:
factorial: (n@num) => {
0|1 -> 1
_ -> n * (n-1)<>factorial
}
类型后缀系统
:
price@num = 99.9
name@str = "Flow"
matrix@grid = [[1,2],[3,4]]
四、设计哲学
-
读写一致性
:代码执行方向与阅读顺序一致
-
零副作用
:所有数据操作产生新对象
-
渐进类型
:动态类型为主,关键位置可加类型约束