The Core Module

The core-module is a very special module containing built-in procedures. Everything from this module is imported by default, as if you did use core::* at the top of every file.

addr_eq

Either takes two objects or two arrays and returns true if modifying the object or array behind one of the references will also modify the object or array behind the other. In other words it doesn't check if the referenced objects or arrays are equal, but rather if they refer to the exact same object or array.

mod example

use std::io::println

proc main() {
val a = { value = 5 }
val b = { value = 10 }
val c = { value = 5 }
val d = a
println(a == b) // prints 'false'
println(a == c) // prints 'true'
println(a == d) // prints 'true'
println(addr_eq(a, b)) // prints 'false'
println(addr_eq(a, c)) // prints 'false'
println(addr_eq(a, d)) // prints 'true'
}

as_flt

Returns the given integer or float as a float.

mod example

proc any_sqrt(x) = x
|> as_flt()
|> std::math::sqrt()

proc main() {
std::io::println(any_sqrt(25)) // prints '5.0'
std::io::println(any_sqrt(16)) // prints '4.0'
std::io::println(any_sqrt(4.0)) // prints '2.0'
}

as_int

Returns the given integer or float as an integer. Floats are truncated (rounded towards zero).

mod example

proc int_div(a, b) = as_flt(as_int(a) / as_int(b))

proc main() {
std::io::println(int_div(4.1, 2.5)) // prints '2.0'
std::io::println(int_div(9.4, 3.2)) // prints '3.0'
std::io::println(int_div(4.2, 4.6)) // prints '1.0'
}

as_str

Returns the given value as a string.

mod example

proc greet(thing) = "Hello, _!"
|> std::str::fmt([ as_str(thing) ]) // replaces '_'s with contents of array
|> std::io::println()

proc main() {
greet("world") // prints 'Hello, world!'
greet({ name = "Cookie" }) // prints 'Hello, <object>!'
greet(std::math::PI) // prints 'Hello, 3.141593!'
}

concat

Returns a new string by appending the second string to the end of the first.

mod example

proc main() {
val a = "Hello, "
val b = "world!"
val c = "Gera!"
std::io::println(concat(a, b)) // prints 'Hello, world!'
std::io::println(concat(a, c)) // prints 'Hello, Gera!'
}

exhaust

Gets and discards the next elements from the given iterator until the iterator reaches the end. This is the backbone of the entire iterator API.

mod example

use std::iter::map
use std::io::println

// this is exactly how 'std::iter::for_each' works
proc for_each(iter, f) = iter
// create a new iterator with the values of the old mapped using a function
|> map(f)
// doesn't matter what type of value 'f' returns, even if it's unit,
// since it's discarded by 'exhaust'
|> exhaust()

proc main() {
0..10 |> for_each(println)
}

hash

Returns a 64-bit hash of the given value. Note that hash objects, arrays and closures are hashed by their references, not the values they contain.

mod example

use std::io::println

proc main() {
val a = { name = "Cookie", age = 5 }
println(hash(a)) // prints a different number each time
println(hash(a.name)) // prints the same value each time
println(hash(a.age)) // prints the same value each time
}

length

Returns the length of the given string (in code points) or array (in elements) as an integer.

mod example

use std::io::println

proc main() {
println(length("Hello")) // prints '5'
println(length([2, 3, 5, 7])) // prints '4'
println(length("This string is 28 chars long")) // prints '28'
}

panic

Logs the given string reason along with other information useful for debugging and makes the program exit immediately.

This mechanism is supposed to be used in situations where the programmer made a mistake, not an actual valid state of the program. That's also why you're not able to handle a panic in any way. Use a result if you want the error to be handled.

Since the procedure never actually returns, it returns a theoretical value of type any, meaning it may be used as a placeholder for any other value.

mod example

proc weekday(n) {
case n {
0 -> return "Monday"
1 -> return "Tuesday"
2 -> return "Wednesday"
3 -> return "Thursday"
4 -> return "Friday"
5 -> return "Saturday"
6 -> return "Sunday"
} return panic("Invalid input!") // used as a placeholder for a string here
}

range

Returns an iterator over all integers starting at the first argument up to (not including) the integer passed as the second argument. This procedure is called when you use the start..end-syntax.

mod example

use std::(iter::for_each, io::println)

proc main() {
0..10 |> for_each(println) // prints '0', '1', ..., '8', '9'
}

range_incl

Returns an iterator over all integers starting at the first argument up to (including) the integer passed as the second argument. This procedure is called when you use the start..=end-syntax.

mod example

use std::(iter::for_each, io::println)

proc main() {
0..=10 |> for_each(println) // prints '0', '1', ..., '9', '10'
}

substring

Creates a new string by copying the part of the given source string provided by the first argument starting at the code point index provided by the second argument up to the code point index provided by the third argument. If any index is negative, the length of the source string will be added to it.

mod example

use std::io::println

proc main() {
val greeting = "Hello, world!"
println(substring(greeting, 0, 4)) // prints 'Hell'
println(substring(greeting, 7, -1)) // prints 'world'
}

tag_eq

Given two union values, this procedure compares them only by their variant, not the value they hold.

mod example

use std::io::println

proc main() {
val a = #cat { name = "Snowball", hunger = 0.2 }
val b = #cat { name = "Cookie", hunger = 0.6 }
val c = #dog { name = "Rex", volume = 3.0 }
val d = #cat { name = "Snowball", hunger = 0.2 }
println(tag_eq(a, b)) // prints 'true'
println(tag_eq(a, c)) // prints 'false'
println(tag_eq(a, d)) // prints 'true'
}