1
0
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:
Calvin Rose
2018-03-13 16:40:56 -04:00
parent 2b1dd79f55
commit 9cb7c92ca7
7 changed files with 83 additions and 54 deletions

13
lib/fizzbuzz.dst Normal file
View 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
View File

@@ -0,0 +1,3 @@
# Prints hello
(print "hello, world!")

View File

@@ -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
View 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)