Tags
October 26, 2023
by
Alexander VerbruggenRead more about this author

Varargs

Introduction

Varargs is a concept from the java world where you can pass in a variable amount of arguments to a method rather than the explicitly defined amount.

For instance if you define a method like this:

public void sum(Integer a, Integer b);

You can (and must) pass in exactly two parameters. If you want to sum three numbers, well, too bad.

You can also write this:

public void sum(Integer...integers);

This allows you to pass no parameters at all or a variable amount of integers to the sum method. This can make it easier to use and more flexible.

Glue

Glue is written in java and all of the core methods are of course implemented in java in some way. A lot of them are directly exposing methods built in java.

Because I really like the varargs concept, I added it to glue as well but because of its java history, this means it is bound by the same limitations:

  • varargs can only be applied to the last parameter of a function
  • there can only be 1 varargs parameter in a function definition

Last argument

All of the functions that operate on series in glue will have the series as the last parameter. This allows you to pass in explicit series or implicit ones.

A lot of functions that seemingly work on a single data point actually also support series which means they also expect it as the last parameter.

For instance string functions like split and join not only work on a single string but also a series of strings, which is why the string content is the last parameter. For instance we can use it on a single string:

echo(split(":", "test1:test2"))
[test1, test2]

But also on multiple strings passed in as an implicit series:

echo(split(":", "test1:test2", "test3:test4"))
[test1, test2, test3, test4]

Which is identical to passing in an explicit series:

simple = series("test1:test2", "test3:test4")
echo(split(":", simple))
[test1, test2, test3, test4]

Because glue supports named parameters, you can change the order as needed or skip optional parameters that might not be relevant for you. For instance, this yields the same result:

echo(split(string: "test1:test2", "test3:test4", regex: ":"))

Unwrap

Some functions that use varargs do not convert multiple parameters into a series but actually expect a variable amount of parameters. You can unwrap an explicit series to apply them as separate parameters if needed.

For example echo supports varargs but uses it to print multiple messages rather than build a series. If we want to print a series:

simple = series(1, 2, 3)
echo(simple)
[1, 2, 3]

If you want to print every item in the series as a separate item, you can unwrap it:

simple = series(1, 2, 3)
echo(unwrap(simple))
1
2
3

Glue-based varargs

When defining your own functions in glue, you can also use the concept of varargs there. If your last input parameter is explicitly defined as being a series, glue will add any additional arguments to that parameter, for example:

sum = lambda
    [] numbers ?= null
    @return
    sum = 0
    for (number : numbers)
        sum = sum + number
        
echo(sum(1, 2, 3))
6