-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathegg_array.js
More file actions
28 lines (25 loc) · 840 Bytes
/
egg_array.js
File metadata and controls
28 lines (25 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* The array function gets a different amount of arguments each time.
* Since we can prospect the number of arguments the function is
* going to get, it expects none (function())
* The "arguments" keyword is an object that is created with each function
* and contains the list of arguments as an array-like object
*/
topEnv["array"] = function() {
return Array.prototype.slice.call(arguments, 0);
};
topEnv["length"] = function(arr) {
return arr.length;
};
topEnv["element"] = function(arr, i) {
return arr[i];
};
run("do(define(sum, fun(array,",
" do(define(i, 0),",
" define(sum, 0),",
" while(<(i, length(array)),",
" do(define(sum, +(sum, element(array, i))),",
" define(i, +(i, 1)))),",
" sum))),",
" print(sum(array(1, 2, 3))))");
// → 6