ys::std — The 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🔗
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🔗
See FS Library
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(Opts? Cmd Str*) → Result— Execute a commandprocess(Opts? Cmd Str*) → Result— Execute a commandsh(Opts? Cmd Str*) → Result— Execute a commandshell(Opts? Cmd Str*) → Result— Execute a commandsh-out(Opts? Cmd Str*) → Result— Execute a command and return the outputbash(Opts? Cmd Str*) → Result— Command wrapped withbash -c '...'bash-out(Opts? Cmd Str*) → Result— Runbashand return output
Opts is an optional mapping that supports the following options:
:in- stdin for command:out- file to redirect stdout to:err- file to redirect stderr to:dir- working directory:env- environment variables to use
See also: https://github.com/babashka/process/blob/master/API.md#babashka.process/process
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