Skip to main content

Overview

What are functions ?

  • One of the major goals of our language is to help the user achieve what they want as quickly as possible. To facilitate this, we ship Plasma with a huge set of functions.
  • A function can be thought of a helper piece of code or utility that performs a particular action. For example, you might want to convert a string to upperCase. You can use upperCase function for that. Another use case would be that you want to add a bunch of numbers. You can use sum function for that.

Anatomy of a function ?

A function has conceptually 3 parts to it:

  • Function Name - This is the predefined case-sensitive name of the function
  • Arguments - Arguments (also called parameters) are the input values to a function. Each function has a specific number of arguments, specific type of arguments and should be sent in the exact order as expected
  • Return Value - This is the output of a function. Each function returns a specific type of value. Sometimes, this type will depend on the input values. Sometimes, no value is returned!. You'll find examples of these kinds of functions as you read on.

How to use a function ?

Let's take the function upperCase. It takes in 1 input of type string and returns string.

First way

def string emotion = "cool!".upperCase(); // emotion = "COOL!"

Second way

def string emotion = upperCase("cool!"); // emotion = "COOL!"

First way

Let's take the function removeAll. It takes in 2 inputs; both of type string and returns string.

def string text = "something somewhere".removeAll("some"); // text = "thing where"

Second way

def string text = removeAll("something somewhere", "some"); // text = "thing where"
note

Every function (except log and mapOf) can be used in both the ways.


How to negate a function value

First way

As described in Operators, you can use the negation operator (exclamation point !)before a function to negate its value

def boolean b1 = ["a", "b"].isEmpty(); // false
def boolean b2 = !(["a", "b"].isEmpty()); // true

Second way

You can also prefix a function with not to achieve the same result

def boolean b1 = ["a", "b"].isEmpty(); // false
def boolean b2 = ["a", "b"].notIsEmpty(); // true
note

Note that, when using the not prefix, the first letter of the function name is capitalized. As seen in the above example, the i in isEmpty is lowercase but the i in notIsEmpty is capitalized.


Common Functions

log(... values)

  • log is used to log values to the console. It is one of the most powerful tools you can have in your arsenal for testing your code.
  • It takes in any number of arguments (at least one).
def string text = "something";
log("the value of text is: ", text); // Will log:- the value of text is: something

mapOf(MapEntry... entries)

  • You might have seen this function in Types Documentation
  • It takes in any number of arguments of type MapEntry and returns a Map
def Map<string, string> friendToInstrument = mapOf("Jimmy" to "guitar", "Robert" to "vocals", "Bonham" to "Drums");
def Map<string, real> employeeToSalary;
employeeToSalary = mapOf("John Doe" to 50000.0, "Jane Doe" to 75000.35);

toString(anything)

  • This function takes any value and converts it to type string
  • If the value is null, it will throw an error
def int myNumber = 7;
def int myNumberAsString = myNumber.toString(); // myNumberAsString = "7"
def int myNumber = 7;
def int myNumberAsString = toString(myNumber); // myNumberAsString = "7"
def int myNumber = null;
def int myNumberAsString = toString(myNumber); // Will throw an error

isNull(anything)

  • This function takes any value and returns true if it is null
  • You can use the not variant of this function as detailed here
def int myNumber = 7;
def boolean myNumberIsNull1 = myNumber.isNull(); // myNumberIsNull1 = false
def boolean myNumberIsNull2 = isNull(myNumber); // myNumberIsNull2 = false

isInRange(int | real | Date | Time | DateTime value, int | real | Date | Time | DateTime lhs, int | real | Date | Time | DateTime rhs)

  • This function returns true if the value is in between lhs and rhs (both inclusive)
  • You can use the not variant of this function as detailed here
def int myNumber = 7;
def boolean b1 = myNumber.isInRange(3, 9); // true
def boolean b2 = isInRange(myNumber, 3, 9); // true
def boolean b3 = myNumber.isInRange(8, 10); // false
def boolean b4 = isInRange(myNumber, 8, 10); // false

hasAllInRange(List<int | real | Date | Time | DateTime> list, int | real | Date | Time | DateTime lhs, int | real | Date | Time | DateTime rhs)

  • This function returns true if all the values of the list are in between lhs and rhs (both inclusive)
  • You can use the not variant of this function as detailed here
def List<int> myNumbers = [5,6,7];
def boolean b1 = myNumbers.hasAllInRange(3, 9); // true
def boolean b2 = hasAllInRange(myNumbers, 3, 9); // true
def boolean b3 = myNumbers.hasAllInRange(8, 10); // false
def boolean b4 = hasAllInRange(myNumbers, 8, 10); // false

