mirror of
https://github.com/janet-lang/janet
synced 2025-10-08 12:32:30 +00:00
Change primary looping macro to 'loop' instead of 'for'.
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
(import examples.iterators :as "")
|
||||
|
||||
(defn sum3
|
||||
"Solve the 3SUM problem in O(n^2) time."
|
||||
[s]
|
||||
(def tab @{})
|
||||
(def solutions @{})
|
||||
(def len (length s))
|
||||
(for [k 0 len]
|
||||
(loop [k :range [0 len]]
|
||||
(put tab (get s k) k))
|
||||
(for [i 0 len]
|
||||
(for [j 0 len]
|
||||
(loop [i :range [0 len], j :range [0 len]]
|
||||
(def k (get tab (- 0 (get s i) (get s j))))
|
||||
(when (and k (not= k i) (not= k j) (not= i j))
|
||||
(put solutions {i true j true k true} true))))
|
||||
(iter2array (map (fn [x] (iter2array (keys x))) (keys solutions))))
|
||||
(put solutions {i true j true k true} true)))
|
||||
(map keys (keys solution)))
|
||||
|
@@ -3,7 +3,7 @@
|
||||
(defn fizzbuzz
|
||||
"Prints the fizzbuzz problem."
|
||||
[]
|
||||
(for [i 1 101]
|
||||
(loop [i :range [1 101]]
|
||||
(let [fizz (zero? (% i 3))
|
||||
buzz (zero? (% i 5))]
|
||||
(print (cond
|
||||
|
@@ -4,7 +4,8 @@
|
||||
"Get the number of occurences of each value in a indexed structure."
|
||||
[ind]
|
||||
(def freqs @{})
|
||||
(each (fn [x]
|
||||
(let [n (get freqs x)]
|
||||
(put freqs x (if n (+ 1 n) 1)))) ind)
|
||||
(loop
|
||||
[x :in ind]
|
||||
(def n (get freqs x))
|
||||
(put freqs x (if n (+ 1 n) 1)))
|
||||
freqs)
|
||||
|
@@ -1,3 +1,5 @@
|
||||
# Prints hello
|
||||
|
||||
(import examples.3sum)
|
||||
|
||||
(print "hello, world!")
|
||||
|
@@ -4,10 +4,10 @@
|
||||
"Returns a list of prime numbers less than n."
|
||||
[n]
|
||||
(def list @[])
|
||||
(for [i 2 n]
|
||||
(loop [i :range [2 n]]
|
||||
(var isprime? true)
|
||||
(def len (length list))
|
||||
(for [j 0 len]
|
||||
(loop [j :range [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