Standard Library
YS has over 1000 functions that are always available to your YS programs without
needing to explicitly import (use) any libraries.
Over 800 of these can be used without a namespace prefix like str/ or math/.
Most of these functions are from the Clojure standard library
, but YS also has the
ys::std standard
library that provides additional functions.
This document describes the ys::std functions and how to use them.
It also links to related functions in the Clojure core library.
Note
In a few cases, the YS standard library replaces some Clojure functions with
a version more suited to YS.
In those cases, the original Clojure function is still available in the
ys::clj namespace.
String functionsπ
base64-decode(Str) β Strβ Decode a base64 stringbase64-encode(Str) β Strβ Encode a string to base64base64-points(Str) β [Int]β Decode a base64 string to code pointsbase64(Str) β Strβ Alias of base64-encode-
blank?(Str) β Strβ Alias ofclojure.string/blank?True if string is nil, empty or only whitespace
-
chomp(Str) β Strβ Alias ofclojure.string/trim-newlineRemove trailing newlines
-
chop(Str) β Strβ Remove last character -
ends?(Str) β Bβ Alias ofclojure.string/ends-with?True if string ends with suffix
-
escape(Str {Chr:Str}) β Strβ Alias ofclojure.string/escapeEscape special characters in a string
-
index(Str Str) β Intβ Alias ofclojure.string/index-ofFind index of substring
-
join([Str]) β Strβ Join strings or seqs with"" join(sep [Str]) β Strβ Join strings or seqs with a separatorjoins([Str]) β Strβ Join strings with" "-
lc(Str) β Strβ Alias ofclojure.string/lower-caseLowercase a string
-
lines(Str) β [Str]β Split a string into lines pretty(Str) β Strβ Pretty print a valuereplace(X) β Xβ Alias ofclojure.core/replace-
replace(Str Rgx Str?) β Strβ Alias ofclojure.string/replaceReplace all occurrences of regex with new string (default "")
-
replace1(Str Rgx Str) β Strβ Alias ofclojure.string/replace-firstReplace first occurrence of regex with new string
-
rindex(Str Str) β Intβ Alias ofclojure.string/last-index-ofFind last index of substring
-
split(Str) β [Str]β Likeclojure.string/splitSplit on
"" -
split(Str rgx) β [Str]β Alias ofclojure.string/splitSplit a string by a regex
-
starts?(Str Str) β Strβ Alias ofclojure.string/starts-with?True if string starts with prefix
-
substr(Str Int Int?) β Strβ Substring function with Perl semanticsOffset is 0-based, negative offset counts from end; Optional length is from offset and defaults to end of string; Negative length counts from end of string
-
text([Str]) β Strβ Join list of strings with newlines, adding a newline at the end -
trim(Str) β Strβ Alias ofclojure.string/trimTrim whitespace from both ends
-
triml(Str) β Strβ Alias ofclojure.string/trimlTrim whitespace from left end
-
trimr(Str) β Strβ Alias ofclojure.string/trimrTrim whitespace from right end
-
uc(Str) β Strβ Alias ofclojure.string/upper-caseUppercase a string
-
uc1(Str) β Strβ Alias ofclojure.string/capitalizeUppercase the first character
-
words(Str) β [Str]β Split a string into words (split on whitespace)
See also: https://clojuredocs.org/quickref#strings-characters
Collection functionsπ
-
diff(Col Col) β [Str Str Str]β Alias ofclojure.data/diffReturn the difference of two collections
-
flat(Vec) β Vecβ Likeclojure.core/flattenOnly flattens one level
-
get+(Col Key) β Xβ Get a string, keyword or symbol from a map or sequence grep(Fn Col) β Colβ Filter a collection by a predicate functionhas?(Col) β Fnβ Returns a partial function closed over Col-
has?(Col X) β Bβ True if collection has XWorks with strings, maps and sequences
-
in?(Col) β Fnβ Returns a partial function closed over Col -
in?(X Col) β Bβ True if X is in collectionWorks with strings, maps and sequences
-
omap([X]) β Omapβ Create an ordered map reverse(Col) β Colβ Reverse a string, vector or sequence-
rng(Int Int) β [Int]β Create a range of numbers or characters, Y is inclusiveIf X is greater than Y, the range is descending
-
slice(Col [Key]) β [X]β Get a sequence of values from the keys
See also: https://clojuredocs.org/quickref#collections
Math functionsπ
add(Num*) β Numβ Alias ofclojure.core/+sub(Num+) β Numβ Alias ofclojure.core/-mul(Num*) β Numβ Alias ofclojure.core/*div(Num+) β Numβ Division function that returns a float if needed-
add+(X+) β Xβ Polymorphic addition functionAdds numbers, strings, chars, sequences, maps, sets and functions
-
sub+(X+) β Xβ Polymorphic subtraction functionSubtracts numbers, strings, chars, sequences, maps and sets
-
mul+(X+) β Xβ Polymorphic multiplication functionMultiplies numbers, strings and sequences
-
digits(Str) β [Int]β Convert a string of digits to a sequence of numbers -
floor(Num) β Numβ Alias of CM/floorRound down to the nearest integer
-
pow(Num Num+) β Numβ Raise a number to a power sqr(Num) β Numβ Square a numbercube(Num) β Numβ Cube a numbersqrt(Num) β Numβ Square root of a numbersum([Num]) β Numβ Sum a sequence of numbers
See also: https://clojure.github.io/clojure/clojure.math-api.html
Infix operatorsπ
.β For chaining functions (foo.bar.baz()):β Thisfoo:baris same asfoo.bar()+β Foradd+-β Forsub+*β Formul+/β Fordiv**β Forpow..β Forrng=~β Forre-find!~β Forre-find + not==β Foreq!=β Forne>β Forgt>=β Forge<β Forlt<=β Forle&&β Forand||β Foror&&&β Forand?|||β Foror?
Chaining short formsπ
value.#β Short forvalue.count()value.$β Short forvalue.last()value.++β Short forvalue.inc()value.--β Short forvalue.dec()value.?β Short forvalue.truey?()value.!β Short forvalue.falsey?()value.??β Short forvalue.boolean()value.!!β Short forvalue.not()value.@β Short for `value.deref()-
value.>>>β Short forvalue.DBG()Print value/data to stderr and return the value unchanged
Control functionsπ
-
call(Fn X*) β Xβ Call a function or valueFunction can be a string, symbol or function
-
die(Msg)β Idiomatic error function each(Bindings Body) β Xβ Non-lazyclojure.core/foreval(Str) β Xβ Evaluate a string as YS codeexit(RC=0)β Exit the programif(Cond Then Else) β Xβ Functional if used in dot chainingsleep(Secs) β Xβ Sleep for a number of seconds-
value(X) β Xβ Get var value from var, symbol or stringOften used to convert a string to a function.
-
when+(Cond Body) β Xβ Likeclojure.core/whenBinds the result of the condition to the
_symbol
See also: https://clojuredocs.org/quickref#flow-control
Function functionsπ
defn flip(Fn) β Xβ Flip the arguments of a function
Regex functionsπ
=~ Xβ Infix re-find operator!~ Xβ Infix re-find complement operator
See also: https://clojuredocs.org/quickref#regular-expressions
I/O functionsπ
err(Str*) β nilβ Print to stderr-
out(Str*) β nilβ Print to stdoutFlushes stdout after printing
-
pp(X) β nilβ Pretty print a value -
print(Str*) β nilβ Print to stdout without newlineFlushes stdout after printing
-
read(path) β Strβ Alias ofclojure.core/slurpRead a file into a string
-
say(Str*) β nilβ Print to stdout with newline -
warn(Str*) β nilβ Print to stderr with newlineFlushes stderr after printing
-
write(path Str) β nilβ Alias ofclojure.core/spitWrite a string to a file
Shorter named alias functionsπ
a(X) β Xβ Alias ofclojure.core/identitylen(X) β Intβ Alias ofclojure.core/count
Quoting functionsπ
q(form) β Xβ Alias ofclojure.core/quoteqr(str) β Rgxβ Alias ofclojure.core/re-patternqw(symbols) β [Str]β Turn symbols into a vector of strings
Named function for infix operatorsπ
eqβ Alias ofclojure.core/=neβ Alias ofclojure.core/not=gtβ Alias ofclojure.core/>geβ Alias ofclojure.core/>=ltβ Alias ofclojure.core/<leβ Alias ofclojure.core/<=
Common type conversion functionsπ
to-bool(X) β Bβ Convert X to a booleanto-char(X) β Chrβ Convert X to a characterto-float(X) β Fltβ Convert X to a floatto-int(X) β Intβ Convert X to an integerto-keyw(X) β Kwdβ Convert X to a keywordto-list(X) β Listβ Convert X to a listto-map(X) β Mapβ Convert X to a mapto-num(X) β Numβ Convert X to a numberto-omap(X) β Omapβ Convert X to an ordered mapto-set(X) β Setβ Convert X to a setto-str(X) β Strβ Convert X to a string-
to-type(X) β Strβ Convert X to a string name of its type:"atom","bool","char","class","float","fun","int","keyw","list","map","nil","num","rgx","seq","set","str","sym","var","vec" -
to-vec(X) β Vecβ Convert X to a vector
Single character casting functionsπ
B(X)β Convert to a booleanC(X)β Convert to a characterD(X)β Deref an atomF(X)β Convert to a floatI(X)β Convert to an integerK(X)β Convert to a keywordL(X)β Convert to a listM(X)β Convert to a mapN(X)β Convert to a numberO(X)β Convert to an ordered mapS(X)β Convert to a setT(X)β Convert to a type name stringV(X)β Convert to a vectorL+(X*) β Listβ Convert to a listM+(X*) β Mapβ Convert to a mapO+(X*) β Omapβ Convert to an ordered mapV+(X*) β Vecβ Convert to a vector
Alternate truth functionsπ
falsey?(X) β Xβ True if X is falsey - 0, nil, false, emptyF?(X)β Short forfalsey?truey?(X) β Xβ True if X is not falseyT?(X)β Short fortruey?or?(X X+) β Xβ Return first truey value or niland?(X X+) β Xβ Return last truey value or nil
File system functionsπ
fs-d(Path) β Bβ True if path is a directoryfs-e(Path) β Bβ True if path existsfs-f(Path) β Bβ True if path is a regular filefs-l(Path) β Bβ True if path is a symbolic linkfs-r(Path) β Bβ True if path is readablefs-s(Path) β Bβ True if path is not emptyfs-w(Path) β Bβ True if path is writablefs-x(Path) β Bβ True if path is executablefs-z(Path) β Bβ True if path is emptyfs-abs(Path) β Pathβ Get the absolute pathfs-abs?(Path) β Bβ True if path is absolutefs-basename(Path Ext?) β Bβ Get the file name of a path, without extensionfs-dirname(Path) β Dirβ Get the directory name of a pathfs-filename(Path) β Fileβ Get the file name of a pathfs-glob(Path) β [Path]β Glob a pathfs-ls(dir) β [File]β List a directoryfs-mtime(file) β Intβ Get the modification time of a filefs-rel(Path) β Pathβ Get the relative pathfs-rel?(Path) β Bβ True if path is relativefs-which(name) β Bβ Find the path of an executable
See also: https://github.com/babashka/fs/blob/master/API.md
Date/Time functionsπ
now() β Instantβ Returnsjava.time.Instantobject
Security functionsπ
md5(S) β Xβ Calculate the MD5 hash of a stringsha1(S) β Xβ Calculate the SHA-1 hash of a stringsha256(S) β Xβ Calculate the SHA-256 hash of a string
IPC functionsπ
exec(Cmd Str*) β Resultβ Execute a commandprocess(Cmd Str*) β Resultβ Execute a commandsh(Cmd Str*) β Resultβ Execute a commandshell(Cmd Str*) β Resultβ Execute a commandshout(Cmd Str*) β Resultβ Execute a command and return the output
See also: https://github.com/babashka/process#readme
External library functionsπ
use-pod(pod-name version) β nilβ Load an external library pod
HTTP functionsπ
curl(URL) β Strβ Get a URL and return the body
YAML document result stashing functionsπ
-
stream() β Xβ Mapping of document resultsThe symbol
_at the top level compiles to(stream)
Atom functionsπ
atom() β Atomβ Create an atom with a nil valueatom(X) β Atomβ Create an atom with a value X-
reset(Atom X) β Xβ Alias ofclojure.core/reset!Set the value of an atom
-
swap(Atom Fn Arg*) β Xβ Alias ofclojure.core/swap!Update the value of an atom
Special functionsπ
-
source(X*) β nilβ Run a YS file as a Bash scriptActs like
clojure.core/comment.Does nothing in YS. Intended for Bash.
See Alsoπ
- The
ys::stdsource code - The
clojure::coresource code