About ChaiScript

ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility in mind. It is an ECMAScript-inspired, embedded functional-like language.

ChaiScript is licensed under the BSD license.

Download

Version: 2.3.3 Released: 5/15/2010

Source
Windows
Linux

Standard Library (aka The Prelude)

ChaiScript, at its core, has some very functional programming-inspired habits. Few places show this off as clearly as the prelude, itself a name taken as a nod to the popular functional language Haskell. This prelude is available to all standard ChaiScript applications, and provides a simple foundation for using numbers, strings, and ranges (the general category of containers and their iteration).

Strings

to_string(x): Converts x into a string.

eval> to_string(3).is_type("string")
true

puts(x): Prints x to the terminal, without a trailing carriage return.

eval> puts("hi, "); puts("there")
hi, there

print(x): Prints x to the terminal, with a trailing carriage return.

eval> print("hello")
hello

find(str, substr): Finds the first instance of substr in str.

eval> find("abab", "ab")
0

rfind(str, substr): Finds the last instance of substr in str.

eval> rfind("abab", "ab")
2

find_first_of(str, list): Finds the first of characters in list in the str string.

eval> find_first_of("abab", "bec")
1

find_last_of(str, list): Finds the last of characters in list in the str string.

eval> find_last_of("abab", "bec")
3

find_first_not_of(str, list): Finds the first non-matching character to list in the str string.

eval> find_first_not_of("abcd", "fec")
0

find_last_not_of(str, list): Finds the last non-matching character to list in the str string.

eval> find_last_not_of("abcd", "fec")
3

ltrim(str): Removes whitespace from the front of the string.

eval> ltrim("  bob")
bob

rtrim(str): Removes whitespace from the back of the string.

eval> rtrim("bob  ") + "|"
bob|

trim(str): Removes whitespace from the front and back of the string.

eval> trim("  bob  ") + "|"
bob|

Numbers

max(a, b): Returns the maximum value of a or b.

eval> max(4, 10)
10

min(a, b): Returns the minimum value of a or b.

eval> min(4, 10)
4

even(x): Returns true if x is even, otherwise returns false.

eval> even(4)
true

odd(x): Returns true if x is odd, otherwise returns false.

eval> odd(4)
false

Containers

for_each(container, f): Applies the function f over each element in the container.

eval> for_each([1, 2, 3], print)
1
2
3

map(container, f): Applies f over each element in the container, joining all the results.

eval> map([1, 2, 3], odd)
[true, false, true]

foldl(container, f, initial): Starts with the initial value and applies the function f to it and the first element of the container. The result is then applied to the second element, and so on until the elements are exhausted.

eval> foldl([1, 2, 3, 4], `+`, 0)
10

sum(container): Returns the sum total of the values in the container.

eval> sum([1, 2, 3, 4])
10

product(container): Returns the product of the value in the container.

eval> product([1, 2, 3, 4])
24

take(container, num): Takes num elements from the container, returning them.

eval> take([1, 2, 3, 4], 2)
[1, 2]

take_while(container, f): Takes elements from the container that match function f, stopping at the first non-match, returning them as a new Vector.

eval> take_while([1, 2, 3], odd)
[1]

drop(container, num): Drops num elements from the container, returning the remainder.

eval> drop([1, 2, 3, 4], 2)
[3, 4]

drop_while(container, f): Drops elements from the container that match f, stopping at the first non-match, returning the remainder.

eval> drop_while([1, 2, 3], odd)         
[2, 3]

reduce(container, f): Similar to foldl, this takes the first two elements as its starting values for f. This assumes container has at least 2 elements.

eval> reduce([1, 2, 3, 4], `+`)
10

filter(container, f): Takes elements from container that match function f, return them.

eval> filter([1, 2, 3, 4], odd)
[1, 3]

join(container, delim): Joins the elements of the container into a string, delimiting each with the delim string.

eval> join([1, 2, 3], "*") 
1*2*3

reverse(container): Returns the contents of the container in reversed order.

eval> reverse([1, 2, 3, 4, 5, 6, 7])
[7, 6, 5, 4, 3, 2, 1]

generate_range(x, y): Generates a new Vector filled with values starting at x and ending with y.

eval> generate_range(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

concat(x, y): Returns a new Vector with x and y concatenated.

eval> concat([1, 2, 3], [4, 5, 6])
[1, 2, 3, 4, 5, 6]

collate(x, y): Returns a new Vector with x and y as its values.

eval> collate(1, 2)
[1, 2]

zip_with(f, x, y): Applies f to elements of x and y, returning a new Vector with the result of each application.

eval> zip_with(`+`, [1, 2, 3], [4, 5, 6])
[5, 7, 9]

zip(x, y): Collates elements of x and y, returning a new Vector with the result.

eval> zip([1, 2, 3], [4, 5, 6])
[[1, 4], [2, 5], [3, 6]]