Skip to content

Feat: Implement Lambda Functions and Callable Interface #39

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

quaaantumdev
Copy link

This pull request introduces support for lambda functions and the underlying Callable interface that makes them possible. It closes #38. Regular functions are still fully supported, no change there.

This pull request's code should only be checked once #36 is merged, as both of them modify the lib/src/parser.dart and this pull request is already built on the changes introduced by #36. So looking at the code before the other pull request is merged will also show changes from the other pull request.

The Callable Interface

A new abstract class, Callable, has been introduced to act as a formal contract for function-like objects. When evaluating a call expression, the evaluator now checks if the callee is a Callable.

This is a worthwhile addition in its own right, as it allows users to implement custom functions that can validate their arguments and throw descriptive exceptions for parameter mismatches, which was not possible with the previous Function.apply approach.

Lambda Functions

This PR adds parser support for lambda functions using the common arrow syntax: (p1, p2) => body_expression.
Evaluating a lambda expression now creates an internal Callable object that captures its defining context (lexical scope). This simple enhancement unlocks powerful new capabilities, allowing for expressive collection-based operations directly in the expression language.

  • Filtering: [1, 9, 2, 5, 3, 2].where((e) => e > 2)
  • Mapping: [1, 2, 3].map((e) => e * 2)

While the lambda receives its own arguments, it also retains access to the context in which it was created, allowing for complex expressions that mix local parameters with external variables.

The README.md and tests have been updated to reflect these powerful new features. We also put the example code of the README.md into appropriate code blocks to enable syntax highlighting on Github.

This pull request solves #38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Lambda Functions and Custom Callables
1 participant