top of page

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:

M 0.18.3 jao@loon ~/git/marcel/marcel$ ls | select (lambda f: now() - f.mtime < days(1))
drwxrwxr-x   jao    jao        4096   2023 Oct 27 11:54:55   __pycache__
-rw-rw-r--   jao    jao       13314   2023 Oct 27 11:54:55
-rw-rw-r--   jao    jao       10895   2023 Oct 27 11:54:55
drwxr-xr-x   jao    jao        4096   2023 Oct 27 11:54:55   object
-rw-rw-r--   jao    jao       39519   2023 Oct 27 11:54:55

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.


Marcel allows lambda to be omitted, so the function could have been written as (f: now() - f.mtime < days(1)).

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:

M 0.18.3 jao@loon ~/git/marcel/marcel$ ls *.py | map (f: (, f.size))
('', 652)
('', 8299)
('', 19326)
('', 1817)
('', 19539)
('', 13314)
('', 4523)
('', 3158)
('', 20752)
('', 11452)
('', 1081)
('', 1736)
('', 10895)
('', 6700)
('', 2558)
('', 3465)
('', 39519)
('', 3683)
('', 1522)
('', 1207)
('', 8101)
('', 6006)
('', 672)

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

M 0.18.3 jao@loon ~/git/marcel/marcel$ map (2 ** 50)

Marcel allows map to be omitted. A function invocation by itself is understood to be part of a map operator. So this works too:

M 0.18.3 jao@loon ~/git/marcel/marcel$ (2 ** 50)

In Python, the symbols in a function are 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:

M 0.18.3 jao@loon ~/git/marcel/marcel$ (USER)

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

  • import modules in ~/, the configuration file.

  • Define symbols in ~/

  • Import them.

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

M 0.18.3 jao@loon ~/git/marcel/marcel$ (pi)
Error: Running map(lambda: pi): name 'pi' is not defined
M 0.18.3 jao@loon ~/git/marcel/marcel$ (math.pi)
Error: Running map(lambda: math.pi): name 'math' is not defined

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

M 0.18.3 jao@loon ~/git/marcel/marcel$ import math
M 0.18.3 jao@loon ~/git/marcel/marcel$ (math.pi)

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

bottom of page