1
0
mirror of https://github.com/janet-lang/janet synced 2025-06-13 12:04:12 +00:00

Merge branch 'master' of github.com:janet-lang/janet

This commit is contained in:
Calvin Rose 2021-12-09 18:47:36 -06:00
commit d998f24d26
4 changed files with 36 additions and 16 deletions

View File

@ -1,6 +1,6 @@
(defn dowork [name n] (defn dowork [name n]
(print name " starting work...") (print name " starting work...")
(os/execute [(dyn :executable) "-e" (string "(os/sleep " n ")")]) (os/execute [(dyn :executable) "-e" (string "(os/sleep " n ")")] :p)
(print name " finished work!")) (print name " finished work!"))
# Will be done in parallel # Will be done in parallel

View File

@ -76,9 +76,16 @@ void num_array_put(void *p, Janet key, Janet value) {
} }
} }
static Janet num_array_length(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
num_array *array = (num_array *)janet_getabstract(argv, 0, &num_array_type);
return janet_wrap_number(array->size);
}
static const JanetMethod methods[] = { static const JanetMethod methods[] = {
{"scale", num_array_scale}, {"scale", num_array_scale},
{"sum", num_array_sum}, {"sum", num_array_sum},
{"length", num_array_length},
{NULL, NULL} {NULL, NULL}
}; };
@ -109,6 +116,11 @@ static const JanetReg cfuns[] = {
"(numarray/scale numarray factor)\n\n" "(numarray/scale numarray factor)\n\n"
"scale numarray by factor" "scale numarray by factor"
}, },
{
"sum", num_array_sum,
"(numarray/sum numarray)\n\n"
"sums numarray"
},
{NULL, NULL, NULL} {NULL, NULL, NULL}
}; };

View File

@ -1,4 +1,4 @@
(import build/numarray) (import /build/numarray)
(def a (numarray/new 30)) (def a (numarray/new 30))
(print (get a 20)) (print (get a 20))

View File

@ -532,16 +532,19 @@
(defmacro loop (defmacro loop
``` ```
A general purpose loop macro. This macro is similar to the Common Lisp A general purpose loop macro. This macro is similar to the Common Lisp loop
loop macro, although intentionally much smaller in scope. macro, although intentionally much smaller in scope. The head of the loop
The head of the loop should be a tuple that contains a sequence of should be a tuple that contains a sequence of either bindings or
either bindings or conditionals. A binding is a sequence of three values conditionals. A binding is a sequence of three values that define something
that define something to loop over. They are formatted like: to loop over. Bindings are written in the format:
binding :verb object/expression binding :verb object/expression
Where `binding` is a binding as passed to def, `:verb` is one of a set of where `binding` is a binding as passed to def, `:verb` is one of a set of
keywords, and `object` is any expression. The available verbs are: keywords, and `object` is any expression. Each subsequent binding creates a
nested loop within the loop created by the previous binding.
The available verbs are:
* `:iterate` - repeatedly evaluate and bind to the expression while it is * `:iterate` - repeatedly evaluate and bind to the expression while it is
truthy. truthy.
@ -565,14 +568,19 @@
where `:modifier` is one of a set of keywords, and `argument` is keyword-dependent. where `:modifier` is one of a set of keywords, and `argument` is keyword-dependent.
`:modifier` can be one of: `:modifier` can be one of:
* `:while expression` - breaks from the loop if `expression` is falsey. * `:while expression` - breaks from the current loop if `expression` is
* `:until expression` - breaks from the loop if `expression` is truthy. falsey.
* `:let bindings` - defines bindings inside the loop as passed to the `let` macro. * `:until expression` - breaks from the current loop if `expression` is
* `:before form` - evaluates a form for a side effect before the next inner loop. truthy.
* `:after form` - same as `:before`, but the side effect happens after the next inner loop. * `:let bindings` - defines bindings inside the current loop as passed to the
`let` macro.
* `:before form` - evaluates a form for a side effect before the next inner
loop.
* `:after form` - same as `:before`, but the side effect happens after the
next inner loop.
* `:repeat n` - repeats the next inner loop `n` times. * `:repeat n` - repeats the next inner loop `n` times.
lets try putting a loop item on multiple lines. * `:when condition` - only evaluates the current loop body when `condition`
* `:when condition` - only evaluates the loop body when condition is true. is true.
The `loop` macro always evaluates to nil. The `loop` macro always evaluates to nil.
``` ```