Dot Chain Special Operators
Yesterday we wrote a YS program by porting a Clojure program from Rosetta Code.
We introduced a bunch of new things without really explaining them.
Today I'll explain a set of them.
The Dot Chain Special Operators🔗
Yesterday we had a piece of code that looked like this:
sparks =: '▁▂▃▄▅▆▇█'
quantize =:
\(round(7.0 * ((_ - low) / spread)))
And I surmised that the 7 was the length of the sparks string minus 1.
So I changes 7.0 to sparks.#.--.
Since the length of the sparks string is 8, this gave us 7.
But what is .#.-- all about?
In Clojure (and thus YS) the count
function returns the length of a
sequence, and a string is a sequence of characters.
The dec
function (decrement)
returns the value of its argument minus 1.
So we could have written the sparks.count().dec() or sparks:count:dec.
The .# is a special function that is short for .count().
It only works in a dot chain and doesn't use any () parentheses.
The .-- is short for .dec().
The count and dec functions are used so often that it's nice to have a
shorter way to write them.
All the Dot Chain Special Operators🔗
There's a few more of these specials:
.#is short for.count().++is short for.inc().--is short for.dec().0is short for.first().$is short for.last().??is short for.boolean().!!is short for.not().?is short for.truey?().!is short for.falsey?().@is short for.deref().>>>is short for.DBG()
Actually .0 is short for .nth(0) but it looks nice in that list.
I'll explain what those functions do another time.
I think I'll cover truey? and falsey? tomorrow.
It's actually pretty interesting.
Most programming languages have a set of rules for truthiness but YS has 2 sets!