YS Shorties
From the start, YAML has always been about making data clean and easy to read.
This carries over to YS code as well.
Even though YS code compiles to Clojure, YS often has shorter alternatives for Clojure's commonly used and longer function names.
To Name a Few🔗
Here are some shorter alternative functions that YS provides. Note: you can almost always use the Clojure version if you want to.
say is short for println
.
This is one of the most commonly used functions and found in almost every
program.
a is short for identity
.
len is short for count
.
count isn't that long, but lots of languages use len for this.
In YS you can use either!
Or even use .# in a chaining context.
out and err are short for print
to stdout and stderr respectively.
read and write are short for slurp
and
spit
respectively.
write is longer, but spit is gross!
q is short for quote
.
In YS, a single quote is used for a string literal that doesn't have
interpolation semantics.
In Clojure, a single quote is used for form quoting (using a form without
evaluating it) and is just sugar for the
quote function.
For instance in Clojure you might do '(1 2 3).
In YS you'd use q((1 2 3)) or (1 2 3):q
To my Clojure Friends
I know that quote isn't really a function; it's a special form.
And I know that I've called macros "functions" in this series.
Forgive me, but it's just not that important to make the distinction; at
least not at this point.
Also, macros are much less important to YS than they are to Clojure. I'll explain why in a future post.
chomp is short for str/trim-newline
.
uc and lc are short for str/upper-case
and
str/lower-case
respectively.
uc1 is short for str/capitalize
.
index and rindex are short for str/index-of
and
str/last-index-of
respectively.
replace1 is short for str/replace-first
.
now returns a time object.
Casting Shorties🔗
YS has a bunch of functions whose job is to cast a value from one type to
another.
Functions like to-int, to-map and to-char.
Each of these has a single character short name:
Bis short forto-bool(boolean)Cis short forto-char(character)Fis short forto-float(floating point number)Iis short forto-int(integer)Kis short forto-keyw(keyword)Lis short forto-list(list)Mis short forto-map(mapping)Nis short forto-num(number)Ois short forto-omap(ordered mapping)Sis short forto-str(string)Tis short forto-type(string name of the type)Vis short forto-vec(vector)
(Maybe we'll talk about YS's data types later this week.)
There's also a few data collection type constructors that take a list of values:
L+is short forlistM+is short forhash-mapO+is short foromap-
V+is short forvector -
T?is short fortruey? F?is short forfalsey?
Global Shorties🔗
Clojure has a set of variables called builtin dynamic variables.
They have funny looking names like *out* and *command-line-args*.
They are sometimes referred to as "earmuff variables" because they begin and end
with an asterisk.
Theses names are invalid syntax in YS. In YS we call them global variables, they use capital letters, and their names are usually short.
INis short for*in*(stdin)OUTis short for*out*(stdout)ERRis short for*err*(stderr)ARGVis short for*command-line-args*(command line arguments)ARGSisARGVwith number arguments auto-castedNSis short for*ns*(current namespace)VERSIONis short for*clojure-version*(but for the YS version)RUNis a mapping of runtime informationENVis a mapping of environment variablesFILEis short for*file*(current file)
That's it for today.
I could probably say more on this topic, but...
Let's keep it short!