hasAnyInRange(List<int | real | Date | Time | DateTime> list, int | real | Date | Time | DateTime lhs, int | real | Date | Time | DateTime rhs)

  • This function returns true if any values in the list is in between lhs and rhs (both inclusive)
  • You can use the not variant of this function as detailed here
def int myNumbers = [5,6,7];
def boolean b1 = myNumber.hasAnyInRange(3, 9); // true
def boolean b2 = hasAnyInRange(myNumber, 3, 9); // true
def boolean b3 = myNumber.hasAnyInRange(8, 10); // false
def boolean b4 = hasAnyInRange(myNumber, 8, 10); // false

length(string | List | Map value)

  • This function takes either
    • string - Returns the size of the string value (Number of characters in string)
    • List - Return the number of elements in List
    • Map - Return the number of elements in Map
def Map<string, string> friendToInstrument = mapOf("Jimmy" to "guitar", "Robert" to "vocals", "Bonham" to "Drums");
def int numberOfFriends = friendToInstrument.length(); // numberOfFriends = 3
def List<int> myFavNumbers = [3,4,12,48];
def int numberOfFavNumbers = myFavNumbers.length(); // myFavNumbers = 4
def string myName = "Jimmy Page";
def int numberOfLettersInMyName = myName.length(); // numberOfLettersInMyName = 10
def Map<string, string> friendToInstrument = mapOf("Jimmy" to "guitar", "Robert" to "vocals", "Bonham" to "Drums");
def int numberOfFriends = length(friendToInstrument); // numberOfFriends = 3
def List<int> myFavNumbers = [3,4,12,48];
def int numberOfFavNumbers = length(myFavNumbers); // myFavNumbers = 4
def string myName = "Jimmy Page";
def int numberOfLettersInMyName = length(myName); // numberOfLettersInMyName = 10

isEmpty(string | List | Map value)

  • This function takes either
    • string - Returns whether the string is empty
    • List - Return if List is empty
    • Map - Return if Map is empty
  • This function is equivalent to calling length(value) == 0
  • You can use the not variant of this function as detailed here
def Map<string, string> friendToInstrument = mapOf("Bonham" to "Drums");
def boolean isFriendToInstrument = friendToInstrument.isEmpty(); // isFriendToInstrument = false
def List<int> myFavNumbers = [];
def boolean isMyFavNumbersEmpty = myFavNumbers.isEmpty(); // isMyFavNumbersEmpty = true
def string myName = "John";
def boolean isMyNameEmpty = myName.isEmpty(); // isMyNameEmpty = false
def Map<string, string> friendToInstrument = mapOf("Bonham" to "Drums");
def boolean isFriendToInstrument = isEmpty(friendToInstrument); // isFriendToInstrument = false
def List<int> myFavNumbers = [];
def boolean isMyFavNumbersEmpty = isEmpty(myFavNumbers); // isMyFavNumbersEmpty = true
def string myName = "John";
def boolean isMyNameEmpty = isEmpty(myName); // isMyNameEmpty = false

isIn(T element, List<T> list)

  • This function can be used to check if a value is contained in a list
  • You can use the not variant of this function as detailed here
def string musician = "Bonham";

def List<string> ledZeppelin = ["Bonham", "Robert", "Jimmy"];
def boolean isMusicianInLedZeppelin1 = musician.isIn(ledZeppelin); // true
def boolean isMusicianInLedZeppelin2 = isIn(musician, ledZeppelin); // true

def List<string> pinkFloyd = ["Roger", "David", "Syd"];
def boolean isMusicianInPinkFloyd1 = musician.isIn(pinkFloyd); // false
def boolean isMusicianInPinkFloyd2 = isIn(musician, pinkFloyd); // false

geoDistance(numeric lat1, numeric lng1, numeric lat2, numeric lng2, (Optional) string unit)

  • This function takes in latitude and longitude of two co-ordinates as 4 numeric arguments and returns the distance in kms between them using Haversine formula
  • Optional unit can be passed as the 5th argument:- "mi" for miles; "m" for metres
def real distanceFromMyHomeToOffice = geoDistance(10.2233,11.8123,9.9978,11.6981); // distanceFromMyHomeToOffice = 28.018
def real distanceFromMyHomeToOfficeInMiles = geoDistance(10.2233,11.8123,9.9978,11.6981, "mi"); // distanceFromMyHomeToOfficeInMiles = 17.41
def real distanceFromMyHomeToOfficeInMetres = geoDistance(10.2233,11.8123,9.9978,11.6981, "m"); // distanceFromMyHomeToOfficeInMetres = 28018.0