mirror of
https://github.com/janet-lang/janet
synced 2024-11-24 09:17:17 +00:00
Updare examples. Delete ugly iterator example.
This commit is contained in:
parent
f860b950fc
commit
ea4465f58e
@ -1,13 +1,10 @@
|
||||
# A simple fizz buzz example
|
||||
|
||||
(defn fizzbuzz
|
||||
"Prints the fizzbuzz problem."
|
||||
[]
|
||||
(loop [i :range [1 101]]
|
||||
(let [fizz (zero? (% i 3))
|
||||
buzz (zero? (% i 5))]
|
||||
(print (cond
|
||||
(and fizz buzz) "fizzbuzz"
|
||||
fizz "fizz"
|
||||
buzz "buzz"
|
||||
i)))))
|
||||
(loop [i :range [1 101]
|
||||
:let [fizz (zero? (% i 3))
|
||||
buzz (zero? (% i 5))]]
|
||||
(print (cond
|
||||
(and fizz buzz) "fizzbuzz"
|
||||
fizz "fizz"
|
||||
buzz "buzz"
|
||||
i)))
|
||||
|
@ -1,107 +0,0 @@
|
||||
###
|
||||
###
|
||||
### Iterators
|
||||
###
|
||||
###
|
||||
|
||||
(def iter (do
|
||||
(defn array-iter [x]
|
||||
(def len (length x))
|
||||
(var i 0)
|
||||
{:more (fn [] (< i len))
|
||||
:next (fn []
|
||||
(def ret (get x i))
|
||||
(:= i (+ i 1))
|
||||
ret)})
|
||||
(def iters {
|
||||
:array array-iter
|
||||
:tuple array-iter
|
||||
:struct identity})
|
||||
(fn [x]
|
||||
(def makei (get iters (type x)))
|
||||
(if makei (makei x) (error "expected sequence")))))
|
||||
|
||||
(defn range2 [bottom top]
|
||||
(var i bottom)
|
||||
{
|
||||
:more (fn [] (< i top))
|
||||
:next (fn []
|
||||
(def ret i)
|
||||
(:= i (+ i 1))
|
||||
ret)
|
||||
})
|
||||
|
||||
(defn range [top] (range2 0 top))
|
||||
|
||||
(defn doiter [itr]
|
||||
(def {:more more :next next} (iter itr))
|
||||
(while (more) (next)))
|
||||
|
||||
(defn foreach [itr f]
|
||||
(def {:more more :next next} (iter itr))
|
||||
(while (more) (f (next))))
|
||||
|
||||
(defn iter2array [itr]
|
||||
(def {:more more :next next} (iter itr))
|
||||
(def a @[])
|
||||
(while (more) (array.push a (next)))
|
||||
a)
|
||||
|
||||
(defn map [f itr]
|
||||
(def {:more more :next next} (iter itr))
|
||||
{:more more :next (fn [] (f (next)))})
|
||||
|
||||
(defn reduce [f start itr]
|
||||
(def itr (iter itr))
|
||||
(def {:more more :next next} itr)
|
||||
(if (more)
|
||||
(reduce f (f start (next)) itr)
|
||||
start))
|
||||
|
||||
(defn filter [pred itr]
|
||||
(def itr (iter itr))
|
||||
(def {:more more :next next} itr)
|
||||
(var alive true)
|
||||
(var temp nil)
|
||||
(var isnew true)
|
||||
(defn nextgood []
|
||||
(if alive
|
||||
(if (more)
|
||||
(do
|
||||
(def n (next))
|
||||
(if (pred n) n (nextgood)))
|
||||
(:= alive false))))
|
||||
(defn nnext [] (def ret temp) (:= temp (nextgood)) ret)
|
||||
(defn nmore [] (when isnew (:= isnew false) (nnext)) alive)
|
||||
{:more nmore :next nnext})
|
||||
|
||||
(defn pairs [x]
|
||||
(var lastkey (next x nil))
|
||||
{
|
||||
:more (fn [] lastkey)
|
||||
:next (fn []
|
||||
(def ret (tuple lastkey (get x lastkey)))
|
||||
(:= lastkey (next x lastkey))
|
||||
ret)
|
||||
})
|
||||
|
||||
(defn keys [x]
|
||||
(var lastkey (next x nil))
|
||||
{
|
||||
:more (fn [] lastkey)
|
||||
:next (fn []
|
||||
(def ret lastkey)
|
||||
(:= lastkey (next x lastkey))
|
||||
ret)
|
||||
})
|
||||
|
||||
(defn values [x]
|
||||
(var lastkey (next x nil))
|
||||
{
|
||||
:more (fn [] lastkey)
|
||||
:next (fn []
|
||||
(def ret (get x lastkey))
|
||||
(:= lastkey (next x lastkey))
|
||||
ret)
|
||||
})
|
||||
|
@ -25,11 +25,8 @@
|
||||
[state x1 y1 x2 y2]
|
||||
(def cellset @{})
|
||||
(loop [cell :in state] (put cellset cell true))
|
||||
(loop [:before (print "+" (string.repeat "--" (inc (- y2 y1))) "+")
|
||||
:after (print "+" (string.repeat "--" (inc (- y2 y1))) "+")
|
||||
x :range [x1 (+ 1 x2)]
|
||||
:before (file.write stdout "|")
|
||||
:after (file.write stdout "|\n")
|
||||
(loop [x :range [x1 (+ 1 x2)]
|
||||
:after (print)
|
||||
y :range [y1 (+ 1 y2)]]
|
||||
(file.write stdout (if (get cellset (tuple x y)) "X " ". ")))
|
||||
(print))
|
||||
|
@ -18,7 +18,6 @@
|
||||
'[[3]
|
||||
[7 10]
|
||||
[4 3 7]
|
||||
[8 9 1 3]
|
||||
])
|
||||
[8 9 1 3]])
|
||||
|
||||
(print (maxpath triangle))
|
||||
|
@ -416,6 +416,7 @@
|
||||
"Create a generator expression using the loop syntax. Returns a fiber
|
||||
that yields all values inside the loop in order. See loop for details."
|
||||
[head & body]
|
||||
# `(fiber.new (fn [&] (loop ,head (yield (do ,@body)))))
|
||||
(tuple fiber.new
|
||||
(tuple 'fn '[&]
|
||||
(tuple 'loop head (tuple yield (tuple.prepend body 'do))))))
|
||||
|
Loading…
Reference in New Issue
Block a user