-
Notifications
You must be signed in to change notification settings - Fork 9
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
Use symbol ! as factorial #11
Comments
Thanks for your interest in this library. Sorry for the delay in response! I'm a little wary of adding support for factorials because they blow up so quickly and don't really fit into the context of However, we could implement this by first adding a function definition to .
.
.
/// A mapping of string representations to functions.
final Map<String, num Function(num)> oneParameterFunctionMap = {
"abs": (num x) => x.abs(),
.
.
.
// Add this code: -----------------------------------------------------
"fact": (num x) =>
[for (var i = 1; i <= x; i++) i].fold<double>(1.0, (a, b) => a * b),
// --------------------------------------------------------------- snip!
.
.
. and then replacing expressions of the form /// Returns a sanitized version of `expression`.
String cleanExpression(String expression) {
final junk = RegExp(r"[^0-9a-zA-Z_.+\-/*%^(),]"),
// Add this code: --------------------------------------
factorial = RegExp(r"([0-9]+) *!");
if (factorial.hasMatch(expression)) {
expression = expression.replaceAllMapped(
factorial, (match) => "fact(${match.group(1)!})");
}
// ----------------------------------------------- snip!
.
.
. Now something like print("2 * 5!".interpret()); should work as expected. Hope that helps! Feel free to fork and implement if it suits your needs! |
Hi,
Thank you for creating this great plugin.
I am planned to add factorial function.
Example 5! -> 1 x 2 x 3 x 4 x 5 = 120
Do you have idea how to add this new function (using ! symbol) to compute factorial.
Thank you
The text was updated successfully, but these errors were encountered: