Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!
(range 10000 ) # @[0 1 2 ... 9997 9998 9999]
# By default, pp truncates. You can change its behavior:
(setdyn :pretty-format "%j" ) # jdn format
(setdyn :pretty-format "%m" ) # full printing without truncation
# For a list of all print formats: https://janet-lang.org/capi/writing-c-functions.html#Panicking
(range 10000 ) # Prints all of them according to your dyn
(map math/abs [-2.9 -2.1 2.1 2.9 ]) # => @[ 2.9 2.1 2.1 2.9 ]
(map math/floor [-2.9 -2.1 2.1 2.9 ]) # => @[ -3 -3 2 2 ]
(map math/ceil [-2.9 -2.1 2.1 2.9 ]) # => @[ -2 -2 3 3 ]
(map math/round [-2.9 -2.1 2.1 2.9 ]) # => @[ -3 -2 2 3 ]
(map math/trunc [-2.9 -2.1 2.1 2.9 ]) # => @[ -2 -2 2 2 ]
(peg/find ':a "Battery temperature: 40 °C" )
# => 0 index of the first upper/lower case character
# :a (range "az" "AZ") # janet 1.10.1
(array/remove @[1 2 3 ] 0 ) # => @[2 3]
(array/remove @[1 2 3 ] 1 ) # => @[1 3]
(array/remove @[1 2 3 ] 2 ) # => @[1 2]
(array/remove @[1 2 3 ] 3 ) # => @[1 2 3]
(array/remove @[1 2 3 ] 4 ) # error: index out of range
(array/remove @[1 2 3 ] -1 ) # => @[1 2 3]
(array/remove @[1 2 3 ] -2 ) # => @[1 2]
(array/remove @[1 2 3 ] -3 ) # => @[1 3]
(array/remove @[1 2 3 ] -4 ) # => @[2 3]
(array/remove @[1 2 3 ] -5 ) # error: index out of range
(array/remove @[1 2 3 4 ] 1 1 ) # => @[1 3 4]
(array/remove @[1 2 3 4 ] 1 2 ) # => @[1 4]
(array/remove @[1 2 3 4 ] 1 3 ) # => @[1]
(array/remove @[1 2 3 4 ] 1 4 ) # => @[1]
(array/remove @[1 2 3 4 ] 1 99 ) # => @[1]
# Janet v1.42.0 -- See %src/include/janet.h.
(defn set? [position ] (["not set" "set" ] (band janet/config-bits position )))
(printf ```
Configurations:
* NANBOX %s.
* SINGLE_THREADED %s.
* NANBOX_64_POINTER_SHIFT %s.``` (set? 0x1 ) (set? 0x2 ) (set? 0x4 ))
# janet 1.10.1
# note that os/cd does not appear to update PWD in the shell's environment.
(os/cwd ) # => "/Users/cell/tmp"
(os/getenv "PWD" ) # => "/Users/cell/tmp"
(os/cd "/tmp" ) # => nil
(os/cwd ) # => "/private/tmp" (on Apple, /tmp is actually /private/tmp)
(os/getenv "PWD" ) # => "/Users/cell/tmp"
(get default-peg-grammar :h )
# => '(range "09" "af" "AF") (import joy )
# given a handler
(defn handle-func [request ]
(joy/text/plain (string "Hi " ((request :params ) :name ))))
# joy/routes will make handlers
(def r (joy/routes [:get "/hi/:name" :handle-func ]))
# which you can pass to joy/app
(joy/server (joy/app {:routes r }) 9002 )
# visit at localhost:9002/hi/bob (peg/replace-all '(set "aiou" ) "e" "The quick brown fox jumps over the lazy dog" )
# returns The qeeck brewn fex jemps ever the lezy deg (var x 1 )
(+= x 2 3 4 )
# x = 10 (loop [x :range [1 10 ]
:let [square (* x x )]
:until (> square 9 )
:before (print "before" )
:after (print "after" )
:repeat 2 ]
(print (string "square: " square )))
# before
# square: 1
# square: 1
# after
# before
# square: 4
# square: 4
# after
# before
# square: 9
# square: 9
# after
You can see janet datastructure values by typing:
(string/format "%q" {:a 1 :b 2 }) # => {:a 1 :b 2} (string/find "needle"
"hay hay hay needle hay" )
# => 12 (when (def x 10 )
(string "Got value: " x ))
# "Got value: 10"
(when (def x nil )
(string "Got value: " x ))
# nil # nested iteration
(loop [a :in [100 200 300 ]
b :in [1 2 3 ]]
(print (+ a b )))
# 101
# 102
# 103
# 201
# 202
# 203
# 301
# 302
# 303 (import spork/misc )
(def d (misc/capout (doc misc/capout )))
d # returns:
@"\n\n macro\n /usr/local/lib/janet/spork/misc.janet on
line 340, column 1\n\n \e[97m(capout & body)\e[39m\n\n
Captures the standard output of the variadic \e[97mbody\e[39m
and returns it as \n a buffer.\n\n\n"
(reduce2 + [1 2 3 ]) # -> 6
(accumulate2 + [1 2 3 ]) # -> @[1 3 6] (math/floor 1.1 ) # => 1
(map math/floor [1.1 1.2 1.3 ]) # => @[1 1 1]
# foo.txt is a file with contents "hello\nworld\n".
(slurp "foo.txt" ) # => @"hello\nworld\n"
(string/split "\n" (slurp "foo.txt" )) # => @["hello" "world" ""]
(defn slurp-lines [path ]
(string/split "\n" (slurp path )))
(slurp-lines "foo.txt" ) # => @["hello" "world" ""]
(string/join @["hello" "world" "" ] "\n" ) # => "hello\nworld\n"
(spit "foo2.txt" (string/join @["hello" "world" "" ] "\n" ))
# The contents of foo.txt and foo2.txt are now identical.
(defn spit-lines [path lines ]
(spit path (string/join lines "\n" )))
(spit-lines "foo3.txt" (slurp-lines "foo.txt" ))
# The contents of foo.txt and foo3.txt are now identical.
(filter (fn [x ] (> x 2 )) [1 2 3 4 5 ]) # @[3 4 5]