To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Official ZPE/YASS documentationFunctions

YASS
function main($args)
  //Do some stuff here
end function
  

Functions became a huge part of YASS in version 1.3.4 when they became importable using the import function. This meant that compiled scripts could be imported into the current script.

Since version 1.4.1, functions can be given a scope. A scope can be normal, private or public. The previous example showed a function with the normal scope. This is roughly equivalent to private. Private means that the function may not be accessed in any program other than it's own. Public allows other scripts or programs to access the function.

Because of the powerful way in which ZPE handles this, the import command is extremely useful for importing existing, compiled scripts.

The following sample demonstrates three functions, each with a different scope.

YASS
function main($args)
  //This function has the normal scope
end function
private function main($args)
  //This function has the private scope
end function
public function main($args)
  //This function has the public scope
end function
  

Nested functions

Many languages allow the use of nested functions. Since functions are first-class citizens, they also allow for encapsulation of internal methods. In this case, a function may itself contain a function:

YASS
function main($args)
  function say_name()
    print("Hello there Jamie")
  end function

  say_name()
  other_function()
end function

function other_function()
  //This will throw a ZPE WARNING as it cannot find the say_name function
  say_name()
end function

Lambda functions

As of version 1.3.7.44 lambda (λ) expressions have been supported in ZPE. These are known as anonymous functions and they are designed to make the language more flexible and simpler. They are, without any doubt, my favourite feature of the language and you will see that a lot of my functions benefit from the use of lambda functions.

A lambda function or lambda abstraction is a function that is not bound to any identifier, that is, the function is never defined as a function but it can be assigned to a variable and so on. ZPE version 1.3.7.44 added support for lambda expressions.

The syntax is shown below:

YASS
$foo = function($x, $y, $z){return $x + $y + $z}
$bar = function($x, $y, $z) return $x + $y + $z end function
print($foo(3, 4, 6))
print($bar(3, 4, 6))
  

The language provides two ways of defining these functions, one is the C-like way of using curly braces ({}) around the function and the second is to use the end function keywords to terminate the function.

Functions need not have a return value, that is, no return function is required in order for this to work:

YASS
$foo = function($x){print ($x + 5)}
$foo(3)
  

Lambda expressions also can be given values straight from the declaration, which can make them more useful in other functions:

YASS
$v = 10
print(function($x){return ($x + 5)}($v))

The result of this evaluation is 10, so the print function will simply print 10. Variables, functions and so on can all be used as well.

Lambda functions are also very useful with the special csv_decode function, which takes a function as it's optional second parameter. Rather than defining some function that may only be used once and then calling the handle_of function on that function, it may make more sense to define a lambda function:

YASS
$content = "32, 41, 91, 36"
$out = csv_decode($content, function($x){return $x + 5})
//Should print 37, 46, 96, 41
print($out)
$out = csv_decode($content, function($x, $rowpos, $columnpos){
  return $x + 5 + $columnpos
})
//Should print 37, 47, 98, 44 because the column position is added to the value
print($out)
  

Also note, that unlike Python, lambda functions can be defined across many lines, not just one.

The previous example could also be rewritten to store a lambda function in a variable an call it using it's variable reference:

YASS
$content = "32, 41, 91, 36"
$out = csv_decode($content, function($x){return $x + 5})
//Should print 37, 46, 96, 41
print($out)

$csv_func =
function($x, $rowpos, $columnpos)
  return $x + 5 + $columnpos
end function

$out = csv_decode($content, $csv_func)

//Should print 37, 47, 98, 44 because the column position is added to the value
print($out)
  

It is important to note that ordering of lambda variables is important. For a function like csv_decode the order is crucial but it can be any number of parameters so for instance $columnpos need not be specified.

Also, parameter names do not matter either since when the csv_decode function is run it allocates values to the parameters from left to right, not based on names.

Short-hand lambda functions

Version 1.6.7 introduces an alternative syntax similar to JavaScript that allows anonymous functions to be defined with the fat arrow:

YASS
$foo = ($x, $y, $z) => {return $x + $y + $z}
$bar = ($x, $y, $z) => return $x + $y + $z end function
  
Comments

There are no comments on this page.

New comment
Reply
Provide feedback on this page
Comments are sent via email to me.