實踐中發現如下運算經常用到:
a + b + c +d
a * b * c * d
連減也偶有見
a - b - c - d
連除絕少遇見
a / b / c / d
而目前的“加……以……”或“加……於……”似乎只適於二元運算,欲表達如上同算子的鏈式運算頗有些煩瑣。所以參考“列”之“充”算子,或可寫作如下:
充‘列’以一以二以三……
加‘甲’以一以二以三……
乘‘甲’以一以二以三……
如果數學式中包含多種混合運算,則仍建議拆分條語句,表達更清晰。
支持類似鏈式的,不妨稱之為鏈式算子,可考慮再加一字標記結束,如“兮”,然非必需。
充‘列’以一以二以三兮
加‘甲’以一以二以三兮
乘‘甲’以一以二以三兮
另外一種常見需求,自賦值運算符如 += *=,亦有一提議如:
夫‘甲’,乃加以一 // i++
夫‘甲’,乃加以二 // i += 2
夫‘甲’,乃加以一以二以三 // i += (1+2+3)
用“夫”字表示堆棧,很好。只不過按若自然語言思考,只追憶棧頂一個元素時絕妙,“取二” 以上理解難度開始加大。
君以為如何?
鏈式算子“充”還可以擴展到修改“物”類型,例如:
充‘某物’以名“甲”曰三。
充‘某物’
以名“甲”曰三、
以名“乙”曰陽、
以名“丙”曰‘某已定義變量’
兮
因為從某種(實現)角度觀之,列(array)與物(object)可視為同源。
變量有名有實(值),在大多語境下,我們更關注其實,“曰”可理解為“實曰”的省略。
所以“列”之諸項只有實,“物”之諸項,是“名實”對 (key - value pair)
思及此,我更傾向於將純數據的 map/dict/object 譯為“表”,與“列”亦可合稱為一詞“列表”,可達其意。
不過“物”更適於“有術”之對象,而不僅限於數據。
吾有五數。曰一曰二曰三曰四。名之曰「甲」曰「乙」曰「丙」曰「丁」曰「戊」。
夫「甲」。乘其以「乙」。乘其以「丙」。乘其以「丁」。書之。
夫「甲」。加其以「乙」。加其以「丙」。加其以「丁」。書之。
夫「甲」。加其以「乙」。加其以「丙」。加其以「丁」。昔之「戊」者。今其是矣。
@cuixiping that's exactly the correct way of chaining operators, thanks for the example!
@lymslive now that wenyan-lang is more type strict, 物 will have a fixed signature and will be more like structs in C/C++, where new properties should not be added at runtime. For that, there will be a new Map or Dict like data structure in STDLIB, where serializable keys maps to a given type, e.g. Map<string,int> or Map<int,bool>. Thanks for bringing this up!
@cuixiping
多谢示例。
@LingDong-
其实我之前一直在琢磨,“充”这个词到底算是“运算符”还是应该来自 stdlib 列经的操作函数。
然后我也喜欢“充”这个句式,就希望能迁移到其他操作上。
从认知论上讲,就希望用户能举一反三,对于相似的概念与操作,若能用相似的语法就更好。
另外,从一开始看“快排”的例子,我就觉得“其余”只特定表示 slice(1) ,这似乎太特殊了,还绑定了一个参数
@lymslive good questions,
The idea behind putting certain things in the syntax and certain things in stdlib is that, we'll have just enough syntax as a bootstrap, so the stdlib can be implemented without resorting to target-specific code (i.e. JS) and without having massive performance compromise. 充, 銜, 其餘, 其一二三四⋯⋯ are exactly for that.
其餘 is a standard from the functional programming world, equivalent to cdr/rest in lisp and tl in SML. It is handy for many recursive functions, where you specify what to do with 其一, and call the function again on the rest of the elements. The key concept is, every bunch of things can be divided into the first thing, and the rest of the things, and the rest of the things can also be divided into the first of its things, and the rest of the rest of the things. It's a pretty neat idea that deserves a syntax if you think about it.
其餘 does not see much usage in stdlib because most major browsers do not support tail-call optimization yet. But in theory the syntax of wenyan should not be limited by what JS lacks, or, maybe we can even add optimization in the wenyan compiler so that it unrolls tail-calls into loops.
Thanks!
I have released a wyg package 連加連乘 and that supports convenient chained operator. Check out that.
Most helpful comment
I have released a wyg package 連加連乘 and that supports convenient chained operator. Check out that.