Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Feature Request] Support lambda expression #99

Open
sicusa opened this issue Dec 18, 2019 · 0 comments
Open

[Feature Request] Support lambda expression #99

sicusa opened this issue Dec 18, 2019 · 0 comments
Labels
syntax Suggestions to improve the syntax

Comments

@sicusa
Copy link

sicusa commented Dec 18, 2019

This enables developers to write 文言 in functional programming style, which increases its interoperability with modern frameworks and makes the language much more expressive.

The simplest way to achieve this is to use the original grammar for named functions, with slight modification:

First, let's define 「周立」。

吾有一術。名之曰「周立」。欲行是術。必先得一列。曰「甲」。一術。曰「转」。乃行是術曰。
吾有一列。名之曰「乙」。
凡「甲」中之「物」。
施「转」於「物」。名之曰「新」。充「乙」以「新」。
云云。
乃得「乙」。
是謂「周立」之術也。

Then use it with lambda expression:

吾有一列。名之曰「甲」。充「甲」以一。以二。以三。
吾有一列。名之曰「果」。欲得此列。施「周立」
於「甲」。
於術。欲行是術。必先得一數。曰「甲」。乃行是術曰。
加「甲」以一。名之曰「乙」。乃得「乙」也。

Notice that for better readability, "也" is used to indicate the end of the lambda expression, and "名之曰" is replaced by "吾有一列。名之曰「果」。欲得此列" to receive the result of function invocation.

But the way function arguments are specified is too tedious to be used in such functional style. Therefore, I propose a "specify-on-use" solution:

If function arguments are not specified beforehand, the compiler tracks appearances of "argument placeholders" with the form of “某” + type in the function body. For example, "某数",“某列”,“某言”. Then for each of them, the compiler appends a new argument to the argument list, and replaces the placeholder with the actual reference to that argument.

This grammar can also be used in ordinary functions:

吾有一術。名之曰「周立」。是術曰。
吾有一列。名之曰「乙」。
凡某列中之「物」。
施某術於「物」。名之曰「新」。充「乙」以「新」。
云云。
乃得「乙」。
是謂「周立」之術也。

The compiler sees "某列" first, so it knows that the first argument of 「周立」 is an array, and it replaces the "某列" with the reference to that argument. So is "某術". Thus the generated code is identical to the original version.

Of course we should use it cautiously, since not specifying function arguments beforehand would cause a lot of confusion for large functions. On the other hand, it can be a perfect way for reducing boilerplate when used in lambda expressions, which are generally very short:

吾有一列。名之曰「甲」。充「甲」以一。以二。以三。
吾有一列。名之曰「果」。欲得此列。施「周立」
於「甲」。
於術。曰加某数以一。名之曰「乙」。乃得「乙」也。

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
syntax Suggestions to improve the syntax
Projects
None yet
Development

No branches or pull requests

2 participants