mirror of
https://github.com/janet-lang/janet
synced 2025-10-27 13:47:42 +00:00
Add some builtin functions and some examples.
This commit is contained in:
13
lib/fizzbuzz.dst
Normal file
13
lib/fizzbuzz.dst
Normal file
@@ -0,0 +1,13 @@
|
||||
# A simple fizz buzz example
|
||||
|
||||
(defn fizzbuzz
|
||||
"Prints the fizzbuzz problem."
|
||||
[]
|
||||
(for [i 1 101]
|
||||
(let [fizz (zero? (% i 3))
|
||||
buzz (zero? (% i 5))]
|
||||
(print (cond
|
||||
(and fizz buzz) "fizzbuzz"
|
||||
fizz "fizz"
|
||||
buzz "buzz"
|
||||
i)))))
|
||||
3
lib/hello.dst
Normal file
3
lib/hello.dst
Normal file
@@ -0,0 +1,3 @@
|
||||
# Prints hello
|
||||
|
||||
(print "hello, world!")
|
||||
57
lib/pp.dst
57
lib/pp.dst
@@ -1,31 +1,7 @@
|
||||
(def a (fn [] 2))
|
||||
#
|
||||
# Copyright (c) 2017 Calvin Rose
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
# deal in the Software without restriction, including without limitation the
|
||||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
# sell copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
# IN THE SOFTWARE.
|
||||
#
|
||||
# Implements a simple pretty printer. Offers similar functionality
|
||||
# as the describe function.
|
||||
|
||||
# Define a simple pretty printer
|
||||
|
||||
(def pp (do
|
||||
|
||||
(defn pp-seq [pp seen buf a start end]
|
||||
(defn- pp-seq [pp seen buf a start end]
|
||||
(if (get seen a)
|
||||
(buffer-push-string buf "<cycle>")
|
||||
(do
|
||||
@@ -35,11 +11,10 @@
|
||||
(for [i 0 len]
|
||||
(when (not= i 0) (buffer-push-string buf " "))
|
||||
(pp seen buf (get a i)))
|
||||
(buffer-push-string buf end)
|
||||
))
|
||||
(buffer-push-string buf end)))
|
||||
buf)
|
||||
|
||||
(defn pp-dict [pp seen buf a start end]
|
||||
|
||||
(defn- pp-dict [pp seen buf a start end]
|
||||
(if (get seen a)
|
||||
(buffer-push-string buf "<cycle>")
|
||||
(do
|
||||
@@ -52,26 +27,26 @@
|
||||
(buffer-push-string buf " ")
|
||||
(pp seen buf v)
|
||||
(varset! k (next a k))
|
||||
(when k (buffer-push-string buf " "))
|
||||
)
|
||||
(buffer-push-string buf end)
|
||||
))
|
||||
(when k (buffer-push-string buf " ")))
|
||||
(buffer-push-string buf end)))
|
||||
buf)
|
||||
|
||||
(def _printers {
|
||||
(def printers :private {
|
||||
:array (fn [pp seen buf x] (pp-seq pp seen buf x "[" "]"))
|
||||
:tuple (fn [pp seen buf x] (pp-seq pp seen buf x "(" ")"))
|
||||
:table (fn [pp seen buf x] (pp-dict pp seen buf x "@{" "}"))
|
||||
:struct (fn [pp seen buf x] (pp-dict pp seen buf x "{" "}"))
|
||||
})
|
||||
|
||||
(defn _default_printer [pp seen buf x]
|
||||
(defn- default_printer [pp seen buf x]
|
||||
(buffer-push-string buf (string x))
|
||||
buf)
|
||||
|
||||
(defn pp1 [seen buf x]
|
||||
(def pmaybe (get _printers (type x)))
|
||||
(def p (if pmaybe pmaybe _default_printer))
|
||||
(defn- pp1 [seen buf x]
|
||||
(def pmaybe (get printers (type x)))
|
||||
(def p (if pmaybe pmaybe default_printer))
|
||||
(p pp1 seen buf x))
|
||||
|
||||
(fn [x] (print (pp1 @{} @"" x)))))
|
||||
(defn pp
|
||||
"Pretty print a value x."
|
||||
[x] (print (pp1 @{} @"" x)))
|
||||
|
||||
14
lib/primes.dst
Normal file
14
lib/primes.dst
Normal file
@@ -0,0 +1,14 @@
|
||||
# Return an array of primes. This is a trivial and extremely naive algorithm.
|
||||
|
||||
(defn primes
|
||||
"Returns a list of prime numbers less than n."
|
||||
[n]
|
||||
(def list [])
|
||||
(for [i 2 n]
|
||||
(var isprime? true)
|
||||
(def len (length list))
|
||||
(for [j 0 len]
|
||||
(def trial (get list j))
|
||||
(if (zero? (% i trial)) (varset! isprime? false)))
|
||||
(if isprime? (array-push list i)))
|
||||
list)
|
||||
Reference in New Issue
Block a user