Community discussions

MUM Europe 2020
 
User avatar
astounding
Member Candidate
Member Candidate
Topic Author
Posts: 121
Joined: Tue Dec 16, 2008 12:17 am

Feature request: :lambda or :func or :code or :proc

Sat Nov 22, 2014 12:37 am

Now that MikroTik scripting allows passing around code chunks and makes it more easy to create them by setting variables (no more complex parsing of strings with crazy escape sequences), I'd like to see MikroTik add anonymous inline code abilities that don't need a variable. For example:
{
  :local Map do={
    :local list $1;
    :local out [:toarray ""];
    :if ([:typeof $list]!="array") do={
      :set list ([:toarray ""],$list);  ## In case input is a single item (non-array)
    };
    :do {
      :foreach k,v in=$list do={
        :set out ($out,[$2 $k $v]);
      };
    } on-error={
      :log error "Map(array,func) failed to execute func for array";
    };
    :return $out;
  };
  :local func do={
    :return $2;
  };
  :local citystateassoc {"Houston"="Texas";"Denver"="Colorado";"Washington"="D.C.";"Las Vegas"="Nevada";"Portland"="Maine";"Dallas"="Texas"};
  :local states [$Map $citystateassoc $func];
  :put "STATES (in no particular order, may be duplicates):"
  :put $states;
}
WITH inline anonymous code blocks able to be passed as function arguments, I could instead do:
 :local citystateassoc {"Houston"="Texas";"Denver"="Colorado";"Washington"="D.C.";"Las Vegas"="Nevada";"Portland"="Maine";"Dallas"="Texas"};
  :local states [$Map $citystateassoc [:func do={:return $2;}]];
  :put "STATES (in no particular order, may be duplicates):"
  :put $states;
The relevant bit is the [:func do={:return $2;}] (or it could be called :lamda, :proc, :code, etc...). The command would return a special code value (or like the do={} variable setting stuff, return an array of length 2 containing {nil;(code)}).

How about it?
 
User avatar
astounding
Member Candidate
Member Candidate
Topic Author
Posts: 121
Joined: Tue Dec 16, 2008 12:17 am

Re: Feature request: :lambda or :func or :code or :proc

Fri Dec 05, 2014 8:46 pm

A VERY powerful, a more difficult-to-implement feature that would be extremely useful (especially in conjunction with the above feature), would be this: The ability of a :func do={} defined or :local var dp={} defined code block to get access to the enclosing scope's local variables:
{
  :local citystateassoc {"Houston"="Texas";"Denver"="Colorado";"Washington"="D.C.";"Las Vegas"="Nevada";"Portland"="Maine";"Dallas"="Texas"};
  :local internalstatecodes {"Texas"=5234;"Colorado"=1002521;"D.C."=722;"Nevada"=6661;"Maine"=2322156};
  :local codes [$Map $citystateassoc [:func do={:return ($internalstatecodes->$2);}]];
  :put "CODES (in no particular order, may be duplicates):"
  :put $codes;
}
This would be even more useful if even after execution passed outside of the enclosing scope, a code block defined with the scope but stored in a variable that exists outside the scope could still have access to values:
  :global foo;
  {
    :local bar "Hi, Mom!";
    :global foo;
    :set foo do={ :put $bar };
  }
  :put [$foo];
That would give scripting a sort of closure ability.

Who is online

Users browsing this forum: No registered users and 8 guests