1
0
mirror of https://github.com/janet-lang/janet synced 2025-02-03 02:39:09 +00:00

Merge pull request #6 from Gavlooth/master

Some thoughts
This commit is contained in:
Calvin Rose 2018-03-24 11:53:57 -04:00 committed by GitHub
commit 3c57980ef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 52 deletions

View File

@ -109,15 +109,14 @@ body once, and then memoizes the result."
#Iterators is a conscept that looks a lot like lazy seq #Iterators is a conscept that looks a lot like lazy seq
#The following functions turn iterators to lazy seq and vice versa #The following functions turn iterators to lazy seq and vice versa
(defn- iter-self
[next]
(delay (tuple (next) (iter-self next))))
(defn iter2lazy (defn iter2lazy
"Create a lazy sequence froma an iterator" "Create a lazy sequence from an iterator"
[iter] [iter]
(def {:more more :next next} iter) (def {:more more :next next} iter)
(iter-self next)) (if (more)
(delay (tuple (next) (iter2lazy iter)))
empty-seq))
(defn lazy2iter (defn lazy2iter
"turn a lazy-seq to an iterator" "turn a lazy-seq to an iterator"
@ -138,8 +137,7 @@ body once, and then memoizes the result."
#data structures as their values are references to this #data structures as their values are references to this
#data structures. Same is true for iterators #data structures. Same is true for iterators
(defn filter2 [pred coll] (defn filter2 [pred coll] (iter2lazy (filter pred coll)))
(tail (iter2lazy (filter pred coll))))
(def arr [0 -1 -2 33 -3 0 302 -3 2 8 54 3 -2 0]) (def arr [0 -1 -2 33 -3 0 302 -3 2 8 54 3 -2 0])

View File

@ -379,7 +379,7 @@ If no match is found, returns nil"
(defmacro when-let (defmacro when-let
"Takes the first one or two forms in vector and if true binds "Takes the first one or two forms in vector and if true binds
all the forms with let and evaluates body" all the forms with let and evaluates the body"
[bindings & body] [bindings & body]
(def head (ast-unwrap1 bindings)) (def head (ast-unwrap1 bindings))
(tuple 'let head (tuple 'let head
@ -403,21 +403,21 @@ If no match is found, returns nil"
(array-slice functions 5 -1))))) (array-slice functions 5 -1)))))
(defn zipcoll (defn zipcoll
"Creates an table or tuple from two arrays/tuples. Result is table if no "Creates an table or tuple from two arrays/tuples. If a third argument of
third argument is given" :struct is givent resault is struct else is table."
[coll-1 coll-2 the-type] [coll-1 coll-2 the-type]
(var zipping-table @{}) (var zipping-table @{})
(def {:more more1 :next next1} (iter coll-1)) (def {:more more1 :next next1} (iter coll-1))
(def {:more more2 :next next2} (iter coll-2)) (def {:more more2 :next next2} (iter coll-2))
(while (and (more1) (more2)) (while (and (more1) (more2))
(put zipping-table (next1) (next2))) (put zipping-table (next1) (next2)))
(if (= :struct the-type) (if (struct? the-type)
(table-to-struct zipping-table) (table-to-struct zipping-table)
zipping-table)) zipping-table))
(defn update (defn update
"Accepts a key argument and passes its associated value to a function. "Accepts a key argument and passes its' associated value to a function.
The key, then is associated to that value" The key then, is associated to the function's return value"
[coll a-key a-function & args] [coll a-key a-function & args]
(def old-value (get coll a-key) ) (def old-value (get coll a-key) )
(put coll a-key (apply a-function old-value args))) (put coll a-key (apply a-function old-value args)))

View File

@ -614,7 +614,7 @@ DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
return ret; return ret;
} }
/* Keep in lexographic order */ /* Keep in lexicographic order */
static const DstSpecial dstc_specials[] = { static const DstSpecial dstc_specials[] = {
{":=", dstc_varset}, {":=", dstc_varset},
{"ast-quote", dstc_astquote}, {"ast-quote", dstc_astquote},