Arturo is a very simple language. Even without any prior experience, I estimate it would take you roughly half an hour before you are comfortable enough to write your first program.
Here, you'll find everything you may need to know about the language (and perhaps a bit more). In a single page.
- Introduction
- The main components
- Precedence and evaluation
- Scope and rules
- In-place variable modification
- Syntactic sugar
- Conclusion
Arturo's syntax is probably as easy as it could get. Basically, you could say: there is no syntax.
Let's try to resume some key points of Arturo's no-syntax:
- Code is just a list of words, labels, symbols, values and attributes (you'll learn what all this is about very soon!)
- Code can be grouped into blocks (that is: a list of words, labels, symbols, and values within square brackets:
[ ... ] - A block has no meaning until it is given one, or interpreted within some specific context, that is:
[ lorem ipsum ]is perfectly valid Arturo code, until you try to "run" it - where the interpreter will complain that it has no idea whatloremandipsumis. Pretty much like if I tell you a word in Swahili - you'll most likely need a dictionary to figure out what it might mean. - There are no reserved "keywords". Every word can be re-defined.
- You can format your code any way you want: no semicolons to add, no obligatory newlines, no commas, no parentheses, no Python-style indentation rules
- Every function expects a pre-determined number of arguments. Initially, we check during evaluation, and during runtime, we "consume" values until we reach the number of arguments required.
As you can see, there is not much to learn from scratch here:
Once you learn what the language main components are & a few details about precedence and evaluation, then only with the Library Reference at hand (the built-in functions that are already there for you, available to use), you're pretty much ready to write any program. ;-)
So, let's get to the gist of the matter!
Words in Arturo are pretty much like words in English: a series of characters separated by spaces or some symbol. E.g.: this is a series of words (in this case, this, is, a, series, of, words are all - well... - words.
💡 In Arturo, a word can be start with any letter or underscore followed by any letter/numbers/underscore or a question mark (
?).
As with a real (spoken) language, every word has a specific meaning. And if you don't know the meaning of a word, you'll have to look it up in the dictionary. That's pretty much the way things work in Arturo as well.
In Arturo, a word may have 3 main different uses:
- refer to a value (that is: a variable, e.g.
x + 2) - refer to an action, which does something (that is: a function, e.g.
doSomething) - Arturo comes with close to 150 already defined such words/functions - refer to nothing (that is: a word without meaning, as in the
[lorem ipsum]example above)
A label is nothing but Arturo's way of assigning meaning (to be read as a value) to a word - what you would normally call variable assignment or variable initialization. (In Arturo, these two terms can be used invariably, since there is practically no difference: you can set and re-define a word/variable as many times as you wish).
So, let's say you want to give a new meaning to the word x:
x: 2That was it: from now on, x will mean 2 (until and if it's changed). So if you follow the above statement with:
print x...Arturo will print 2 for you.
As the mere word says, symbols are used to symbolize something, mainly as an alias to an existing word - although, by convention, as infix operators.
Hence, let's take the function add. This takes two parameters, adds them up, and returns the result.
So, you may write:
print add 2 3and Arturo will print out 5 for you.
Now, if you don't want to use the add function (and prefix notation, which is the standard for all function calls), there is a symbol-alias for that: +
So, you could just as well write:
print 2 + 3Only, this time you're expressing it more like you would in a normal math expression: with infix notation.
Some of the existing aliases in the built-in dictionary:
%ALIAS_LIST%| Symbol | Aliased word |
|---|
💡 For the complete of recognized symbols in Arturo, you may have a look at here.
Values are the very core of Arturo and are used to refer to pretty much all the different kinds of data you can have.
💡 Words, labels, and symbols - that we already saw above - can be considered "values" as well; and treated as such, unless we proceed to evaluate them!
We could split values into 2 categories: a) literal values - that is values you can directly define in your code and b) constructible - values that are created using some function.
The complete list follows:
Null values generally denote nothing and it's mostly used as a return value by functions to inform us that something went wrong. If you want to set it as a value, you may just use the word null, like:
x: nullLogicals are Arturo's logical values. They are like normal boolean values (true, false), with a twist: they fully support ternary logic and an additional maybe value.
x: true
y: false
z: maybeIntegers represent positive or negative integers. When they are declared they are comprised only by digits ([0-9]+) and they can be as long as you want - Arturo does support big numbers.
x: 2
y: 876528347613438247982374913423423947329Floating values are basically floating-point numbers, that is: decimals. They begin with an initial all-digit part, followed by a . (dot) and another all-digit part: [0-9]+\.[0-9]+
pi: 3.14159Even though there is no special syntax for defining a complex number, you can always create one using to. Arturo comes with full support for complex numbers and all the associated operations.
a: to :complex [1 2] ; a: 1.0+2.0i
b: to :complex @[pi pi] ; b: 3.141592653589793+3.141592653589793i
print a + b
; 4.141592653589793+5.141592653589793iAs with complex numbers, rationals can be defined using to. And again, Arturo comes with full support for rational numbers and all the associated operations.
a: to :rational [1 2] ; 1/2
b: to :rational [3 4] ; 3/4
print a + b
; 5/4Version values are nothing but a fancy and portable way of defining SemVer-compliant versions (e.g. for packaging an application). The rule is rather simple: it has three numeric parts, separated by a . (dot) and an optional part with build or prerelease information.
package: 1.0.3
if package > 1.0.0 -> print "yep, the version is right!"or
version: 1.2.3-rc1+build123Type is a value representing another... type. To specify a type value, the format is a : (colon) followed by a word - the type's name: :\w+
myType: :integeror
if (type x) = :integer [ print "it's an integer!" ]Characters in Arturo can be declared using backticks: `\w`
ch: `a`A string is nothing but a series of characters, seen as one unit. In Arturo, to define a string, there are various ways:
Single-line strings
- using double quotes:
x: "this is a string"(with escaped characters) - using right smart-quote notation
x: « This is a string(in this case, everything following«till the end of the line, will be stripped and considered one string)
Multi-line strings
-
using curly-brace blocks (the result will be stripped and un-indented):
x: { this is yet another very long string that spans more than one line }
-
using verbatim curly-brace blocks (the result will remain exactly as-is):
x: {: this is yet another very long string that spans more than one line :}
-
using dash notation (where everything after the line, until the end of the file, is a string - stripped and un-indented):
x: ------ this is the last very long string that spans more than one line
💡 If you want your string to contain sub-expressions that will be evaluated on the fly - that is string interpolation - all you have to do is include your code inside the string within pipe-bars and then call the function
render(or~) to process it accordingly: e.g.x: 2 print ~"my variable is: |x|" ; prints: ; my variable is: 2
A regex value is nothing but a string containing a regular expression (that is then compiled and treated as such).
The normal syntax is {/regex/}:
type {/[A-Z]/} ; => :regex
replace "HelLo" {/[A-Z]/} "X" ; here we replace all capital letters
; with an XWhile we may - optionally - use any of the supported PCRE-compliant flags:
i: case-insensitive matchingm: multilines: dot-all
match "Hello" {/he/i} ; => ["He"]Literals in Arturo are just a way of referring to the name of a word or symbol. Think of it as the string version of a word, or like Ruby's symbols.
For example, the function info takes as an argument the name of the function for which you want some information. If you wrote like info print, the interpreter would execute the function print and try to... print something (which would not be there). If you wanted to refer to the name of the function -- that is: without actually calling it -- you would precede it with a ' (single-quote): '[\w+]
func: 'print
info funcHowever, literals may be used for much more - e.g. modifying a passed parameter in-place, without having to re-assign the result of an operation to a new variable. To learn more, have a look at In-place variable modification.
Symbol literals are to symbols pretty much what literals are to words. That is: the "literal", unevaluated form of the symbol.
To declare a symbol literal, we may follow the example of normal, literals: single quote + accompanied by the symbol in question:
type '++ ; => :symbolliteralPaths in Arturo are a way of defining some hierarchy between values, something along the lines of parent -> child -> grandchild. For this, in Arturo, we'd use a series of values or words delimited with a \ (backslash). You can think of them as indexes in other programming languages.
print user\nameor
x: "name"
print user\(x)or
myArray: ["zero" "one" "two" "three"]
print myArray\1
; prints "one"If paths are the way of defining some hierarchy between values, with pathLabels we are also able to assign values to some specific path.
user: #[
name: "John"
surname: "Doe"
]
print user\name ; will print "John"
; let's change this user's name
user\name: "Jane"
print user\name ; ok, now it should print "Jane"Inlines in Arturo generally denote a list of words/symbols/values that are grouped and given some type of priority in evaluation. An inline block is denoted by (...) (parentheses).
print (2+3)*4Please note though that, apart from precedence, :inline is a value type on its own:
a: [(one) two three]
print type a\0 ; that is: (one)
; prints :inlineBlocks are a fundamental part of Arturo.
As we've already said, it's just a [...] (square-bracket enclosed) block of words/symbols/values that - in contrast with inline blocks above which are evaluated in-place - are not evaluated until it's necessary.
myBlock: [one two three]
anArray: [1 2 3 4 5]
anotherArray: ["zero" 1 "two" 3 "cuatro"]As you can see, blocks can contain practically anything: any word, any symbol, and any value. Of course, they can contain other blocks too:
x: [
1 2 [
3 4 [
5 "six" 7
]
8
]
9
]Dictionaries in Arturo as what in other languages you'd call an associative array or hash table. If you want to create one, just give the dictionary function (or use the # alias) a block, with different labels, and it'll automatically convert it to a dictionary.
user: #[
name: "John"
surname: "Doe"
age: 34
]What the # function here does is:
- execute the block
- retrieve only the words/variables defined in there
- return a dictionary with the aforementioned words
💡 As you can probably assume from the above definition, a dictionary block doesn't necessarily have to contain just labels and word definitions - it may contain whatever you want, even function calls; only it will return you back just a table with the defined words in there
Functions are another important value type in Arturo - and yes, you heard right: functions a value too. You can assign them to a word/variable, pass them around, re-define them and whatever you want with them, pretty much as you would do with another value, let's say a number.
To define a function, all you have to do is call the function... function (or use the $ alias) followed by two parameters:
- the parameters' names (this can be either a literal, e.g.
'x- if it's just one argument - or a block, e.g.[x y]. If you want to use commas, for readability, like[x,y]you are free to do so: Arturo will simply ignore them.
multiply: function [x y][
x * y
]
print multiply 2 3
; would print 6or
addThemUp: $[x,y][x+y]
print addThemUp 2 3
; would print 5Colors can be easily defined using the #xxxxxx syntax, where xxxxxx is either the RGB value of the color in hex, or its common-name alias, like #red, #green or #blue:
color1: #red
color2: #0077BB
print color1 + color2Dates in Arturo are a distinct type of value. If you want to create one, you'll have to use one of the corresponding functions. For example, the function now returns a :date object, representing the current point in time. And it can be handled pretty much like you would handle a :dictionary.
print now
; would print 2020-10-26T10:27:14+01:00
print now\year
; would print 2020Database values cannot be constructed literally. However, using the function open, you can create a database value and then use it to query the database in question and much more.
db: open.sqlite "my.db"
print query db "SELECT * FROM users"
print type db ; would print: :databaseBinary values are used to represent binary data, that is: an array of bytes. You cannot define them directly, however, you can sure convert other data to binary.
print to :binary "Hello world!"
; would print 4865 6C6C 6F20 776F 726C 6421Bytecode values cannot be constructed literally. However, you can create them indirectly, e.g. using the function to.
; let's create some Arturo bytecode from scratch
x: to :bytecode [
["print"] ; first block contains our constants
[1 112 155] ; second block contains the opcodes to be executed,
; in this case: ConstI1 (0x01), Call (0x70), End (0x9B)
]
; and execute it!
do x ; this will simply print... 1Another interesting feature of Arturo is what we'll analyze here: attributes.
Technically, attributes are nothing but an easy way of defining optional named parameters for functions - but which can however transcend between different function calls.
There are two types:
a. attributes
b. attribute labels
Attributes are actually optional on/off-type of values, set during a function call, that is there to denote some variation of the initial meaning of the function. To define an attribute, we'll be using a .(dot) followed by a normal word: \.\w+
Let's say we used the function split, to split a string into parts:
split "hello world"
; => [`h` `e` `l` `l` `o` ` ` `w` `o` `r` `l` `d` ]That does what it says: splits the string into an array of :chars.
What if we want for example to split the string into words? For that, there is the .words attribute for the function split. So:
split.words "hello world"
; = ["hello" "world"]💡 The order in which you pass the different attributes does not matter. Also, there is no issue at all whether you want to use spaces between them and the surrounding function call; Arturo will still be able to parse them and recognize them fine
Attribute labels are pretty much like simple attributes, only they can also take a value. As it worked with attributes, we'll be using a .(dot) followed by a normal word, but now also followed by a :(colon) -- exactly like with normal labels, as we've seen above.
Here's an example:
split .every: 2 "hello world"
; => ["he" "ll" "ow" "or" "ld"]The easiest way to explain precedence rules in Arturo is pretty much like it happened with our introduction: there are no precedence rules whatsoever.
So, in an expression like 2 * 3 + 4, if you'd normally expect to get a result of 10, you would be wrong: the result is 14.
But in order to understand why, you'd have to understand how evaluation in Arturo works.
The main expression evaluation order of Arturo is right-to-left. But with a tiny asterisk: Your code will be evaluated from left to right, it is the expressions passed to your function calls that will be evaluated from right-to-left.
Let's take a very simple example:
print add 1 2
print "Done"As you would expect, the first function to be executed is the first print function and then the second one. Nothing new here.
Now let's take the first print. How is it working?
Let's see:
- First, the value
2is pushed onto the stack - Then, we push the value
1 - Then, we execute the function
add: it pops two values from the stack, adds them up, and pushes the result (3) back onto the stack - Finally, we execute the function
print: it pops the top value from the stack (3) and prints it.
Then, execution would move on and... print "Done."
What you have to understand here is that evaluation within an expression will always be done from right to left, irrespective of what you might know from other languages or math operator precedence. In Arturo, you have to learn no precedence rules at all. You'll just have to remember to always calculate from right to left.
Re-visiting our previous, seemingly paradoxical, example:
2 * 3 + 4💡 Remember: our
+and*operators are nothing but simple infix aliases to the functionsaddandmulrespectively -- nothing more!
This is as if we had written (moving the operators in front):
* 2 + 3 4which practically means: FIRST add 3 and 4 and THEN take the result and multiply it with 2.
If this is not what intended, then the right way in Arturo would be, either:
4 + 2 * 3or (giving precedence to the multiplication, artificially, using parentheses):
(2 * 3) + 4Contrary to what you might expect, Arturo doesn't feature a traditional concept of scope. There are no real local or global variables, no local or global functions, no local or global blocks, no local or global anything.
Generally, if a variable has been previously declared at the moment and location of its usage, then it is available. Otherwise, it is not.
But let's see a couple of concrete cases to make this clearer.
Arturo doesn't have a block scope.
In a few words, this means:
- A variable declared inside a block is available outside of it
- A variable previously declared outside of a block is available inside
- The changes of an existing variable, inside a block, persist after the block
x: 1 ; here, we declare a variable x
; and set it to 1
do [
x: 2 ; here, we re-assign the value of x
; to 2
print x ; prints 2
y: 3 ; here, we declare a variable y
; and set it to 3
]
print x ; prints 2 (the value of x has been changed)
print y ; prints 3 (the value of y is still available)Iterators (such as loop, map, etc) always work with a block as well. But in a special way.
Basically, the logic is identical to the one of blocks, but with a slight difference:
- the injected variables (e.g. the iteration arguments), are available only inside the block, but not after the iteration is over
- any previous value is "protected" and restored if needed
x: 3
loop.with:'i ["one" "two" "three"] 'x [
print i ; prints 0, 1, 2,...
print x ; prints "one", "two", "three",...
]
print x ; prints 3 (the value of x has been restored)
print i ; ERROR: variable not found
; (i is not available anymore)Functions are totally different. Why? Because they do have their own scope.
The general idea is:
- A variable declared inside a function is available only inside the function
- A variable previously declared outside of a function is available inside
- The changes of an existing variable, inside a function, do not persist after the function
If we want to export a specific symbol to the outer scope, that is, make it available outside of the function, we can use the .export option.
If we want to export all of the symbols - thus, practically making the function scope-less, we may use the .exportable option.
In Arturo, every time you pass a parameter to a function, you can rest assured that the value of that parameter won't change (unless it's a string, block or dictionary and you - consciously - decided to use set on it, in which case it does alter its inner structure).
So, basically, you when do this...
a: [1 5 2 4 3]
sort a...all you do is to take an array, sort it, and push the sorted array onto the stack. Variable a simply does not change.
So, what would you do if you wanted to get the array back, but sorted?
The simple - and most obvious - way would be to re-assign the returned result from sort:
a: [1 5 2 4 3]
a: sort aAnd now, yes, a does contain the sorted version of the initial array.
But, what if you want to perform the modification in-place, which is normally faster and without the need for intermediate variables? Literals come to the rescue!
As we've already said, "literals" ('i 'am 'a 'literal) are nothing but string representations of a word, that is... the word itself. For that reason, they may come in very handy when you want to modify a variable in-place.
Let's revisit the above example and what the syntax of sort is:
sort collection :literal :dictionary :blockAs we can see, sort takes one parameter (collection) which is either a :dictionary or :block OR a :literal.
Why pass a literal? Simply because this turns in-place modification on! Let's have a look:
a: [1 5 2 4 3]
sort 'a ; yep, now a *has* been sorted in-place!And this is very powerful: in Arturo, most of the built-in functions in the library come with this feature included. Practically, whenever you see a function taking a literal first parameter, that means you can use it for in-place modifying a variable (yes, even add works like that!).
⚠️ Word of caution: Values in Arturo are always passed by reference - unless they are constant/readonly values. So if you want to assign one variable to another and then modify one of them in-place, make sure you usenew; otherwise, both values will change!a: 1 b: a inc 'a ; both a and b are now 2 c: 1 d: new c ; we copy the value of c into d inc 'c ; now c is 2 and d is 1, as expected
As you have hopefully seen so far, Arturo is rather simple, with fairly simple rules and that's pretty much it.
However, we also have some "syntactic sugar": a fancy way of referring to syntactic constructs, so that something more complicated will look better, or easier-to-write, or more readable.
Here you'll learn about some useful examples of syntactic sugar supported in Arturo.
The function of the right operator is rather straightforward: basically, it wraps the following terminal value inside a block.
Let's take a simple example.
x: -> 2This is 100% equivalent to:
x: [2]You can also use the right-arrow operator to make many common constructs far more readable.
For example:
if x=2 -> print "x was 2!"is the same as writing:
if x=2 [ print "x was 2!" ]As you can see, it can be pretty handy. Just remember that -> can wrap only one terminal value.
For example:
x: -> 2 3This doesn't result in x: [2 3] but in x: [2] 3
Another interesting way of making use of the power of the right-arrow operator:
loop 1..10 'x -> print xwhich is the same as writing (only much more elegant):
loop 1..10 'x [ print x ]The fat right-arrow operator is like a super-charged simple right arrow operator (->) as described above.
If -> was used to wrap the following terminal into a block, the => operator does even more.
Let's take this simple example:
x: $ => addThis is equivalent to writing:
x: $[x,y][add x y]Basically, it reads the following word, and if it's a function, pushes all its needed arguments as anonymous variables.
The same could work with a block argument, where & can be used as a placeholder for the needed anonymous variables:
x: $ => [add & &](The first & will pop the first argument, and the second the next one - and so on...)
As you can already imagine, this is perfect for easily defining functions or action blocks that take exactly one parameter.
For example, to print an array of the even numbers between 1 and 10:
print select 1..10 'x [even? x]This could be easily written as (using the -> operator):
print select 1..10 'x -> even? xBut pushing the limits more, we can also use the => operator:
print select 1..10 => even?That's surely much more readable, isn't it?
⚠️ This is experimental and may still not be stable enough for use in production scripts
The pipe operator is an easy way of reversing the default prefix notation of function calls and simulating what in OOP languages is called function chain.
Let's take this simple example:
1..10 | printThis equivalent to:
print 1..10Or a bit more elaborate example (using pipes and the => operator):
1..10 | map => 2*
| select => even?
| printwhich would be like writing:
print select map 1..10 'x [2*x] 'x [even? x] If you made it here, then I can assure you: you've already learned more than you need in order to be fully proficient in Arturo.
Just head to the Library Reference and have a look at the built-in functions (with explanations and example code) and see what's already available for you or - if you want to see the language in action - just browse through the Examples: there are many (many!) working examples, to get more than just an idea.
Welcome on board! :)
This section is inspired by Learn X in Y minutes and its goal is to present the most basic features of Arturo, in a concise way, so that you can get started fast.
So... With X = Arturo and Y = ~15 minutes, here you are:
; this is a comment
; this is another comment
;---------------------------------
; VARIABLES & VALUES
;---------------------------------
; numbers
a1: 2
a2: 3.14
a3: to :complex [1 2.0] ; 1.0+2.0i
; strings
c1: "this is a string"
c2: {
this is a multiline string
that is indentation-agnostic
}
c3: {:
this is
a verbatim
multiline string
which will remain exactly
as the original
:}
; characters
ch: `c`
; blocks/arrays
d: [1 2 3]
; dictionaries
e: #[
name: "John"
surname: "Doe"
age: 34
likes: [pizza spaghetti]
]
; yes, functions are values too
f: function [x][
2 * x
]
; colors - right, you can directly define them as well!
g1: #red
g2: #0077BF
; dates
h: now ; 2021-05-03T17:10:48+02:00
; logical values
i1: true
i2: false
i3: maybe
;---------------------------------
; BASIC OPERATORS
;---------------------------------
; simple arithmetic
1 + 1 ; => 2
8 - 1 ; => 7
4.2 - 1.1 ; => 3.1
10 * 2 ; => 20
35 / 4 ; => 8
35 // 4 ; => 8.75
2 ^ 5 ; => 32
5 % 3 ; => 2
; bitwise operators
and 3 5 ; => 1
or 3 5 ; => 7
xor 3 5 ; => 6
; pre-defined constants
pi ; => 3.141592653589793
epsilon ; => 2.718281828459045
null ; => null
true ; => true
false ; => false
;---------------------------------
; COMPARISON OPERATORS
;---------------------------------
; equality
1 = 1 ; => true
2 = 1 ; => false
; inequality
1 <> 1 ; => false
2 <> 1 ; => true
; more comparisons
1 < 10 ; => true
1 =< 10 ; => true
10 =< 10 ; => true
1 > 10 ; => false
1 >= 10 ; => false
11 >= 10 ; => true
;---------------------------------
; CONDITIONALS
;---------------------------------
; logical operators
and? true true ; => true
and? true false ; => false
or? true false ; => true
or? false false ; => false
and? [1=2][2<3] ; => false
; (the second block will not be evaluated)
; simple if statements
if 2 > 1 [ print "yes!"] ; yes!
if 3 <> 2 -> print "true!" ; true!
; if/else statements
; note it's 'if?' not 'if'
if? 2 > 3 -> print "2 is greater than 3"
else -> print "2 is not greater than 3" ; 2 is not greater than 3
; switch statements
switch 2 > 3 -> print "2 is greater than 3"
-> print "2 is not greater than 3" ; 2 is not greater than 3
a: (2 > 3)?["yes"]["no"] ; a: "no"
a: (2 > 3)? -> "yes" -> "no" ; a: "no" (exactly the same as above)
; case/when statements
case [1]
when? [>2] -> print "1 is greater than 2. what?!"
when? [<0] -> print "1 is less than 0. nope..."
else -> print "here we are!" ; here we are!
;---------------------------------
; LOOPS
;---------------------------------
; with `loop`
arr: [1 4 5 3]
loop arr 'x [
print ["x =" x]
]
; x = 1
; x = 4
; x = 5
; x = 3
; with loop and custom index
loop.with:'i arr 'x [
print ["item at position" i "=>" x]
]
; item at position 0 => 1
; item at position 1 => 4
; item at position 2 => 5
; item at position 3 => 3
; using ranges
loop 1..3 'x -> ; since it's a single statement
print x ; there's no need for [block] notation
; we can wrap it up using the `->` syntactic sugar
loop `a`..`c` 'ch ->
print ch
; a
; b
; c
; picking multiple items
loop 1..10 [x y] ->
print ["x =" x ", y =" y]
; x = 1 , y = 2
; x = 3 , y = 4
; x = 5 , y = 6
; x = 7 , y = 8
; x = 9 , y = 10
; looping through a dictionary
dict: #[name: "John", surname: "Doe", age: 34]
loop dict [key value][
print [key "->" value]
]
; name -> John
; surname -> Doe
; age -> 34
; while loops
i: new 0
while [i<3][
print ["i =" i]
inc 'i
]
; i = 0
; i = 1
; i = 2
;---------------------------------
; STRINGS
;---------------------------------
; case
a: "tHis Is a stRinG"
print upper a ; THIS IS A STRING
print lower a ; this is a string
print capitalize a ; THis Is a stRinG
; concatenation
a: "Hello " ++ "World!" ; a: "Hello World!"
; strings as an array
split "hello" ; => [h e l l o]
split.words "hello world" ; => [hello world]
print first "hello" ; h
print last "hello" ; o
; conversion
to :string 123 ; => "123"
to :integer "123" ; => 123
; joining strings together
join ["hello" "world"] ; => "helloworld"
join.with:"-" ["hello" "world"] ; => "hello-world"
; string interpolation
x: 2
print ~"x = |x|" ; x = 2
; interpolation with `print`
print ["x =" x] ; x = 2
; (`print` works by calculating the given block
; and joining the different values as strings
; with a single space between them)
; templates
print render.template {
<||= switch x=2 [ ||>
Yes, x = 2
<||][||>
No, x is not 2
<||]||>
} ; Yes, x = 2
; matching
prefix? "hello" "he" ; => true
suffix? "hello" "he" ; => false
contains? "hello" "ll" ; => true
contains? "hello" "he" ; => true
contains? "hello" "x" ; => false
in? "ll" "hello" ; => true
in? "x" "hello" ; => false
;---------------------------------
; BLOCKS
;---------------------------------
; calculate a block
arr: [1 1+1 1+1+1]
@arr ; => [1 2 3]
; execute a block
sth: [print "Hello world"] ; this is perfectly valid,
; could contain *anything*
; and will not be executed...
do sth ; Hello world
; (...until we tell it to)
; array indexing
arr: ["zero" "one" "two" "three"]
print first arr ; zero
print arr\0 ; zero
print last arr ; three
print arr\3 ; three
x: 2
print get arr x ; two
print arr\[x] ; two
; setting an array element
arr\0: "nada"
set arr 2 "dos"
print arr ; nada one dos three
; adding elements to an array
arr: new []
'arr ++ "one"
'arr ++ "two"
print arr ; one two
; remove elements from an array
arr: new ["one" "two" "three" "four"]
'arr -- "two" ; arr: ["one" "three" "four"]
remove 'arr .index 0 ; arr: ["three" "four"]
; getting the size of an array
arr: ["one" 2 "three" 4]
print size arr ; 4
; getting a slice of an array
print slice ["one" "two" "three" "four"] 0 1 ; one two
; check if array contains a specific element
print contains? arr "one" ; true
print contains? arr "five" ; false
; sorting array
arr: [1 5 3 2 4]
sort arr ; => [1 2 3 4 5]
sort.descending arr ; => [5 4 3 2 1]
; mapping values
map 1..10 [x][2*x] ; => [2 4 6 8 10 12 14 16 18 20]
map 1..10 'x -> 2*x ; same as above
map 1..10 => [2*&] ; same as above
map 1..10 => [2*] ; same as above
; selecting/filtering array values
select 1..10 [x][odd? x] ; => [1 3 5 7 9]
select 1..10 => odd? ; same as above
filter 1..10 => odd? ; => [2 4 6 8 10]
; (now, we leave out all odd numbers -
; while select keeps them)
; misc operations
arr: ["one" 2 "three" 4]
reverse arr ; => [4 "three" 2 "one"]
shuffle arr ; => [2 4 "three" "one"]
unique [1 2 3 2 3 1] ; => [1 2 3]
permutate [1 2 3] ; => [[1 2 3] [1 3 2] [3 1 2] [2 1 3] [2 3 1] [3 2 1]]
take 1..10 3 ; => [1 2 3]
repeat [1 2] 3 ; => [1 2 1 2 1 2]
;---------------------------------
; FUNCTIONS
;---------------------------------
; declaring a function
f: function [x][ 2*x ]
f: function [x]-> 2*x ; same as above
f: $[x]->2*x ; same as above (only using the `$` alias
; for the `function`... function)
; calling a function
f 10 ; => 20
; returning a value
g: function [x][
if x < 2 -> return 0
res: 0
loop 0..x 'z [
res: res + z
]
return res
]
;---------------------------------
; CUSTOM TYPES
;---------------------------------
; defining a custom type
define :person [
; define a new custom type "Person"
; with fields: name, surname, age
init: method [name surname age][
\name: capitalize name
\surname: surname
\age: age
]
; custom print function
string: method [][
render "NAME: |\name|, SURNAME: |\surname|, AGE: |\age|"
]
; custom comparison operator
compare: sortable 'age
]
; create a method for our custom type
sayHello: function [this :person][
print ["Hello" this\name]
]
; create new objects of our custom type
a: to :person ["John" "Doe" 34] ; let's create 2 "Person"s
b: to :person ["jane" "Doe" 33] ; and another one
; call pseudo-inner method
sayHello a ; Hello John
sayHello b ; Hello Jane
; access object fields
print ["The first person's name is:" a\name] ; The first person's name is: John
print ["The second person's name is:" b\name] ; The second person's name is: Jane
; changing object fields
a\name: "Bob"
sayHello a ; Hello Bob
; verifying object type
print type a ; :person
print is? :person a ; true
; printing objects
print a ; NAME: Bob, SURNAME: Doe, AGE: 34
; sorting user objects (using custom comparator)
print sort @[a b] ; Jane..., Bob...
print sort.descending @[a b] ; Bob..., Jane... Basic arithmetic operations (addition, subtraction, multiplication, etc) for integers and floating-point numbers
add given values and return result
valueA(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:color,:object)valueB(:integer,:floating,:complex,:rational,:quantity,:color,:object)
:integer,:floating,:complex,:rational,:quantity,:color,:object,:nothing
print add 1 2 ; 3
print 1 + 3 ; 4
a: 4
add 'a 1 ; a: 5
decrease given value by 1
value(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:object)
:integer,:floating,:complex,:rational,:quantity,:object,:nothing
print dec 5 ; 4
a: 4
dec 'a ; a: 3
perform integer division between given values and return result
valueA(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:object)valueB(:integer,:floating,:complex,:rational,:quantity,:object)
:integer,:floating,:complex,:rational,:quantity,:object,:nothing
print div 5 2 ; 2
print 9 / 3 ; 3
a: 6
div 'a 3 ; a: 2
perform integer division between given values and return tuple with quotient and remainder
valueA(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity)valueB(:integer,:floating,:complex,:rational,:quantity)
:block,:nothing
print divmod 15 5 ; 3 0
print 14 /% 3 ; 4 2
[q,r]: 10 /% 3 ; q: 3, r: 1
a: 6
divmod 'a 4 ; a: [1, 2]
divide given values and return result
valueA(:integer,:floating,:rational,:literal,:pathliteral,:quantity,:object)valueB(:integer,:floating,:rational,:quantity)
:floating,:rational,:quantity,:object,:nothing
print fdiv 5 2 ; 2.5
a: 6
fdiv 'a 3 ; a: 2.0
increase given value by 1
value(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:object)
:integer,:floating,:complex,:rational,:quantity,:object,:nothing
print inc 5 ; 6
a: 4
inc 'a ; a: 5
calculate the modulo of given values and return result
valueA(:integer,:floating,:rational,:literal,:pathliteral,:quantity,:object)valueB(:integer,:floating,:rational,:quantity)
:integer,:floating,:rational,:quantity,:object,:nothing
print mod 5 2 ; 1
print 9 % 3 ; 0
a: 8
mod 'a 3 ; a: 2
calculate the product of given values and return result
valueA(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:object)valueB(:integer,:floating,:complex,:rational,:quantity,:object)
:integer,:floating,:complex,:rational,:quantity,:object,:nothing
print mul 1 2 ; 2
print 2 * 3 ; 6
a: 5
mul 'a 2 ; a: 10
reverse sign of given value and return it
value(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:object)
:integer,:floating,:complex,:rational,:quantity,:object,:nothing
print neg 1 ; -1
a: 5
neg 'a ; a: -5
calculate the power of given values and return result
valueA(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:object)valueB(:integer,:floating)
:integer,:floating,:complex,:rational,:quantity,:object,:nothing
print pow 2 3 ; 8
print 3 ^ 2 ; 9
a: 5
pow 'a 2 ; a: 25
subtract given values and return result
valueA(:integer,:floating,:complex,:rational,:literal,:pathliteral,:quantity,:color,:object)valueB(:integer,:floating,:complex,:rational,:quantity,:color,:object)
:integer,:floating,:complex,:rational,:quantity,:color,:object,:nothing
print sub 2 1 ; 1
print 5 - 3 ; 2
a: 7
sub 'a 2 ; a: 5
Bit manipulation methods & bitwise operators (AND, OR, XOR, etc) for integer values
calculate the binary AND for the given values
valueA(:integer,:literal,:pathliteral,:binary)valueB(:integer,:binary)
:integer,:binary,:nothing
print and 2 3 ; 2
a: 2
and 'a 3 ; a: 2
calculate the binary NAND for the given values
valueA(:integer,:literal,:pathliteral,:binary)valueB(:integer,:binary)
:integer,:binary,:nothing
print nand 2 3 ; -3
a: 2
nand 'a 3 ; a: -3
calculate the binary NOR for the given values
valueA(:integer,:literal,:pathliteral,:binary)valueB(:integer,:binary)
:integer,:binary,:nothing
print nor 2 3 ; -4
a: 2
nor 'a 3 ; a: -4
calculate the binary complement the given value
value(:integer,:literal,:pathliteral,:binary)
:integer,:binary,:nothing
print not 123 ; -124
a: 123
not 'a ; a: -124
calculate the binary OR for the given values
valueA(:integer,:literal,:pathliteral,:binary)valueB(:integer,:binary)
:integer,:binary,:nothing
print or 2 3 ; 3
a: 2
or 'a 3 ; a: 3
shift-left first value bits by second value
value(:integer,:literal,:pathliteral)bits(:integer)
safe(:logical): check for overflows
:integer,:nothing
print shl 2 3 ; 16
a: 2
shl 'a 3 ; a: 16
shift-right first value bits by second value
value(:integer,:literal,:pathliteral)bits(:integer)
:integer,:nothing
print shr 16 3 ; 2
a: 16
shr 'a 3 ; a: 2
calculate the binary XNOR for the given values
valueA(:integer,:literal,:pathliteral,:binary)valueB(:integer,:binary)
:integer,:binary,:nothing
print xnor 2 3 ; -2
a: 2
xnor 'a 3 ; a: -2
calculate the binary XOR for the given values
valueA(:integer,:literal,:pathliteral,:binary)valueB(:integer,:binary)
:integer,:binary,:nothing
print xor 2 3 ; 1
a: 2
xor 'a 3 ; a: 1
Functions and helpers for manipulating and dealing with different types of collections (blocks, dictionaries, and strings)
append value to given collection
collection(:char,:string,:literal,:pathliteral,:binary,:object,:block)value(:any)
:string,:binary,:object,:block,:nothing
append "hell" "o" ; => "hello"
append [1 2 3] 4 ; => [1 2 3 4]
append [1 2 3] [4 5] ; => [1 2 3 4 5]
print "hell" ++ "o!" ; hello!
print [1 2 3] ++ [4 5] ; [1 2 3 4 5]
a: "hell"
append 'a "o"
print a ; hello
b: [1 2 3]
'b ++ 4
print b ; [1 2 3 4]
create array from given block, by reducing/calculating all internal values
source(:any)
of(:integer,:block): initialize an empty n-dimensional array with given dimensions
:block
none: @[] ; none: []
a: @[1 2 3] ; a: [1 2 3]
b: 5
c: @[b b+1 b+2] ; c: [5 6 7]
d: @[
3+1
print "we are in the block"
123
print "yep"
]
; we are in the block
; yep
; => [4 123]
; initializing empty array with initial value
x: array.of: 2 "done"
inspect.muted x
; [ :block
; done :string
; done :string
; ]
; initializing empty n-dimensional array with initial value
x: array.of: [3 4] 0 ; initialize a 3x4 2D array
; with zeros
; => [[0 0 0 0] [0 0 0 0] [0 0 0 0]]
remove last item from given collection
collection(:string,:literal,:pathliteral,:block)
times(:integer): remove multiple items
:string,:block,:nothing
chop "hellox" ; => "hello"
chop chop "hellox" ; => "hell"
str: "some text"
chop.times:5 str ; => some
chop.times: neg 5 str ; => text
arr: @1..10
chop.times:3 'arr
arr ; => [1 2 3 4 5 6 7]
chop [1 2 3] ; => [1 2]
chop.times:1 [1 2 3] ; => [1 2]
chop.times:2 [1 2 3] ; => [1]
chop.times:3 [1 2 3] ; => []
chop.times:4 [1 2 3] ; => []
chop.times: neg 1 [1 2 3] ; => [2 3]
chop.times: neg 2 [1 2 3] ; => [3]
get all possible combinations of the elements in given collection
collection(:block)
by(:integer): define size of each setrepeated(:logical): allow for combinations with repeated elementscount(:logical): just count the number of combinations
:integer,:block
combine [A B C]
; => [[A B C]]
combine.repeated [A B C]
; => [[A A A] [A A B] [A A C] [A B B] [A B C] [A C C] [B B B] [B B C] [B C C] [C C C]]
combine.by:2 [A B C]
; => [[A B] [A C] [B C]]
combine.repeated.by:2 [A B C]
; => [[A A] [A B] [A C] [B B] [B C] [C C]]
combine.repeated.by: 3 [A B]
; => [[A A A] [A A B] [A B B] [B B B]]
combine.count [A B C]
; => 1
combine.count.repeated.by:2 [A B C]
; => 6
check if collection contains given value
collection(:string,:dictionary,:object,:block,:range)value(:any)
at(:integer): check at given location within collectiondeep(:logical): searches recursively in deep for a value.
:logical
arr: [1 2 3 4]
contains? arr 5 ; => false
contains? arr 2 ; => true
user: #[
name: "John"
surname: "Doe"
]
contains? dict "John" ; => true
contains? dict "Paul" ; => false
contains? keys dict "name" ; => true
contains? "hello" "x" ; => false
contains? "hello" `h` ; => true
contains?.at:1 "hello" "el" ; => true
contains?.at:4 "hello" `o` ; => true
print contains?.at:2 ["one" "two" "three"] "two"
; false
print contains?.at:1 ["one" "two" "three"] "two"
; true
print contains?.deep [1 2 4 [3 4 [5 6] 7] 8 [9 10]] 6
; true
user: #[
name: "John" surname: "Doe"
mom: #[ name: "Jane" surname: "Doe" ]
]
print contains?.deep user "Jane"
; true
get combination of elements in given collections as array of tuples
collectionA(:block)collectionB(:block)
:block
couple ["one" "two" "three"] [1 2 3]
; => [[1 "one"] [2 "two"] [3 "three"]]
get tuple of collections from a coupled collection of tuples
collection(:literal,:pathliteral,:block)
:block
c: couple ["one" "two" "three"] [1 2 3]
; c: [[1 "one"] [2 "two"] [3 "three"]]
decouple c
; => ["one" "two" "three"] [1 2 3]
create dictionary from given block or file, by getting all internal symbols
source(:string,:block)
with(:block): embed given symbolsraw(:logical): create dictionary from raw blocklower(:logical): automatically convert all keys to lowercase
:dictionary
none: #[] ; none: []
a: #[
name: "John"
age: 34
]
; a: [name: "John", age: 34]
d: #[
name: "John"
print "we are in the block"
age: 34
print "yep"
]
; we are in the block
; yep
; d: [name: "John", age: 34]
inspect fromBlock: #.raw [a b c d]
; [ :dictionary
; a : b :word
; c : d :word
; ]
e: #.lower [
Name: "John"
suRnaMe: "Doe"
AGE: 35
]
; e: [name:John, surname:Doe, age:35]
remove first item from given collection
collection(:string,:literal,:pathliteral,:block)
times(:integer): remove multiple items
:string,:block,:nothing
drop "xhello" ; => "hello"
drop drop "xhello" ; => "ello"
str: "some text"
drop.times:5 str ; => text
drop.times: neg 5 str ; => some
arr: @1..10
drop.times:3 'arr
arr ; => [4 5 6 7 8 9 10]
drop [1 2 3] ; => [2 3]
drop.times:1 [1 2 3] ; => [2 3]
drop.times:2 [1 2 3] ; => [3]
drop.times:3 [1 2 3] ; => []
drop.times:4 [1 2 3] ; => []
drop.times: neg 1 [1 2 3] ; => [1 2]
drop.times: neg 2 [1 2 3] ; => [1]
empty given collection
collection(:literal,:pathliteral)
:nothing
a: [1 2 3]
empty 'a ; a: []
str: "some text"
empty 'str ; str: ""
check if given collection is empty
collection(:null,:string,:dictionary,:block)
:logical
empty? "" ; => true
empty? [] ; => true
empty? #[] ; => true
empty? [1 "two" 3] ; => false
get new dictionary by merging given ones
parent(:literal,:pathliteral,:dictionary)additional(:dictionary)
:dictionary
person: #[ name: "john" surname: "doe" ]
print extend person #[ age: 35 ]
; [name:john surname:doe age:35]
return the first item of the given collection
collection(:string,:block,:range)
n(:integer): get first n items
:null,:any
print first "this is some text" ; t
print first ["one" "two" "three"] ; one
print first.n:2 ["one" "two" "three"] ; one two
flatten given collection by eliminating nested blocks
collection(:literal,:pathliteral,:block)
once(:logical): do not perform recursive flattening
:block
arr: [[1 2 3] [4 5 6]]
print flatten arr
; 1 2 3 4 5 6
arr: [[1 2 3] [4 5 6]]
flatten 'arr
; arr: [1 2 3 4 5 6]
flatten [1 [2 3] [4 [5 6]]]
; => [1 2 3 4 5 6]
flatten.once [1 [2 3] [4 [5 6]]]
; => [1 2 3 4 [5 6]]
get collection's item by given index
collection(:complex,:string,:error,:errorkind,:date,:binary,:dictionary,:object,:store,:block,:range,:bytecode)index(:any)
:any
user: #[
name: "John"
surname: "Doe"
]
print user\name ; John
print get user 'surname ; Doe
print user\surname ; Doe
arr: ["zero" "one" "two"]
print arr\1 ; one
print get arr 2 ; two
y: 2
print arr\[y] ; two
str: "Hello world!"
print str\0 ; H
print get str 1 ; e
z: 0
print str\[z+1] ; e
print str\[0..4] ; Hello
a: to :complex [1 2]
print a\real ; 1.0
print a\imaginary ; 2.0
print a\1 ; 2.0
check if value exists in given collection
value(:any)collection(:string,:dictionary,:object,:block,:range)
at(:integer): check at given location within collectiondeep(:logical): searches recursively in deep for a value.
:logical
arr: [1 2 3 4]
in? 5 arr ; => false
in? 2 arr ; => true
user: #[
name: "John"
surname: "Doe"
]
in? "John" dict ; => true
in? "Paul" dict ; => false
in? "name" keys dict ; => true
in? "x" "hello" ; => false
in? `h` "hello" ; => true
in?.at:1 "el" "hello" ; => true
in?.at:4 `o` "hello" ; => true
print in?.at:2 "two" ["one" "two" "three"]
; false
print in?.at:1 "two" ["one" "two" "three"]
; true
print in?.deep 6 [1 2 4 [3 4 [5 6] 7] 8 [9 10]]
; true
user: #[
name: "John" surname: "Doe"
mom: #[ name: "Jane" surname: "Doe" ]
]
print in?.deep "Jane" user
; true
return first index of value in given collection
collection(:string,:dictionary,:block,:range)value(:any)
:null,:integer,:string
ind: index "hello" "e"
print ind ; 1
print index [1 2 3] 3 ; 2
type index "hello" "x"
; :null
insert value in collection at given index
collection(:string,:literal,:pathliteral,:dictionary,:block)index(:integer,:string)value(:any)
:string,:dictionary,:block,:nothing
insert [1 2 3 4] 0 "zero"
; => ["zero" 1 2 3 4]
print insert "heo" 2 "ll"
; hello
dict: #[
name: John
]
insert 'dict 'name "Jane"
; dict: [name: "Jane"]
check if collection contains given key
collection(:dictionary,:object)key(:any)
:logical
user: #[
name: "John"
surname: "Doe"
]
key? user 'age ; => false
if key? user 'name [
print ["Hello" user\name]
]
; Hello John
get list of keys for given collection
dictionary(:dictionary,:object)
:block
user: #[
name: "John"
surname: "Doe"
]
keys user
=> ["name" "surname"]
return the last item of the given collection
collection(:string,:block,:range)
n(:integer): get last n items
:null,:any
print last "this is some text" ; t
print last ["one" "two" "three"] ; three
print last.n:2 ["one" "two" "three"] ; two three
get maximum element in given collection
collection(:block,:range)
index(:logical): retrieve index of maximum element
:null,:any
print max [4 2 8 5 1 9] ; 9
get minimum element in given collection
collection(:block,:range)
index(:logical): retrieve index of minimum element
:null,:any
print min [4 2 8 5 1 9] ; 1
check if given number or collection size is one
number(:null,:integer,:floating,:string,:dictionary,:block,:range)
:logical
one? 5 ; => false
one? 4-3 ; => true
one? 1.0 ; => true
one? 0.0 ; => false
items: ["apple"]
one? items ; => true
items: [1 2 3]
one? items ; => false
one? ø ; => false
get all possible permutations of the elements in given collection
collection(:block)
by(:integer): define size of each setrepeated(:logical): allow for permutations with repeated elementscount(:logical): just count the number of permutations
:block
permutate [A B C]
; => [[A B C] [A C B] [B A C] [B C A] [C A B] [C B A]]
permutate.repeated [A B C]
; => [[A A A] [A A B] [A A C] [A B A] [A B B] [A B C] [A C A] [A C B] [A C C] [B A A] [B A B] [B A C] [B B A] [B B B] [B B C] [B C A] [B C B] [B C C] [C A A] [C A B] [C A C] [C B A] [C B B] [C B C] [C C A] [C C B] [C C C]]
permutate.by:2 [A B C]
; => [[A B] [A C] [B A] [B C] [C A] [C B]]
permutate.repeated.by:2 [A B C]
; => [[A A] [A B] [A C] [B A] [B B] [B C] [C A] [C B] [C C]]
permutate.repeated.by:3 [A B]
; => [[A A A] [A A B] [A B A] [A B B] [B A A] [B A B] [B B A] [B B B]]
permutate.count [A B C]
; => 6
permutate.count.repeated.by:2 [A B C]
; => 9
remove and return the last item from given collection
collection(:literal,:pathliteral)
n(:integer): remove multiple items. (Must be greater than 0.)
:any
a: [0 1 2 3 4 5]
b: pop 'a
inspect a
; [ :block
; 0 :integer
; 1 :integer
; 2 :integer
; 3 :integer
; 4 :integer
; ]
inspect b ; 5 :integer
b: pop.n: 2 'a
inspect a
; [ :block
; 0 :integer
; 1 :integer
; 2 :integer
; ]
inspect b
; [ :block
; 3 :integer
; 4 :integer
; ]
a: "Arturoo"
b: pop 'a
inspect a ; Arturo :string
inspect b ; o :char
b: pop.n: 3 'a
inspect a ; Art :string
inspect b ; uro :string
prepend value to given collection
collection(:char,:string,:literal,:pathliteral,:binary,:block)value(:any)
:string,:binary,:block,:nothing
prepend "uro" "Art" ; => "Arturo"
prepend [2 3 4] 1 ; => [1 2 3 4]
prepend [3 4 5] [1 2] ; => [1 2 3 4 5]
a: "pend"
prepend 'a "pre"
print a ; prepend
get list of values in given range (inclusive)
from(:integer,:char)to(:integer,:floating,:char)
step(:integer): use step between range values
:range
remove value from given collection
collection(:string,:literal,:pathliteral,:dictionary,:object,:block)value(:any)
key(:logical): remove dictionary keyonce(:logical): remove only first occurenceindex(:logical): remove specific indexprefix(:logical): remove first matching prefix from stringsuffix(:logical): remove first matching suffix from stringinstance(:logical): remove an instance of a block, instead of its elements.
:string,:dictionary,:block,:nothing
remove "hello" "l" ; => "heo"
print "hello" -- "l" ; heo
str: "mystring"
remove 'str "str"
print str ; mying
print remove.once "hello" "l"
; helo
; Remove each element of given block from collection once
remove.once [1 2 [1 2] 3 4 1 2 [1 2] 3 4] [1 2]
; [[1 2] 3 4 1 2 [1 2] 3 4]
remove [1 2 3 4] 4 ; => [1 2 3]
remove.instance [1 [6 2] 5 3 [6 2] 4 5 6] [6 2] ; => [1 5 3 4 5 6]
remove.instance.once [1 [6 2] 5 3 [6 2] 4 5 6] [6 2] ; => [1 5 3 [6 2] 4 5 6]
repeat value the given number of times and return new one
value(:literal,:pathliteral,:any)times(:integer)
:string,:block
print repeat "hello" 3
; hellohellohello
repeat [1 2 3] 3
; => [1 2 3 1 2 3 1 2 3]
repeat 5 3
; => [5 5 5]
repeat [[1 2 3]] 3
; => [[1 2 3] [1 2 3] [1 2 3]]
reverse given collection
collection(:string,:literal,:pathliteral,:block,:range)
exact(:logical): make sure the reverse range contains the same elements
:string,:block,:nothing
print reverse [1 2 3 4] ; 4 3 2 1
print reverse "Hello World" ; dlroW olleH
str: "my string"
reverse 'str
print str ; gnirts ym
right-rotate collection by given distance
collection(:string,:literal,:pathliteral,:block)distance(:integer)
left(:logical): left rotation
:string,:block,:nothing
rotate [a b c d e] 1 ; => [e a b c d]
rotate.left [a b c d e] 1 ; => [b c d e a]
rotate 1..6 4 ; => [3 4 5 6 1 2]
get a random element from given collection
collection(:block,:range)
:null,:any
sample [1 2 3] ; (return a random number from 1 to 3)
print sample ["apple" "appricot" "banana"]
; apple
set collection's item at index to given value
collection(:string,:binary,:dictionary,:object,:store,:block,:bytecode)index(:any)value(:any)
:nothing
myDict: #[
name: "John"
age: 34
]
set myDict 'name "Michael" ; => [name: "Michael", age: 34]
arr: [1 2 3 4]
set arr 0 "one" ; => ["one" 2 3 4]
arr\1: "dos" ; => ["one" "dos" 3 4]
x: 2
arr\[x]: "tres" ; => ["one" "dos" "tres" 4]
str: "hello"
str\0: `x`
print str
; xello
get given collection shuffled
collection(:literal,:pathliteral,:block)
:block,:nothing
shuffle [1 2 3 4 5 6] ; => [1 5 6 2 3 4 ]
arr: [2 5 9]
shuffle 'arr
print arr ; 5 9 2
get size/length of given collection
collection(:null,:string,:binary,:dictionary,:object,:block,:range)
:integer,:floating
arr: ["one" "two" "three"]
print size arr ; 3
dict: #[name: "John", surname: "Doe"]
print size dict ; 2
str: "some text"
print size str ; 9
print size "你好!" ; 3
get a slice of collection between given indices
collection(:string,:literal,:pathliteral,:block)from(:integer)to(:integer)
:string,:block
slice "Hello" 0 3 ; => "Hell"
print slice 1..10 3 4 ; 4 5
sort given block in ascending order
collection(:literal,:pathliteral,:dictionary,:block)
as(:literal): localized by ISO 639-1 language codesensitive(:logical): case-sensitive sortingdescending(:logical): sort in descending ordervalues(:logical): sort dictionary by valuesby(:string,:literal): sort array of dictionaries by given key
:block,:nothing
a: [3 1 6]
print sort a ; 1 3 6
print sort.descending a ; 6 3 1
b: ["one" "two" "three"]
sort 'b
print b ; one three two
check if given collection is already sorted
collection(:block)
descending(:logical): check for sorting in ascending order
:logical
sorted? [1 2 3 4 5] ; => true
sorted? [4 3 2 1 5] ; => false
sorted? [5 4 3 2 1] ; => false
sorted?.descending [5 4 3 2 1] ; => true
sorted?.descending [4 3 2 1 5] ; => false
sorted?.descending [1 2 3 4 5] ; => false
split collection to components
collection(:string,:literal,:pathliteral,:block)
words(:logical): split string by whitespacelines(:logical): split string by linesby(:string,:regex,:block): split using given separatorat(:integer): split collection at given positionevery(:integer): split collection every n elementspath(:logical): split path components in string
:block,:nothing
split "hello" ; => [`h` `e` `l` `l` `o`]
split.words "hello world" ; => ["hello" "world"]
split.every: 2 "helloworld"
; => ["he" "ll" "ow" "or" "ld"]
split.at: 4 "helloworld"
; => ["hell" "oworld"]
arr: 1..9
split.at:3 'arr
; => [ [1 2 3 4] [5 6 7 8 9] ]
reduce adjacent elements in given collection
collection(:string,:literal,:pathliteral,:block)
:string,:block,:nothing
print squeeze [1 1 2 3 4 2 3 4 4 5 5 6 7]
; 1 2 3 4 2 3 4 5 6 7
arr: [4 2 1 1 3 6 6]
squeeze 'arr ; a: [4 2 1 3 6]
print squeeze "hello world"
; helo world
keep first of elements from given collection and return the remaining ones
collection(:string,:literal,:pathliteral,:block,:range)number(:integer)
:string,:block,:nothing
str: "some text"
take str 4 ; => some
take str neg 4 ; => text
take 1..3 2 ; => [1 2]
arr: @1..10
take 'arr 3
arr ; => arr: [1 2 3]
take [1 2 3] 3 ; => [1 2 3]
take [1 2 3] 4 ; => [1 2 3]
find number of occurences of each value within given block and return as dictionary
collection(:string,:block)
:dictionary
tally "helloWorld"
; => [h:1 e:1 l:3 o:2 W:1 r:1 d:1]
tally [1 2 4 1 3 5 6 2 6 3 5 7 2 4 2 4 5 6 2 1 1 1]
; => [1:5 2:5 4:3 3:2 5:3 6:3 7:1]
get given collection without duplicates
collection(:string,:literal,:pathliteral,:block)
id(:logical): generate unique id using given prefix
:block,:nothing
arr: [1 2 4 1 3 2]
print unique arr ; 1 2 4 3
arr: [1 2 4 1 3 2]
unique 'arr
print arr ; 1 2 4 3
get list of values for given collection
dictionary(:dictionary,:object,:block,:range)
:block
user: #[
name: "John"
surname: "Doe"
]
values user
=> ["John" "Doe"]
check if given number or collection size is zero
number(:null,:integer,:floating,:string,:dictionary,:block,:range)
:logical
zero? 5-5 ; => true
zero? 4 ; => false
zero? 1.0 ; => false
zero? 0.0 ; => true
items: [1 2 3]
zero? items ; => false
items: []
zero? items ; => true
zero? ø ; => true
Functions and helpers for manipulating color values
blend given colors and get result
colorA(:literal,:pathliteral,:color)colorB(:color)
balance(:floating): use different mix of color (0.0-1.0, default:0.5)
:color
blend #red #CCCCCC ; => #E66666
blend .balance: 0.75 #red #CCCCCC
; => #D99999
darken color by given percentage (0.0-1.0)
color(:literal,:pathliteral,:color)percent(:floating)
:color
darken #red 0.2 ; => #CC0000
darken #red 0.5 ; => #7F0000
darken #9944CC 0.3 ; => #6B308F
desaturate color by given percentage (0.0-1.0)
color(:literal,:pathliteral,:color)percent(:floating)
:color
desaturate #red 0.2 ; => #E61919
desaturate #red 0.5 ; => #BF4040
desaturate #9944CC 0.3 ; => #9558B8
convert color to grayscale
color(:literal,:pathliteral,:color)
:color
grayscale #red ; => #808080
grayscale #green ; => #404040
grayscale #FF44CC ; => #A2A2A2
get complement for given color
color(:literal,:pathliteral,:color)
:color
print #orange ; #FFA500
invert #orange ; => #0059FF
lighten color by given percentage (0.0-1.0)
color(:literal,:pathliteral,:color)percent(:floating)
:color
print #lightblue ; #ADD8E6
lighten #lightblue 0.2 ; => #D0FFFF
lighten #lightblue 0.5 ; => #FFFFFF
lighten #9944CC 0.3 ; => #C758FF
create palette using given base color
color(:color)
triad(:logical): generate a triad palettetetrad(:logical): generate a tetrad palettesplit(:logical): generate a split complement paletteanalogous(:logical): generate an analogous palettemonochrome(:logical): generate a monochromatic paletterandom(:logical): generate random palette based on color triadssize(:integer): specify the size of the generated palette
:block
palette.triad #red ; => [#FF0000 #00FF00 #0000FF]
palette.tetrad #red ; => [#FF0000 #80FF00 #00FFFF #7F00FF]
palette.monochrome #red
; => [#FF0000 #D40000 #AA0000 #7F0000 #550000 #2A0000]
palette.monochrome.size:10 #red
; => [#FF0000 #E50000 #CC0000 #B20000 #990000 #7F0000 #660000 #4C0000 #330000 #190000]
palette.analogous #red
; => [#FF0099 #FF0066 #FF0033 #FF0000 #FF3300 #FF6600]
palette.analogous.size:10 #red
; => [#FF00FF #FF00CC #FF0099 #FF0066 #FF0033 #FF0000 #FF3300 #FF6600 #FF9900 #FFCC00]
palette.random #red
; => [#FF0000 #00EC00 #0000D2 #00F000 #0000FF #00FF00]
palette.random.size:10 #red
; => [#FF0000 #00FF00 #0000FF #00FE00 #F30000 #00FD00 #0000ED #EC0000 #00F800 #0000D8]
saturate color by given percentage (0.0-1.0)
color(:literal,:pathliteral,:color)percent(:floating)
:color
print #lightblue ; #ADD8E6
saturate #lightblue 0.2 ; => #A7DBEC
saturate #lightblue 0.5 ; => #9FDFF4
saturate #9944CC 0.3 ; => #A030E0
spin color around the hue wheel by given amount
color(:literal,:pathliteral,:color)amount(:integer)
:color
spin #red 90 ; => #80FF00
spin #red 180 ; => #00FFFF
spin #123456 45 ; => #231256
spin #123456 360 ; => #123456
Comparison operations for any kind of value
check if given value is between the given values (inclusive)
value(:any)rangeFrom(:any)rangeTo(:any)
:logical
between? 1 2 3 ; => false
between? 2 0 3 ; => true
between? 3 2 3 ; => true
between? 3 3 2 ; => true
1 <=> 2 3 ; => false
1 <=> 3 2 ; => false
2 <=> 0 3 ; => true
2 <=> 3 0 ; => true
3 <=> 2 3 ; => true
compare given values and return -1, 0, or 1 based on the result
valueA(:any)valueB(:any)
:integer
compare 1 2 ; => -1
compare 3 3 ; => 0
compare 4 3 ; => 1
check if valueA = valueB (equality)
valueA(:any)valueB(:any)
:logical
equal? 5 2 ; => false
equal? 5 6-1 ; => true
print 3=3 ; true
check if valueA > valueB (greater than)
valueA(:any)valueB(:any)
:logical
greater? 5 2 ; => true
greater? 5 6-1 ; => false
print 3>2 ; true
check if valueA >= valueB (greater than or equal)
valueA(:any)valueB(:any)
:logical
greaterOrEqual? 5 2 ; => true
greaterOrEqual? 5 4-1 ; => false
print 2>=2 ; true
check if valueA < valueB (less than)
valueA(:any)valueB(:any)
:logical
less? 5 2 ; => false
less? 5 6+1 ; => true
print 2<3 ; true
check if valueA =< valueB (less than or equal)
valueA(:any)valueB(:any)
:logical
lessOrEqual? 5 2 ; => false
lessOrEqual? 5 6-1 ; => true
print 2=<3 ; true
check if valueA <> valueB (not equal)
valueA(:any)valueB(:any)
:logical
notEqual? 5 2 ; => true
notEqual? 5 6-1 ; => false
print 2<>3 ; true
check if given values are exactly the same (identity)
valueA(:any)valueB(:any)
:logical
same? 1 2 ; => false
same? 3 3 ; => true
same? 3 3.0 ; => false
Functions and helpers to create common objects (functions, arrays, dictionaries) and utilities for converting between different types and formats
format given value as implied type
value(:any)
binary(:logical): format integer as binaryhex(:logical): format integer as hexadecimaloctal(:logical): format integer as octalagnostic(:logical): convert words in block to literals, if not in contextdata(:logical): parse input as Arturo data blockcode(:logical): convert value to valid Arturo codepretty(:logical): prettify generated codeunwrapped(:logical): omit external block notation
:any
print as.binary 123 ; 1111011
print as.octal 123 ; 173
print as.hex 123 ; 7b
get value from string, using given representation
value(:string,:literal)
binary(:logical): get integer from binary representationhex(:logical): get integer from hexadecimal representationoctal(:logical): get integer from octal representationopcode(:logical): get opcode by from opcode literal
:any
print from.binary "1011" ; 11
print from.octal "1011" ; 521
print from.hex "0xDEADBEEF" ; 3735928559
from.opcode 'push1
=> 33
Basic functions and constants that make up the very core of the language
assign symbol to given function
symbol(:string,:symbol,:symbolliteral,:block)function(:string,:word,:literal)
infix(:logical): use infix precedence
:nothing
addThem: function [x, y][
x + y
]
alias --> 'addThem
print --> 2 3
; 5
multiplyThem: function [x, y][ x * y ]
alias.infix {<=>} 'multiplyThem
print 2 <=> 3
; 6
break out of current block or loop
:nothing
loop 1..5 'x [
print ["x:" x]
if x=3 -> break
print "after check"
]
print "after loop"
; x: 1
; after check
; x: 2
; after check
; x: 3
; after loop
call function with given list of parameters
function(:string,:word,:literal,:pathliteral,:function,:method)params(:block)
external(:string): path to external libraryexpect(:type): expect given return type
:any
multiply: function [x y][
x * y
]
call 'multiply [3 5] ; => 15
call $[x][x+2] [5] ; 7
; Calling external (C code) functions
; compile with:
; clang -c -w mylib.c
; clang -shared -o libmylib.dylib mylib.o
;
; NOTE:
; * If you're using GCC, just replace `clang` by `gcc`
; * If you're not on MacOS, replace your `dylib` by the right extension
; normally they can be `.so` or `.dll` in other Operational Systems.
; #include <stdio.h>
;
; void sayHello(char* name){
; printf("Hello %s!\n", name);
; }
;
; int doubleNum(int num){
; return num * 2;
;}
; call an external function directly
call.external: "mylib" 'sayHello ["John"]
; map an external function to a native one
doubleNum: function [num][
ensure -> integer? num
call .external: "mylib"
.expect: :integer
'doubleNum @[num]
]
loop 1..3 'x [
print ["The double of" x "is" doubleNum x]
]
initiate a case block to check for different cases
predicate(:null,:block)
:nothing
a: 2
case [a]
when? [<2] -> print "a is less than 2"
when? [=2] -> print "a is 2"
else -> print "a is greater than 2"
if first value is null or false, return second value; otherwise return the first one
value(:any)alternative(:any)
:any
; Note that 'attr returns null if it has no attribute
print coalesce attr "myAttr" "attr not found"
print (attr "myAttr") ?? "attr not found"
print (myData) ?? defaultData
immediately continue with next iteration
:nothing
loop 1..5 'x [
print ["x:" x]
if x=3 -> continue
print "after check"
]
print "after loop"
; x: 1
; after check
; x: 2
; after check
; x: 3
; x: 4
; after check
; x: 5
; after check
; after loop
evaluate and execute given code
code(:string,:block,:bytecode)
times(:integer): repeat block execution given number of times
:any
do "print 123" ; 123
do [
x: 3
print ["x =>" x] ; x => 3
]
print do "https://raw.githubusercontent.com/arturo-lang/arturo/master/examples/projecteuler/euler1.art"
; 233168
do.times: 3 [
print "Hello!"
]
; Hello!
; Hello!
; Hello!
; Importing modules
; let's say you have a 'module.art' with this code:
;
; pi: 3.14
;
; hello: $[name :string] [
; print ["Hello" name]
;]
do relative "module.art"
print pi
; 3.14
do [
hello "John Doe"
; Hello John Doe
]
; Note: always use imported functions inside a 'do block
; since they need to be evaluated beforehand.
; On the other hand, simple variables can be used without
; issues, as 'pi in this example
duplicate the top of the stack and convert non-returning call to a do-return call
value(:any)
:any
; a label normally consumes its inputs
; and returns nothing
; using dup before a call, the non-returning function
; becomes a returning one
a: b: <= 3
print a ; 3
print b ; 3
perform action, if last condition was not true
otherwise(:block,:bytecode)
:nothing
x: 2
z: 3
if? x>z [
print "x was greater than z"
]
else [
print "nope, x was not greater than z"
]
assert given condition is true, or exit
condition(:block)
that(:string): prints a custom message when ensure fails
:nothing
num: input "give me a positive number"
ensure [num > 0]
print "good, the number is positive indeed. let's continue..."
ensure.that: "Wrong calc" -> 0 = 1 + 1
; >> Assertion | "Wrong calc": [0 = 1 + 1]
; error |
export given container children to current scope
module(:dictionary,:object,:module)
all(:logical): export everything, regardless of whether it's been marked as public (makes sense only for modules)
:nothing
create function with given arguments and body
arguments(:literal,:block)body(:block)
import(:block): import/embed given list of symbols from current environmentexport(:block): export given symbols to parentmemoize(:logical): store results of function callsinline(:logical): execute function without scope
:function
f: function [x][ x + 2 ]
print f 10 ; 12
f: $[x][x+2]
print f 10 ; 12
multiply: function [x,y][
x * y
]
print multiply 3 5 ; 15
; forcing typed parameters
addThem: function [
x :integer
y :integer :floating
][
x + y
]
; adding complete documentation for user function
; using data comments within the body
addThem: function [
x :integer :floating
y :integer :floating
][
;; description: « takes two numbers and adds them up
;; options: [
;; mul: :integer « also multiply by given number
;; ]
;; returns: :integer :floating
;; example: {
;; addThem 10 20
;; addThem.mult:3 10 20
;; }
mult?: attr 'mult
if? not? null? mult? ->
return mult? * x + y
else ->
return x + y
]
info'addThem
; |--------------------------------------------------------------------------------
; | addThem :function 0x10EF0E528
; |--------------------------------------------------------------------------------
; | takes two numbers and adds them up
; |--------------------------------------------------------------------------------
; | usage addThem x :integer :floating
; | y :integer :floating
; |
; | options .mult :integer -> also multiply by given number
; |
; | returns :integer :floating
; |--------------------------------------------------------------------------------
publicF: function .export:['x] [z][
print ["z =>" z]
x: 5
]
publicF 10
; z => 10
print x
; 5
; memoization
fib: $[x].memoize[
if? x<2 [1]
else [(fib x-1) + (fib x-2)]
]
loop 1..25 [x][
print ["Fibonacci of" x "=" fib x]
]
perform action, if given condition is not false or null
condition(:any)action(:block,:bytecode)
:nothing
x: 2
if x=2 -> print "yes, that's right!"
; yes, that's right!
perform action, if given condition is not false or null and return condition result
condition(:any)action(:block)
:logical
x: 2
result: if? x=2 -> print "yes, that's right!"
; yes, that's right!
print result
; true
x: 2
z: 3
if? x>z [
print "x was greater than z"
]
else [
print "nope, x was not greater than z"
]
import given package
package(:string,:literal,:block)
version(:version): specify package versionmin(:logical): get any version >= the specified onebranch(:string,:literal): use specific branch for repository url (default: main)latest(:logical): always check for the latest version availablelean(:logical): return as a dictionary, instead of importing in main namespaceonly(:block): import only selected symbols, if availableverbose(:logical): output extra information
:dictionary,:block,:nothing
import "dummy" ; import the package 'dummy'
do ::
print dummyFunc 10 ; and use it :)
import.version:0.0.3 "dummy" ; import a specific version
import.min.version:0.0.3 "dummy" ; import at least the give version;
; if there is a newer one, it will pull this one
import.latest "dummy" ; whether we already have the package or not
; always try to pull the latest version
import "https://github.com/arturo-lang/dummy-package"
; we may also import user repositories directly
import.branch:"main" "https://github.com/arturo-lang/dummy-package"
; even specifying the branch to pull
import "somefile.art" ; importing a local file is possible
import "somepackage" ; the same works if we have a folder that
; is actually structured like a package
d: import.lean "dummy" ; importing a package as a dictionary
; for better namespace isolation
do [
print d\dummyFunc 10 ; works fine :)
]
set symbol to given value
symbol(:string,:word,:literal,:block)value(:any)
:nothing
let 'x 10 ; x: 10
print x ; 10
; variable assignments
"a": 2 ; a: 2
{_someValue}: 3
print var {_someValue} ; 3
; multiple assignments
[a b]: [1 2]
print a ; 1
print b ; 2
; multiple assignment to single value
[a b c]: 5
print a ; 5
print b ; 5
print c ; 5
; unpacking slices and multiple assignment
[a [b] d c]: [1 2 3 4 5]
print a ; 1
print b ; [2 3]
print c ; 4
print d ; 5
; tuple unpacking
divmod: function [x,y][
@[x/y x%y]
]
[d,m]: divmod 10 3 ; d: 3, m: 1
create type method with given arguments and body
arguments(:literal,:block)body(:block)
distinct(:logical): shouldn't be treated as a magic methodpublic(:logical): make method public (relevant only in modules!)
:method
define :cat [
init: method [nick :string age :integer][
this\nick: join.with: " " @["Mr." capitalize nick]
this\age: age
]
; Function overloading
add: method [years :integer][
this\age: age + this\age
]
meow: method [][
print [~"|this\nick|:" "'meow!'"]
]
]
a: to :cat [15 15]
; >> Assertion | [is? :string nick]
; error |
snowflake: to :cat ["snowflake" 3]
snowflake\meow
; Mr. Snowflake: 'meow!'
; use `do -> snowflake\meow` instead
; when running the above code from a file
add snowflake 3
snowflake\age
; => 6
snowflake\add 3
print snowflake\age
; => 9
; use `do [snowflake\add 3]` instead
; when running the above code from a file
create new module with given contents
contents(:dictionary,:block)
with(:block): use given initialization parameters
:module
create new value by cloning given one
value(:any)
:any
c: "Hello"
d: new c ; make a copy of the older string
; changing one string in-place
; will change only the string in question
'd ++ "World"
print d ; HelloWorld
print c ; Hello
the NULL constant
return given value from current function
value(:any)
:nothing
f: function [x][
loop 1..x 'y [
if y=5 [ return y*2 ]
]
return x*2
]
print f 3 ; 6
print f 6 ; 10
check if given variable is defined
symbol(:string,:literal)
:logical
boom: 12
print set? 'boom ; true
print set? 'zoom ; false
if condition is not false or null perform given action, otherwise perform alternative action
condition(:any)action(:block)alternative(:block)
:any
x: 2
switch x=2 -> print "yes, that's right!"
-> print "nope, that's not right!"
; yes, that's right!
perform action, if given condition is false or null
condition(:any)action(:block,:bytecode)
:nothing
x: 2
unless x=1 -> print "yep, x is not 1!"
; yep, x is not 1!
perform action, if given condition is false or null and return condition result
condition(:any)action(:block,:bytecode)
:logical
x: 2
result: unless? x=1 -> print "yep, x is not 1!"
; yep, x is not 1!
print result
; true
z: 1
unless? x>z [
print "yep, x was not greater than z"
]
else [
print "x was greater than z"
]
; x was greater than z
undefine given symbol, if already defined
symbol(:string,:literal)
:nothing
a: 2
print a
; 2
unset 'a
print a
; will throw an error
pop top values from stack
number(:integer)
discard(:logical): do not return anything
:any
1 2 3
a: unstack 1 ; a: 3
1 2 3
b: unstack 2 ; b: [3 2]
1 2 3
unstack.discard 1 ; popped 3 from the stack
execute action until the given condition is not false or null
action(:block,:bytecode)condition(:block,:bytecode)
:nothing
i: 0
until [
print ["i =>" i]
i: i + 1
][i = 10]
; i => 0
; i => 1
; i => 2
; i => 3
; i => 4
; i => 5
; i => 6
; i => 7
; i => 8
; i => 9
get symbol value by given name
symbol(:string,:word,:literal,:pathliteral)
:any
a: 2
print var 'a ; 2
f: function [x][x+2]
print f 10 ; 12
g: var 'f
print g 10 ; 12
check if a specific condition is fulfilled and, if so, execute given action
condition(:block)action(:block)
:logical
a: 2
case [a]
when? [<2] -> print "a is less than 2"
when? [=2] -> print "a is 2"
else -> print "a is greater than 2"
execute action while the given condition is is not false or null
condition(:null,:block,:bytecode)action(:block,:bytecode)
:nothing
i: 0
while [i<10][
print ["i =>" i]
i: i + 1
]
; i => 0
; i => 1
; i => 2
; i => 3
; i => 4
; i => 5
; i => 6
; i => 7
; i => 8
; i => 9
while ø [
print "something" ; infinitely
]
create closure-style block by embedding given words
embed(:string,:word,:literal,:dictionary,:block)body(:block)
:block
f: function [x][
with [x][
"the multiple of" x "is" 2*x
]
]
multiplier: f 10
print multiplier
; the multiple of 10 is 20
Cryptography- or encoding-related functions
calculate the CRC32 polynomial of given string
value(:string,:literal,:pathliteral)
:string,:nothing
print crc "The quick brown fox jumps over the lazy dog"
; 414FA339
decode given value (default: base-64)
value(:string,:literal,:pathliteral)
url(:logical): decode URL based on RFC3986
:string,:nothing
print decode "TnVtcXVhbSBmdWdpZW5zIHJlc3BleGVyaXM="
; Numquam fugiens respexeris
print decode.url "http%3A%2F%2Ffoo+bar%2F"
; http://foo bar/
get digest for given value (default: MD5)
value(:string,:literal,:pathliteral)
sha(:logical): use SHA1
:string,:nothing
print digest "Hello world"
; 3e25960a79dbc69b674cd4ec67a72c62
print digest.sha "Hello world"
; 7b502c3a1f48c8609ae212cdfb639dee39673f5e
encode given value (default: base-64)
value(:string,:literal,:pathliteral)
url(:logical): encode URL based on RFC3986spaces(:logical): also encode spacesslashes(:logical): also encode slashesfrom(:string): source character encoding (default: CP1252)to(:string): target character encoding (default: UTF-8)
:string,:nothing
print encode "Numquam fugiens respexeris"
; TnVtcXVhbSBmdWdpZW5zIHJlc3BleGVyaXM=
print encode.url "http://foo bar/"
; http%3A%2F%2Ffoo+bar%2F
get hash for given value
value(:any)
string(:logical): get as a string
:integer,:string
print hash "hello" ; 613153351
print hash [1 2 3] ; 645676735036410
print hash 123 ; 123
a: [1 2 3]
b: [1 2 3]
print (hash a)=(hash b) ; true
Functions for creating and query databases
close given database
database(:database)
:nothing
db: open "my.db" ; opens an SQLite database named 'my.db'
print query db "SELECT * FROM users"
close db ; and close it
opens a new database connection and returns database
name(:string)
sqlite(:logical): support for SQLite databasesmysql(:logical): support for MySQL databases
:database
db: open "my.db" ; opens an SQLite database named 'my.db'
execute command or block of commands in given database and get returned rows
database(:database)commands(:string,:block)
id(:logical): return last INSERT idwith(:block): use arguments for parametrized statement
:null,:integer,:block
db: open "my.db" ; opens an SQLite database named 'my.db'
; perform a simple query
print query db "SELECT * FROM users"
; perform an INSERT query and get back the record's ID
username: "johndoe"
lastInsertId: query.id db ~{!sql INSERT INTO users (name) VALUES ('|username|')}
; perform a safe query with given parameters
print query db .with: ["johndoe"] {!sql SELECT * FROM users WHERE name = ?}
create or load a persistent store on disk
path(:string,:literal)
deferred(:logical): save to disk only on program terminationglobal(:logical): save as global storenative(:logical): force native/Arturo formatjson(:logical): force Json formatdb(:logical): force database/SQlite format
:range
; create a new store with the name `mystore`
; it will be automatically live-stored in a file in the same folder
; using the native Arturo format
data: store "mystore"
; store some data
data\name: "John"
data\surname: "Doe"
data\age: 36
; and let's retrieve our data
data
; => [name:"John" surname:"Doe" age:36]
; create a new "global" configuration store
; that will be saved automatically in ~/.arturo/stores
globalStore: store.global "configuration"
; we are now ready to add or retrieve some persistent data!
; create a new JSON store with the name `mystore`
; it will be automatically live-stored in a file in the same folder
; with the name `mystore.json`
data: store.json "mystore"
; store some data
da\people: []
; data can be as complicated as in any normal dictionary
da\people: da\people ++ #[name: "John" surname: "Doe"]
; check some specific store value
da\people\0\name
; => "John"
; create a new deferred store with the name `mystore`
; it will be automatically saved in a file in the same folder
; using the native Arturo format
defStore: store.deferred "mystore"
; let's save some data
defStore\name: "John"
defStore\surname: "Doe"
; and print it
print defStore
; [name:John surname:Doe]
; in this case, all data is available at any given moment
; but will not be saved to disk for each and every operation;
; instead, it will be saved in its totality just before
; the program terminates!
Functions for manipulating dates
get date after given one using interval
date(:literal,:pathliteral,:date)
nanoseconds(:integer): add given number of nanosecondsmilliseconds(:integer): add given number of millisecondsseconds(:integer): add given number of secondsminutes(:integer): add given number of minuteshours(:integer): add given number of hoursdays(:integer): add given number of daysweeks(:integer): add given number of weeksmonths(:integer): add given number of monthsyears(:integer): add given number of years
:date
print now
; 2021-03-22T11:25:30+01:00
print after.weeks:2 now
; 2021-04-05T11:25:42+02:00
get date before given one using interval
date(:literal,:pathliteral,:date)
nanoseconds(:integer): subtract given number of nanosecondsmilliseconds(:integer): subtract given number of millisecondsseconds(:integer): subtract given number of secondsminutes(:integer): subtract given number of minuteshours(:integer): subtract given number of hoursdays(:integer): subtract given number of daysweeks(:integer): subtract given number of weeksmonths(:integer): subtract given number of monthsyears(:integer): subtract given number of years
:date
print now
; 2021-03-22T11:27:00+01:00
print before.weeks:2 now
; 2021-03-08T11:27:14+01:00
print before.years:1 now
; 2020-03-22T11:27:23+01:00
check if given date is a Friday
date(:date)
:logical
print friday? now ; false
check if given date is in the future
date(:date)
:logical
futureDate: after.weeks:2 now
print future? now ; false
print future? futureDate ; true
check if given year is a leap year
year(:integer,:date)
:logical
print leap? now ; false
print map 2019..2021 => leap?
; false true false
check if given date is a Monday
date(:date)
:logical
print sunday? now ; false
get date/time now
:date
print now ; 2020-10-23T14:16:13+02:00
time: now
inspect time
; [ :date
; hour : 14 :integer
; minute : 16 :integer
; second : 55 :integer
; nanosecond : 82373000 :integer
; day : 23 :integer
; Day : Friday :string
; month : 10 :integer
; Month : October :string
; year : 2020 :integer
; utc : -7200 :integer
; ]
print now\year ; 2020
check if given date is in the past
date(:date)
:logical
pastDate: before.weeks:2 now
futureDate: after.weeks:1 now
print past? futureDate ; false
print past? pastDate ; true
print past? now ; true ("now" has already become past...)
check if given date is a Saturday
date(:date)
:logical
print saturday? now ; false
check if given date is a Sunday
date(:date)
:logical
print sunday? now ; false
check if given date is a Thursday
date(:date)
:logical
print thursday? now ; false
check if given date is today
date(:date)
:logical
print today? now ; true
print today? after.hours: 24 now ; false
check if given date is a Tuesday
date(:date)
:logical
print tuesday? now ; true
check if given date is a Wednesday
date(:date)
:logical
print wednesday? now ; false
Exceptions and error handling
an arithmetic error
an assertion error
a conversion error
an index error
a library error
a name error
a package error
a generic runtime error
a syntax error
a system error
throw an error with given message
message(:string,:errorkind)
as(:errorkind): consider the error as one of given subtype
:nothing
err: try -> throw "Page not found"
err\kind
; => Generic Error
err\message
; => Page not found
; or you can alternatively use custom errorKind
pageNotFound: to :errorKind "404: Page not Found"
err: try -> throw.as: pageNotFound "Seems that the page does not exist"
err\kind
; => 404: Page not Found
err\message
; => Seems that the page does not exist
; Or even use the :errorKind's label as the message itself
err: try -> throw pageNotFound
err\kind
; => 404: Page not Found
err\message
; => 404: Page not Found
perform action, and return true if errors were thrown
action(:block,:bytecode)
:logical
throws? [
1 + 2
]
; => false
throws? -> 1/0
; => true
perform action and catch possible errors
action(:block,:bytecode)
verbose(:logical): print all error messages as usual
:null,:error
err: try [
; let's try something dangerous
print 10 / 0
]
type err
; => :error
; Tips: mixing errors and returned values
f: $[][ throw "some error" ]
g: $[][ return "hi" ]
(genericError = err: <= try -> val: f)?
-> print err
-> print val
; => Generic Error: some error
(genericError = err: <= try -> val: g)?
-> print err
-> print val
; => hi
a type error
a UI error
a value error
a VM error
Functions for reading, writing, and manipulating files
copy file at path to given destination
file(:string)destination(:string)
directory(:logical): path is a directory
:nothing
copy "testscript.art" normalize.tilde "~/Desktop/testscript.art"
; copied file
copy "testfolder" normalize.tilde "~/Desktop/testfolder"
; copied whole folder
delete file at given path
file(:string)
directory(:logical): path is a directory
:nothing
delete "testscript.art"
; file deleted
check if given path exists and corresponds to a directory
path(:string)
:logical
if directory? "src" [
print "directory exists!"
]
check if file/directory at given path exists
path(:string)
:logical
if exists? "somefile.txt" [
print "path exists!"
]
check if given path exists and corresponds to a file
path(:string)
:logical
if file? "somefile.txt" [
print "file exists!"
]
hidden?
check if file/folder at given path is hidden
file(:string)
:logical
hidden? "README.md" ; => false
hidden? ".git" ; => true
move file at path to given destination
file(:string)destination(:string)
directory(:logical): path is a directory
:nothing
move "testscript.art" normalize.tilde "~/Desktop/testscript.art"
; moved file
move "testfolder" normalize.tilde "~/Desktop/testfolder"
; moved whole folder
check permissions of given file
file(:string)
set(:dictionary): set using given file permissions
:null,:dictionary
inspect permissions "bin/arturo"
; [ :dictionary
; user : [ :dictionary
; read : true :boolean
; write : true :boolean
; execute : true :boolean
; ]
; group : [ :dictionary
; read : true :boolean
; write : false :boolean
; execute : true :boolean
; ]
; others : [ :dictionary
; read : true :boolean
; write : false :boolean
; execute : true :boolean
; ]
; ]
permissions.set:#[others:#[write:true]] "bin/arturo"
; gave write permission to 'others'
read file from given path
file(:string)
lines(:logical): read file lines into blockjson(:logical): read Json into valuecsv(:logical): read CSV file into a block of rowsdelimiter(:char): read CSV file with a specific delimiterwithHeaders(:logical): read CSV headershtml(:logical): read HTML into node dictionaryxml(:logical): read XML into node dictionarymarkdown(:logical): read Markdown and convert to HTMLtoml(:logical): read TOML into valuebytecode(:logical): read file as Arturo bytecodebinary(:logical): read as binaryfile(:logical): read as file (throws an error if not valid)
:string,:binary,:block
; reading a simple local file
str: read "somefile.txt"
; also works with remote urls
page: read "http://www.somewebsite.com/page.html"
; we can also "read" JSON data as an object
data: read.json "mydata.json"
; or even convert Markdown to HTML on-the-fly
html: read.markdown "## Hello" ; "<h2>Hello</h2>"
rename file at path using given new path name
file(:string)name(:string)
directory(:logical): path is a directory
:nothing
rename "README.md" "READIT.md"
; file renamed
create symbolic link of file to given destination
file(:string)destination(:string)
hard(:logical): create a hard link
:nothing
symlink relative "arturo/README.md"
"/Users/drkameleon/Desktop/gotoREADME.md"
; creates a symbolic link to our readme file
; in our desktop
symlink.hard relative "arturo/README.md"
"/Users/drkameleon/Desktop/gotoREADME.md"
; hard-links (effectively copies) our readme file
; to our desktop
check if given path exists and corresponds to a symlink
path(:string)
:logical
if symlink? "somefile" [
print "symlink exists!"
]
get file timestamps
file(:string)
:null,:dictionary
timestamp "README.md"
; => [created:2022-09-21T12:35:04+02:00 accessed:2022-09-21T12:35:04+02:00 modified:2022-09-21T12:35:04+02:00]
timestamp "some-file-that-does-not-exist.txt"
; => null
unzip given archive to destination
destination(:string)original(:string)
:nothing
unzip "folder" "archive.zip"
get file size for given path
file(:string)
:quantity
volume "README.md"
; => 13704B
; (size in bytes)
write content to file at given path
file(:null,:string)content(:any)
append(:logical): append to given filedirectory(:logical): create directory at pathjson(:logical): write value as Jsoncompact(:logical): produce compact, non-prettified Json codebinary(:logical): write as binary
:nothing
; write some string data to given file path
write "somefile.txt" "Hello world!"
; we can also write any type of data as JSON
write.json "data.json" myData
; append to an existing file
write.append "somefile.txt" "Yes, Hello again!"
zip given files to file at destination
destination(:string)files(:block)
:nothing
zip "dest.zip" ["file1.txt" "img.png"]
Functions and utilities for using the terminal and standard input/output
clear terminal
:nothing
clear ; (clears the screen)
get colored version of given string
color(:color)string(:string)
bold(:logical): bold fontunderline(:logical): show underlinedkeep(:logical): don't reset color at string end
:string
print color #green "Hello!" ; Hello! (in green)
print color #red.bold "Some text" ; Some text (in red/bold)
turn cursor visibility on/off
visible(:logical)
:nothing
cursor false ; (hides the cursor)
cursor true ; (shows the cursor)
move cursor to given coordinates
x(:null,:integer)y(:null,:integer)
:nothing
goto 10 15 ; (move cursor to column 10, line 15)
goto 10 ø ; (move cursor to column 10, same line)
print prompt and get user input. If the prompt is ø, get a single character
prompt(:null,:string)
repl(:logical): get input as if in a REPLhistory(:string): set path for saving historycomplete(:block): use given array for auto-completionshint(:dictionary): use given dictionary for typing hints
:char,:string
name: input "What is your name? "
; (user enters his name: Bob)
print ["Hello" name "!"]
; Hello Bob!
; creating a simple REPL
while [true][
inp: input.repl
.history: "myhistory.txt"
.complete:["Hello there", "Hello world!"]
.hint:#[he: "perhaps you want to say hello?"]
"## "
print ["got:" inp]
]
; will show a REPL-like interface, with arrow navigation enabled
; show previous entries with arrow-up, store entries in
; a recoverable file and also use autocompletions and hints
; based on give reference
character: input ø
; (User types a key, for example "A")
print character
; A
print given value to screen with newline
value(:any)
lines(:logical): print each value in block in a new line
:nothing
print "Hello world!" ; Hello world!
print given value to screen
value(:any)
:nothing
prints "Hello "
prints "world"
print "!"
; Hello world!
get info about terminal
:dictionary
print terminal ; [width:107 height:34]
terminal\width ; => 107
Functional helpers for easier block iteration (loops, filtering, mapping, etc)
sort items in collection using given action, in ascending order
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexdescending(:logical): sort in descending order
:block,:nothing
arrange ["the" "brown" "fox" "jumped" "over" "the" "lazy" "dog"] => size
; => ["the" "fox" "the" "dog" "over" "lazy" "brown" "jumped"]
arrange.descending 1..10 'x -> size factors.prime x
; => [8 4 6 9 10 2 3 5 7 1]
chunk together consecutive items in collection that abide by given predicate
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexvalue(:logical): also include condition values
:block,:nothing
chunk [1 1 2 2 3 22 3 5 5 7 9 2 5] => even?
; => [[1 1] [2 2] [3] [22] [3 5 5 7 9] [2] [5]]
chunk.value [1 1 2 2 3 22 3 5 5 7 9 2 5] 'x [ odd? x ]
; => [[true [1 1]] [false [2 2]] [true [3]] [false [22]] [true [3 5 5 7 9]] [false [2]] [true [5]]]
chunk.with:'i ["one" "two" "three" "four" "five" "six"] [] -> i < 4
; => [["one" "two" "three" "four"] ["five" "six"]]
chunk [1 7 5 4 3 6 8 2] [x y]-> even? x+y
; => [[1 7] [5 4 3 6] [8 2]]
group together items in collection that abide by given predicate
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexvalue(:logical): also include condition values
:block,:nothing
cluster 1..10 => odd?
; => [[1 3 5 7 9] [2 4 6 8 10]]
cluster 1..10 'x -> prime? x
; => [[1 4 6 8 9 10] [2 3 5 7]]
cluster 1..10 [x y] -> 10 < x+y
; => [[1 2 3 4] [5 6 7 8 9 10]]
cluster.value 1..10 'x -> prime? x
; => [[false [1 4 6 8 9 10]] [true [2 3 5 7]]]
#.raw flatten.once cluster.value 1..10 'x [
(prime? x)? -> "prime"
-> "composite"
]
; => [composite:[1 4 6 8 9 10] prime:[2 3 5 7]]
cluster.with: 'i ["one" "two" "three" "four" "five" "six"] [] -> even? i
; => [["one" "three" "five"] ["two" "four" "six"]]
collect items from given collection condition while is true
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexafter(:logical): start collecting after given condition becomes true
:block,:nothing
collect [1 3 5 4 6 7] => odd?
; => [1 3 5]
collect [1 2 3 4 3 2 1 2 3] 'x -> x < 4
; => [1 2 3]
collect.after [4 6 3 5 2 0 1] => odd?
; => [3 5 2 0 1]
collect.after 1..10 'x -> x > 4
; => [5 6 7 8 9 10]
calculate the number of given collection's items that satisfy condition
collection(:integer,:string,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given index
:integer
enumerate 1..10000000 => odd?
; => 5000000
enumerate.with:'i ["one" "two" "three" "four"] 'x -> i < 3
; => 3
check if every item in collection satisfies given condition
collection(:integer,:string,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given index
:logical
if every? [2 4 6 8] 'x [even? x]
-> print "every number is an even integer"
; every number is an even integer
print every? 1..10 'x -> x < 11
; true
print every? 1..10 [x y]-> 20 > x+y
; true
print every? [2 3 5 7 11 14] 'x [prime? x]
; false
print every?.with:'i ["one" "two" "three"] 'x -> 4 > (size x)-i
; true
get collection's items by filtering those that do not fulfil given condition
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexfirst(:logical,:integer): only filter first element/slast(:logical,:integer): only filter last element/s
:block,:nothing,:any
print filter 1..10 [x][
even? x
]
; 1 3 5 7 9
arr: 1..10
filter 'arr 'x -> even? x
print arr
; 1 3 5 7 9
filter [1 1 2 3 5 8 13 21] [x y]-> odd? x+y
; => [1 1 13 21]
filter.with:'i ["zero" "one" "two" "three" "four" "five"] []-> even? i
; => ["one" "three" "five"]
filter.first 1..10 => odd?
=> [2 3 4 5 6 7 8 9 10]
filter.first:3 1..10 => odd?
=> [2 4 6 7 8 9 10]
filter.last 1..10 => odd?
=> [1 2 3 4 5 6 7 8 10]
filter.last:3 1..10 => odd?
=> [1 2 3 4 6 8 10]
left-fold given collection returning accumulator
collection(:integer,:string,:dictionary,:object,:inline,:block,:range)params(:null,:block)action(:block,:bytecode)
with(:literal): use given indexseed(:any): use specific seed valueright(:logical): perform right folding
:null,:block,:nothing
fold 1..10 [x,y]-> x + y
; => 55 (1+2+3+4..)
fold 1..10 .seed:1 [x,y][ x * y ]
; => 3628800 (10!)
fold 1..3 [x,y]-> x - y
; => -6
fold.right 1..3 [x,y]-> x - y
; => 2
fold.seed:"0" to [:string] 1..5 [x,y] ->
"(" ++ x ++ "+" ++ y ++ ")"
; => (((((0+1)+2)+3)+4)+5)
fold.right.seed:"0" to [:string] 1..5 [x,y] ->
"(" ++ x ++ "+" ++ y ++ ")"
; => (1+(2+(3+(4+(5+0)))))
fold 1..10 [x y z] [
print [x y z]
x + z - y
]
; 0 1 2
; 1 3 4
; 2 5 6
; 3 7 8
; 4 9 10
; => 5
fold.with:'i 1..5 [x y][
print [i x y]
i * x+y
]
; 0 0 1
; 1 0 2
; 2 2 3
; 3 10 4
; 4 42 5
; => 188
group items in collection by block result and return as dictionary
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given index
:dictionary,:nothing
print gather [1 2 3 4 5 6] 'x [
x % 2
]
; [1:[1 3 5] 0:[2 4 6]]
print gather ["New York" "Washington" "Minnesota" "Montana" "New Hampshire" "New Mexico"] 'x [
size x
]
; [8:[New York] 10:[Washington New Mexico] 9:[Minnesota] 7:[Montana] 13:[New Hampshire]]
gather.with:'i ["one" "two" "three" "four"] 'x -> i%2
; [0:[one three] 1:[two four]]
loop through collection, using given iterator and block
collection(:integer,:string,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)action(:block,:bytecode)
with(:literal): use given indexforever(:logical): cycle through collection infinitely
:nothing
loop [1 2 3] 'x [
print x
]
; 1
; 2
; 3
loop 1..3 [x][
print ["x =>" x]
]
; x => 1
; x => 2
; x => 3
loop [A a B b C c] [x y][
print [x "=>" y]
]
; A => a
; B => b
; C => c
user: #[
name: "John"
surname: "Doe"
]
loop user [k v][
print [k "=>" v]
]
; name => John
; surname => Doe
loop.with:'i ["zero" "one" "two"] 'x [
print ["item at:" i "=>" x]
]
; 0 => zero
; 1 => one
; 2 => two
loop.forever [1 2 3] => print
; 1 2 3 1 2 3 1 2 3 ...
map collection's items by applying given action
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given index
:block,:nothing
print map 1..5 [x][
2*x
]
; 2 4 6 8 10
arr: 1..5
map 'arr 'x -> 2*x
print arr
; 2 4 6 8 10
map 1..6 [x y][
print ["mapping" x "and" y "->" x+y]
x+y
]
; mapping 1 and 2 -> 3
; mapping 3 and 4 -> 7
; mapping 5 and 6 -> 11
; => [3 7 11]
map.with:'i ["one" "two" "three" "four"] 'x [
(even? i)? -> upper x -> x
]
; => ["ONE" "two" "THREE" "four"]
get maximum item from collection based on given predicate
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexvalue(:logical): also include predicate values
:block,:nothing
maximum 1..10 'x -> size factors.prime x
; => 8
; 8 has the maximum number of
; prime factors: 2, 2, 2 (3)
maximum.value 1..10 'x -> size factors.prime x
; => [8 3]
get minimum item from collection based on given predicate
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexvalue(:logical): also include predicate values
:block,:nothing
minimum [4 17 20] 'x -> size factors.prime x
; => 17
; 17 has the minimum number of
; prime factors: 17 (1)
minimum.value [4 17 20] 'x -> size factors.prime x
; => [17 1]
get collection's items that fulfil given condition
collection(:integer,:string,:literal,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given indexfirst(:logical,:integer): only return first element/slast(:logical,:integer): only return last element/sn(:integer): only return n-th element
:block,:nothing,:any
print select 1..10 [x][
even? x
]
; 2 4 6 8 10
arr: 1..10
select 'arr 'x -> even? x
print arr
; 2 4 6 8 10
select [1 1 2 3 5 8 13 21] [x y]-> odd? x+y
; => [2 3 5 8]
select.with:'i ["zero" "one" "two" "three" "four" "five"] []-> even? i
; => ["zero" "two" "four"]
select.first 1..10 => odd?
=> [1]
select.first:3 1..10 => odd?
=> [1 3 5]
select.last 1..10 => odd?
=> [9]
select.last:3 1..10 => odd?
=> [5 7 9]
check if any of collection's items satisfy given condition
collection(:integer,:string,:dictionary,:object,:inline,:block,:range)params(:null,:literal,:block)condition(:block,:bytecode)
with(:literal): use given index
:logical
if some? [1 3 5 6 7] 'x [even? x]
-> print "at least one number is an even integer"
; at least one number is an even integer
print some? 1..10 'x -> x > 9
; true
print some? [4 6 8 10] 'x [prime? x]
; false
print some? 1..10 [x y]-> 15 < x+y
; true
print some? [2 4 6 9] 'x [prime? x]
; true
print some?.with:'i ["three" "two" "one" "four" "five"] 'x -> i >= size x
; true
Logical operations (AND, OR, XOR, etc), helpers and constants for boolean values
check if all values in given block are true
conditions(:block)
:logical
if all? @[2>1 "DONE"=upper "done" true]
-> print "yes, all are true"
; yes, all are true
print all? @[true false true true]
; false
all? []
; => true
return the logical AND for the given values
valueA(:logical,:block)valueB(:logical,:block)
:logical
x: 2
y: 5
if and? x=2 y>5 [
print "yep, that's correct!"]
]
; yep, that's correct!
check if any of the values in given block is true
conditions(:block)
:logical
if any? @[false 3=4 2>1]
-> print "yes, one (or more) of the values is true"
; yes, one (or more) of the values is true
print any? @[false false false]
; false
the FALSE logical constant
returns true if given value is false; otherwise, it returns false
value(:any)
:logical
print false? 1 = 2 ; true
print false? 1 <> 2 ; false
print false? odd? 2 ; true
print false? [1 2 3] ; false
the MAYBE logical constant
return the logical NAND for the given values
valueA(:logical,:block)valueB(:logical,:block)
:logical
x: 2
y: 3
if? nand? x=2 y=3 [
print "yep, that's correct!"]
]
else [
print "nope, that's not correct"
]
; nope, that's not correct
return the logical NOR for the given values
valueA(:logical,:block)valueB(:logical,:block)
:logical
x: 2
y: 3
if? nor? x>2 y=3 [
print "yep, that's correct!"]
]
else [
print "nope, that's not correct"
]
; nope, that's not correct
return the logical complement of the given value
value(:logical,:block)
:logical
ready: false
if not? ready [
print "we're still not ready!"
]
; we're still not ready!
return the logical OR for the given values
valueA(:logical,:block)valueB(:logical,:block)
:logical
x: 2
y: 4
if or? x=2 y>5 [
print "yep, that's correct!"]
]
; yep, that's correct!
the TRUE logical constant
returns true if given value is true; otherwise, it returns false
value(:any)
:logical
print true? 1 = 2 ; false
print true? 1 <> 2 ; true
print true? even? 2 ; true
print true? [1 2 3] ; false
return the logical XNOR for the given values
valueA(:logical,:block)valueB(:logical,:block)
:logical
x: 2
y: 3
if? xnor? x=2 y=3 [
print "yep, that's correct!"]
]
else [
print "nope, that's not correct"
]
; yep, that's not correct
return the logical XOR for the given values
valueA(:logical,:block)valueB(:logical,:block)
:logical
x: 2
y: 3
if? xor? x=2 y=3 [
print "yep, that's correct!"]
]
else [
print "nope, that's not correct"
]
; nope, that's not correct
Network-related functions and helpers
open given URL with default browser
url(:string)
:nothing
browse "https://arturo-lang.io"
; opens Arturo's official website in a new browser window
download file from url to disk
url(:string)
as(:string): set target file
:nothing
download "https://github.com/arturo-lang/arturo/raw/master/logo.png"
; (downloads file as "logo.png")
download.as:"arturoLogo.png"
"https://github.com/arturo-lang/arturo/raw/master/logo.png"
; (downloads file with a different name)
send mail using given title and message to selected recipient
recipient(:string)title(:string)message(:string)
using(:dictionary): use given configuration
:nothing
mail .using: #[
server: "mymailserver.com"
username: "myusername"
password: "mypass123"
]
"[email protected]" "Hello from Arturo" "Arturo rocks!"
perform HTTP request to url with given data and get response
url(:string)data(:null,:dictionary)
get(:logical): perform a GET request (default)post(:logical): perform a POST requestpatch(:logical): perform a PATCH requestput(:logical): perform a PUT requestdelete(:logical): perform a DELETE requestjson(:logical): send data as Jsonheaders(:dictionary): send custom HTTP headersagent(:string): use given user agenttimeout(:integer): set a timeoutproxy(:string): use given proxy urlcertificate(:string): use SSL certificate at given pathraw(:logical): return raw response without processing
:dictionary
print request "https://httpbin.org/get" #[some:"arg" another: 123]
; [version:1.1 body:{
; "args": {
; "another": "123",
; "some": "arg"
; },
; "headers": {
; "Content-Length": "0",
; "Host": "httpbin.org",
; "User-Agent": "Arturo HTTP Client / 0.9.75",
; "X-Amzn-Trace-Id": "Root=1-608fd4b2-6b8a34291cc2fbd17a678b0f"
; },
; "origin": "92.59.209.80",
; "url": "https://httpbin.org/get?some=arg&another=123"
; } headers:[server:gunicorn/19.9.0 content-length:341 access-control-allow-credentials:true content-type:application/json date:2021-05-03T10:47:14+02:00 access-control-allow-origin:* connection:keep-alive] status:200]
r: request "https://httpbin.org/get" #[some:"arg" another: 123]
body: read.json r\body
inspect body\headers
; [ :dictionary
; Content-Length : 0 :string
; Host : httpbin.org :string
; User-Agent : Arturo HTTP Client / 0.9.75 :string
; X-Amzn-Trace-Id : Root=1-608fd5f3-7e47203117863c111a3aef3b :string
; ]
print (request "https://httpbin.org/get" #[]) \ 'status
; 200
print request.post "https://httpbin.org/post" #[some:"arg" another: 123]
; ...same as above...
start web server using given routes
routes(:block)
port(:integer): use given portverbose(:logical): print info logchrome(:logical): open in Chrome windows as an app
:nothing
serve .port:18966 [
GET "/" [ "This is the homepage" ]
GET "/post" $[id][
send.html ~"This is the post with id: |id|"
]
POST "/getinfo" $[id][
send.json write.json ø #[
i: id
msg: "This is some info"
]
]
]
; run the app and go to localhost:18966 - that was it!
; the app will respond to GET requests to "/" or "/post?id=..."
; and also POST requests to "/getinfo" with an 'id' parameter
Functions and helpers for more advanced math-related operations
get the absolute value for given integer
value(:integer,:floating,:complex,:rational)
:integer,:floating
print abs 6 ; 6
print abs 6-7 ; 1
abs to :complex @[pi 1]
; => 3.296908309475615
calculate the inverse cosine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print acos 0 ; 1.570796326794897
print acos 0.3 ; 1.266103672779499
print acos 1.0 ; 0.0
acos to :complex @[pi 1]
; => 0.3222532939814587-1.86711439316026i
calculate the inverse hyperbolic cosine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print acosh 1.0 ; 0.0
print acosh 2 ; 1.316957896924817
print acosh 5.0 ; 2.292431669561178
acosh to :complex @[pi 1]
; => 1.86711439316026+0.3222532939814587i
calculate the inverse cosecant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print acsec 0 ; nan
print acsec 1.0 ; 1.570796326794897
print acsec 10 ; 0.1001674211615598
acsec to :complex @[pi 1]
; => 0.2918255976444114-0.0959139808172324i
calculate the inverse hyperbolic cosecant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print acsech 0 ; inf
print acsech 1.0 ; 0.0
print acsech 10 ; 0.09983407889920758
acsech to :complex @[pi 1]
; => 0.2862356627279947-0.08847073864038091i
calculate the inverse cotangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print actan 0 ; 1.570796326794897
print actan 1 ; 0.7853981633974483
print actan 10.0 ; 0.09966865249116204
actan to :complex @[pi 1]
; => 0.2834557524705047-0.08505998507745414i
calculate the inverse hyperbolic cotangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print actanh 0 ; nan
print actanh 1 ; inf
print actanh 10.0 ; 0.1003353477310756
actanh to :complex @[pi 1]
; => 0.2946214403408572-0.09996750087543603i
calculate the phase angle of given number
number(:complex)
:floating
a: to complex [1 1] ; a: 1.0+1.0i
print angle a ; 0.7853981633974483
calculate the inverse secant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print asec 0 ; nan
print asec 45 ; 1.548572275176629
print asec 5 ; 1.369438406004566
asec to :complex @[pi 1]
; => 1.278970729150485+0.09591398081723231i
calculate the inverse hyperbolic secant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print asech 0 ; inf
print asech 0.45 ; 1.436685652839686
print asech 1 ; 0.0
asech to :complex @[pi 1]
; => 0.09591398081723221-1.278970729150485i
calculate the inverse sine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print asin 0 ; 0.0
print asin 0.3 ; 0.3046926540153975
print asin 1.0 ; 1.570796326794897
asin to :complex @[pi 1]
; => 1.248543032813438+1.867114393160262i
calculate the inverse hyperbolic sine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print asinh 0 ; 0.0
print asinh 0.3 ; 0.2956730475634224
print asinh 1.0 ; 0.881373587019543
asinh to :complex @[pi 1]
; => 1.904627686970658+0.2955850342116299i
calculate the inverse tangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print atan 0 ; 0.0
print atan 0.3 ; 0.2914567944778671
print atan 1.0 ; 0.7853981633974483
atan to :complex @[pi 1]
; => 1.287340574324392+0.08505998507745416i
calculate the inverse tangent of y / x
y(:integer,:floating,:rational)x(:integer,:floating,:rational)
:floating,:complex
atan2 1 1 ; 0.7853981633974483
atan2 1 1.5 ; 0.9827937232473291
calculate the inverse hyperbolic tangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print atanh 0 ; 0.0
print atanh 0.3 ; 0.3095196042031118
print atanh 1.0 ; inf
atanh to :complex @[pi 1]
; => 0.2946214403408571+1.470828825919461i
calculate the smallest integer not smaller than given value
value(:integer,:floating,:rational)
:integer
print ceil 2.1 ; 3
print ceil 2.9 ; 3
print ceil neg 3.5 ; -3
print ceil 4 ; 4
print ceil to :rational @[neg 7 2] ; -3
force value within given range
number(:integer,:floating,:rational)range(:block,:range)
:integer,:floating,:rational
clamp 2 1..3 ; 2
clamp 0 1..3 ; 1
clamp 4 1..3 ; 3
clamp 4 3..1 ; 3
clamp 5 range.step: 2 0 5 ; 4
clamp 4.5 0..6 ; 4.5
clamp to :rational [1 5] 0..1 ; 1/5
clamp 4.5 [1 2.5] ; 2.5
clamp 2 [5 10] ; 5
clamp 2 [10 5] ; 5
clamp 2.5 @[1 to :rational [5 2]] ; 2.5
calculate the complex conjugate of given number
number(:complex)
:complex
b: to :complex [1 2] ; b: 1.0+2.0i
print conj b ; 1.0-2.0i
calculate the cosine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print cos 0 ; 1.0
print cos 0.3 ; 0.955336489125606
print cos 1.0 ; 0.5403023058681398
cos to :complex [1 1]
; => 0.8337300251311491-0.9888977057628651i
calculate the hyperbolic cosine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print cosh 0 ; 1.0
print cosh 0.3 ; 1.04533851412886
print cosh 1.0 ; 1.543080634815244
cosh to :complex [2 1]
; => 2.032723007019666+3.0518977991518i
calculate the cosecant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print csec 0 ; inf
print csec 0.3 ; 3.383863361824123
print csec 1.0 ; 1.188395105778121
csec to :complex [1 1]
; => 0.6215180171704283-0.3039310016284264i
calculate the hyperbolic cosecant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print csech 0 ; inf
print csech 0.3 ; 3.283853396698424
print csech 1.0 ; 0.8509181282393216
csech to :complex [1 1]
; => 0.3039310016284264-0.6215180171704283i
calculate the cotangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print ctan 0 ; inf
print ctan 0.3 ; 3.232728143765828
print ctan 1.0 ; 0.6420926159343308
ctan to :complex [1 1]
; => 0.2176215618544027-0.8680141428959249i
calculate the hyperbolic cotangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print ctanh 0 ; inf
print ctanh 0.3 ; 3.432738430321741
print ctanh 1.0 ; 1.313035285499331
ctanh to :complex [1 1]
; => 0.8680141428959249-0.2176215618544027i
get the denominator of given number
number(:integer,:floating,:rational)
:integer
num: to :rational 12.4 ; num: 62/5
print denominator num
; => 5
print denominator 10
; => 1
convert a number into an array of digits or an array of digits back into a number
number(:integer,:block)
base(:integer): use given based (default: 10)
:block
digits 123
; => [1 2 3]
digits [1 2 3]
; => 123
digits 0
; => [0]
digits neg 12345
; => [1 2 3 4 5]
; digits 1231231231231231231231231231023
; => [1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 0 2 3]
the constant e, Euler's number
check if given number is even
number(:integer)
:logical
even? 4 ; => true
even? 3 ; => false
print select 1..10 => even? ; 2 4 6 8 10
calculate the exponential function for given value
value(:integer,:floating,:complex,:rational)
:floating,:complex
print exp 1.0 ; 2.718281828459045
print exp 0 ; 1.0
print exp neg 1.0 ; 0.3678794411714423
exp to :complex @[pi 1]
; => 12.50296958887651+19.47222141884161i
calculate the factorial of given value
value(:integer)
:integer
factorial 1 ; => 1
factorial 5 ; => 120
factorial 20 ; => 2432902008176640000
get list of factors for given integer
number(:integer)
prime(:logical): prime factorization
:block
factors 16 ; => [1 2 4 8 16]
factors.prime 48 ; => [2 2 2 2 3]
unique factors.prime 48 ; => [2 3]
factors.prime 18446744073709551615123120
; => [2 2 2 2 3 5 61 141529 26970107 330103811]
calculate the largest integer not greater than given value
value(:integer,:floating,:rational)
:integer
print floor 2.1 ; 2
print floor 2.9 ; 2
print floor neg 3.5 ; -4
print floor 4 ; 4
print floor to :rational @[neg 7 2] ; -4
calculate the gamma function for given value
value(:integer,:floating,:rational)
:floating
print gamma 3.0 ; 2.0
print gamma 10.0 ; 362880.0
print gamma 15 ; 87178291199.99985
calculate greatest common divisor for given collection of integers
numbers(:block)
:integer
print gcd [48 60 120] ; 12
calculate the hypotenuse of a right-angle triangle with given base and height
base(:integer,:floating,:rational)height(:integer,:floating,:rational)
:floating
print hypot 3 4
; 5.0
print hypot 4.0 5.0
; 6.403124237432849
the IEEE floating point value of positive infinity
check whether given value is an infinite one
value(:any)
:logical
infinite? 4 ; false
infinite? infinite ; true
infinite? ∞ ; true
a: infinite
infinite? a ; true
b: 0
infinite? b ; false
calculate least common multiplier for given collection of integers
numbers(:block)
:integer
print lcm [48 60 120] ; 240
calculate the natural logarithm of given value
value(:integer,:floating,:complex,:rational)
:floating,:complex
print ln 1.0 ; 0.0
print ln 0 ; -inf
print ln neg 7.0 ; nan
ln to :complex @[pi 1]
; => 1.19298515341341+0.308169071115985i
calculate the logarithm of value using given base
value(:integer,:floating,:rational)base(:integer,:floating,:rational)
:floating
print log 9 3 ; 2.0
print log 32.0 2.0 ; 5.0
print log 0.0 2 ; -inf
print log 100.0 10.0 ; 2.0
check if given number is negative
number(:integer,:floating,:complex,:rational)
:logical
negative? 5 ; => false
negative? 6-7 ; => true
get the numerator of given number
number(:integer,:floating,:rational)
:integer
num: to :rational 12.4 ; num: 62/5
print numerator num
; => 62
print numerator 10
; => 10
check if given number is odd
number(:integer)
:logical
odd? 4 ; => false
odd? 3 ; => true
print select 1..10 => odd? ; 1 3 5 7 9
the number pi, mathematical constant
check if given number is positive
number(:integer,:floating,:complex,:rational)
:logical
positive? 5 ; => true
positive? 6-7 ; => false
modular exponentation: calculate the result of (base^exponent) % divider
base(:integer)exponent(:integer)divider(:integer)
:null,:integer
powmod 1 10 3 ; => 1
powmod 3 2 6 ; => 3
powmod 5 5 15 ; => 5
powmod 2 3 5 ; => 3
powmod 2 4 5 ; => 1
print (powmod 2 168277 673109) = (2 ^ 168277) % 673109
; true
check if given integer is prime
number(:integer)
:logical
prime? 2 ; => true
prime? 6 ; => false
prime? 11 ; => true
; let's check the 14th Mersenne:
; 53113799281676709868958820655246862732959311772703192319944413
; 82004035598608522427391625022652292856688893294862465010153465
; 79337652707239409519978766587351943831270835393219031728127
prime? (2^607)-1 ; => true
calculate the product of all values in given list
collection(:block,:range)
cartesian(:logical): return the cartesian product of given sublists
:integer,:floating,:rational
print product [3 4] ; 12
print product [1 2 4 6] ; 48
print product [] ; 1
print product 1..10 ; 3628800
product.cartesian [[A B C][D E]]
; => [[A D] [A E] [B D] [B E] [C D] [C E]]
get a random integer between given limits
lowerLimit(:integer,:floating,:rational)upperLimit(:integer,:floating,:rational)
:integer,:floating
rnd: random 0 60 ; rnd: (a random number between 0 and 60)
calculate the reciprocal of given number
value(:integer,:floating,:rational)
:rational
r: to :rational [3 2]
print reciprocal r
; 2/3
reciprocal 3 ; => 1/3
reciprocal 3.2 ; => 5/16
round given value
value(:integer,:floating,:rational)
to(:integer): round to given decimal places
:floating
print round 2.1 ; 2.0
print round 2.9 ; 3.0
print round 6 ; 6.0
print round to :rational [29 10] ; 3.0
print round to :rational [21 10] ; 2.0
print round to :rational [5 2] ; 3.0
print round pi ; 3.0
print round.to:5 pi ; 3.14159
print round.to:2 pi ; 3.14
calculate the secant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print sec 0 ; 1.0
print sec 0.3 ; 1.046751601538086
print sec 1.0 ; 1.850815717680925
sec to :complex [1 1]
; => 0.4983370305551868+0.591083841721045i
calculate the hyperbolic secant of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print sech 0 ; 1.0
print sech 0.3 ; 0.9566279119002483
print sech 1.0 ; 0.6480542736638855
sech to :complex [1 1]
; => 0.4983370305551868-0.5910838417210451i
calculate the sine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print sin 0 ; 0.0
print sin 0.3 ; 0.2955202066613395
print sin 1.0 ; 0.8414709848078965
sin to :complex [1 1]
; => 0.4983370305551868-0.5910838417210451i
calculate the hyperbolic sine of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print sinh 0 ; 0.0
print sinh 0.3 ; 0.3045202934471426
print sinh 1.0 ; 1.175201193643801
sinh to :complex [1 1]
; => 0.6349639147847361+1.298457581415977i
get square root of given value
value(:integer,:floating,:complex,:rational)
integer(:logical): get the integer square root
:floating
print sqrt 4 ; 2.0
print sqrt 16.0 ; 4.0
print sqrt 1.45 ; 1.20415945787923
sqrt to :complex @[pi 1]
; => 1.794226987182141+0.2786715413222365i
calculate the sum of all values in given list
collection(:block,:range)
:integer,:floating,:rational
print sum [3 4] ; 7
print sum [1 2 4 6] ; 13
print sum 1..10 ; 55
calculate the tangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print tan 0 ; 0.0
print tan 0.3 ; 0.3093362496096232
print tan 1.0 ; 1.557407724654902
tan to :complex [1 1]
; => 0.2717525853195119+1.083923327338695i
calculate the hyperbolic tangent of given angle
angle(:integer,:floating,:complex,:rational,:quantity)
:floating,:complex
print tanh 0 ; 0.0
print tanh 0.3 ; 0.2913126124515909
print tanh 1.0 ; 0.7615941559557649
tanh to :complex [1 1]
; => 1.083923327338695+0.2717525853195117i
the number tau, mathematical constant
Functions for path manipulation and information retrieval
check if given path is an absolute path
path(:string)
:logical
absolute? "/usr/bin" ; => true
absolute? "usr/bin" ; => false
extract components from path
path(:string,:color)
directory(:logical): get path directorybasename(:logical): get path basename (filename+extension)filename(:logical): get path filenameextension(:logical): get path extensionscheme(:logical): get scheme field from URLhost(:logical): get host field from URLport(:logical): get port field from URLuser(:logical): get user field from URLpassword(:logical): get password field from URLpath(:logical): get path field from URLquery(:logical): get query field from URLanchor(:logical): get anchor field from URLred(:logical): get red component from colorgreen(:logical): get green component from colorblue(:logical): get blue component from coloralpha(:logical): get alpha component from colorhsl(:logical): get HSL representation from colorhsv(:logical): get HSV representation from colorhue(:logical): get hue component from colorsaturation(:logical): get saturation component from colorluminosity(:logical): get luminosity component from color
:string,:dictionary
path: "/this/is/some/path.txt"
print extract.directory path ; /this/is/some
print extract.basename path ; path.txt
print extract.filename path ; path
print extract.extension path ; .txt
print extract path
; [directory:/this/is/some basename:path.txt filename:path extension:.txt]
url: "http://subdomain.website.com:8080/path/to/file.php?q=something#there"
print extract.scheme url ; http
print extract.host url ; subdomain.website.com
print extract.port url ; 8080
print extract.user url ;
print extract.password url ;
print extract.path url ; /path/to/file.php
print extract.query url ; q=something
print extract.anchor url ; there
print extract url
; [scheme:http host:subdomain.website.com port:8080 user: password: path:/path/to/file.php query:q=something anchor:there]
extract #magenta
; => [red:255 green:0 blue:255]
extract.red #FF126D
; => 255
extract.hsl #magenta
; => [hue:300 saturation:1.0 luminosity:0.5]
extract.hue #magenta
; => 300
get files in given path
path(:string)
recursive(:logical): perform recursive searchrelative(:logical): get relative paths
:block
loop list "." 'file [
print file
]
; tests
; var
; data.txt
get normalized version of given path
path(:string,:literal,:pathliteral)
executable(:logical): treat path as executabletilde(:logical): expand tildes in path
:string,:nothing
normalize "one/../two/../../three"
; => ../three
normalize "~/one/../two/../../three"
; => three
normalize.tilde "~/one/../two/../../three"
; => /Users/three
normalize.tilde "~/Documents"
; => /Users/drkameleon/Documents
normalize.executable "myscript"
; => ./myscript
get relative path for given path, based on current script's location
path(:string)
:string
; we are in folder: /Users/admin/Desktop
print relative "test.txt"
; /Users/admin/Desktop/test.txt
Functions related to Quantities, physical units and constants
the mass of an alpha particle
one ten-billionth of a meter
the mass of an atomic mass unit
the number of atoms in 12 grams of carbon-12
the radius of the first Bohr orbit of the hydrogen atom
the ratio of the universal gas constant to Avogadro's number
the radius of an electron
the conductance of a superconductor
check if given quantities/units are compatible
a(:unit,:quantity)b(:unit,:quantity)
:logical
conforms? 3`m `m ; => true
conforms? 4`m `cm ; => true
4`yd := 5`m ; => true
5`m := `s ; => false
givenValue: 6`yd/s
conforms? givenValue `m ; => false
conforms? givenValue `km/h ; => true
3`m := 4`m ; => true
5`W := 5`N ; => false
5`W := 3`J/s ; => true
convert quantity to given unit
value(:integer,:floating,:rational,:quantity)unit(:string,:word,:literal,:unit)
:quantity
print convert 3`m `cm
; 300.0 cm
print 1`yd2 --> `m2
; 0.836127 m²
the mass of a deuteron
the charge of an electron
the mass of an electron
the energy equivalent of the mass of an electron
the gravitational constant
the energy of the ground state of the hydrogen atom
the mass of a helion
the impedance of free space
convert quantity to given unit
unit(:string,:word,:literal,:unit)value(:integer,:floating,:rational,:quantity)
:quantity
print in`cm 3`m
; 300.0 cm
print in`m2 1`yd2
; 0.836127 m²
the inverse of the conductance of a superconductor
The inverse of the flux quantum
the magnetic flux of a superconductor
the universal gas constant
the mass of a muon
the mass of a neutron
the ratio of the energy of a photon to its frequency
the length of the Planck scale
the mass of the Planck scale
the temperature of the Planck scale
the time of the Planck scale
get the described property of given quantity or unit
quantity(:unit,:quantity)
hash(:logical): get property as a hash
:integer,:literal
property 3`m ; => 'length
property 4`m2 ; => 'area
property 5`m3 ; => 'volume
property 6`J/s ; => 'power
property 3`V ; => 'potential
the mass of a proton
the energy equivalent of the mass of a proton
the ratio of the energy of a photon to its frequency
the Rydberg constant
get quantity value in the appropriate numeric type
value(:quantity)
:integer,:floating,:rational
scalar 3`m ; => 3
scalar 4.0`m2 ; => 4
scalar 10:2`m3 ; => 5
scalar 3.1`m ; => 3.1
scalar 5:2`m ; => 2.5
scalar 13:3`m ; => 13/3
define new user unit
name(:string,:literal)value(:unit,:quantity)
symbol(:string): define main unit symboldescribes(:string): set corresponding property for new unitproperty(:logical): define a new property
:literal
specify 'nauMile 1.1508`mi
print 2`nauMile ; 2 nauMile
print 3`nauMile --> `km ; 5.5560992256 km
specify.symbol:"NM" 'nauMile 1.1508`mi
print 2`nauMile ; 2 NM
specify.describes:"coding speed" 'lph `lines/h
print 100`lph ; 100 lph
print property 100`lph ; coding speed
specify.property "sweetness" `tspSugar
print property 3`tspSugar ; sweetness
the speed of light in a vacuum
the volume of one mole of an ideal gas at standard temperature and pressure
the standard pressure
the standard temperature
the mass of a tau
the cross section of an electron
the mass of a triton
get the units of given quantity
value(:unit,:quantity)
base(:logical): get base units
:unit
units 3`m ; => `m
units `m2 ; => `m2
units 8`J/s ; => `J/s
units 7`W ; => `W
units.base 3`m ; => `m
units.base `m2 ; => `m2
units.base 8`J/s ; => `J/s
units.base 7`W ; => `J/s
specify 'ff 3`items
units 3`ff ; => `items
units.base 3`ff ; => `items
units.base 3`ff.ha ; => `items.m2
specify 'kk 3`m2
units 3`kk ; => `kk
units.base 3`kk ; => `m2
the permeability of free space
the permittivity of free space
the resistance of a superconductor
Functions and helpers for retrieving runtime information about different objects, components, or types
get index of function arities
:dictionary
print arity\print ; 1
get given attribute, if it exists
name(:string,:literal)
:null,:any
multiply: function [x][
if? attr? "with" [
x * attr "with"
]
else [
2*x
]
]
print multiply 5
; 10
print multiply.with: 6 5
; 60
check if given attribute exists
name(:string,:literal)
:logical
greet: function [x][
switch attr? 'later
-> ~"|x| I'm afraid, I'll greet you later"
-> ~"Hello, |x|!"
]
greet "John"
; => Hello, John!
greet.later "John"
; => John I'm afraid, I'll greet you later!
; Have in mind that `attr?` won't pop your attribute's stack
greet "Joe"
; => Joe I'm afraid, I'll greet you later!
get dictionary of set attributes
:dictionary
greet: function [x][
print ["Hello" x "!"]
print attrs
]
greet.later "John"
; Hello John!
; [
; later: true
; ]
benchmark given code
action(:block,:bytecode)
get(:logical): get benchmark time
:floating,:nothing
benchmark [
; some process that takes some time
loop 1..10000 => prime?
]
; [benchmark] time: 0.065s
benchmark.get [
loop 1..10000 => prime?
]
; => 0.3237628936767578
print info for given symbol
symbol(:string,:word,:literal,:pathliteral,:symbolliteral)
get(:logical): get information as dictionary
:dictionary,:nothing
info 'print
; |--------------------------------------------------------------------------------
; | print :function Io
; |--------------------------------------------------------------------------------
; | print given value to screen with newline
; |--------------------------------------------------------------------------------
; | usage print value :any
; |
; | options .lines -> print each value in block in a new line
; |
; | returns :nothing
; |--------------------------------------------------------------------------------
info '++
; |--------------------------------------------------------------------------------
; | append :function 0x107555A10
; | alias ++
; |--------------------------------------------------------------------------------
; | append value to given collection
; |--------------------------------------------------------------------------------
; | usage append collection :char :string :literal :block
; | value :any
; |
; | returns :string :block :nothing
; |--------------------------------------------------------------------------------
print info.get 'print
; [name:print address:0x1028B3410 type::function module:Io args:[value:[:any]] attrs:[] returns:[:nothing] description:print given value to screen with newline example:print "Hello world!" ; Hello world!]
print full dump of given value to screen
value(:any)
muted(:logical): don't use color output
:nothing
inspect 3 ; 3 :integer
a: "some text"
inspect a ; some text :string
get list of methods for given object or module
object(:object,:module)
:block
define :cat [
init: method [nick][
this\nick: join.with: " " @["Mr." capitalize nick]
]
meow: method [][
print [this\nick ":" "'meow!'"]
]
]
snowflake: to :cat ["snowflake"]
methods snowflake
; => [init meow]
get current stack
:dictionary
1 2 3 "done"
print stack
; 1 2 3 done
checks if current script runs from the command-line
:logical
doSomething: function [x][
print ["I'm doing something with" x]
]
if standalone? [
print "It's running from command line and not included."
print "Nothing to do!"
]
get currently defined symbols
:dictionary
a: 2
b: "hello"
print symbols
; [
; a: 2
; b: "hello"
;_]
Common functions and operations for sets (union, intersection, difference, etc)
return the difference of given sets
setA(:literal,:pathliteral,:block)setB(:block)
symmetric(:logical): get the symmetric difference
:block,:nothing
print difference [1 2 3 4] [3 4 5 6]
; 1 2
a: [1 2 3 4]
b: [3 4 5 6]
difference 'a b
; a: [1 2]
print difference.symmetric [1 2 3 4] [3 4 5 6]
; 1 2 5 6
check if given sets are disjoint (they have no common elements)
setA(:block)setB(:block)
:logical
disjoint? [1 2 3 4] [3 4 5 6]
; => false
disjoint? [1 2 3 4] [5 6 7 8]
; => true
check if given sets intersect (they have at least one common element)
setA(:block)setB(:block)
:logical
intersect? @1..10 @8..12
; => true
intersect? ["one" "two" "three"] ["three" "four" "five"]
; => true
intersect? ["one" "two" "three"] ["four" "five" "six"]
; => false
return the intersection of given sets
setA(:literal,:pathliteral,:block)setB(:block)
:block,:nothing
print intersection [1 2 3 4] [3 4 5 6]
; 3 4
a: [1 2 3 4]
b: [3 4 5 6]
intersection 'a b
; a: [3 4]
return the powerset of given set
set(:literal,:pathliteral,:block)
:block,:nothing
powerset [1 2 3]
; [[] [1] [2] [1 3] [3] [1 2] [2 3] [1 2 3]]
check if given set is a subset of second set
setA(:block)setB(:block)
proper(:logical): check if proper subset
:logical
subset? [1 3] [1 2 3 4]
; => true
subset?.proper [1 3] [1 2 3 4]
; => true
subset? [1 3] [3 5 6]
; => false
subset? [1 3] [1 3]
; => true
subset?.proper [1 3] [1 3]
; => false
check if given set is a superset of second set
setA(:block)setB(:block)
proper(:logical): check if proper superset
:logical
superset? [1 2 3 4] [1 3]
; => true
superset?.proper [1 2 3 4] [1 3]
; => true
superset? [3 5 6] [1 3]
; => false
superset? [1 3] [1 3]
; => true
superset?.proper [1 3] [1 3]
; => false
return the union of given sets
setA(:literal,:pathliteral,:block)setB(:block)
:block,:nothing
print union [1 2 3 4] [3 4 5 6]
; 1 2 3 4 5 6
a: [1 2 3 4]
b: [3 4 5 6]
union 'a b
; a: [1 2 3 4 5 6]
High-level socket interface and relevant socket communication methods
accept incoming connection and return corresponding socket
server(:socket)
:socket
server: listen.blocking 18966
print "started server connection..."
client: accept server
print ["accepted incoming connection from:" client]
create new socket connection to given server port
port(:integer)
to(:string): set socket addressudp(:logical): use UDP instead of TCP
:socket
; connect to local server on port 18966
server: connect 18966
; "connect" to a udp server on port 12345
server: connect.udp 12345
; connect to a remote server on port 18966
server: connect.to:"123.456.789.123" 18966
start listening on given port and return new socket
port(:integer)
blocking(:logical): set blocking mode (default: false)udp(:logical): use UDP instead of TCP
:socket
; start a server listening on port 18966
server: listen 18966
receive line of data from selected socket
origin(:socket)
size(:integer): set maximum size of received datatimeout(:integer): set timeout (in milliseconds)
:string
server: listen.blocking 18966
print "started server connection..."
client: accept server
print ["accepted incoming connection from:" client]
keepGoing: true
while [keepGoing][
message: receive client
print ["received message:" message]
if message = "exit" [
unplug client
keepGoing: false
]
]
unplug server
send given message to selected socket
destination(:socket)message(:string)
chunk(:logical): don't send data as a line of data
:nothing
; connect to a local server on port 256
socket: connect.to:"localhost" 256
; send a message to the server
send socket "Hello Socket World"
send given message to selected socket and return true if successful
destination(:socket)message(:string)
:logical
; connect to a local server on port 256
socket: connect.to:"localhost" 256
; send a message to the server
; and check if it was successful
sent?: send? socket "Hello Socket World"
print ["Message was sent successfully:" sent?]
close given socket
socket(:socket)
:nothing
; connect to a local server on port 256
socket: connect.to:"localhost" 256
; send a message to the server
send socket "Hello Socket World"
; disconnect from the server
unplug socket
Functions and helpers for working with statistics and samples
get average from given collection of numbers
collection(:block,:range)
:floating
print average [2 4 5 6 7 2 3]
; 4.142857142857143
get population standard deviation of given collection of numbers
collection(:block)
sample(:logical): calculate the sample standard deviation
:floating
arr: [1 2 3 4]
arr2: [3 120 4 7 87 2 6 34]
print deviation arr ; 1.118033988749895
print deviation arr2 ; 42.70959347734417
deviation.sample arr ; => 1.290994448735806
deviation.sample arr2 ; => 45.65847597731914
get population kurtosis of given collection of numbers
collection(:block)
sample(:logical): calculate the sample kurtosis
:floating
arr: [1 2 3 4]
arr2: [3 120 4 7 87 2 6 34]
print kurtosis arr ; -1.36
print kurtosis arr2 ; -0.3863717894076322
kurtosis.sample arr ; => -1.200000000000001
kurtosis.sample arr2 ; => 0.5886192422439724
get median from given collection of numbers
collection(:block)
:null,:integer,:floating
print median [2 4 5 6 7 2 3]
; 6
print median [1 5 2 3 4 7 9 8]
; 3.5
get population skewness of given collection of numbers
collection(:block)
sample(:logical): calculate the sample skewness
:floating
arr: [1 2 3 4]
arr2: [3 120 4 7 87 2 6 34]
print skewness arr ; 0.0
print skewness arr2 ; 1.127950016816592
skewness.sample arr ; => 0.0
skewness.sample arr2 ; => 1.40680083744453
get population variance of given collection of numbers
collection(:block)
sample(:logical): calculate the sample variance
:floating
arr: [1 2 3 4]
arr2: [3 120 4 7 87 2 6 34]
print variance arr ; 1.25
print variance arr2 ; 1824.109375
variance.sample arr ; => 1.666666666666667
variance.sample arr2 ; => 2084.696428571428
Functions and helpers for manipulating and dealing with strings or character blocks
get dictionary-index charset for given locale
locale(:string,:literal)
lower(:logical): return lowercase characters (default)upper(:logical): return uppercase charactersall(:logical): also return non-dictionary characters
:null,:block
alphabet'es
; => [a b c d e f g h i j k l m n ñ o p q r s t u v w x y z]
alphabet.upper 'es
; => [A B C D E F G H I J K L M N Ñ O P Q R S T U V W X Y Z]
alphabet.all 'es
; => [a b c d e f g h i j k l m n ñ o p q r s t u v w x y z á é í ó ú ü]
alphabet.lower.upper.all 'es
; => [a b c d e f g h i j k l m n ñ o p q r s t u v w x y z á é í ó ú ü A B C D E F G H I J K L M N Ñ O P Q R S T U V W X Y Z Á É Í Ó Ú Ü]
check if given character/string is in ASCII
string(:char,:string)
:logical
ascii? `d` ; true
ascii? `😀` ; false
ascii? "hello world" ; true
ascii? "Hællø wœrld" ; false
ascii? "Γειά!" ; false
convert given string to capitalized
string(:char,:string,:literal,:pathliteral)
:char,:string,:nothing
print capitalize "hello World" ; "Hello World"
str: "hello World"
capitalize 'str ; str: "Hello World"
escape given string
string(:string,:literal,:pathliteral)
json(:logical): for literal use in JSON stringsregex(:logical): for literal use in regular expressionshell(:logical): for use in a shell commandxml(:logical): for use in an XML document
:string,:nothing
str: {a long "string" + with \diffe\rent symbols.}
print escape str
; "a long \"string\" + with \\diffe\\rent symbols."
print escape.json str
; a long \"string\" + with \\diffe\\rent symbols.
print escape.regex str
; a\x20long\x20\x22string\x22\x20\x2B\x20with\x20\x5Cdiffe\x5Crent\x20symbols\x2E
print escape.shell str
; 'a long "string" + with \diffe\rent symbols.'
print escape.xml str
; a long "string" + with \diffe\rent symbols.
indent each line of given text
text(:string,:literal,:pathliteral)
n(:integer): pad by given number of spaces (default: 4)with(:string): use given padding
:string,:nothing
str: "one\ntwo\nthree"
print indent str
; one
; two
; three
print indent .n:10 .with:"#" str
; ##########one
; ##########two
; ##########three
calculate Jaro distance/similarity between given strings
stringA(:string)stringB(:string)
:floating
jaro "one" "one" ; => 1.0
jaro "crate" "trace" ; => 0.7333333333333334
jaro "dwayne" "duane" ; => 0.8222222222222223
jaro "abcdef" "fedcba" ; => 0.3888888888888888
jaro "abcde" "vwxyz" ; => 0.0
join collection of values into string
collection(:literal,:pathliteral,:block)
with(:char,:string): use given separatorpath(:logical): join as path components
:string,:nothing
arr: ["one" "two" "three"]
print join arr
; onetwothree
print join.with:"," arr
; one,two,three
join 'arr
; arr: "onetwothree"
print join ['H' 'e' 'l' 'l' 'o' '!']
; Hello!
print join @["1 + 2 = " 1+2]
; 1 + 2 = 3
join.with:'-' ["Hello" "world"]
; => "Hello-world"
calculate Levenshtein distance/similarity between given strings
stringA(:string)stringB(:string)
align(:logical): return aligned stringswith(:char): use given filler for alignment (default: -)
:integer,:block
print levenshtein "for" "fur" ; 1
print levenshtein "one" "one" ; 0
print join.with:"\n" levenshtein .align "ACTGCACTGAC" "GCATGACTAT"
; AC-TGCACTGAC
; GCATG-ACT-AT
convert given string to lowercase
string(:char,:string,:literal,:pathliteral)
:char,:string,:nothing
print lower "hello World, 你好!" ; "hello world, 你好!"
str: "hello World, 你好!"
lower 'str ; str: "hello world, 你好!"
ch: `A`
lower ch
; => `a`
check if given string is lowercase
string(:char,:string)
:logical
lower? "ñ" ; => true
lower? "X" ; => false
lower? "Hello World" ; => false
lower? "hello" ; => true
get matches within string, using given regular expression
string(:string)regex(:char,:string,:regex)
once(:logical): get just the first matchcount(:logical): just get number of matchescapture(:logical): get capture groups onlynamed(:logical): get named capture groups as a dictionarybounds(:logical): get match bounds onlyin(:range): get matches within given rangefull(:logical): get results as an array of match results
:integer,:dictionary,:block
match "hello" "hello" ; => ["hello"]
match "x: 123, y: 456" {/[0-9]+/} ; => ["123" "456"]
match "this is a string" {/[0-9]+/} ; => []
match.once "x: 123, y: 456" {/[0-9]+/} ; => ["123"]
match.count "some words" {/\w+/} ; => 2
match.capture "abc" {/(.)/} ; => ["a" "b" "c"]
match.capture "x: 123, y: 456 - z: 789, w: 012"
{/\w: (\d+), \w: (\d+)/}
; => [["123" "456"] ["789" "012"]]
inspect match.capture.named "x: 123, y: 456 - z: 789, w: 012"
{/\w: (?<numA>\d+), \w: (?<numB>\d+)/}
;[ :block
; [ :dictionary
; numA : 123 :string
; numB : 456 :string
; ]
; [ :dictionary
; numA : 789 :string
; numB : 012 :string
; ]
;]
match.bounds "hELlo wORLd" {/[A-Z]+/}
; => [1..2 7..9]
match.in:0..2 "hello" {/l/} ; => ["l"]
check if string matches given regular expression
string(:string)regex(:string,:regex)
in(:range): get matches within given range
:logical
match? "hello" {/l/} ; => true
match? "hello" {/x/} ; => false
match? "hello" "l" ; => true
match?.in:0..1 "hello" {/l/} ; => false
match?.in:2..4 "hello" {/l/} ; => true
check if given string is numeric
string(:char,:string)
:logical
numeric? "hello" ; => false
numeric? "3.14" ; => true
numeric? "18966" ; => true
numeric? "123xxy" ; => false
outdent each line of given text, by using minimum shared indentation
text(:string,:literal,:pathliteral)
n(:integer): unpad by given number of spaceswith(:string): use given padding
:string,:nothing
print outdent {:
one
two
three
:}
; one
; two
; three
print outdent.n:1 {:
one
two
three
:}
; one
; two
; three
align string by adding given padding
string(:string,:literal,:pathliteral)padding(:integer)
center(:logical): add padding to both sidesright(:logical): add right paddingwith(:char): pad with given character
:string
pad "good" 10 ; => " good"
pad.right "good" 10 ; => "good "
pad.center "good" 10 ; => " good "
a: "hello"
pad 'a 10 ; a: " hello"
pad.with:`0` to :string 123 5
; => 00123
check if string starts with given prefix
string(:string)prefix(:string,:regex)
:logical
prefix? "hello" "he" ; => true
prefix? "boom" "he" ; => false
render template with |string| interpolation
template(:string,:literal,:pathliteral)
once(:logical): don't render recursivelytemplate(:logical): render as a template
:string,:nothing
x: 2
greeting: "hello"
print ~"|greeting|, your number is |x|" ; hello, your number is 2
replace every matched substring/s by given replacement string and return result
string(:string,:literal,:pathliteral)match(:string,:regex,:block)replacement(:string,:block)
:string,:nothing
replace "hello" "l" "x" ; => "hexxo"
str: "hello"
replace 'str "l" "x" ; str: "hexxo"
replace "hello" ["h" "l"] "x" ; => "xexxo"
replace "hello" ["h" "o"] ["x" "z"] ; => "xellz"
strip whitespace from given string
string(:string,:literal,:pathliteral)
start(:logical): strip leading whitespaceend(:logical): strip trailing whitespace
:string,:nothing
str: " Hello World "
print ["strip all:" ">" strip str "<"]
print ["strip leading:" ">" strip.start str "<"]
print ["strip trailing:" ">" strip.end str "<"]
; strip all: > Hello World <
; strip leading: > Hello World <
; strip trailing: > Hello World <
check if string ends with given suffix
string(:string)suffix(:string,:regex)
:logical
suffix? "hello" "lo" ; => true
suffix? "boom" "lo" ; => false
takes a dictionary of translations and replaces each instance sequentially
string(:string,:literal,:pathliteral)translations(:dictionary)
:string,:nothing
print translate "the brown fox jumped over the lazy dog" #[
brown: "green"
fox: "wolf"
jumped:"flew"
dog:"cat"
]
; the green wolf flew over the lazy cat
truncate string at given length
string(:string,:literal,:pathliteral)cutoff(:integer)
with(:string): use given fillerpreserve(:logical): preserve word boundaries
:string,:nothing
str: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse erat quam"
truncate str 30
; => "Lorem ipsum dolor sit amet, con..."
truncate.preserve str 30
; => "Lorem ipsum dolor sit amet,..."
truncate.with:"---" str 30
; => "Lorem ipsum dolor sit amet, con---"
truncate.preserve.with:"---" str 30
; => "Lorem ipsum dolor sit amet,---"
convert given string to uppercase
string(:char,:string,:literal,:pathliteral)
:char,:string,:nothing
print upper "hello World, 你好!" ; "HELLO WORLD, 你好!"
str: "hello World, 你好!"
upper 'str ; str: "HELLO WORLD, 你好!"
ch: `a`
upper ch
; => `A`
check if given string is uppercase
string(:char,:string)
:logical
upper? "Ñ" ; => true
upper? "x" ; => false
upper? "Hello World" ; => false
upper? "HELLO" ; => true
check if given string consists only of whitespace
string(:string)
:logical
whitespace? "hello" ; => false
whitespace? " " ; => true
whitespace? "\n \n" ; => true
word wrap a given string
string(:string,:literal,:pathliteral)
at(:integer): use given max line width (default: 80)
:string
print wordwrap {Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget mauris non justo mattis dignissim. Cras in lobortis felis, id ultricies ligula. Curabitur egestas tortor sed purus vestibulum auctor. Cras dui metus, euismod sit amet suscipit et, cursus ullamcorper felis. Integer elementum condimentum neque, et sagittis arcu rhoncus sed. In luctus congue eros, viverra dapibus mi rhoncus non. Pellentesque nisl diam, auctor quis sapien nec, suscipit aliquam velit. Nam ac nisi justo.}
; Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget mauris non
; justo mattis dignissim. Cras in lobortis felis, id ultricies ligula. Curabitur
; egestas tortor sed purus vestibulum auctor. Cras dui metus, euismod sit amet
; suscipit et, cursus ullamcorper felis. Integer elementum condimentum neque, et
; sagittis arcu rhoncus sed. In luctus congue eros, viverra dapibus mi rhoncus
; non. Pellentesque nisl diam, auctor quis sapien nec, suscipit aliquam velit. Nam
; ac nisi justo.
print wordwrap.at: 10 "one two three four five six seven eight nine ten"
; one two
; three four
; five six
; seven
; eight nine
; ten
Functions and helpers for interacting with the operation system and shell
get command-line arguments as a list
:block
; called with no parameters
arg ; => []
; called with: 1 two 3
arg ; => ["1" "two" "3"]
get all command-line arguments parsed as a dictionary
:dictionary
; called with: 1 two 3
args
; => #[
; 1
; "two"
; 3
; ]
; called with switches: -c -b
args
; => #[
; c : true
; b : true
; values: []
; ]
; called with switches: -c -b and values: 1 two 3
args
; => #[
; c : true
; b : true
; values: [1 "two" 3]
; ]
; called with named parameters: -c:2 --name:newname myfile.txt
args
; => #[
; c : 2
; name : "newname"
; values: ["myfile.txt"]
; ]
get local or global configuration
:store
; `config` searches for `config.art` into your current directory.
; if not found, it returns from `~/.arturo/stores/config.art`
config
; => []
; `config.art` is empty at first, but we can change this manually
write.append path\home ++ normalize ".arturo/stores/config.art"
"language: {Arturo}"
config
; => []
; this stills empty, but now try to relaunch Arturo:
exit
config
; => [language:Arturo]
get environment variables
:dictionary
print env\SHELL
; /bin/zsh
print env\HOME
; /Users/drkameleon
print env\PATH
; /Users/drkameleon/.arturo/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
execute given shell command
command(:string)
args(:block): use given command argumentsasync(:logical): execute asynchronously as a process and return idcode(:logical): return process exit codedirectly(:logical): execute command directly, as a shell command
:string,:dictionary
print execute "pwd"
; /Users/admin/Desktop
split.lines execute "ls"
; => ["tests" "var" "data.txt"]
exit program
:nothing
exit ; (terminates the program)
exit program with error message
message(:string)
code(:integer): return given exit codeunstyled(:logical): don't use default error template
:logical
panic.code:1 "something went terribly wrong. quitting..."
; quits with the given code and
; prints a properly format error with the given message
panic.unstyled "oops! that was wrong"
; quits with the default exit code (= 0) and
; just outputs a simple - unformatted - message
get path information
:dictionary
path
; => [current:C:\Users\me\my-proj home:C:\Users\me\ temp:C:\Users\me\AppData\Local\Temp\
pause program's execution~for the given amount of time
time(:integer,:quantity)
:nothing
print "wait a moment"
pause 1000 ; sleeping for 1000ms = one second
print "done. let's continue..."
print "waiting for 2 seconds
pause 2:s ; let's sleep for a while
print "done!"
get information on current process/program
:dictionary
print process\id
; 78046
inspect process
; [ :dictionary
; id : 78046 :integer
; memory : [ :dictionary
; occupied : 1783104 :integer
; free : 360448 :integer
; total : 2379776 :integer
; max : 2379776 :integer
; ]
; ]
get embedded information about the current script
:dictionary
;; author: {Me :P}
;; year: 2023
;; license: Some License
;;
;; description: {
;; This is an example of documentation.
;;
;; You can get this by using ``script``.
;; }
;;
;; hint: {
;; Notice that we use `;;` for documentation,
;; while the single `;` is just a comment,
;; that will be ignored.
;; }
;;
;; export: [
;; 'myFun
;; 'otherFun
;; 'someConst
;; ]
;;
inspect script
; [ :dictionary
; author : Me :P :string
; year : 2023 :integer
; license : [ :block
; Some :string
; License :string
; ]
; description : This is an example of documentation.
;
; You can get this by using ``script``. :string
; hint : Notice that we use `;;` for documentation,
; while the single `;` is just a comment,
; that will be ignored. :string
; export : [ :block
; myFun :string
; otherFun :string
; someConst :string
; ]
; ]
check if current user has administrator/root privileges
:logical
; when running as root
superuser? ; => true
; when running as regular user
superuser? ; => false
get current system information
:dictionary
inspect sys
;[ :dictionary
; author : Yanis Zafirópulos :string
; copyright : (c) 2019-2024 :string
; version : 0.9.84-alpha+3126 :version
; built : [ :date
; hour : 16 :integer
; minute : 19 :integer
; second : 25 :integer
; nanosecond : 0 :integer
; day : 12 :integer
; Day : Wednesday :string
; days : 163 :integer
; month : 6 :integer
; Month : June :string
; year : 2024 :integer
; utc : -7200 :integer
; ]
; deps : [ :dictionary
; gmp : 6.3.0 :version
; mpfr : 4.2.1 :version
; sqlite : 3.39.5 :version
; pcre : 8.45.0 :version
; ]
; binary : /Users/drkameleon/.arturo/bin/arturo :string
; cpu : [ :dictionary
; arch : amd64 :literal
; endian : little :literal
; cores : 8 :integer
; ]
; os : macos :string
; hostname : drkameleons-MBP.home :string
; release : full :literal
;]
kill process with given id
id(:integer)
code(:integer): use given error code
:nothing
; start process
pid: execute.async "someProcessThatDoesSomethingInTheBackground"
; wait for 5 seconds
pause 5000
; terminate background process
terminate pid
Built-in types, custom user types/objects and related helpers
checks if given value is of type :attribute
value(:any)
:logical
attribute? first [.something x]
; => true
checks if given value is of type :attributeLabel
value(:any)
:logical
attributeLabel? first [.something: x]
; => true
checks if given value is of type :binary
value(:any)
:logical
binary? to :binary "string"
; => true
checks if given value is of type :block
value(:any)
:logical
print block? [1 2 3] ; true
print block? #[name: "John"] ; false
print block? "hello" ; false
print block? 123 ; false
checks if given value is of type :bytecode
value(:any)
:logical
code: [print 1 + 2]
bcode: to :bytecode code
print bytecode? bcode ; true
print bytecode? code ; false
checks if given value is of type :char
value(:any)
:logical
print char? `a` ; true
print char? 123 ; false
checks if given value is of type :color
value(:any)
:logical
print color? #FF0000 ; true
print color? #green ; true
print color? 123 ; false
checks if given value is of type :complex
value(:any)
:logical
c: to :complex [1 2]
print complex? c ; true
print complex? 123 ; false
create a type constructor method automatically using given arguments
arguments(:literal,:block)
:method
define :cat [
init: constructor [nick :string]
meow: method [][
print [this\nick ":" "'meow!'"
]
]
snowflake: to :cat [15]
; Assertion | [is? :string nick]
; error |
snowflake: to :cat ["Snowflake"]
snowflake\meow
; Snowflake: 'meow!'
checks if given value is of type :database
value(:any)
:logical
database? open "my.db"
; => true
checks if given value is of type :date
value(:any)
:logical
print date? now ; true
print date? "hello" ; false
define new type with given prototype
type(:type)prototype(:type,:dictionary,:block)
:nothing
; define a simple type
define :person [name surname]
; and create an object
someone: to :person ["John" "Doe"]
print someone ; [name:John surname:Doe]
; define a simple type
define :person [name surname]
; and create an object
; using a dictionary with field values
someone: to :person #[surname: "Doe", name: "John"]
print someone ; [name:John surname:Doe]
; define a new type
; with custom constructor
define :person [
init: method [name, surname, age][
this\name: name
this\surname: surname
this\dob: now\year - age
]
]
; create an object
jd: to :person ["John" "Doe" 38]
print jd ; [name:John surname:Doe dob:1986]
; define type with overloaded
; magic methods
define :natural [
init: constructor [value]
; custom `+` overload
add: method [x :integer :natural][
(integer? x)? -> this\value + x
-> to :natural @[this\value + x\value]
]
; custom `to :string` overload
string: method [][
to :string this\value
]
]
; create two new 'natural' numbers
n1: to :natural @[3]
n2: to :natural @[5]
print n1 + n2 ; 8
checks if given type is defined
type(:type,:string,:word,:literal)
:logical
defined? :cat
; => false
defined? "cat"
; => false
defined? 'cat
; => false
define :cat [
init: constructor [name :string age :integer]
]
defined? :cat
; => true
defined? "cat"
; => true
defined? 'cat
; => true
defined? :dog
; => false
checks if given value is of type :dictionary
value(:any)
:logical
print dictionary? #[name: "John"] ; true
print dictionary? 123 ; false
checks if given value is of type :error
value(:any)
:logical
error? try -> throw "Some Error"
; => true
checks if given value is of type :errorKind
value(:any)
:logical
errorKind? to :errorKind "Some error kind"
; => true
errorKind? genericError
; => true
checks if given value is of type :floating
value(:any)
:logical
print floating? 3.14 ; true
print floating? 123 ; false
print floating? "hello" ; false
checks if given value is of type :function
value(:any)
builtin(:logical): check if, internally, it's a built-in
:logical
print function? $[x][2*x] ; true
print function? var 'print ; true
print function? "print" ; false
print function? 123 ; false
f: function [x][x+2]
function? var'f ; => true
function? var'print ; => true
function?.builtin var'f ; => false
function?.builtin var'print ; => true
checks if given value is of type :inline
value(:any)
:logical
inline? first [(something) x]
; => true
checks if given value is of type :integer
value(:any)
big(:logical): check if, internally, it's a bignum
:logical
print integer? 123 ; true
print integer? "hello" ; false
integer?.big 123 ; => false
integer?.big 12345678901234567890 ; => true
get derivative type with given prototype
type(:type)prototype(:dictionary,:block)
:type
define :animal [
init: constructor [nick :string age :integer]
speak: method [][
print "..."
]
]
define :fish is :animal []
define :cat is :animal [
speak: method [][
print [~"|this\nick|:" "'meow!'"]
]
]
a: to :cat []
; >> Runtime | cannot initialize object of type :cat
; error | wrong number of parameters: 0
; | expected: 2 (nick, age)
scooby: to :animal ["Scooby" 7]
scooby\speak
; ...
bubble: to :fish ["Bubble" 1]
bubble\speak
; ...
snowflake: to :cat ["Snowflake" 3]
snowflake\speak
; Snowflake: 'meow!'
check whether value is of given type
type(:type,:block)value(:any)
:logical
is? :string "hello" ; => true
is? :block [1 2 3] ; => true
is? :integer "boom" ; => false
is? [:string] ["one" "two"] ; => true
is? [:integer] [1 "two] ; => false
checks if given value is of type :label
value(:any)
:logical
label? first [something: x]
; => true
checks if given value is of type :literal
value(:any)
:logical
print literal? 'x ; true
print literal? "x" ; false
print literal? 123 ; false
checks if given value is of type :logical
value(:any)
:logical
print logical? true ; true
print logical? false ; true
print logical? maybe ; true
print logical? 1=1 ; true
print logical? 123 ; false
checks if given value is of type :method
value(:any)
:logical
greet: method [name :string][print ~"How are you, |name|?"]
reply: function [name :string][print ~"Hi, I'm fine |name|!"]
method? greet
; => true
method? reply
; => false
checks if given value is of type :null
value(:any)
:logical
print null? null ; true
print null? ø ; true
print null? 123 ; false
checks if given value is a custom-type object
value(:any)
:logical
define :person [name,surname][]
x: to :person ["John","Doe"]
print object? x ; true
print object? "hello" ; false
checks if given value is of type :path
value(:any)
:logical
path? first [a\b\c x]
; => true
checks if given value is of type :pathLabel
value(:any)
:logical
pathLabel? first [a\b\c: x]
; => true
checks if given value is of type :pathLiteral
value(:any)
:logical
pathLiteral? 'a\b\c
; => true
checks if given value is of type :quantity
value(:any)
big(:logical): check if, internally, it's a bignum
:logical
print quantity? 1:m ; true
print quantity? 2:yd2 ; true
print quantity? 3 ; false
checks if given value is of type :range
value(:any)
:logical
r: 1..3 ; r: [1 2 3]
print range? r ; true
print range? [1 2 3] ; false
checks if given value is of type :rational
value(:any)
big(:logical): check if, internally, it's a bignum
:logical
r: to :rational 3.14 ; r: 157/50
print rational? r ; true
print rational? 3.14 ; false
checks if given value is of type :regex
value(:any)
:logical
print regex? {/[a-z]+/} ; true
print regex? "[a-z]+" ; false
print regex? 123 ; false
checks if given value is of type :socket
value(:any)
:logical
server: listen 18966
socket? server
; => true
create a sort descriptor method automatically using given type field
field(:literal)
:method
define :cat [
init: constructor [name :string age :integer]
compare: sortable 'age
]
snowflake: to :cat ["Snowflake" 3]
smith: to :cat ["Smith" 6]
compare snowflake smith
; => -1
snowflake < smith
; => true
checks if given value is of type :store
value(:any)
:logical
store? config
; => true
checks if given value is of type :string
value(:any)
:logical
print string? "x" ; true
print string? 'x ; false
print string? 123 ; false
checks if given value is of type :symbol
value(:any)
:logical
symbol? first [+ x]
; => true
checks if given value is of type :symbolLiteral
value(:any)
:logical
symbolLiteral? '++
; => true
convert value to given type
type(:type,:block)value(:any)
format(:string): use given format (for dates or floating-point numbers)unit(:string,:literal): use given unit (for quantities)intrepid(:logical): convert to bytecode without error-line trackinghsl(:logical): convert HSL block to colorhsv(:logical): convert HSV block to color
:any
to :integer "2020" ; 2020
to :integer `A` ; 65
to :char 65 ; `A`
to :integer 4.3 ; 4
to :floating 4 ; 4.0
to :complex [1 2] ; 1.0+2.0i
; make sure you're using the `array` (`@`) converter here, since `neg` must be evaluated first
to :complex @[2.3 neg 4.5] ; 2.3-4.5i
to :rational [1 2] ; 1/2
to :rational @[neg 3 5] ; -3/5
to :boolean 0 ; false
to :boolean 1 ; true
to :boolean "true" ; true
to :literal "symbol" ; 'symbol
to :string 2020 ; "2020"
to :string 'symbol ; "symbol"
to :string :word ; "word"
to :string .format:"dd/MM/yy" now
; 22/03/21
to :string .format:".2f" 123.12345
; 123.12
to :block "one two three" ; [one two three]
do to :block "print 123" ; 123
to :date 0 ; => 1970-01-01T01:00:00+01:00
print now ; 2021-05-22T07:39:10+02:00
to :integer now ; => 1621661950
to :date .format:"dd/MM/yyyy" "22/03/2021"
; 2021-03-22T00:00:00+01:00
to [:string] [1 2 3 4]
; ["1" "2" "3" "4"]
to [:char] "hello"
; [`h` `e` `l` `l` `o`]
define :person [name surname age][]
to :person ["John" "Doe" 35]
; [name:John surname:Doe age:35]
to :color [255 0 10]
; => #FF000A
to :color .hsl [255 0.2 0.4]
; => #5C527A
get type of given value
value(:any)
:type
print type 18966 ; :integer
print type "hello world" ; :string
checks if given value is of type :type
value(:any)
:logical
print type? :string ; true
print type? "string" ; false
print type? 123 ; false
checks if given value is of type :unit
value(:any)
:logical
unit? `m
; => true
checks if given value is of type :version
value(:any)
:logical
print version? 1.0.2 ; true
print version? "1.0.2" ; false
checks if given value is of type :word
value(:any)
:logical
word? first [something x]
; => true
UI- and webview-related helpers
show notification with given title and message
title(:string)message(:string)
info(:logical): show informational notificationwarning(:logical): show notification as a warningerror(:logical): show notification as an error
:nothing
alert "Hello!" "This is a notification..."
; show an OS notification without any styling
alert.error "Ooops!" "Something went wrong!"
; show an OS notification with an error message
set clipboard content to given text
content(:string)
:nothing
clip "this is something to be pasted into the clipboard"
show a file selection dialog and return selection
title(:string)
folder(:logical): select folders instead of filespath(:string): use given starting path
:string
selectedFile: dialog "Select a file to open"
; gets full path for selected file, after dialog closes
selectedFolder: dialog.folder "Select a folder"
; same as above, only for folder selection
show popup dialog with given title and message and return result
title(:string)message(:string)
info(:logical): show informational popupwarning(:logical): show popup as a warningerror(:logical): show popup as an errorquestion(:logical): show popup as a questionok(:logical): show an OK dialog (default)okCancel(:logical): show an OK/Cancel dialogyesNo(:logical): show a Yes/No dialogyesNoCancel(:logical): show a Yes/No/Cancel dialogretryCancel(:logical): show a Retry/Cancel dialogretryAbortIgnore(:logical): show an Abort/Retry/Ignore dialogliteral(:logical): return the literal value of the pressed button
:logical,:literal
popup "Hello!" "This is a popup message"
; shows a message dialog with an OK button
; when the dialog is closed, it returns: true
if popup.yesNo "Hmm..." "Are you sure you want to continue?" [
; a Yes/No dialog will appear - if the user clicks YES,
; then the function will return true; thus we can do what
; we want here
]
popup.okCancel.literal "Hello" "Click on a button"
; => 'ok (if user clicked OK)
; => 'cancel (if user clicked Cancel)
get clipboard content
:string
; paste something into the clipboard (optionally)
clip "this is something to be pasted into the clipboard"
; now, let's fetch whatever there is in the clipboard
unclip
; => "this is something to be pasted into the clipboard"
show webview window with given url or html source
content(:string,:literal)
title(:string): set window titlewidth(:integer): set window widthheight(:integer): set window heightfixed(:logical): window shouldn't be resizablemaximized(:logical): start in maximized modefullscreen(:logical): start in fullscreen modeborderless(:logical): show as borderless windowtopmost(:logical): set window as always-on-topdebug(:logical): add inspector consoleon(:dictionary): execute code on specific eventsinject(:string): inject JS code on webview initialization
:nothing
webview "Hello world!"
; (opens a webview windows with "Hello world!")
webview .width: 200
.height: 300
.title: "My webview app"
---
<h1>This is my webpage</h1>
<p>
This is some content
</p>
---
; (opens a webview with given attributes)