The YAMLScript Standard Library
The YAMLScript standard library is a collection of functions that are available to all YAMLScript programs. This document describes the functions in the standard library and how to use them.
YAMLScript exposes most of the functions available in the Clojure Core standard library. See Core Library Essentials for an overview of those functions.
The YAMLScript standard library replaces some Clojure functions with a version more suited to YAMLScript. In those cases, the original Clojure function is still available in the ys::clj
namespace.
Special functions
- source(*) - Run a YAMLScript file as a Bash script
Shorter named alias functions
a
-clojure.core/identity
aliaslen
-clojure.core/count
alias
Quoting functions
q(form)
-clojure.core/quote
aliasqr(str)
-clojure.core/re-pattern
aliasqw(symbols)
- Turn symbols into a vector of strings
Alternate truth functions
falsey?(x)
- True if x is falsey - 0, nil, false, emptytruey?(x)
- True if x is not falseyor?(x *xs)
- Return first truey value or niland?(x *xs)
- Return last truey value or nil|||
-or?
operator&&&
-and?
operator
Named function aliases for infix operators
eq
-clojure.core/=
ne
-clojure.core/not=
gt
-clojure.core/>
ge
-clojure.core/>=
lt
-clojure.core/<
le
-clojure.core/<=
Common type conversion functions
to-bool(x)
- Convert x to a booleanto-booly(x)
- Convert truey x to a booleanto-float(x)
- Convert x to a floatto-int(x)
- Convert x to an integerto-list(x)
- Convert x to a listto-map(x)
- Convert x to a mapto-num(x)
- Convert x to a numberto-set(x)
- Convert x to a setto-vec(x)
- Convert x to a vector
Math functions
add(*)
-clojure.core/+
aliassub(*)
-clojure.core/-
aliasmul(*)
-clojure.core/*
aliasdiv(*)
- Division function that returns a float if neededsum(nums)
- Sum a sequence of numberspow(x *ys)
- Raise a number to a power - right associativesqr(x)
- Square a numbercube(x)
- Cube a numbersqrt(x)
- Square root of a numberabs(x)
- Absolute value of a numberadd+(x *xs)
- Polymorphic addition functionsub+(x *xs)
- Polymorphic subtraction functiondiv+(x *xs)
- Polymorphic division functionmul+(x *xs)
- Polymorphic multiplication function
Document result stashing functions
$
- Mapping of document results$#
- Index of last document result$$
- Get the last document result
Control functions
value(x)
- Get var value from var, symbol or string. Often used to convert a string to a function.call(f *args)
- Call a function or value. Functionf
can be a string, symbol or function.die(msg)
- Idiomatic error functioneach(bindings *body)
- Non-lazy for looperr(*xs)
- Print to stderrexit(rc=0)
- Exit the programif(cond then else)
- Functionalif
used in dot chainingsleep(secs)
- Sleep for a number of seconds
String functions
chomp(s)
- Remove trailing newlinesindex(s, sub)
- Find index of substringjoin(s, *xs)
- Join strings or seqs with a separatorlc(s)
- Lowercase a stringlines(s)
- Split a string into linespretty(x)
- Pretty print a valuereplace(s, old, new)
- Replace all occurrences of old with newreplace1(s, old, new)
- Replace first occurrence of old with newsplit(s, re)
- Split a string by a regextrim(s)
- Trim whitespace from both endstriml(s)
- Trim whitespace from left endtrimr(s)
- Trim whitespace from right enduc(s)
- Uppercase a stringuc1(s)
- Capitalize a stringwords(s)
- Split a string into words
Regex functions
=~
- Infix re-find operator!~
- Infix re-find complement operator
Collection functions
get+(C, K)
- Get a string, keyword or symbol from a map or sequencegrep(P, C)
- Filter a collection by a predicatehas?(C, x)
- True if collection has xin?(x, C)
- True if x is in collectionomap(*xs)
- Create an ordered mapreverse(x)
- Reverse a string, vector or sequencerng(x, y)
- Create a range of numbers or characters, y is inclusive..
- Infix rng operator
I/O functions
out(*xs)
- Print to stdoutpp(x)
- Pretty print a valuesay(*xs)
- Print to stdout with newlinewarn(*xs)
- Print to stderr with newline
File system functions
fs-d(path)
- True if path is a directoryfs-e(path)
- True if path existsfs-f(path)
- True if path is a regular filefs-l(path)
- True if path is a symbolic linkfs-r(path)
- True if path is readablefs-s(path)
- True if path is not emptyfs-w(path)
- True if path is writablefs-x(path)
- True if path is executablefs-z(path)
- True if path is emptyfs-abs(path)
- Get the absolute pathfs-abs?(path)
- True if path is absolutefs-basename(path, *ext)
- Get the file name of a path, without extensionfs-dirname(path)
- Get the directory name of a pathfs-filename(path)
- Get the file name of a pathfs-glob(path)
- Glob a pathfs-ls(dir)
- List a directoryfs-mtime(file)
- Get the modification time of a filefs-rel(path)
- Get the relative pathfs-rel?(path)
- True if path is relativefs-which(name)
- Find the path of an executable
IPC functions
See babashka.process
for more information about these functions.
exec(cmd, *xs)
- Execute a commandprocess(cmd, *xs)
- Execute a commandsh(cmd, *xs)
- Execute a commandshell(cmd, *xs)
- Execute a commandshout(cmd, *xs)
- Execute a command and return the output
External library functions
use-pod(pod-name, version)
- Load an external library pod
HTTP functions
curl(url)
- Get a URL and return the body