Soooo, I did the thing again. I did create yet another programming language. I call i Musa
In this post I will talk about a small but useful syntax feature in Musa
for piping function calls together.
Alright, let us get some basic syntax out of the way. Musa
is based on the old trusty procedural paradigm, in which the function call
is the main building block.
Let's begin with a simple example.
addI(1, 20, multI(3, 100))
As you probably already have guessed addI()
is a function for adding integer numbers and multI()
is a function for multiplying integer numbers. Easy, peasy, straight forward syntax. Ain't it not?
But, there is a problem with the standard procedural syntax. Take a look at this montrosity:
map(str.split(str.trim('aa bbb cccc ddddd '), ' '), <a| str.getSize(a); >)
[2, 3, 4, 5]
As one is putting function calls as arguments of other function calls things can sometime get a bit messy and hard to grasp.
The expression in words: Take the string 'aa bbb cccc ddddd '
, trim any white-spaces from the beginning and end. Split the resulting string into a list of strings using ' '
as a separator. Map into a new list using the lambda expression <a| str.getSize(a); >
(more about the syntax for lambdas in Musa
in another post).
Oh, there must be a simpler way to express this! But of course there is. Lets use Musa
s pipe syntax. Enabling us to rewrite the unwieldy monstrosity as:
'aa bbb cccc ddddd ' | str.trim() | str.split(' ') | map(<a| str.getSize(a); >)
The workings of the piping construct is rather simple: The result of the expression to the right of a piping symbol, |
, is prepended as the first argument of the function call to the right of the pipe symbol.
Which means that
5 | someFunction(99)
is the same as
a() | b(5) | c()
is the same as
You can use the pipe syntax in any expression. For example as arguments of function calls.
a(5 | b(100))
which is the same as
The pipe syntax of Musa
might seem like a minor thing. But the thing is that after getting used to it I have found myself sometimes missing it while writing code in other programming languages.
Mikael K J Eriksson