objective c - Can anyone point to a tutorial of adding new functions in ddmathparser? -


i new programming. want add new functions such derivatives , integrations ddmathparser. 1 can find short tutorial on ddmathparser's wiki page https://github.com/davedelong/ddmathparser/wiki/adding-new-functions . however, can't follow because it's short , after reading several times, still can't understand it's doing. can elaborate steps add new function or give me more detailed tutorials of doing this? did research can't find one. much.

ddmathparser author here.

here's how add multiply two function:

ddmathevaluator *evaluator = [ddmathevaluator sharedmathevaluator]; // function takes arguments, variable values, evaluator, , error pointer // , returns new expression [evaluator registerfunction:^ddexpression *(nsarray *args, nsdictionary *vars, ddmathevaluator *eval, nserror *__autoreleasing *error) {     ddexpression *final = nil;      // multiplyby2() can handle single argument     if ([args count] == 1) {         // argument , wrap in "multiply()" function         ddexpression *argexpression = [args objectatindex:0];         ddexpression *twoexpression = [ddexpression numberexpressionwithnumber:@2];         final = [ddexpression functionexpressionwithfunction:ddoperatormultiply arguments:@[argexpression, twoexpression] error:nil];     } else if (error) {         // there wasn't 1 argument         nsstring *description = [nsstring stringwithformat:@"multiplyby2() requires 1 argument.  %ld given", [args count]];         *error = [nserror errorwithdomain:ddmathparsererrordomain code:dderrorcodeinvalidnumberofarguments userinfo:@{nslocalizeddescriptionkey: description}];     }     return final;  } forname:@"multiplyby2"]; 

now can do:

nsnumber *result = [@"multiplyby2(21)" stringbyevaluatingstring]; 

and @42.


what's going on here:

internally, ddmathevaluator has big nsdictionary keeps list of functions knows about, keyed off name of function, kind of this:

_functions = @{   @"multiply" : multiplyfunctionblock,   @"add" : addfunctionblock,   ... }; 

(obviously it's bit more complicated that, that's basic idea)

when evaluator evaluates string , comes across function, looks in dictionary block function is. retrieves block, , executes block arguments (if there any) string. result of block result of function.

that result gets substituted in, , evaluation continues.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -