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:
B
is short forto-bool
(boolean)C
is short forto-char
(character)F
is short forto-float
(floating point number)I
is short forto-int
(integer)K
is short forto-keyw
(keyword)L
is short forto-list
(list)M
is short forto-map
(mapping)N
is short forto-num
(number)O
is short forto-omap
(ordered mapping)S
is short forto-str
(string)T
is short forto-type
(string name of the type)V
is 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 forlist
M+
is short forhash-map
O+
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.
IN
is short for*in*
(stdin)OUT
is short for*out*
(stdout)ERR
is short for*err*
(stderr)ARGV
is short for*command-line-args*
(command line arguments)ARGS
isARGV
with number arguments auto-castedNS
is short for*ns*
(current namespace)VERSION
is short for*clojure-version*
(but for the YS version)RUN
is a mapping of runtime informationENV
is a mapping of environment variablesFILE
is short for*file*
(current file)
That's it for today.
I could probably say
more on this topic, but...
Let's keep it short!