mirror of
https://github.com/janet-lang/janet
synced 2025-10-23 11:47:40 +00:00
Multisyms for easier access into structures.
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
# A game of life implementation
|
||||
|
||||
(def- window
|
||||
(fora [x :range [-1 2]
|
||||
(seq [x :range [-1 2]
|
||||
y :range [-1 2]
|
||||
:when (not (and (zero? x) (zero? y)))]
|
||||
(tuple x y)))
|
||||
|
||||
(defn- neighbors
|
||||
[[x y]]
|
||||
(mapa (fn [[x1 y1]] (tuple (+ x x1) (+ y y1))) window))
|
||||
(map (fn [[x1 y1]] (tuple (+ x x1) (+ y y1))) window))
|
||||
|
||||
(defn tick
|
||||
"Get the next state in the Game Of Life."
|
||||
[state]
|
||||
(def cell-set (frequencies state))
|
||||
(def neighbor-set (frequencies (mapcat neighbors state)))
|
||||
(fora [coord :keys neighbor-set
|
||||
(seq [coord :keys neighbor-set
|
||||
:let [count (get neighbor-set coord)]
|
||||
:when (or (= count 3) (and (get cell-set coord) (= count 2)))]
|
||||
coord))
|
||||
@@ -24,7 +24,7 @@
|
||||
"Draw cells in the game of life from (x1, y1) to (x2, y2)"
|
||||
[state x1 y1 x2 y2]
|
||||
(def cellset @{})
|
||||
(loop [cell :in state] (put cellset cell true))
|
||||
(each cell state (put cellset cell true))
|
||||
(loop [x :range [x1 (+ 1 x2)]
|
||||
:after (print)
|
||||
y :range [y1 (+ 1 y2)]]
|
||||
@@ -37,7 +37,7 @@
|
||||
|
||||
(var *state* '[(0 0) (-1 0) (1 0) (1 1) (0 2)])
|
||||
|
||||
(loop [i :range [0 20]]
|
||||
(for i 0 20
|
||||
(print "generation " i)
|
||||
(draw *state* -7 -7 7 7)
|
||||
(:= *state* (tick *state*)))
|
||||
|
@@ -1,73 +0,0 @@
|
||||
# Simpler iteration primitives example.
|
||||
|
||||
(defn- iter-for
|
||||
[prelude binding start end body]
|
||||
(def $end (gensym))
|
||||
(tuple 'do
|
||||
prelude
|
||||
(tuple 'var binding start)
|
||||
(tuple 'def $end end)
|
||||
(tuple 'while (tuple < binding $end)
|
||||
body
|
||||
(tuple '++ binding))))
|
||||
|
||||
(defn- iter-keys
|
||||
[prelude binding tab body]
|
||||
(tuple 'do
|
||||
prelude
|
||||
(tuple 'var binding (tuple next tab nil))
|
||||
(tuple 'while (tuple not= nil binding)
|
||||
body
|
||||
(tuple := binding (tuple next tab binding)))))
|
||||
|
||||
(defmacro do-range
|
||||
"Iterate over a half open integer range."
|
||||
[binding start end & body]
|
||||
(def $iter (gensym))
|
||||
(iter-for nil $iter start end
|
||||
(apply tuple 'do (tuple 'def binding $iter) body)))
|
||||
|
||||
(defmacro each
|
||||
"Iterate over an indexed data structure."
|
||||
[binding ind & body]
|
||||
(def $iter (gensym))
|
||||
(def $ind (gensym))
|
||||
(iter-for (tuple 'def $ind ind)
|
||||
$iter 0 (tuple length $ind)
|
||||
(apply tuple 'do (tuple 'def binding (tuple get $ind $iter)) body)))
|
||||
|
||||
|
||||
(defmacro each-key
|
||||
"Iterate over keys of a table or structure."
|
||||
[binding tab & body]
|
||||
(def $tab (gensym))
|
||||
(def $key (gensym))
|
||||
(iter-keys
|
||||
(tuple 'def $tab tab)
|
||||
$key
|
||||
$tab
|
||||
(apply tuple 'do (tuple 'def binding $key) body)))
|
||||
|
||||
(defmacro each-value
|
||||
"Iterate over values of a table or structure."
|
||||
[binding tab & body]
|
||||
(def $tab (gensym))
|
||||
(def $key (gensym))
|
||||
(iter-keys
|
||||
(tuple 'def $tab tab)
|
||||
$key
|
||||
$tab
|
||||
(apply tuple 'do (tuple 'def binding (tuple 'get $tab $key)) body)))
|
||||
|
||||
(defmacro each-pair
|
||||
"Iterate over keys and values of a table or structure."
|
||||
[k v tab & body]
|
||||
(def $tab (gensym))
|
||||
(def $key (gensym))
|
||||
(iter-keys
|
||||
(tuple 'def $tab tab)
|
||||
$key
|
||||
$tab
|
||||
(apply tuple 'do
|
||||
(tuple 'def k $key)
|
||||
(tuple 'def v (tuple 'get $tab $key)) body)))
|
@@ -4,10 +4,10 @@
|
||||
"Returns a list of prime numbers less than n."
|
||||
[n]
|
||||
(def list @[])
|
||||
(loop [i :range [2 n]]
|
||||
(for i 2 n
|
||||
(var isprime? true)
|
||||
(def len (length list))
|
||||
(loop [j :range [0 len]]
|
||||
(for j 0 len
|
||||
(def trial (get list j))
|
||||
(if (zero? (% i trial)) (:= isprime? false)))
|
||||
(if isprime? (array.push list i)))
|
||||
|
Reference in New Issue
Block a user