1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-16 13:44:48 +00:00

Update boot.dst and lazyseq examples.

This commit is contained in:
bakpakin 2018-03-16 13:40:10 -04:00
parent e8dfe673f2
commit d3a1d97649
2 changed files with 44 additions and 23 deletions

View File

@ -17,7 +17,7 @@
(defmacro delay (defmacro delay
"Macro for lazy evaluation" "Macro for lazy evaluation"
[x] (tuple mem0 (tuple 'fn [] x))) [& forms] (tuple mem0 (apply tuple (array-concat ['fn []] forms))))
# Use tuples instead of structs to save memory # Use tuples instead of structs to save memory
(def HEAD :private 0) (def HEAD :private 0)
@ -32,6 +32,11 @@
[h t] [h t]
(delay (tuple h t))) (delay (tuple h t)))
(defn cons1
"Create a new sequence cons by prepending a value to the original sequence."
[h t]
(tuple h t))
(defn empty? (defn empty?
"Check if a sequence is empty." "Check if a sequence is empty."
[s] [s]
@ -47,17 +52,24 @@
[s] [s]
(get (s) TAIL)) (get (s) TAIL))
(defn range (defn range2
"Return a sequence of integers [start, end)." "Return a sequence of integers [start, end)."
[start end] [start end]
(if (< start end) (if (< start end)
(cons start (range (+ 1 start) end)) (cons start (range (+ 1 start) end))
empty-seq)) empty-seq))
(defn range
"Return a sequence of integers [0, end)."
[end]
(range2 0 end))
(defn map (defn map
"Return a sequence that is the result of apply f to each value in s." "Return a sequence that is the result of apply f to each value in s."
[f s] [f s]
(if (s) (cons (f (head s)) (map f (tail s))) empty-seq)) (delay
(def x (s))
(if x (tuple (f (get x HEAD)) (map f (get x TAIL))))))
(defn realize (defn realize
"Force evaluation of a lazy sequence." "Force evaluation of a lazy sequence."
@ -71,7 +83,9 @@
(defn drop (defn drop
"Ignores the first n values of the sequence and returns the rest." "Ignores the first n values of the sequence and returns the rest."
[n s] [n s]
(if (s) (if (zero? n) s (drop (- n 1) (tail s))) empty-seq)) (delay
(def x (s))
(if (s) (if (zero? n) s (drop (- n 1) (tail s))) empty-seq))
(defn take (defn take
"Returns at most the first n values of s." "Returns at most the first n values of s."
@ -80,3 +94,8 @@
(cons (head s) (take (- n 1) (tail s))) (cons (head s) (take (- n 1) (tail s)))
empty-seq)) empty-seq))
(defn take-while
"Returns a sequence of values until the predicate is false."
[pred s]
(delay (if (s) )))

View File

@ -1,11 +1,6 @@
# Bootstrap the dst environment # Bootstrap the dst environment
# Copyright 2018 (C) Calvin Rose # Copyright 2018 (C) Calvin Rose
# Capture the current env
(var *env*
"A var that points to the current environment."
_env)
(def defn :macro (def defn :macro
"Define a function" "Define a function"
(fn [name & more] (fn [name & more]
@ -82,16 +77,20 @@ are matched. If there are no matches, return nil."
(aux (+ i 2)))))) (aux (+ i 2))))))
(aux 0)) (aux 0))
(defn doc (defn doc*
"Shows documentation for the given symbol." [env sym]
[sym] (def x (get env sym))
(def x (get *env* sym))
(if (not x) (if (not x)
(print "symbol " x " not found.") (print "symbol " x " not found.")
(do (do
(def d (get x 'doc)) (def d (get x 'doc))
(print "\n" (if d d "no documentation found.") "\n")))) (print "\n" (if d d "no documentation found.") "\n"))))
(defmacro doc
"Shows documentation for the given symbol."
[sym]
(tuple doc* '_env sym))
(defmacro select (defmacro select
"Select the body that equals the dispatch value. When pairs "Select the body that equals the dispatch value. When pairs
has an odd number of arguments, the last is the default expression. has an odd number of arguments, the last is the default expression.
@ -141,13 +140,13 @@ If no match is found, returns nil"
(def iters { (def iters {
:array array-iter :array array-iter
:tuple array-iter :tuple array-iter
:struct (fn [x] x)}) :struct identity})
(fn [x] (fn [x]
(def makei (get iters (type x))) (def makei (get iters (type x)))
(if makei (makei x) (error "expected sequence"))))) (if makei (makei x) (error "expected sequence")))))
(defn range [top] (defn range2 [bottom top]
(var i 0) (var i bottom)
{ {
:more (fn [] (< i top)) :more (fn [] (< i top))
:next (fn [] :next (fn []
@ -156,6 +155,8 @@ If no match is found, returns nil"
ret) ret)
}) })
(defn range [top] (range2 0 top))
(defn doiter [itr] (defn doiter [itr]
(def {:more more :next next} (iter itr)) (def {:more more :next next} (iter itr))
(while (more) (next))) (while (more) (next)))
@ -337,6 +338,7 @@ If no match is found, returns nil"
(defn make-env [parent] (defn make-env [parent]
(def parent (if parent parent _env)) (def parent (if parent parent _env))
(def newenv (setproto @{} parent)) (def newenv (setproto @{} parent))
(put newenv '_env @{'value newenv})
newenv) newenv)
# Remove the reference to the default _env # Remove the reference to the default _env
@ -401,10 +403,7 @@ onvalue."
(if (= (fiber-status f) :error) (if (= (fiber-status f) :error)
(onerr "runtime" res) (onerr "runtime" res)
(onvalue res))) (onvalue res)))
(def oldenv *env*)
(varset! *env* env)
(foreach (val-stream chunks onerr) doone) (foreach (val-stream chunks onerr) doone)
(varset! *env* oldenv)
env))) env)))
(def require (do (def require (do
@ -426,14 +425,17 @@ onvalue."
(put loading path nil) (put loading path nil)
newenv))))) newenv)))))
(defn import [path & args] (defn import* [env path & args]
(def env (require path)) (def newenv (require path))
(def { (def {
:prefix prefix :prefix prefix
} (apply table args)) } (apply table args))
(foreach (pairs env) (fn [[k v]] (foreach (pairs newenv) (fn [[k v]]
(when (not (get v :private)) (when (not (get v :private))
(put *env* (symbol (if prefix prefix "") k) v))))) (put env (symbol (if prefix prefix "") k) v)))))
(defmacro import [path & args]
(apply tuple (array-concat [import* '_env path] args)))
(defn repl [getchunk] (defn repl [getchunk]
(def newenv (make-env)) (def newenv (make-env))