Core
basic functions and constants that make up the very core of the language
Functions
Predicates
Constants
The Core module provides the fundamental building blocks of Arturo: functions for control flow, variable assignment, function definition, and basic program structure.
Key Concepts
- Variable and symbol management
- Control flow and conditionals
- Function and method definitions
- Scope management and modules
- Basic constants (null, any)
Basic Usage
Variable Assignment
; basic assignment
x: 5 ; assign value to symbol
let 'x 5 ; alternative syntax
; multiple assignments
[a,b,c]: 1,2,3 ; assign multiple values
Conditionals
Arturo provides several ways to handle conditional execution:
; simple if statement
if x=2 [
print "x is two"
]
; using the arrow operator for single statements
if x=2 ->
print "x is two"
; switch statement (if-else)
switch x=2 -> print "x is two"
-> print "x is not two"
; the exact same thing, using `?`
(x=2)? -> print "x is two"
-> print "x is not two"
; case statement (value matching)
case value [
1 -> print "one"
2 -> print "two"
"abc" -> print "found abc"
any -> print "something else" ; default case
]
; multiple conditions
when [
x=1 -> print "x is one"
x>10 -> print "x is greater than ten"
x<0 -> print "x is negative"
true -> print "none of the above" ; default case
]
; check all conditions without breaking
when.any [
prime? x -> print "x is prime"
even? x -> print "x is even"
x>100 -> print "x is large"
]
; with shared condition target
when.has: x [
[>10] -> print "greater than 10"
[<0] -> print "negative"
[=5] -> print "equals 5"
]
Note
The->
arrow operator can be used to wrap a single expression in a block, making code more readable.
Function Definition
; basic function
sum: function [a b][
a + b
]
; with the $ shorthand
multiply: $[x y][
x * y
]
; using arrow syntax for simple functions
double: $[x]-> x * 2
Modules
Modules are a way of isolating pieces of code so that we can expose only the functions we need, while still safeguarding parts of the internal implementation.
; create new module
math: module [
pwr: function [x][
x * x
]
calculate: method.public [z][
\pwr z
]
]
export math!
; `calculate` has been exported
print calculate 10 ; 100
; but `pwr` wouldn't be available here...
Common Patterns
Safe Operations
; coalesce operator
value: null
result: value ?? "default" ; use default if null
; safe variable checking
if set? 'x [ ; check if x exists
print x
]
counter: 0
; variable modification
'counter + 1 ; in-place increment
Loop Control
; break from loop
done?: false
while [true][
if done? -> break
doSomething
]
; continue to next iteration
loop 1..10 'x [
if odd? x -> continue
print x
]