I have written several functions funA (x), funB (x), funC (x), funD (x), funE (x), funF (x ), I have a requirement to implement different calculations based on a specific parameter 'str. For example, when str 'ABC' is executed, it can execute x → funA (x) → funB (x) → funC (x) → y. when str 'AC' is executed, it can execute... I have written several functions funA (x), funB (x), funC (x), funD (x), funE (x), funF (x ), I have a requirement, according to a specific parameter
StrTo implement different computations. For example, when
Str = 'abc'You can run x → funA (x) → funB (x) → funC (x) → y. when str 'acs' is run, you can run x → funA (x) → funC (x) → funE (x) → y.
function fun_abc(x){ var a = funA(x); var b = funB(a); var c = funC(b); var y = c; return y;}
I know that you can write a switch based on str for case and jump to different fun_XXX (), but this is not flexible. The final goal is to have an array with different function names written in order. input x during the call, and the function corresponding to the first element in the array will run until the last function, output final result y.
Is there any other way to simplify it? Or can it be freely combined. for example, if a function of the same name is directly executed based on a variable like funstr = 'abc', the function abc (x) is executed when a function similar to funstr (x) is run )?
JavaScript or php syntax.
Reply content:
I have written several functions funA (x), funB (x), funC (x), funD (x), funE (x), funF (x ), I have a requirement, according to a specific parameterStrTo implement different computations. For example, whenStr = 'abc'You can run x → funA (x) → funB (x) → funC (x) → y. when str 'acs' is run, you can run x → funA (x) → funC (x) → funE (x) → y.
function fun_abc(x){ var a = funA(x); var b = funB(a); var c = funC(b); var y = c; return y;}
I know that you can write a switch based on str for case and jump to different fun_XXX (), but this is not flexible. The final goal is to have an array with different function names written in order. input x during the call, and the function corresponding to the first element in the array will run until the last function, output final result y.
Is there any other way to simplify it? Or can it be freely combined. for example, if a function of the same name is directly executed based on a variable like funstr = 'abc', the function abc (x) is executed when a function similar to funstr (x) is run )?
JavaScript or php syntax.
Let me give you an example. eval is too rough... Look at me.
function run(str, param) { let handle = { a: a, b: b, c: c, d: d }; return str.split('').reduce((res, name) => handle[name](res), param); function a(x) { return x + '1'; } function b(x) { return x + '2'; } function c(x) { return x + '3'; } function d(x) { return x + '4'; }}run('acb',0); // "0132"
For example, similar ideas can be freely used.
@ Improvement on the basis of a fish that does not like tomatoes
Const funcTable = {a, B, c, d} function run (str, initialVal) {return str. split (''). reduce (result, funcName) => funcTable [funcName] (result), initialVal)} // define function a, function B...
Simplest, eval
Trouble point {'ABC': fabc}
The idea is simple,php
Andnode
I will post a sample code.
php
Version
Function funstr ($ str, $ param) {$ len = strlen ($ str); for ($ I = 0; $ I <$ len; $ I ++) {$ fun = 'fun '. strtoupper (substr ($ str, $ I, 1); if (function_exists ($ fun) {$ param = $ fun ($ param) ;}} return $ param ;} function funA ($ x) {return $ x + 1;} function funB ($ x) {return $ x + 2;} echo funstr ('AB', 0 ); // output 3
node
Version
Function funstr (str, x) {var len = str. length; for (var I = 0; I
Put all the defined functions in an object, and set the return value to call the function;
Var AB = {
A: function (x) {console. log (x); return 1}, B: function (x) {console. log (x); return 2}, c: function (x) {console. log (x); return 3}, d: function (x) {console. log (x); return 4},} function run (str, x) {// str is the defined sequence, such as acb; x is the parameter var B = x; var aa = str. split ("") aa. forEach (function (x) {AB [x] (B); B = AB [x] (B);}) return B}