Functions

Several marcel operators take functions as arguments. Marcel functions are written in Python. For example, the files produced by the ls operator can be piped to the select operator, which uses a function to keep only files modified in the past day:

Functions are always delimited by parentheses. Here, the function takes one argument, f, which will be bound to Files piped in from ls. now() is a function built in to marcel which returns the current time, as seconds since the epoch. f.mtime returns the modification time of file f. days(1) invokes another builtin function that returns the number of seconds in one day.

The net effect is to filter the Files returned by ls by keeping only those modified in the last day. Remember that an out operator is appended if needed, so this command prints the qualifying files.

The map operator uses a function to transform incoming data. For example, this command lists the name and size of each .py file in the current directory:

You can write functions with no arguments. For example, to evaluate 2**50:

In cases like this -- map calling a function with no arguments, as the first thing on the command line -- you can omit writing map, it will be understood. So this works too:

In Python, the symbols in a function are always resolved against a namespace. Marcel functions run in the marcel namespace, a Python namespace maintained by marcel. This namespace includes environment variables, so you can use this abbreviated notation to examine environment variables:

You can add symbols to the marcel namespace in a number of ways:

‚Äč

  • import modules in ~/.marcel.py, the configuration file.

  • Define symbols in ~/.marcel.py.

  • Import them.


For example, math.pi is not present in the marcel namespace by default:

However, you can import math, and then use its symbols:

(import math * would import the symbols contained in math, similar to the Python statement from math import *.)

  • github
  • Twitter
  • email