Skip to content

Commit

Permalink
add Array>>#min
Browse files Browse the repository at this point in the history
  • Loading branch information
takano32 committed Mar 10, 2016
1 parent c66ffe0 commit 3446253
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions citrine.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ ctr_object* ctr_array_put(ctr_object* myself, ctr_argument* argumentList);
ctr_object* ctr_array_from_to(ctr_object* myself, ctr_argument* argumentList);
ctr_object* ctr_array_add(ctr_object* myself, ctr_argument* argumentList);
ctr_object* ctr_array_map(ctr_object* myself, ctr_argument* argumentList);
ctr_object* ctr_array_min(ctr_object* myself, ctr_argument* argumentList);
ctr_object* ctr_array_sum(ctr_object* myself, ctr_argument* argumentList);
ctr_object* ctr_array_product(ctr_object* myself, ctr_argument* argumentList);

Expand Down
26 changes: 26 additions & 0 deletions collections.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ ctr_object* ctr_array_push(ctr_object* myself, ctr_argument* argumentList) {
return myself;
}

/**
* [Array] min
*
* Returns the minimum value from an array.
*
* Usage:
*
* a := Array <- 8 ; 4 ; 2 ; 16.
* m := a min. #2
*
*/
ctr_object* ctr_array_min(ctr_object* myself, ctr_argument* argumentList) {
double min = 0;
double v = 0;
ctr_object* el;
size_t i = 0;
for(i = 0; i < myself->value.avalue->head; i++) {
el = *(myself->value.avalue->elements + i);
v = ctr_internal_cast2number(el)->value.nvalue;
if (i == 0 || v < min) {
min = v;
}
}
return ctr_build_number_from_float(min);
}

/**
* [Array] sum
*
Expand Down
1 change: 1 addition & 0 deletions world.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ void ctr_initialize_world() {
ctr_internal_create_func(CtrStdArray, ctr_build_string("+", 1), &ctr_array_add);
ctr_internal_create_func(CtrStdArray, ctr_build_string("map:", 4), &ctr_array_map);
ctr_internal_create_func(CtrStdArray, ctr_build_string("each:", 5), &ctr_array_map);
ctr_internal_create_func(CtrStdArray, ctr_build_string("min", 3), &ctr_array_min);
ctr_internal_create_func(CtrStdArray, ctr_build_string("sum", 3), &ctr_array_sum);
ctr_internal_create_func(CtrStdArray, ctr_build_string("product", 7), &ctr_array_product);
ctr_internal_object_add_property(CtrStdWorld, ctr_build_string("Array", 5), CtrStdArray, 0);
Expand Down

1 comment on commit 3446253

@gabordemooij
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To write a test:

write a little program:

 tests/test0XXX.ctr -- where XXX is the next test number

Is the output correct? Run:

bash misc/isgood.bash tests/test0XXX.ctr

you now get a:

tests/test0XXX.exp

to run all tests:

bash runtests.bash

Please sign in to comment.