Getting started
Remember: Yolk is currently in very early development. Expect some breakages and issues. Always have a good backup of your files before using Yolk in this stage. You have been warned.
How dotfiles are stored
Yolk manages your dotfiles by storing them in a separate directory, typically inside ~/.config/yolk
.
This allows you to keep your dotfiles in version control easily, and lets you manage your configuration from one central location.
Yolk groups dotfiles into so-called "eggs", which are packages of configuration, typically for one single application (although you can group them however you want, or even just have one egg for all your configuration files).
When an egg is "deployed", Yolk creates symlinks in the target location pointing towards the egg directory. This way, the configured appliactions will see the configuration files as they expect to see them.
To define where a set of configuration files should be deployed to, you declare each of your eggs in your main yolk configuration file. This allows you, among other things, to define a different target directory per system.
Initial setup
To get started with Yolk, you'll first need to set up the Yolk file structure.
$ yolk init
This will create the yolk directory, with a default yolk.rhai
file, and an eggs
directory.
Adding your first egg
let's say we want to manage the configuration for the alacritty
terminal emulator.
To do this, we first move our alacritty configuration into the eggs
directory:
$ mv ~/.config/alacritty ~/.config/yolk/eggs/
And then configure the corresponding egg deployment:
export let eggs = #{
alacritty: #{
targets: "~/.config/alacritty",
templates: ["alacritty.yml"],
enabled: true,
}
}
Now we can run yolk sync
!
This will set up a symlink from the target location ~/.config/alacritty
back to the alacritty egg directory ~/.config/yolk/eggs/alacritty
.
Committing your dots to git
Now, we want to make sure our dotfiles are in version control and pushed to our git host of choice.
Every interaction with git should be done through the yolk git
command.
This ensures that git sees the canonical (stable) representation of your files, and automatically performs them from within the yolk directory.
$ yolk git init
$ yolk safeguard
$ yolk git add --all
$ yolk git commit -m "Setup alacritty"
To understand what yolk safeguard
does, see safeguarding git.
You can now set up your git reomte and use git as usual -- just remember to always use yolk git
, especially when you're committing your files.
Baby's first template
Because you too are very indecisive about your terminal colors, you now decide you want to use yolk to manage your color theme for alacritty, and any other applications that you might add later. You also decide that you want to use a different color scheme on your desktop and your laptop.
To achieve this, let's first add a declaration of your color theme in your ~/.config/yolk/yolk.rhai
file:
// ... snip ...
const themes = #{
gruvbox: #{
background: "#282828",
foreground: "#ebdbb2",
},
mono: #{
background: "#000000",
foreground: "#ffffff",
},
}
export const data = #{
colors = if SYSTEM.hostname == "laptop" { themes.gruvbox } else { themes.mono }
}
Beautiful!
What we're doing here is setting up an exported table called data
, which will store any user-data we might want to refer to in our templates in the future.
We set up a field colors
, which we then set to a different color scheme depending on the hostname of the system.
Don't forget to export
any variables you might want to reference in your template tags!
Now, let's set up a template in our alacritty config file:
#...
[colors.primary]
background = "#ff0000" # {< replace_color(data.colors.background) >}
foreground = "#ff0000" # {< replace_color(data.colors.foreground) >}
# ...
Let's break down what's happening here:
Inside the comments after the color declarations, we're using "inline template tags", as indicated by the {< ... >}
syntax.
These inline tags transform whatever is before them in the line.
The tag calls the built-in replace_color
function, which looks for a hex-code and replaces it with the value from the data.colors
table.
Let's try it! Run
$ yolk sync
You will see that, your alacritty.toml
has changed, and the colors from your yolk.rhai
file have been applied, depending on your hostname.
Git concepts
Basic familiarity with git is assumed.
Safeguarding git
Yolk wraps the git CLI to ensure that git only ever interacts with your dotfiles in their canonical state. If it didn't do that, you would end up committing the local state of your dotfiles, which would conflict with their state from another machine -- which is what yolk is trying to solve.
To ensure that you're not accidentally using the regular git CLI for your dotfiles, it is recommended to "safeguard" your dotfiles' git directory. To do this, simply run
$ yolk safeguard
after cloning or initializing your dotfiles.
This simply renames the .git
directory to .yolk_git
, which means the regular git CLI won't see the repository anymore.
You are now more or less forced to use the yolk git
command instead -- which conveniently doesn't just ensure consistency of the git state,
but also works from anywhere in your filesystem!
Cloning your dotfiles
To clone your dotfiles on a new machine, simply clone the repository to .config/yolk
, and safeguard your git directory.
$ git clone <your-dots-repo> "$XDG_CONFIG_HOME/yolk"
$ yolk safeguard
After that, you can start yolk sync
ing your eggs!
Interacting with git
To stage or commit changes, get the git diff or status, you can use the yolk git
command, which behaves just like the git
CLI.
So, instead of
git status
, you runyolk git status
,git add .
, you runyolk git add --all
,git commit -m "cool changes"
, you runyolk git commit -m "cool changes
,
and so on. This ensures the files are always in the correct canonical state, and makes it possible to interact with a safeguarded git repository.
Eggs
An egg is one package of configuration, typically for one single application.
For example, your editor configuration ~/.config/nvim
would likely be one egg called nvim
.
According to your yolk.rhai
, these get deployed on your system, and may contain some templated files.
Templates in Yolk
Yolk allows you to use simple templates directly within your config files.
Those templates will be evaluated whenever you run yolk sync
or interact with git (see Git concepts).
Expressions within these templates are written in the Rhai scripting language, and have access to a couple special variables that allow you to reference your configuration and system state.
There are two main kinds of templates in Yolk: conditional templates and template tag functions.
Preparation
To make yolk evaluate your file as a template, you need to explicitly tell yolk about it.
To do this, make sure you include it in the templates
list of your eggs deployment configuration in your yolk.rhai
:
export let eggs = #{
foo: {
targets: "~/.config/foo",
templates: ["foo.toml"],
}
}
Conditionals
You can use Conditional template elements to conditionally include or exclude parts of your configuration. Let's take a look at a simple example:
# {% if SYSTEM.hostname == "epic-desktop" %}
displays = ["DP-1", "DP-2"]
# {% else %}
displays = ["eDP-1"]
# {% end %}
Once you run yolk sync
, yolk will evaluate the condition and comment out the block that doesn't apply.
For example, on your laptop, this config would be turned into:
# {% if SYSTEM.hostname == "epic-desktop" %}
#<yolk> displays = ["DP-1", "DP-2"]
# {% else %}
displays = ["eDP-1"]
# {% end %}
Template tag functions
In addition to conditionals, Yolk provides a wide variety of functions to edit your configuration, such as to insert values from, say, your color theme. These are regular rhai functions that modify their attached block of text (see below).
All of the built-in template tag functions are documented in the Rhai reference, and you can also define your own!
Different types of tags
Yolk supports three different types of tags:
- Next-line tags (
{# ... #}
): These tags operate on the line following the tag. - Inline tags (
{< ... >}
): These tags operate on everything before the tag within the same line. - Block tags (
{% ... %} ... {% end %}
): These tags operate on everything between the tag and the corresponding{% end %}
tag.
You can use whichever of these you want, wherever you want. For example, all of these do the same:
background_color = "#000000" # {< replace_color(colors.background) >}
# {# replace_color(colors.background) #}
background_color = "#000000"
# {% replace_color(colors.background) %}
background_color = "#000000"
# {% end %}
Example: Templating your color scheme
In many cases, you'll want to make specific values, such as colors or paths, be set through one central source, rather than specifying them in every config file.
Yolk allows you to do this (and more) by using various template functions.
For example, the replace_quoted
directive takes any value and replaces whatever is in quotes with the result of the expression.
# {# replace_quoted(colors.background) #}
background_color = "#000000"
# {# replace_quoted(colors.foreground) #}
foreground_color = "#ffffff"
After running yolk sync
, yolk will replace the regex patterns with the corresponding result of the Lua expression.
For example, depending on how you configured your colors
in your yolk.rhai
, this could turn into:
# {# replace_quoted(colors.background) #}
background_color = "#282828"
# {# replace_quoted(colors.foreground) #}
foreground_color = "#ebdbb2"
Yolk will refuse to evaluate directives that are non-reversible (i.e. if you replace_re
d ".*"
with foo
, as foo
will no longer match that regex pattern).
The yolk.rhai file
The yolk.rhai
file is the heart of your Yolk configuration.
It's where you define all of your eggs (packages), as well as export any variables and functionality you will then refer to inside your templates.
If you're familiar with Rhai, this is loaded as a module, imported into the global scope of your template tags.
Basic structure
The yolk.rhai
file is a Rhai script that is run by Yolk to generate your configuration.
Everything you declare in yolk.rhai
will be available to use in your templates.
Your yolk.rhai
needs to define one special variable called eggs
.
This is a map of all the eggs you have in your egg directory,
which describes where their files should be deployed to,
and which files should be treated as template files.
Let's look at an example:
export let eggs = #{
foot: #{
targets: "~/.config/foot",
templates: ["foot.ini"],
},
zsh: #{
targets: #{
".zshrc": "~/.config/zsh/.zshrc",
".zshenv": "~/.zshenv",
},
main_file: ".zshrc"
},
nvim: #{
targets: "~/.config/nvim",
// Note that you can use shell-style glob patterns for the templates list
templates: ["**/*.lua"],
},
niri: #{
targets: "~/.config/niri",
templates: ["config.kdl"],
enabled: SYSTEM.hostname == "cool-desktop",
},
alacritty: "~/.config/alacritty",
// This uses the `merge` deployment strategy, which
// will merge the directory structures during deployment,
// allowing a stow-style pattern
// of mirroring your home directory structure in your egg dir
zed: { targets: "~", strategy: "merge" }
}
now, let's break this down.
For every entry in our ~/.config/yolk/eggs
directory, we have a corresponding egg configuration here in the eggs object.
This configuration can either just be the path where the eggs files should be deployed,
or an object.
If it's an object, it can contain the following fields:
enabled
a boolean, describing whether this egg should be deployed or not. This is useful if you only want to deploy an egg on some systems, or depending on some other condition.
targets
Either the path where to deploy the egg, or an object with mappings from file inside the egg directory to the target path.
Providing the string "~/.config/foot"
is a short for #{ ".": "~/.config/foot"}
.
strategy
Either "put"
or "merge"
. Defaults to put
.
-
In put mode, yolk will create a symlink for each mapping from egg directory entry to target path. If a directory or file already exists, Yolk will refuse to create the symlink.
-
In merge mode, yolk will merge the directory structures during deployment. This means, if you want to use a stow-style approach, and have the egg directory mirror your home directory structure, you can use
"~"
(or#{".": "~"}
) as the targets value.
templates
A list of files that should be treated as templates.
This list can contain shell-style glob patterns, so *.lua
will expand to all lua files in the egg directory.
Files that are not listed here will not be edited by yolk during yolk sync
!
main_file
A path, relative to the egg directory, that will be opened when you run yolk edit <eggname>
.
Available variables
To generate your configuration depending on your system, there are a couple global variables that you can reference inside the yolk.rhai
file.
The SYSTEM
variable is a table containing data about your local system.
If the config is being executed in canonical mode, the SYSTEM
table will instead contain a fixed set of values that will be the same across all systems.
To know if you're currently in local or canonical mode, you can check the LOCAL
variable.
Tip:
To look at the contents of those variables or try out your logic, you can always use the yolk eval
command.
$ yolk eval 'print(SYSTEM)'
Conditionals
Yolk allows you to conditionally include parts of your configuration based on the state of your system. It achieves this by commenting or un-commenting blocks of your file.
Conditionals use special template tags that start with the keyword if
,
which instructs Yolk to treat the following expression as a conditional,
rather than a regular template tag function.
Multiline conditionals
The most common form of conditional block is using the multiline template tag syntax. Let's type out a simple example:
# {% if SYSTEM.hostname == "epic-desktop" %}
displays = ["DP-1", "DP-2"]
# {% if SYSTEM.hostname == "business-desktop" %}
displays = ["HDMI-1", "HDMI-2"]
# {% else %}
displays = ["eDP-1"]
# {% end %}
Now, this is of course not a valid configuration just yet, as we're setting the displays
variable thrice.
However, once you run yolk sync
, yolk will evaluate the condition and comment out the blocks that don't apply.
For example, on your laptop, this config might be turned into:
# {% if SYSTEM.hostname == "epic-desktop" %}
#<yolk> displays = ["DP-1", "DP-2"]
# {% if SYSTEM.hostname == "business-desktop" %}
#<yolk> displays = ["HDMI-1", "HDMI-2"]
# {% else %}
displays = ["eDP-1"]
# {% end %}
Note thta yolk added a special <yolk>
prefix to the comments.
Yolk conditionals will only ever add or remove comments with this prefix,
which means that you can still have regular comments in those conditional blocks.
Inline and Next-line conditionals
A more simplified version of this is also supported in inline and next-line tags:
enable_variable_refreshrate // {< if data.is_desktop() >}
// {# if data.enable_xwayland #}
spawn-at-startup "xwayland-satellite"
Custom Template Functions
Given that all of the template functions are just regular Rhai code, you might ask yourself if you can define your own template tag functions. The answer is YES!
How do template tag functions work
Template tag functions are executed in a special context, in which a function called get_yolk_text()
is availale.
This variable contains the text block that the template tag operates on.
A template tag function then returns a string which yolk will replace the old text block with.
Example
Let's define a simple, useless template tag function in your yolk.rhai
.
fn scream_or_not(should_scream)
if should_scream {
get_yolk_text().to_upper()
} else {
get_yolk_text().to_lower()
}
}
That's it! Now, we can go into any templated file, and use our new template tag function.
# {# scream_or_not(SYSTEM.hostname == "loud-host") #}
screaming = "HELLO"
Function Reference
Here you can find the different functions available for use in yolk. This documentation is generated through the code, so while it should be very accurate, the type signatures might look a bit confusing in some places.
Template tag functions
Yolk template tags simply execute rhai functions that transform the block of text the tag operates on.
Quick reminder: Yolk has three different types of tags, that differ only in what text they operate on:
- Next-line tags (
{# ... #}
): These tags operate on the line following the tag. - Inline tags (
{< ... >}
): These tags operate on everything before the tag within the same line. - Block tags (
{% ... %} ... {% end %}
): These tags operate on everything between the tag and the corresponding{% end %}
tag.
Inside these tags, you can call any of Yolks template tag functions (Or, in fact, any rhai expression that returns a string).
namespace: template
replace_between
replace_between(left: &str, right: &str, replacement: &str) -> Result<String>
shorthand: rbet
.
Replaces the text between two delimiters with the replacement
.
Example
ui_font = (Arial) # {< replace_between(`(`, `)`, data.font.ui) >}
Note: we don't need to include the quotes in the replacement here.
replace_color
replace_color(replacement: &str) -> Result<String>
shorthand: rcol
.
Replaces a hex color value with a new hex color.
Example
background_color = "#282828" # {< replace_color(data.colors.bg) >}
replace_in
replace_in(between: &str, replacement: &str) -> Result<String>
shorthand: rin
.
Replaces the text between two delimiters with the replacement
.
Example
ui_font = "Arial" # {< replace_in(`"`, data.font.ui) >}
Note: we don't need to include the quotes in the replacement here.
replace_number
replace_number(replacement: Dynamic) -> Result<String>
shorthand: rnum
.
Replaces a number with another number.
Example
cursor_size = 32 # {< replace_number(data.cursor_size) >}
replace_quoted
replace_quoted(replacement: &str) -> Result<String>
shorthand: rq
.
Replaces a value between quotes with another value
Example
ui_font = "Arial" # {< replace_quoted(data.font.ui) >}
replace_re
replace_re(regex: &str, replacement: &str) -> Result<String>
shorthand: rr
.
Replaces all occurrences of a Regex pattern
with replacement
in the text.
Example
ui_font = "Arial" # {< replace_re(`".*"`, `"{data.font.ui}"`) >}
Note that the replacement value needs to contain the quotes, as those are also matched against in the regex pattern. Otherwise, we would end up with invalid toml.
replace_value
replace_value(replacement: &str) -> Result<String>
shorthand: rv
.
Replaces a value (without spaces) after a :
or a =
with another value
Example
ui_font = Arial # {< replace_value(data.font.ui) >}
Utility functions
A collection of utility functions
namespace: utils
color_hex_to_rgb
color_hex_to_rgb(hex_string: &str) -> Result<Map>
Convert a hex color string to an RGB map.
color_hex_to_rgb_str
color_hex_to_rgb_str(hex_string: &str) -> Result<String>
Convert a hex color string to an RGB string.
color_hex_to_rgba_str
color_hex_to_rgba_str(hex_string: &str) -> Result<String>
Convert a hex color string to an RGBA string.
color_rgb_to_hex
color_rgb_to_hex(rgb_table: Map) -> Result<String>
Convert an RGB map to a hex color string.
regex_captures
regex_captures(pattern: &str, s: &str) -> Result<Option<Vec<String>>>
Match a string against a regex pattern and return the capture groups as a list.
regex_match
regex_match(pattern: &str, haystack: &str) -> Result<bool>
Check if a given string matches a given regex pattern.
regex_replace
regex_replace(pattern: &str, haystack: &str, replacement: &str) -> Result<String>
Replace a regex pattern in a string with a replacement.
IO Functions
A collection of functions that can read the environment and filesystem. These return standardized values in canonical mode.
namespace: io
command_available
command_available(name: &str) -> Result<bool>
Check if a given command is available
env
env(name: &str, def: &str) -> Result<String>
Read an environment variable, or return the given default
path_exists
path_exists(p: &str) -> Result<bool>
Check if something exists at a given path
path_is_dir
path_is_dir(p: &str) -> Result<bool>
Check if the given path is a directory
path_is_file
path_is_file(p: &str) -> Result<bool>
Check if the given path is a file
read_dir
read_dir(p: &str) -> Result<Vec<String>>
Read the children of a given dir
read_file
read_file(p: &str) -> Result<String>
Read the contents of a given file
Rhai Standard Library builtins
Rhai standard library functions.
Note that the typesignatures here do look a bit weird. This is simply a result of how we generate the documentation, and can't easily be improved.
Just try your best to ignore it...
namespace: global
Array.is_empty
get$is_empty(array: &mut Array) -> bool
Return true if the array is empty.
Array.len
get$len(array: &mut Array) -> i64
Number of elements in the array.
Blob.is_empty
get$is_empty(blob: &mut Blob) -> bool
Return true if the BLOB is empty.
Blob.len
get$len(blob: &mut Blob) -> i64
Return the length of the BLOB.
Example
let b = blob(10, 0x42);
print(b); // prints "[4242424242424242 4242]"
print(b.len()); // prints 10
E
E() -> f64
Return the natural number e.
Instant.elapsed
get$elapsed(timestamp: Instant) -> Result<Dynamic, Box<EvalAltResult>>
Return the number of seconds between the current system time and the timestamp.
Example
let now = timestamp();
sleep(10.0); // sleep for 10 seconds
print(now.elapsed); // prints 10.???
PI
PI() -> f64
Return the number π.
Range.end
get$end(range: &mut Range<i64>) -> i64
Return the end of the exclusive range.
Range.is_empty
get$is_empty(range: &mut Range<i64>) -> bool
Return true if the range contains no items.
Range.is_exclusive
get$is_exclusive(range: &mut Range<i64>) -> bool
Return true
if the range is exclusive.
Range.is_inclusive
get$is_inclusive(range: &mut Range<i64>) -> bool
Return true
if the range is inclusive.
Range.start
get$start(range: &mut Range<i64>) -> i64
Return the start of the exclusive range.
RangeInclusive.end
get$end(range: &mut RangeInclusive<i64>) -> i64
Return the end of the inclusive range.
RangeInclusive.is_empty
get$is_empty(range: &mut RangeInclusive<i64>) -> bool
Return true if the range contains no items.
RangeInclusive.is_exclusive
get$is_exclusive(range: &mut RangeInclusive<i64>) -> bool
Return true
if the range is exclusive.
RangeInclusive.is_inclusive
get$is_inclusive(range: &mut RangeInclusive<i64>) -> bool
Return true
if the range is inclusive.
RangeInclusive.start
get$start(range: &mut RangeInclusive<i64>) -> i64
Return the start of the inclusive range.
String.bytes
get$bytes(string: &str) -> i64
Return the length of the string, in number of bytes used to store it in UTF-8 encoding.
Example
let text = "朝には紅顔ありて夕べには白骨となる";
print(text.bytes); // prints 51
String.chars
get$chars(string: &str) -> CharsStream
Return an iterator over all the characters in the string.
Example
for ch in "hello, world!".chars {"
print(ch);
}
String.is_empty
get$is_empty(string: &str) -> bool
Return true if the string is empty.
String.len
get$len(string: &str) -> i64
Return the length of the string, in number of characters.
Example
let text = "朝には紅顔ありて夕べには白骨となる";
print(text.len); // prints 17
abs
abs(x: i32) -> Result<i32, Box<EvalAltResult>>
abs(x: i128) -> Result<i128, Box<EvalAltResult>>
abs(x: i16) -> Result<i16, Box<EvalAltResult>>
abs(x: i8) -> Result<i8, Box<EvalAltResult>>
abs(x: f32) -> f32
abs(x: f64) -> f64
abs(x: i64) -> Result<i64, Box<EvalAltResult>>
Return the absolute value of the number.
acos
acos(x: f64) -> f64
Return the arc-cosine of the floating-point number, in radians.
acosh
acosh(x: f64) -> f64
Return the arc-hyperbolic-cosine of the floating-point number, in radians.
all
all(array: &mut Array, filter: FnPtr) -> Result<bool, Box<EvalAltResult>>
all(array: &mut Array, filter: &str) -> Result<bool, Box<EvalAltResult>>
Return true
if all elements in the array return true
when applied the filter
function.
No Function Parameter
Array element (mutable) is bound to this
.
This method is marked pure; the filter
function should not mutate array elements.
Function Parameters
element
: copy of array elementindex
(optional): current index in the array
Example
let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
print(x.all(|v| v > 3)); // prints false
print(x.all(|v| v > 1)); // prints true
print(x.all(|v, i| i > v)); // prints false
append
append(array: &mut Array, new_array: Array)
append(blob: &mut Blob, character: char)
append(string: &mut ImmutableString, mut item: Dynamic)
append(string: &mut ImmutableString, utf8: Blob)
append(blob1: &mut Blob, blob2: Blob)
append(blob: &mut Blob, string: &str)
append(blob: &mut Blob, value: i64)
Add all the elements of another array to the end of the array.
Example
let x = [1, 2, 3];
let y = [true, 'x'];
x.append(y);
print(x); // prints "[1, 2, 3, true, 'x']"
as_string
as_string(blob: Blob) -> String
Convert the BLOB into a string.
The byte stream must be valid UTF-8, otherwise an error is raised.
Example
let b = blob(5, 0x42);
let x = b.as_string();
print(x); // prints "FFFFF"
asin
asin(x: f64) -> f64
Return the arc-sine of the floating-point number, in radians.
asinh
asinh(x: f64) -> f64
Return the arc-hyperbolic-sine of the floating-point number, in radians.
atan
atan(x: f64) -> f64
atan(x: f64, y: f64) -> f64
Return the arc-tangent of the floating-point number, in radians.
atanh
atanh(x: f64) -> f64
Return the arc-hyperbolic-tangent of the floating-point number, in radians.
bits
bits(value: i64) -> Result<BitRange, Box<EvalAltResult>>
bits(value: i64, range: RangeInclusive<i64>) -> Result<BitRange, Box<EvalAltResult>>
bits(value: i64, from: i64) -> Result<BitRange, Box<EvalAltResult>>
bits(value: i64, range: Range<i64>) -> Result<BitRange, Box<EvalAltResult>>
bits(value: i64, from: i64, len: i64) -> Result<BitRange, Box<EvalAltResult>>
Return an iterator over all the bits in the number.
Example
let x = 123456;
for bit in x.bits() {
print(bit);
}
blob
blob() -> Blob
blob(len: i64) -> Result<Blob, Box<EvalAltResult>>
blob(len: i64, value: i64) -> Result<Blob, Box<EvalAltResult>>
Return a new, empty BLOB.
bytes
bytes(string: &str) -> i64
Return the length of the string, in number of bytes used to store it in UTF-8 encoding.
Example
let text = "朝には紅顔ありて夕べには白骨となる";
print(text.bytes); // prints 51
ceiling
ceiling(x: f64) -> f64
Return the smallest whole number larger than or equals to the floating-point number.
chars
chars(string: &str) -> CharsStream
chars(string: &str, range: Range<i64>) -> CharsStream
chars(string: &str, start: i64) -> CharsStream
chars(string: &str, range: RangeInclusive<i64>) -> CharsStream
chars(string: &str, start: i64, len: i64) -> CharsStream
Return an iterator over the characters in the string.
Example
for ch in "hello, world!".chars() {
print(ch);
}
chop
chop(blob: &mut Blob, len: i64)
chop(array: &mut Array, len: i64)
Cut off the head of the BLOB, leaving a tail of the specified length.
- If
len
≤ 0, the BLOB is cleared. - If
len
≥ length of BLOB, the BLOB is not modified.
Example
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
b.chop(3);
print(b); // prints "[030405]"
b.chop(10);
print(b); // prints "[030405]"
clear
clear(string: &mut ImmutableString)
clear(array: &mut Array)
clear(map: &mut Map)
clear(blob: &mut Blob)
Clear the string, making it empty.
contains
contains(array: &mut Array, value: Dynamic) -> Result<bool, Box<EvalAltResult>>
contains(string: &str, match_string: &str) -> bool
contains(range: &mut Range<i64>, value: i64) -> bool
contains(blob: &mut Blob, value: i64) -> bool
contains(range: &mut RangeInclusive<i64>, value: i64) -> bool
contains(map: &mut Map, property: &str) -> bool
contains(string: &str, character: char) -> bool
Return true
if the array contains an element that equals value
.
The operator ==
is used to compare elements with value
and must be defined,
otherwise false
is assumed.
This function also drives the in
operator.
Example
let x = [1, 2, 3, 4, 5];
// The 'in' operator calls 'contains' in the background
if 4 in x {
print("found!");
}
cos
cos(x: f64) -> f64
Return the cosine of the floating-point number in radians.
cosh
cosh(x: f64) -> f64
Return the hyperbolic cosine of the floating-point number in radians.
crop
crop(string: &mut ImmutableString, range: RangeInclusive<i64>)
crop(string: &mut ImmutableString, start: i64)
crop(string: &mut ImmutableString, range: Range<i64>)
crop(string: &mut ImmutableString, start: i64, len: i64)
Remove all characters from the string except those within an inclusive range
.
Example
let text = "hello, world!";
text.crop(2..=8);
print(text); // prints "llo, wo"
debug
debug() -> ImmutableString
debug(character: char) -> ImmutableString
debug(item: &mut Dynamic) -> ImmutableString
debug(f: &mut FnPtr) -> ImmutableString
debug(value: bool) -> ImmutableString
debug(number: f32) -> ImmutableString
debug(string: &str) -> ImmutableString
debug(array: &mut Array) -> ImmutableString
debug(map: &mut Map) -> ImmutableString
debug(unit: ()) -> ImmutableString
debug(number: f64) -> ImmutableString
Return the empty string.
dedup
dedup(array: &mut Array)
dedup(array: &mut Array, comparer: FnPtr)
dedup(array: &mut Array, comparer: &str) -> Result<(), Box<EvalAltResult>>
Remove duplicated consecutive elements from the array.
The operator ==
is used to compare elements and must be defined,
otherwise false
is assumed.
Example
let x = [1, 2, 2, 2, 3, 4, 3, 3, 2, 1];
x.dedup();
print(x); // prints "[1, 2, 3, 4, 3, 2, 1]"
drain
drain(blob: &mut Blob, range: Range<i64>) -> Blob
drain(map: &mut Map, filter: FnPtr) -> Result<Map, Box<EvalAltResult>>
drain(blob: &mut Blob, range: RangeInclusive<i64>) -> Blob
drain(array: &mut Array, filter: FnPtr) -> Result<Array, Box<EvalAltResult>>
drain(array: &mut Array, range: RangeInclusive<i64>) -> Array
drain(array: &mut Array, range: Range<i64>) -> Array
drain(array: &mut Array, filter: &str) -> Result<Array, Box<EvalAltResult>>
drain(array: &mut Array, start: i64, len: i64) -> Array
drain(blob: &mut Blob, start: i64, len: i64) -> Blob
Remove all bytes in the BLOB within an exclusive range
and return them as a new BLOB.
Example
let b1 = blob();
b1 += 1; b1 += 2; b1 += 3; b1 += 4; b1 += 5;
let b2 = b1.drain(1..3);
print(b1); // prints "[010405]"
print(b2); // prints "[0203]"
let b3 = b1.drain(2..3);
print(b1); // prints "[0104]"
print(b3); // prints "[05]"
elapsed
elapsed(timestamp: Instant) -> Result<Dynamic, Box<EvalAltResult>>
Return the number of seconds between the current system time and the timestamp.
Example
let now = timestamp();
sleep(10.0); // sleep for 10 seconds
print(now.elapsed); // prints 10.???
end
end(range: &mut RangeInclusive<i64>) -> i64
end(range: &mut Range<i64>) -> i64
Return the end of the inclusive range.
ends_with
ends_with(string: &str, match_string: &str) -> bool
Return true
if the string ends with a specified string.
Example
let text = "hello, world!";
print(text.ends_with("world!")); // prints true
print(text.ends_with("hello")); // prints false
exit
exit() -> Result<Dynamic, Box<EvalAltResult>>
exit(value: Dynamic) -> Result<Dynamic, Box<EvalAltResult>>
Exit the script evaluation immediately with ()
as exit value.
Example
exit();
exp
exp(x: f64) -> f64
Return the exponential of the floating-point number.
extract
extract(blob: &mut Blob, range: Range<i64>) -> Blob
extract(array: &mut Array, start: i64) -> Array
extract(blob: &mut Blob, range: RangeInclusive<i64>) -> Blob
extract(array: &mut Array, range: RangeInclusive<i64>) -> Array
extract(array: &mut Array, range: Range<i64>) -> Array
extract(blob: &mut Blob, start: i64) -> Blob
extract(array: &mut Array, start: i64, len: i64) -> Array
extract(blob: &mut Blob, start: i64, len: i64) -> Blob
Copy an exclusive range
of the BLOB and return it as a new BLOB.
Example
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
print(b.extract(1..3)); // prints "[0203]"
print(b); // prints "[0102030405]"
f32.is_zero
get$is_zero(x: f32) -> bool
Return true if the floating-point number is zero.
fill_with
fill_with(map: &mut Map, map2: Map)
Add all property values of another object map into the object map. Only properties that do not originally exist in the object map are added.
Example
let m = #{a:1, b:2, c:3};
let n = #{a: 42, d:0};
m.fill_with(n);
print(m); // prints "#{a:1, b:2, c:3, d:0}"
filter
filter(map: &mut Map, filter: FnPtr) -> Result<Map, Box<EvalAltResult>>
filter(array: &mut Array, filter: FnPtr) -> Result<Array, Box<EvalAltResult>>
filter(array: &mut Array, filter_func: &str) -> Result<Array, Box<EvalAltResult>>
Iterate through all the elements in the object map, applying a filter
function to each
and return a new collection of all elements that return true
as a new object map.
Function Parameters
key
: current keyvalue
(optional): copy of element (bound tothis
if omitted)
Example
let x = #{a:1, b:2, c:3, d:4, e:5};
let y = x.filter(|k| this >= 3);
print(y); // prints #{"c":3, "d":4, "e":5}
let y = x.filter(|k, v| k != "d" && v < 5);
print(y); // prints #{"a":1, "b":2, "c":3}
find
find(array: &mut Array, filter: FnPtr) -> Result<Dynamic, Box<EvalAltResult>>
find(array: &mut Array, filter: FnPtr, start: i64) -> Result<Dynamic, Box<EvalAltResult>>
Iterate through all the elements in the array, applying a filter
function to each element
in turn, and return a copy of the first element that returns true
. If no element returns
true
, ()
is returned.
No Function Parameter
Array element (mutable) is bound to this
.
Function Parameters
element
: copy of array elementindex
(optional): current index in the array
Example
let x = [1, 2, 3, 5, 8, 13];
print(x.find(|v| v > 3)); // prints 5: 5 > 3
print(x.find(|v| v > 13) ?? "not found"); // prints "not found": nothing is > 13
print(x.find(|v, i| v * i > 13)); // prints 5: 3 * 5 > 13
find_map
find_map(array: &mut Array, filter: FnPtr) -> Result<Dynamic, Box<EvalAltResult>>
find_map(array: &mut Array, filter: FnPtr, start: i64) -> Result<Dynamic, Box<EvalAltResult>>
Iterate through all the elements in the array, applying a mapper
function to each element
in turn, and return the first result that is not ()
. Otherwise, ()
is returned.
No Function Parameter
Array element (mutable) is bound to this
.
This method is marked pure; the mapper
function should not mutate array elements.
Function Parameters
element
: copy of array elementindex
(optional): current index in the array
Example
let x = [#{alice: 1}, #{bob: 2}, #{clara: 3}];
print(x.find_map(|v| v.alice)); // prints 1
print(x.find_map(|v| v.dave) ?? "not found"); // prints "not found"
print(x.find_map(|| this.dave) ?? "not found"); // prints "not found"
float.ceiling
get$ceiling(x: f64) -> f64
Return the smallest whole number larger than or equals to the floating-point number.
float.floor
get$floor(x: f64) -> f64
Return the largest whole number less than or equals to the floating-point number.
float.fraction
get$fraction(x: f64) -> f64
Return the fractional part of the floating-point number.
float.int
get$int(x: f64) -> f64
Return the integral part of the floating-point number.
float.is_finite
get$is_finite(x: f64) -> bool
Return true
if the floating-point number is finite.
float.is_infinite
get$is_infinite(x: f64) -> bool
Return true
if the floating-point number is infinite.
float.is_nan
get$is_nan(x: f64) -> bool
Return true
if the floating-point number is NaN
(Not A Number).
float.is_zero
get$is_zero(x: f64) -> bool
Return true if the floating-point number is zero.
float.round
get$round(x: f64) -> f64
Return the nearest whole number closest to the floating-point number. Rounds away from zero.
floor
floor(x: f64) -> f64
Return the largest whole number less than or equals to the floating-point number.
for_each
for_each(array: &mut Array, map: FnPtr) -> Result<(), Box<EvalAltResult>>
Iterate through all the elements in the array, applying a process
function to each element in turn.
Each element is bound to this
before calling the function.
Function Parameters
this
: bound to array element (mutable)index
(optional): current index in the array
Example
let x = [1, 2, 3, 4, 5];
x.for_each(|| this *= this);
print(x); // prints "[1, 4, 9, 16, 25]"
x.for_each(|i| this *= i);
print(x); // prints "[0, 2, 6, 12, 20]"
fraction
fraction(x: f64) -> f64
Return the fractional part of the floating-point number.
get
get(blob: &mut Blob, index: i64) -> i64
get(string: &str, index: i64) -> Dynamic
get(map: &mut Map, property: &str) -> Dynamic
get(array: &mut Array, index: i64) -> Dynamic
Get the byte value at the index
position in the BLOB.
- If
index
< 0, position counts from the end of the BLOB (-1
is the last element). - If
index
< -length of BLOB, zero is returned. - If
index
≥ length of BLOB, zero is returned.
Example
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
print(b.get(0)); // prints 1
print(b.get(-1)); // prints 5
print(b.get(99)); // prints 0
get_bit
get_bit(value: i64, bit: i64) -> Result<bool, Box<EvalAltResult>>
Return true
if the specified bit
in the number is set.
If bit
< 0, position counts from the MSB (Most Significant Bit).
Example
let x = 123456;
print(x.get_bit(5)); // prints false
print(x.get_bit(6)); // prints true
print(x.get_bit(-48)); // prints true on 64-bit
get_bits
get_bits(value: i64, range: RangeInclusive<i64>) -> Result<i64, Box<EvalAltResult>>
get_bits(value: i64, range: Range<i64>) -> Result<i64, Box<EvalAltResult>>
get_bits(value: i64, start: i64, bits: i64) -> Result<i64, Box<EvalAltResult>>
Return an inclusive range of bits in the number as a new number.
Example
let x = 123456;
print(x.get_bits(5..=9)); // print 18
get_fn_metadata_list
get_fn_metadata_list() -> Array
get_fn_metadata_list(name: &str) -> Array
get_fn_metadata_list(name: &str, params: i64) -> Array
Return an array of object maps containing metadata of all script-defined functions.
hypot
hypot(x: f64, y: f64) -> f64
Return the hypotenuse of a triangle with sides x
and y
.
index_of
index_of(array: &mut Array, value: Dynamic) -> Result<i64, Box<EvalAltResult>>
index_of(array: &mut Array, filter: FnPtr) -> Result<i64, Box<EvalAltResult>>
index_of(string: &str, find_string: &str) -> i64
index_of(array: &mut Array, filter: &str) -> Result<i64, Box<EvalAltResult>>
index_of(string: &str, character: char) -> i64
index_of(array: &mut Array, filter: &str, start: i64) -> Result<i64, Box<EvalAltResult>>
index_of(array: &mut Array, value: Dynamic, start: i64) -> Result<i64, Box<EvalAltResult>>
index_of(string: &str, character: char, start: i64) -> i64
index_of(string: &str, find_string: &str, start: i64) -> i64
index_of(array: &mut Array, filter: FnPtr, start: i64) -> Result<i64, Box<EvalAltResult>>
Find the first element in the array that equals a particular value
and return its index.
If no element equals value
, -1
is returned.
The operator ==
is used to compare elements with value
and must be defined,
otherwise false
is assumed.
Example
let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
print(x.index_of(4)); // prints 3 (first index)
print(x.index_of(9)); // prints -1
print(x.index_of("foo")); // prints -1: strings do not equal numbers
insert
insert(blob: &mut Blob, index: i64, value: i64)
insert(array: &mut Array, index: i64, item: Dynamic)
Add a byte value
to the BLOB at a particular index
position.
- If
index
< 0, position counts from the end of the BLOB (-1
is the last byte). - If
index
< -length of BLOB, the byte value is added to the beginning of the BLOB. - If
index
≥ length of BLOB, the byte value is appended to the end of the BLOB.
Only the lower 8 bits of the value
are used; all other bits are ignored.
Example
let b = blob(5, 0x42);
b.insert(2, 0x18);
print(b); // prints "[4242184242]"
int
int(x: f64) -> f64
Return the integral part of the floating-point number.
int.bits
get$bits(value: i64) -> Result<BitRange, Box<EvalAltResult>>
Return an iterator over all the bits in the number.
Example
let x = 123456;
for bit in x.bits {
print(bit);
}
int.is_even
get$is_even(x: i64) -> bool
Return true if the number is even.
int.is_odd
get$is_odd(x: i64) -> bool
Return true if the number is odd.
int.is_zero
get$is_zero(x: i64) -> bool
Return true if the number is zero.
is_anonymous
is_anonymous(fn_ptr: &mut FnPtr) -> bool
Return true
if the function is an anonymous function.
Example
let f = |x| x * 2;
print(f.is_anonymous); // prints true
is_empty
is_empty(blob: &mut Blob) -> bool
is_empty(array: &mut Array) -> bool
is_empty(string: &str) -> bool
is_empty(map: &mut Map) -> bool
is_empty(range: &mut Range<i64>) -> bool
is_empty(range: &mut RangeInclusive<i64>) -> bool
Return true if the BLOB is empty.
is_even
is_even(x: i32) -> bool
is_even(x: u16) -> bool
is_even(x: i16) -> bool
is_even(x: i128) -> bool
is_even(x: i8) -> bool
is_even(x: u32) -> bool
is_even(x: u8) -> bool
is_even(x: u128) -> bool
is_even(x: i64) -> bool
is_even(x: u64) -> bool
Return true if the number is even.
is_exclusive
is_exclusive(range: &mut RangeInclusive<i64>) -> bool
is_exclusive(range: &mut Range<i64>) -> bool
Return true
if the range is exclusive.
is_finite
is_finite(x: f64) -> bool
Return true
if the floating-point number is finite.
is_inclusive
is_inclusive(range: &mut RangeInclusive<i64>) -> bool
is_inclusive(range: &mut Range<i64>) -> bool
Return true
if the range is inclusive.
is_infinite
is_infinite(x: f64) -> bool
Return true
if the floating-point number is infinite.
is_nan
is_nan(x: f64) -> bool
Return true
if the floating-point number is NaN
(Not A Number).
is_odd
is_odd(x: i8) -> bool
is_odd(x: u128) -> bool
is_odd(x: u8) -> bool
is_odd(x: u32) -> bool
is_odd(x: i64) -> bool
is_odd(x: u64) -> bool
is_odd(x: i32) -> bool
is_odd(x: i16) -> bool
is_odd(x: u16) -> bool
is_odd(x: i128) -> bool
Return true if the number is odd.
is_zero
is_zero(x: i64) -> bool
is_zero(x: f64) -> bool
is_zero(x: u64) -> bool
is_zero(x: f32) -> bool
is_zero(x: i8) -> bool
is_zero(x: u8) -> bool
is_zero(x: u128) -> bool
is_zero(x: u32) -> bool
is_zero(x: i16) -> bool
is_zero(x: u16) -> bool
is_zero(x: i128) -> bool
is_zero(x: i32) -> bool
Return true if the number is zero.
keys
keys(map: &mut Map) -> Array
Return an array with all the property names in the object map.
Example
let m = #{a:1, b:2, c:3};
print(m.keys()); // prints ["a", "b", "c"]
len
len(string: &str) -> i64
len(array: &mut Array) -> i64
len(map: &mut Map) -> i64
len(blob: &mut Blob) -> i64
Return the length of the string, in number of characters.
Example
let text = "朝には紅顔ありて夕べには白骨となる";
print(text.len); // prints 17
ln
ln(x: f64) -> f64
Return the natural log of the floating-point number.
log
log(x: f64) -> f64
log(x: f64, base: f64) -> f64
Return the log of the floating-point number with base 10.
make_lower
make_lower(character: &mut char)
make_lower(string: &mut ImmutableString)
Convert the character to lower-case.
Example
let ch = 'A';
ch.make_lower();
print(ch); // prints 'a'
make_upper
make_upper(character: &mut char)
make_upper(string: &mut ImmutableString)
Convert the character to upper-case.
Example
let ch = 'a';
ch.make_upper();
print(ch); // prints 'A'
map
map(array: &mut Array, map: FnPtr) -> Result<Array, Box<EvalAltResult>>
map(array: &mut Array, mapper: &str) -> Result<Array, Box<EvalAltResult>>
Iterate through all the elements in the array, applying a mapper
function to each element
in turn, and return the results as a new array.
No Function Parameter
Array element (mutable) is bound to this
.
This method is marked pure; the mapper
function should not mutate array elements.
Function Parameters
element
: copy of array elementindex
(optional): current index in the array
Example
let x = [1, 2, 3, 4, 5];
let y = x.map(|v| v * v);
print(y); // prints "[1, 4, 9, 16, 25]"
let y = x.map(|v, i| v * i);
print(y); // prints "[0, 2, 6, 12, 20]"
max
max(x: f64, y: f64) -> f64
max(x: i32, y: i32) -> i32
max(x: u128, y: u128) -> u128
max(x: i8, y: i8) -> i8
max(char1: char, char2: char) -> char
max(x: f32, y: i64) -> f32
max(x: i16, y: i16) -> i16
max(x: f32, y: f32) -> f32
max(x: u8, y: u8) -> u8
max(x: u64, y: u64) -> u64
max(x: f64, y: f32) -> f64
max(x: u32, y: u32) -> u32
max(string1: ImmutableString, string2: ImmutableString) -> ImmutableString
max(x: f32, y: f64) -> f64
max(x: i64, y: f64) -> f64
max(x: i64, y: f32) -> f32
max(x: i64, y: i64) -> i64
max(x: u16, y: u16) -> u16
max(x: f64, y: i64) -> f64
max(x: i128, y: i128) -> i128
Return the character that is lexically greater than the other character.
Example
max('h', 'w'); // returns 'w'
min
min(string1: ImmutableString, string2: ImmutableString) -> ImmutableString
min(x: f32, y: i64) -> f32
min(x: f64, y: f32) -> f64
min(x: u32, y: u32) -> u32
min(x: u8, y: u8) -> u8
min(x: u64, y: u64) -> u64
min(x: i16, y: i16) -> i16
min(x: f32, y: f32) -> f32
min(x: i8, y: i8) -> i8
min(char1: char, char2: char) -> char
min(x: i32, y: i32) -> i32
min(x: u128, y: u128) -> u128
min(x: f64, y: f64) -> f64
min(x: f64, y: i64) -> f64
min(x: u16, y: u16) -> u16
min(x: i128, y: i128) -> i128
min(x: i64, y: f32) -> f32
min(x: i64, y: i64) -> i64
min(x: f32, y: f64) -> f64
min(x: i64, y: f64) -> f64
Return the string that is lexically smaller than the other string.
Example
min("hello", "world"); // returns "hello"
mixin
mixin(map: &mut Map, map2: Map)
Add all property values of another object map into the object map. Existing property values of the same names are replaced.
Example
let m = #{a:1, b:2, c:3};
let n = #{a: 42, d:0};
m.mixin(n);
print(m); // prints "#{a:42, b:2, c:3, d:0}"
name
name(fn_ptr: &mut FnPtr) -> ImmutableString
Return the name of the function.
Example
fn double(x) { x * 2 }
let f = Fn("double");
print(f.name); // prints "double"
pad
pad(string: &mut ImmutableString, len: i64, padding: &str) -> Result<(), Box<EvalAltResult>>
pad(blob: &mut Blob, len: i64, value: i64) -> Result<(), Box<EvalAltResult>>
pad(array: &mut Array, len: i64, item: Dynamic) -> Result<(), Box<EvalAltResult>>
pad(string: &mut ImmutableString, len: i64, character: char) -> Result<(), Box<EvalAltResult>>
Pad the string to at least the specified number of characters with the specified string.
If len
≤ length of string, no padding is done.
Example
let text = "hello";
text.pad(10, "(!)");
print(text); // prints "hello(!)(!)"
text.pad(8, '***');
print(text); // prints "hello(!)(!)"
parse_be_float
parse_be_float(blob: &mut Blob, range: Range<i64>) -> f64
parse_be_float(blob: &mut Blob, range: RangeInclusive<i64>) -> f64
parse_be_float(blob: &mut Blob, start: i64, len: i64) -> f64
Parse the bytes within an exclusive range
in the BLOB as a FLOAT
in big-endian byte order.
- If number of bytes in
range
< number of bytes forFLOAT
, zeros are padded. - If number of bytes in
range
> number of bytes forFLOAT
, extra bytes are ignored.
parse_be_int
parse_be_int(blob: &mut Blob, range: Range<i64>) -> i64
parse_be_int(blob: &mut Blob, range: RangeInclusive<i64>) -> i64
parse_be_int(blob: &mut Blob, start: i64, len: i64) -> i64
Parse the bytes within an exclusive range
in the BLOB as an INT
in big-endian byte order.
- If number of bytes in
range
< number of bytes forINT
, zeros are padded. - If number of bytes in
range
> number of bytes forINT
, extra bytes are ignored.
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
let x = b.parse_be_int(1..3); // parse two bytes
print(x.to_hex()); // prints "02030000...00"
parse_float
parse_float(string: &str) -> Result<f64, Box<EvalAltResult>>
Parse a string into a floating-point number.
Example
let x = parse_int("123.456");
print(x); // prints 123.456
parse_int
parse_int(string: &str) -> Result<i64, Box<EvalAltResult>>
parse_int(string: &str, radix: i64) -> Result<i64, Box<EvalAltResult>>
Parse a string into an integer number.
Example
let x = parse_int("123");
print(x); // prints 123
parse_json
parse_json(json: &str) -> Result<Dynamic, Box<EvalAltResult>>
Parse a JSON string into a value.
Example
let m = parse_json(`{"a":1, "b":2, "c":3}`);
print(m); // prints #{"a":1, "b":2, "c":3}
parse_le_float
parse_le_float(blob: &mut Blob, range: RangeInclusive<i64>) -> f64
parse_le_float(blob: &mut Blob, range: Range<i64>) -> f64
parse_le_float(blob: &mut Blob, start: i64, len: i64) -> f64
Parse the bytes within an inclusive range
in the BLOB as a FLOAT
in little-endian byte order.
- If number of bytes in
range
< number of bytes forFLOAT
, zeros are padded. - If number of bytes in
range
> number of bytes forFLOAT
, extra bytes are ignored.
parse_le_int
parse_le_int(blob: &mut Blob, range: RangeInclusive<i64>) -> i64
parse_le_int(blob: &mut Blob, range: Range<i64>) -> i64
parse_le_int(blob: &mut Blob, start: i64, len: i64) -> i64
Parse the bytes within an inclusive range
in the BLOB as an INT
in little-endian byte order.
- If number of bytes in
range
< number of bytes forINT
, zeros are padded. - If number of bytes in
range
> number of bytes forINT
, extra bytes are ignored.
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
let x = b.parse_le_int(1..=3); // parse three bytes
print(x.to_hex()); // prints "040302"
pop
pop(blob: &mut Blob) -> i64
pop(array: &mut Array) -> Dynamic
pop(string: &mut ImmutableString) -> Dynamic
pop(string: &mut ImmutableString, len: i64) -> ImmutableString
Remove the last byte from the BLOB and return it.
If the BLOB is empty, zero is returned.
Example
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
print(b.pop()); // prints 5
print(b); // prints "[01020304]"
print() -> ImmutableString
print(value: bool) -> ImmutableString
print(item: &mut Dynamic) -> ImmutableString
print(character: char) -> ImmutableString
print(number: f64) -> ImmutableString
print(unit: ()) -> ImmutableString
print(map: &mut Map) -> ImmutableString
print(array: &mut Array) -> ImmutableString
print(string: ImmutableString) -> ImmutableString
print(number: f32) -> ImmutableString
Return the empty string.
push
push(blob: &mut Blob, value: i64)
push(array: &mut Array, item: Dynamic)
Add a new byte value
to the end of the BLOB.
Only the lower 8 bits of the value
are used; all other bits are ignored.
Example
let b = blob();
b.push(0x42);
print(b); // prints "[42]"
range
range(range: std::ops::Range<i64>, step: i64) -> Result<StepRange<i64>, Box<EvalAltResult>>
range(range: std::ops::Range<u128>, step: u128) -> Result<StepRange<u128>, Box<EvalAltResult>>
range(from: u8, to: u8) -> Range<u8>
range(from: u64, to: u64) -> Range<u64>
range(range: std::ops::Range<u32>, step: u32) -> Result<StepRange<u32>, Box<EvalAltResult>>
range(from: u32, to: u32) -> Range<u32>
range(range: std::ops::Range<i16>, step: i16) -> Result<StepRange<i16>, Box<EvalAltResult>>
range(from: i16, to: i16) -> Range<i16>
range(range: std::ops::Range<i8>, step: i8) -> Result<StepRange<i8>, Box<EvalAltResult>>
range(from: i8, to: i8) -> Range<i8>
range(range: std::ops::Range<u16>, step: u16) -> Result<StepRange<u16>, Box<EvalAltResult>>
range(range: std::ops::Range<u64>, step: u64) -> Result<StepRange<u64>, Box<EvalAltResult>>
range(from: i32, to: i32) -> Range<i32>
range(from: u128, to: u128) -> Range<u128>
range(range: std::ops::Range<u8>, step: u8) -> Result<StepRange<u8>, Box<EvalAltResult>>
range(from: u16, to: u16) -> Range<u16>
range(from: i128, to: i128) -> Range<i128>
range(range: std::ops::Range<i32>, step: i32) -> Result<StepRange<i32>, Box<EvalAltResult>>
range(range: std::ops::Range<FLOAT>, step: f64) -> Result<StepRange<FLOAT>, Box<EvalAltResult>>
range(range: std::ops::Range<i128>, step: i128) -> Result<StepRange<i128>, Box<EvalAltResult>>
range(from: i64, to: i64) -> Range<i64>
range(from: u8, to: u8, step: u8) -> Result<StepRange<u8>, Box<EvalAltResult>>
range(from: i64, to: i64, step: i64) -> Result<StepRange<i64>, Box<EvalAltResult>>
range(from: u64, to: u64, step: u64) -> Result<StepRange<u64>, Box<EvalAltResult>>
range(from: f64, to: f64, step: f64) -> Result<StepRange<FLOAT>, Box<EvalAltResult>>
range(from: u32, to: u32, step: u32) -> Result<StepRange<u32>, Box<EvalAltResult>>
range(from: i8, to: i8, step: i8) -> Result<StepRange<i8>, Box<EvalAltResult>>
range(from: i32, to: i32, step: i32) -> Result<StepRange<i32>, Box<EvalAltResult>>
range(from: u128, to: u128, step: u128) -> Result<StepRange<u128>, Box<EvalAltResult>>
range(from: i16, to: i16, step: i16) -> Result<StepRange<i16>, Box<EvalAltResult>>
range(from: u16, to: u16, step: u16) -> Result<StepRange<u16>, Box<EvalAltResult>>
range(from: i128, to: i128, step: i128) -> Result<StepRange<i128>, Box<EvalAltResult>>
Return an iterator over an exclusive range, each iteration increasing by step
.
If range
is reversed and step
< 0, iteration goes backwards.
Otherwise, if range
is empty, an empty iterator is returned.
Example
// prints all values from 8 to 17 in steps of 3
for n in range(8..18, 3) {
print(n);
}
// prints all values down from 18 to 9 in steps of -3
for n in range(18..8, -3) {
print(n);
}
reduce
reduce(array: &mut Array, reducer: &str) -> Result<Dynamic, Box<EvalAltResult>>
reduce(array: &mut Array, reducer: FnPtr) -> Result<Dynamic, Box<EvalAltResult>>
reduce(array: &mut Array, reducer: FnPtr, initial: Dynamic) -> Result<Dynamic, Box<EvalAltResult>>
reduce(array: &mut Array, reducer: &str, initial: Dynamic) -> Result<Dynamic, Box<EvalAltResult>>
Reduce an array by iterating through all elements while applying a function named by reducer
.
Deprecated API
This method is deprecated and will be removed from the next major version.
Use array.reduce(Fn("fn_name"))
instead.
Function Parameters
A function with the same name as the value of reducer
must exist taking these parameters:
result
: accumulated result, initially()
element
: copy of array elementindex
(optional): current index in the array
Example
fn process(r, x) {
x + (r ?? 0)
}
fn process_extra(r, x, i) {
x + i + (r ?? 0)
}
let x = [1, 2, 3, 4, 5];
let y = x.reduce("process");
print(y); // prints 15
let y = x.reduce("process_extra");
print(y); // prints 25
reduce_rev
reduce_rev(array: &mut Array, reducer: &str) -> Result<Dynamic, Box<EvalAltResult>>
reduce_rev(array: &mut Array, reducer: FnPtr) -> Result<Dynamic, Box<EvalAltResult>>
reduce_rev(array: &mut Array, reducer: &str, initial: Dynamic) -> Result<Dynamic, Box<EvalAltResult>>
reduce_rev(array: &mut Array, reducer: FnPtr, initial: Dynamic) -> Result<Dynamic, Box<EvalAltResult>>
Reduce an array by iterating through all elements, in reverse order,
while applying a function named by reducer
.
Deprecated API
This method is deprecated and will be removed from the next major version.
Use array.reduce_rev(Fn("fn_name"))
instead.
Function Parameters
A function with the same name as the value of reducer
must exist taking these parameters:
result
: accumulated result, initially()
element
: copy of array elementindex
(optional): current index in the array
Example
fn process(r, x) {
x + (r ?? 0)
}
fn process_extra(r, x, i) {
x + i + (r ?? 0)
}
let x = [1, 2, 3, 4, 5];
let y = x.reduce_rev("process");
print(y); // prints 15
let y = x.reduce_rev("process_extra");
print(y); // prints 25
remove
remove(string: &mut ImmutableString, sub_string: &str)
remove(string: &mut ImmutableString, character: char)
remove(map: &mut Map, property: &str) -> Dynamic
remove(blob: &mut Blob, index: i64) -> i64
remove(array: &mut Array, index: i64) -> Dynamic
Remove all occurrences of a sub-string from the string.
Example
let text = "hello, world! hello, foobar!";
text.remove("hello");
print(text); // prints ", world! , foobar!"
replace
replace(string: &mut ImmutableString, find_character: char, substitute_string: &str)
replace(string: &mut ImmutableString, find_character: char, substitute_character: char)
replace(string: &mut ImmutableString, find_string: &str, substitute_character: char)
replace(string: &mut ImmutableString, find_string: &str, substitute_string: &str)
Replace all occurrences of the specified character in the string with another string.
Example
let text = "hello, world! hello, foobar!";
text.replace('l', "(^)");
print(text); // prints "he(^)(^)o, wor(^)d! he(^)(^)o, foobar!"
retain
retain(array: &mut Array, filter: &str) -> Result<Array, Box<EvalAltResult>>
retain(array: &mut Array, range: Range<i64>) -> Array
retain(blob: &mut Blob, range: RangeInclusive<i64>) -> Blob
retain(map: &mut Map, filter: FnPtr) -> Result<Map, Box<EvalAltResult>>
retain(array: &mut Array, range: RangeInclusive<i64>) -> Array
retain(array: &mut Array, filter: FnPtr) -> Result<Array, Box<EvalAltResult>>
retain(blob: &mut Blob, range: Range<i64>) -> Blob
retain(array: &mut Array, start: i64, len: i64) -> Array
retain(blob: &mut Blob, start: i64, len: i64) -> Blob
Remove all elements in the array that do not return true
when applied a function named by
filter
and return them as a new array.
Deprecated API
This method is deprecated and will be removed from the next major version.
Use array.retain(Fn("fn_name"))
instead.
Function Parameters
A function with the same name as the value of filter
must exist taking these parameters:
element
: copy of array elementindex
(optional): current index in the array
Example
fn large(x) { x >= 3 }
fn screen(x, i) { x + i <= 5 }
let x = [1, 2, 3, 4, 5];
let y = x.retain("large");
print(x); // prints "[3, 4, 5]"
print(y); // prints "[1, 2]"
let z = x.retain("screen");
print(x); // prints "[3, 4]"
print(z); // prints "[5]"
reverse
reverse(blob: &mut Blob)
reverse(array: &mut Array)
Reverse the BLOB.
Example
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
print(b); // prints "[0102030405]"
b.reverse();
print(b); // prints "[0504030201]"
round
round(x: f64) -> f64
Return the nearest whole number closest to the floating-point number. Rounds away from zero.
set
set(map: &mut Map, property: &str, value: Dynamic)
set(string: &mut ImmutableString, index: i64, character: char)
set(array: &mut Array, index: i64, value: Dynamic)
set(blob: &mut Blob, index: i64, value: i64)
Set the value of the property
in the object map to a new value
.
If property
does not exist in the object map, it is added.
Example
let m = #{a: 1, b: 2, c: 3};
m.set("b", 42)'
print(m); // prints "#{a: 1, b: 42, c: 3}"
x.set("x", 0);
print(m); // prints "#{a: 1, b: 42, c: 3, x: 0}"
set_bit
set_bit(value: &mut i64, bit: i64, new_value: bool) -> Result<(), Box<EvalAltResult>>
Set the specified bit
in the number if the new value is true
.
Clear the bit
if the new value is false
.
If bit
< 0, position counts from the MSB (Most Significant Bit).
Example
let x = 123456;
x.set_bit(5, true);
print(x); // prints 123488
x.set_bit(6, false);
print(x); // prints 123424
x.set_bit(-48, false);
print(x); // prints 57888 on 64-bit
set_bits
set_bits(value: &mut i64, range: RangeInclusive<i64>, new_value: i64) -> Result<(), Box<EvalAltResult>>
set_bits(value: &mut i64, range: Range<i64>, new_value: i64) -> Result<(), Box<EvalAltResult>>
set_bits(value: &mut i64, bit: i64, bits: i64, new_value: i64) -> Result<(), Box<EvalAltResult>>
Replace an inclusive range of bits in the number with a new value.
Example
let x = 123456;
x.set_bits(5..=9, 42);
print(x); // print 123200
set_tag
set_tag(value: &mut Dynamic, tag: i64) -> Result<(), Box<EvalAltResult>>
Set the tag of a Dynamic
value.
Example
let x = "hello, world!";
x.tag = 42;
print(x.tag); // prints 42
shift
shift(blob: &mut Blob) -> i64
shift(array: &mut Array) -> Dynamic
Remove the first byte from the BLOB and return it.
If the BLOB is empty, zero is returned.
Example
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
print(b.shift()); // prints 1
print(b); // prints "[02030405]"
sign
sign(x: i16) -> i64
sign(x: i128) -> i64
sign(x: i32) -> i64
sign(x: i64) -> i64
sign(x: f64) -> Result<i64, Box<EvalAltResult>>
sign(x: i8) -> i64
sign(x: f32) -> Result<i64, Box<EvalAltResult>>
Return the sign (as an integer) of the number according to the following:
0
if the number is zero1
if the number is positive-1
if the number is negative
sin
sin(x: f64) -> f64
Return the sine of the floating-point number in radians.
sinh
sinh(x: f64) -> f64
Return the hyperbolic sine of the floating-point number in radians.
sleep
sleep(seconds: f64)
sleep(seconds: i64)
Block the current thread for a particular number of seconds
.
Example
// Do nothing for 10 seconds!
sleep(10.0);
some
some(array: &mut Array, filter: &str) -> Result<bool, Box<EvalAltResult>>
some(array: &mut Array, filter: FnPtr) -> Result<bool, Box<EvalAltResult>>
Return true
if any element in the array that returns true
when applied a function named
by filter
.
Deprecated API
This method is deprecated and will be removed from the next major version.
Use array.some(Fn("fn_name"))
instead.
Function Parameters
A function with the same name as the value of filter
must exist taking these parameters:
element
: copy of array elementindex
(optional): current index in the array
Example
fn large(x) { x > 3 }
fn huge(x) { x > 10 }
fn screen(x, i) { i > x }
let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
print(x.some("large")); // prints true
print(x.some("huge")); // prints false
print(x.some("screen")); // prints true
sort
sort(array: &mut Array) -> Result<(), Box<EvalAltResult>>
sort(array: &mut Array, comparer: FnPtr)
sort(array: &mut Array, comparer: &str) -> Result<(), Box<EvalAltResult>>
Sort the array.
All elements in the array must be of the same data type.
Supported Data Types
- integer numbers
- floating-point numbers
- decimal numbers
- characters
- strings
- booleans
()
Example
let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
x.sort();
print(x); // prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
splice
splice(blob: &mut Blob, range: Range<i64>, replace: Blob)
splice(array: &mut Array, range: Range<i64>, replace: Array)
splice(blob: &mut Blob, range: RangeInclusive<i64>, replace: Blob)
splice(array: &mut Array, range: RangeInclusive<i64>, replace: Array)
splice(blob: &mut Blob, start: i64, len: i64, replace: Blob)
splice(array: &mut Array, start: i64, len: i64, replace: Array)
Replace an exclusive range
of the BLOB with another BLOB.
Example
let b1 = blob(10, 0x42);
let b2 = blob(5, 0x18);
b1.splice(1..4, b2);
print(b1); // prints "[4218181818184242 42424242]"
split
split(string: &str) -> Array
split(array: &mut Array, index: i64) -> Array
split(string: &mut ImmutableString, index: i64) -> Array
split(string: &str, delimiter: char) -> Array
split(blob: &mut Blob, index: i64) -> Blob
split(string: &str, delimiter: &str) -> Array
split(string: &str, delimiter: char, segments: i64) -> Array
split(string: &str, delimiter: &str, segments: i64) -> Array
Split the string into segments based on whitespaces, returning an array of the segments.
Example
let text = "hello, world! hello, foo!";
print(text.split()); // prints ["hello,", "world!", "hello,", "foo!"]
split_rev
split_rev(string: &str, delimiter: &str) -> Array
split_rev(string: &str, delimiter: char) -> Array
split_rev(string: &str, delimiter: char, segments: i64) -> Array
split_rev(string: &str, delimiter: &str, segments: i64) -> Array
Split the string into segments based on a delimiter
string, returning an array of the
segments in reverse order.
Example
let text = "hello, world! hello, foo!";
print(text.split_rev("ll")); // prints ["o, foo!", "o, world! he", "he"]
sqrt
sqrt(x: f64) -> f64
Return the square root of the floating-point number.
start
start(range: &mut RangeInclusive<i64>) -> i64
start(range: &mut Range<i64>) -> i64
Return the start of the inclusive range.
starts_with
starts_with(string: &str, match_string: &str) -> bool
Return true
if the string starts with a specified string.
Example
let text = "hello, world!";
print(text.starts_with("hello")); // prints true
print(text.starts_with("world")); // prints false
sub_string
sub_string(string: &str, start: i64) -> ImmutableString
sub_string(string: &str, range: RangeInclusive<i64>) -> ImmutableString
sub_string(string: &str, range: Range<i64>) -> ImmutableString
sub_string(string: &str, start: i64, len: i64) -> ImmutableString
Copy a portion of the string beginning at the start
position till the end and return it as
a new string.
- If
start
< 0, position counts from the end of the string (-1
is the last character). - If
start
< -length of string, the entire string is copied and returned. - If
start
≥ length of string, an empty string is returned.
Example
let text = "hello, world!";
print(text.sub_string(5)); // prints ", world!"
print(text.sub_string(-5)); // prints "orld!"
tag
tag(value: &mut Dynamic) -> i64
Return the tag of a Dynamic
value.
Example
let x = "hello, world!";
x.tag = 42;
print(x.tag); // prints 42
take
take(value: &mut Dynamic) -> Result<Dynamic, Box<EvalAltResult>>
Take ownership of the data in a Dynamic
value and return it.
The data is NOT cloned.
The original value is replaced with ()
.
Example
let x = 42;
print(take(x)); // prints 42
print(x); // prints ()
tan
tan(x: f64) -> f64
Return the tangent of the floating-point number in radians.
tanh
tanh(x: f64) -> f64
Return the hyperbolic tangent of the floating-point number in radians.
timestamp
timestamp() -> Instant
Create a timestamp containing the current system time.
Example
let now = timestamp();
sleep(10.0); // sleep for 10 seconds
print(now.elapsed); // prints 10.???
to_array
to_array(blob: &mut Blob) -> Array
Convert the BLOB into an array of integers.
Example
let b = blob(5, 0x42);
let x = b.to_array();
print(x); // prints "[66, 66, 66, 66, 66]"
to_binary
to_binary(value: i8) -> ImmutableString
to_binary(value: u8) -> ImmutableString
to_binary(value: u128) -> ImmutableString
to_binary(value: u32) -> ImmutableString
to_binary(value: i64) -> ImmutableString
to_binary(value: u64) -> ImmutableString
to_binary(value: i32) -> ImmutableString
to_binary(value: i16) -> ImmutableString
to_binary(value: u16) -> ImmutableString
to_binary(value: i128) -> ImmutableString
Convert the value
into a string in binary format.
to_blob
to_blob(string: &str) -> Blob
Convert the string into an UTF-8 encoded byte-stream as a BLOB.
Example
let text = "朝には紅顔ありて夕べには白骨となる";
let bytes = text.to_blob();
print(bytes.len()); // prints 51
to_chars
to_chars(string: &str) -> Array
Return an array containing all the characters of the string.
Example
let text = "hello";
print(text.to_chars()); // prints "['h', 'e', 'l', 'l', 'o']"
to_debug
to_debug(number: f32) -> ImmutableString
to_debug(array: &mut Array) -> ImmutableString
to_debug(string: &str) -> ImmutableString
to_debug(map: &mut Map) -> ImmutableString
to_debug(unit: ()) -> ImmutableString
to_debug(number: f64) -> ImmutableString
to_debug(character: char) -> ImmutableString
to_debug(item: &mut Dynamic) -> ImmutableString
to_debug(f: &mut FnPtr) -> ImmutableString
to_debug(value: bool) -> ImmutableString
Convert the value of number
into a string.
to_degrees
to_degrees(x: f64) -> f64
Convert radians to degrees.
to_float
to_float(_)
to_float(_)
to_float(_)
to_float(_)
to_float(_)
to_float(_)
to_float(x: f32) -> f64
to_float(_)
to_float(_)
to_float(_)
to_float(_)
to_float(_)
Convert the 32-bit floating-point number to 64-bit.
to_hex
to_hex(value: i32) -> ImmutableString
to_hex(value: i128) -> ImmutableString
to_hex(value: u16) -> ImmutableString
to_hex(value: i16) -> ImmutableString
to_hex(value: u32) -> ImmutableString
to_hex(value: u8) -> ImmutableString
to_hex(value: u128) -> ImmutableString
to_hex(value: i8) -> ImmutableString
to_hex(value: u64) -> ImmutableString
to_hex(value: i64) -> ImmutableString
Convert the value
into a string in hex format.
to_int
to_int(_)
to_int(x: f64) -> Result<i64, Box<EvalAltResult>>
to_int(_)
to_int(_)
to_int(_)
to_int(_)
to_int(x: f32) -> Result<i64, Box<EvalAltResult>>
to_int(_)
to_int(_)
to_int(_)
to_int(_)
to_int(_)
to_int(_)
Convert the floating-point number into an integer.
to_json
to_json(map: &mut Map) -> String
Return the JSON representation of the object map.
Data types
Only the following data types should be kept inside the object map:
INT
, FLOAT
, ImmutableString
, char
, bool
, ()
, Array
, Map
.
Errors
Data types not supported by JSON serialize into formats that may invalidate the result.
Example
let m = #{a:1, b:2, c:3};
print(m.to_json()); // prints {"a":1, "b":2, "c":3}
to_lower
to_lower(character: char) -> char
to_lower(string: ImmutableString) -> ImmutableString
Convert the character to lower-case and return it as a new character.
Example
let ch = 'A';
print(ch.to_lower()); // prints 'a'
print(ch); // prints 'A'
to_octal
to_octal(value: i8) -> ImmutableString
to_octal(value: u128) -> ImmutableString
to_octal(value: u8) -> ImmutableString
to_octal(value: u32) -> ImmutableString
to_octal(value: i64) -> ImmutableString
to_octal(value: u64) -> ImmutableString
to_octal(value: i32) -> ImmutableString
to_octal(value: i16) -> ImmutableString
to_octal(value: u16) -> ImmutableString
to_octal(value: i128) -> ImmutableString
Convert the value
into a string in octal format.
to_radians
to_radians(x: f64) -> f64
Convert degrees to radians.
to_string
to_string(unit: ()) -> ImmutableString
to_string(number: f64) -> ImmutableString
to_string(number: f32) -> ImmutableString
to_string(string: ImmutableString) -> ImmutableString
to_string(array: &mut Array) -> ImmutableString
to_string(map: &mut Map) -> ImmutableString
to_string(value: bool) -> ImmutableString
to_string(character: char) -> ImmutableString
to_string(item: &mut Dynamic) -> ImmutableString
Return the empty string.
to_upper
to_upper(string: ImmutableString) -> ImmutableString
to_upper(character: char) -> char
Convert the string to all upper-case and return it as a new string.
Example
let text = "hello, world!"
print(text.to_upper()); // prints "HELLO, WORLD!"
print(text); // prints "hello, world!"
trim
trim(string: &mut ImmutableString)
Remove whitespace characters from both ends of the string.
Example
let text = " hello ";
text.trim();
print(text); // prints "hello"
truncate
truncate(blob: &mut Blob, len: i64)
truncate(string: &mut ImmutableString, len: i64)
truncate(array: &mut Array, len: i64)
Cut off the BLOB at the specified length.
- If
len
≤ 0, the BLOB is cleared. - If
len
≥ length of BLOB, the BLOB is not truncated.
Example
let b = blob();
b += 1; b += 2; b += 3; b += 4; b += 5;
b.truncate(3);
print(b); // prints "[010203]"
b.truncate(10);
print(b); // prints "[010203]"
values
values(map: &mut Map) -> Array
Return an array with all the property values in the object map.
Example
let m = #{a:1, b:2, c:3};
print(m.values()); // prints "[1, 2, 3]""
write_ascii
write_ascii(blob: &mut Blob, range: Range<i64>, string: &str)
write_ascii(blob: &mut Blob, range: RangeInclusive<i64>, string: &str)
write_ascii(blob: &mut Blob, start: i64, len: i64, string: &str)
Write an ASCII string to the bytes within an exclusive range
in the BLOB.
Each ASCII character encodes to one single byte in the BLOB. Non-ASCII characters are ignored.
- If number of bytes in
range
< length ofstring
, extra bytes instring
are not written. - If number of bytes in
range
> length ofstring
, extra bytes inrange
are not modified.
let b = blob(8);
b.write_ascii(1..5, "hello, world!");
print(b); // prints "[0068656c6c000000]"
write_be
write_be(blob: &mut Blob, range: RangeInclusive<i64>, value: i64)
write_be(blob: &mut Blob, range: RangeInclusive<i64>, value: f64)
write_be(blob: &mut Blob, range: Range<i64>, value: f64)
write_be(blob: &mut Blob, range: Range<i64>, value: i64)
write_be(blob: &mut Blob, start: i64, len: i64, value: i64)
write_be(blob: &mut Blob, start: i64, len: i64, value: f64)
Write an INT
value to the bytes within an inclusive range
in the BLOB
in big-endian byte order.
- If number of bytes in
range
< number of bytes forINT
, extra bytes inINT
are not written. - If number of bytes in
range
> number of bytes forINT
, extra bytes inrange
are not modified.
let b = blob(8, 0x42);
b.write_be_int(1..=3, 0x99);
print(b); // prints "[4200000042424242]"
write_le
write_le(blob: &mut Blob, range: Range<i64>, value: i64)
write_le(blob: &mut Blob, range: Range<i64>, value: f64)
write_le(blob: &mut Blob, range: RangeInclusive<i64>, value: i64)
write_le(blob: &mut Blob, range: RangeInclusive<i64>, value: f64)
write_le(blob: &mut Blob, start: i64, len: i64, value: i64)
write_le(blob: &mut Blob, start: i64, len: i64, value: f64)
Write an INT
value to the bytes within an exclusive range
in the BLOB
in little-endian byte order.
- If number of bytes in
range
< number of bytes forINT
, extra bytes inINT
are not written. - If number of bytes in
range
> number of bytes forINT
, extra bytes inrange
are not modified.
let b = blob(8);
b.write_le_int(1..3, 0x12345678);
print(b); // prints "[0078560000000000]"
write_utf8
write_utf8(blob: &mut Blob, range: Range<i64>, string: &str)
write_utf8(blob: &mut Blob, range: RangeInclusive<i64>, string: &str)
write_utf8(blob: &mut Blob, start: i64, len: i64, string: &str)
Write a string to the bytes within an exclusive range
in the BLOB in UTF-8 encoding.
- If number of bytes in
range
< length ofstring
, extra bytes instring
are not written. - If number of bytes in
range
> length ofstring
, extra bytes inrange
are not modified.
let b = blob(8);
b.write_utf8(1..5, "朝には紅顔ありて夕べには白骨となる");
print(b); // prints "[00e69c9de3000000]"
zip
zip(array1: &mut Array, array2: Array, map: FnPtr) -> Result<Array, Box<EvalAltResult>>
Iterate through all elements in two arrays, applying a mapper
function to them,
and return a new array containing the results.
Function Parameters
array1
: First arrayarray2
: Second arrayindex
(optional): current index in the array
Example
let x = [1, 2, 3, 4, 5];
let y = [9, 8, 7, 6];
let z = x.zip(y, |a, b| a + b);
print(z); // prints [10, 10, 10, 10]
let z = x.zip(y, |a, b, i| a + b + i);
print(z); // prints [10, 11, 12, 13]