Functions

Function call

The major distinction between PRQL and today’s conventional programming languages such as C or Python is the function call syntax. It consists of the function name followed by arguments separated by whitespace.

If one of the arguments is also a function call, it must be encased in parentheses, so we know where arguments of inner function end and the arguments of outer function start.

Pipeline

There is a alternative way of calling functions: using a pipeline. Regardless of whether the pipeline is delimited by pipe symbol | or a new line, the pipeline is equivalent to applying each of functions as the last argument of the next function.

… is equivalent to …

As you may have noticed, transforms are regular functions too!

PRQL

  1. from employees
  2. filter age > 50
  3. sort name

SQL

  1. SELECT
  2. *
  3. FROM
  4. employees
  5. WHERE
  6. age > 50
  7. ORDER BY
  8. name

… is equivalent to …

PRQL

  1. from employees | filter age > 50 | sort name

SQL

  1. SELECT
  2. *
  3. FROM
  4. employees
  5. WHERE
  6. age > 50
  7. ORDER BY
  8. name

… is equivalent to …

PRQL

  1. filter age > 50 (from employees) | sort name

SQL

  1. SELECT
  2. *
  3. FROM
  4. employees
  5. WHERE
  6. age > 50
  7. ORDER BY
  8. name

… is equivalent to …

PRQL

  1. sort name (filter age > 50 (from employees))

SQL

  1. SELECT
  2. *
  3. FROM
  4. employees
  5. WHERE
  6. age > 50
  7. ORDER BY
  8. name

As you can see, the first example with pipeline notation is much easier to comprehend, compared to the last one with the regular function call notation. This is why it is recommended to use pipelines for nested function calls that are 3 or more levels deep.

Currying and late binding

In PRQL, functions are first class citizens. As cool as that sounds, we need simpler terms to explain it. In essence in means that we can operate with functions are with any other value.