7#ifndef CHAISCRIPT_PRELUDE_HPP_
8#define CHAISCRIPT_PRELUDE_HPP_
15 if (call_exists(`<`, l, r)) {
18 type_name(l) < type_name(r)
24 if (call_exists(`>`, l, r)) {
27 type_name(l) > type_name(r)
32 if (call_exists(`==`, l, r)) {
44 double(x).clone_var_attrs(x)
48 string(x).clone_var_attrs(x)
52 vector(x).clone_var_attrs(x)
57 int(x).clone_var_attrs(x)
60def clone(x) : function_exists(type_name(x)) && call_exists(eval(type_name(x)), x)
62 eval(type_name(x))(x).clone_var_attrs(x);
67def to_string(x) : call_exists(first, x) && call_exists(second, x) {
68 "<" + x.first.to_string() + ", " + x.second.to_string() + ">";
71# to_string for containers
72def to_string(x) : call_exists(range, x) && !x.is_type("string"){
73 "[" + x.join(", ") + "]";
76# Prints to console with no carriage return
78 print_string(x.to_string());
81# Prints to console with carriage return
83 println_string(x.to_string());
86# Returns the maximum value of two numbers
95# Returns the minimum value of two numbers
107# Returns true if the value is odd
118# Returns true if the value is even
130# Inserts the third value at the position of the second value into the container of the first
131# while making a clone.
132def insert_at(container, pos, x)
134 container.insert_ref_at(pos, clone(x));
137# Returns the reverse of the given container
138def reverse(container) {
139 auto retval := new(container);
140 auto r := range(container);
142 retval.push_back(r.back());
149def range(r) : call_exists(range_internal, r)
151 var ri := range_internal(r);
152 ri.get_var_attr("internal_obj") := r;
156# Return a range from a range
157def range(r) : call_exists(empty, r) && call_exists(pop_front, r) && call_exists(pop_back, r) && call_exists(back, r) && call_exists(front, r)
163# The retro attribute that contains the underlying range
166# Creates a retro from a retro by returning the original range
167def retro(r) : call_exists(get_type_name, r) && get_type_name(r) == "retro"
173# Creates a retro range from a range
174def retro::retro(r) : call_exists(empty, r) && call_exists(pop_front, r) && call_exists(pop_back, r) && call_exists(back, r) && call_exists(front, r)
179# Returns the first value of a retro
185# Returns the last value of a retro
191# Moves the back iterator of a retro towards the front by one
194 pop_front(this.m_range)
197# Moves the front iterator of a retro towards the back by one
198def retro::pop_front()
200 pop_back(this.m_range)
203# returns true if the retro is out of elements
209# Performs the second value function over the container first value
210def for_each(container, func) : call_exists(range, container) {
211 var t_range := range(container);
212 while (!t_range.empty()) {
213 func(t_range.front());
218def any_of(container, func) : call_exists(range, container) {
219 var t_range := range(container);
220 while (!t_range.empty()) {
221 if (func(t_range.front())) {
229def all_of(container, func) : call_exists(range, container) {
230 var t_range := range(container);
231 while (!t_range.empty()) {
232 if (!func(t_range.front())) {
241def back_inserter(container) {
242 bind(push_back, container, _);
245def contains(container, item, compare_func) : call_exists(range, container) {
246 auto t_range := range(container);
247 while (!t_range.empty()) {
248 if ( compare_func(t_range.front(), item) ) {
257def contains(container, item) {
258 contains(container, item, eq)
261def map(container, func, inserter) : call_exists(range, container) {
262 auto range := range(container);
263 while (!range.empty()) {
264 inserter(func(range.front()));
269# Performs the second value function over the container first value. Creates a new container with the results
270def map(container, func) {
271 auto retval := new(container);
272 map(container, func, back_inserter(retval));
276# Performs the second value function over the container first value. Starts with initial and continues with each element.
277def foldl(container, func, initial) : call_exists(range, container){
278 auto retval = initial;
279 auto range := range(container);
280 while (!range.empty()) {
281 retval = (func(range.front(), retval));
287# Returns the sum of the elements of the given value
289 foldl(container, `+`, 0.0)
292# Returns the product of the elements of the given value
293def product(container) {
294 foldl(container, `*`, 1.0)
297# Returns a new container with the elements of the first value concatenated with the elements of the second value
298def concat(x, y) : call_exists(clone, x) {
300 auto inserter := back_inserter(retval);
301 auto range := range(y);
302 while (!range.empty()) {
303 inserter(range.front());
310def take(container, num, inserter) : call_exists(range, container) {
311 auto r := range(container);
313 while ((i > 0) && (!r.empty())) {
321# Returns a new container with the given number of elements taken from the container
322def take(container, num) {
323 auto retval := new(container);
324 take(container, num, back_inserter(retval));
329def take_while(container, f, inserter) : call_exists(range, container) {
330 auto r := range(container);
331 while ((!r.empty()) && f(r.front())) {
338# Returns a new container with the given elements match the second value function
339def take_while(container, f) {
340 auto retval := new(container);
341 take_while(container, f, back_inserter(retval));
346def drop(container, num, inserter) : call_exists(range, container) {
347 auto r := range(container);
349 while ((i > 0) && (!r.empty())) {
360# Returns a new container with the given number of elements dropped from the given container
361def drop(container, num) {
362 auto retval := new(container);
363 drop(container, num, back_inserter(retval));
368def drop_while(container, f, inserter) : call_exists(range, container) {
369 auto r := range(container);
370 while ((!r.empty())&& f(r.front())) {
380# Returns a new container with the given elements dropped that match the second value function
381def drop_while(container, f) {
382 auto retval := new(container);
383 drop_while(container, f, back_inserter(retval));
388# Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements.
389def reduce(container, func) : container.size() >= 2 && call_exists(range, container) {
390 auto r := range(container);
391 auto retval = r.front();
393 retval = func(retval, r.front());
396 retval = func(retval, r.front());
403# Returns a string of the elements in container delimited by the second value string
404def join(container, delim) {
406 auto range := range(container);
407 if (!range.empty()) {
408 retval += to_string(range.front());
410 while (!range.empty()) {
412 retval += to_string(range.front());
420def filter(container, f, inserter) : call_exists(range, container) {
421 auto r := range(container);
431# Returns a new Vector which match the second value function
432def filter(container, f) {
433 auto retval := new(container);
434 filter(container, f, back_inserter(retval));
439def generate_range(x, y, inserter) {
448# Returns a new Vector which represents the range from the first value to the second value
449def generate_range(x, y) {
450 auto retval := Vector();
451 generate_range(x,y,back_inserter(retval));
456# Returns a new Vector with the first value to the second value as its elements
462def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) {
463 auto r_x := range(x);
464 auto r_y := range(y);
465 while (!r_x.empty() && !r_y.empty()) {
466 inserter(f(r_x.front(), r_y.front()));
473# Returns a new Vector which joins matching elements of the second and third value with the first value function
474def zip_with(f, x, y) {
475 auto retval := Vector();
476 zip_with(f,x,y,back_inserter(retval));
481# Returns a new Vector which joins matching elements of the first and second
483 zip_with(collate, x, y);
487# Returns the position of the second value string in the first value string
488def string::find(string substr) {
489 find(this, substr, size_t(0));
493# Returns the position of last match of the second value string in the first value string
494def string::rfind(string substr) {
495 rfind(this, substr, size_t(-1));
499# Returns the position of the first match of elements in the second value string in the first value string
500def string::find_first_of(string list) {
501 find_first_of(this, list, size_t(0));
505# Returns the position of the last match of elements in the second value string in the first value string
506def string::find_last_of(string list) {
507 find_last_of(this, list, size_t(-1));
511# Returns the position of the first non-matching element in the second value string in the first value string
512def string::find_first_not_of(string list) {
513 find_first_not_of(this, list, size_t(0));
517# Returns the position of the last non-matching element in the second value string in the first value string
518def string::find_last_not_of(string list) {
519 find_last_not_of(this, list, size_t(-1));
524 drop_while(this, fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'});
529 reverse(drop_while(reverse(this), fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'}));
538def find(container, value, Function compare_func) : call_exists(range, container) {
539 auto range := range(container);
540 while (!range.empty()) {
541 if (compare_func(range.front(), value)) {
551def find(container, value) {
552 find(container, value, eq)
Namespace chaiscript contains every API call that the average user will be concerned with.
Definition chaiscript_prelude.hpp:11
static std::string chaiscript_prelude()
Definition chaiscript_prelude.hpp:12