YS 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🔗
-
blank?(S)
— Alias ofclojure.string/blank?
⧉True if string is nil, empty or only whitespace
-
chomp(S)
— Alias ofclojure.string/trim-newline
⧉Remove trailing newlines
-
chop(S)
— Remove last character -
ends?(S)
— Alias ofclojure.string/ends-with?
⧉True if string ends with suffix
-
escape(S char-map)
— Alias ofclojure.string/escape
⧉Escape special characters in a string
-
index(S sub)
— Alias ofclojure.string/index-of
⧉Find index of substring
-
join(*X)
— Join strings or seqs with""
join(sep *X)
— Join strings or seqs with a separatorjoins(*X)
— Join strings with" "
-
lc(S)
— Alias ofclojure.string/lower-case
⧉Lowercase a string
-
lines(S)
— Split a string into lines pretty(X)
— Pretty print a valuereplace(X)
— Alias ofclojure.core/replace
⧉-
replace(S old)
— Likeclojure.string/replace
⧉Replace all occurrences of old with
""
-
replace(S old new)
— Alias ofclojure.string/replace
⧉Replace all occurrences of old with new
-
replace1(S old new)
— Alias ofclojure.string/replace-first
⧉Replace first occurrence of old with new
-
rindex(S sub)
— Alias ofclojure.string/last-index-of
⧉Find last index of substring
-
split(S)
— Likeclojure.string/split
⧉Split on
""
-
split(S rgx)
— Alias ofclojure.string/split
⧉Split a string by a regex
-
starts?(S)
— Alias ofclojure.string/starts-with?
⧉True if string starts with prefix
-
substr(S off len?)
— 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(*S)
— Join list of strings with newlines, adding a newline at the end -
trim(S)
— Alias ofclojure.string/trim
⧉Trim whitespace from both ends
-
triml(S)
— Alias ofclojure.string/triml
⧉Trim whitespace from left end
-
trimr(S)
— Alias ofclojure.string/trimr
⧉Trim whitespace from right end
-
uc(S)
— Alias ofclojure.string/upper-case
⧉Uppercase a string
-
uc1(S)
— Alias ofclojure.string/capitalize
⧉Uppercase the first character
-
words(S)
— Split a string into words (split on whitespace)
See also: https://clojuredocs.org/quickref#strings-characters ⧉
Collection functions🔗
-
diff(C1 C2)
— Alias ofclojure.data/diff
⧉Return the difference of two collections
-
flat(V)
— Likeclojure.core/flatten
⧉Only flattens one level
-
get+(C K)
— Get a string, keyword or symbol from a map or sequence grep(FN C)
— Filter a collection by a predicate functionhas?(C)
— Returns a partial function closed over C-
has?(C X)
— True if collection has XWorks with strings, maps and sequences
-
in?(C)
— Returns a partial function closed over C -
in?(X C)
— True if X is in collectionWorks with strings, maps and sequences
-
omap(*X)
— Create an ordered map reverse(C)
— Reverse a string, vector or sequence-
rng(X Y)
— Create a range of numbers or characters, Y is inclusiveIf X is greater than Y, the range is descending
-
..
— Infixrng
operator slice(C *K)
— Get a sequence of values from the keys
See also: https://clojuredocs.org/quickref#collections ⧉
Math functions🔗
add(*N)
— Alias ofclojure.core/+
⧉sub(*N)
— Alias ofclojure.core/-
⧉mul(*N)
— Alias ofclojure.core/*
⧉div(*N)
— Division function that returns a float if needed-
add+(*X)
— Polymorphic addition functionAdds numbers, strings, chars, sequences, maps, sets and functions
-
sub+(*X)
— Polymorphic subtraction functionSubtracts numbers, strings, chars, sequences, maps and sets
-
mul+(*X)
— Polymorphic multiplication functionMultiplies numbers, strings and sequences
-
digits(S)
— Convert a string of digits to a sequence of numbers -
floor(X)
— Alias of CM/floorRound down to the nearest integer
-
pow(N *N)
— Raise a number to a power **
— Infixpow
operator - right associativesqr(X)
— Square a numbercube(X)
— Cube a numbersqrt(X)
— Square root of a numbersum(nums)
— 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:bar
is same asfoo.bar()
+
— Foradd+
-
— Forsub+
*
— Formul+
/
— Fordiv
**
— Forpow
..
— Forrng
==
— 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 *args)
— Call a function or valueFunction FN can be a string, symbol or function
-
die(msg)
— Idiomatic error function each(bindings *body)
— Non-lazyclojure.core/for
⧉eval(S)
— Evaluate a string as YS codeexit(rc=0)
— Exit the programif(cond then else)
— Functional if used in dot chainingsleep(secs)
— Sleep for a number of seconds-
value(X)
— Get var value from var, symbol or stringOften used to convert a string to a function.
-
when+(cond *body)
— Likeclojure.core/when
⧉Binds the result of the condition to the
_
symbol
See also: https://clojuredocs.org/quickref#flow-control ⧉
Function functions🔗
defn flip(FN)
— Flip the arguments of a function
Regex functions🔗
=~
— Infix re-find operator!~
— Infix re-find complement operator
See also: https://clojuredocs.org/quickref#regular-expressions ⧉
I/O functions🔗
err(*S)
— Print to stderr-
out(*S)
— Print to stdoutFlushes stdout after printing
-
pp(X)
— Pretty print a value -
print(*S)
— Print to stdout without newlineFlushes stdout after printing
-
read(F)
— Alias ofclojure.core/slurp
⧉Read a file into a string
-
say(*S)
— Print to stdout with newline -
warn(*S)
— Print to stderr with newlineFlushes stderr after printing
-
write(F S)
— Alias ofclojure.core/spit
⧉Write a string to a file
Shorter named alias functions🔗
a
— Alias ofclojure.core/identity
⧉len
— Alias ofclojure.core/count
⧉
Quoting functions🔗
q(form)
— Alias ofclojure.core/quote
⧉qr(str)
— Alias ofclojure.core/re-pattern
⧉qw(symbols)
— 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)
— Convert X to a booleanto-char(X)
— Convert X to a characterto-float(X)
— Convert X to a floatto-int(X)
— Convert X to an integerto-keyw(X)
— Convert X to a keywordto-list(X)
— Convert X to a listto-map(X)
— Convert X to a mapto-num(X)
— Convert X to a numberto-omap(X)
— Convert X to an ordered mapto-set(X)
— Convert X to a setto-str(X)
— Convert X to a string-
to-type(X)
— 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)
— 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)
— Convert to a listM+(*X)
— Convert to a mapO+(*X)
— Convert to an ordered mapV+(*X)
— Convert to a vector
Alternate truth functions🔗
falsey?(X)
— True if X is falsey - 0, nil, false, emptyF?(X)
— Short forfalsey?
truey?(X)
— True if X is not falseyT?(X)
— Short fortruey?
or?(X *X)
— Return first truey value or niland?(X *X)
— Return last truey value or nil|||
—or?
operator&&&
—and?
operator
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
See also: https://github.com/babashka/fs/blob/master/API.md ⧉
Date/Time functions🔗
now()
— Returnsjava.time.Instant
object
Security functions🔗
md5(S)
— Calculate the MD5 hash of a stringsha1(S)
— Calculate the SHA-1 hash of a stringsha256(S)
— Calculate the SHA-256 hash of a string
IPC functions🔗
exec(cmd *S)
— Execute a commandprocess(cmd *S)
— Execute a commandsh(cmd *S)
— Execute a commandshell(cmd *S)
— Execute a commandshout(cmd *S)
— Execute a command and return the output
See also: https://github.com/babashka/process#readme ⧉
External library functions🔗
use-pod(pod-name version)
— Load an external library pod
HTTP functions🔗
curl(url)
— Get a URL and return the body
YAML document result stashing functions🔗
-
stream()
— Mapping of document resultsThe symbol
_
at the top level compiles to(stream)
Special functions🔗
source(*)
— Run a YS file as a Bash script
See Also🔗
- The
ys::std
source code ⧉ - The
clojure::core
source code ⧉