Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!
(let (sinlu (array/new 10 )) # 10 element look up table.
(for i 0 10 (put sinlu i (math/sin (* math/pi (/ i 10 )))))
(pp sinlu ))(type (fiber/new (fn [])))
# =>
:fiber (def [pipe-r pipe-w ] (os/pipe ))
(:write pipe-w "hello" )
(:read pipe-r 5 )
# => @"hello"
# send the string "hello" into the writable stream
# part and read it back from the readable one
(def chan (ev/chan ))
(def f (ev/go (coro (ev/give-supervisor :msg "Hello" )) nil chan ))
(pp (ev/take chan )) # => (:msg "Hello")
(var x 1 )
(loop [n :range-to [1 5 ]]
(*= x n ))
x # => 120 (def a-tuple-1 [:number 10 ])
(def a-tuple-2 [:string "Hello there!" ])
(defn show [what ]
(match what
[:number n ] (printf "It's a number! %d" n )
[:string s ] (printf "Here's a string: %s" s )
_ (printf "I dunno what this is: %q" what )))
(show a-tuple-1 )
(show a-tuple-2 )
(show [:array @[]])(interleave [:a :b :c ] [1 2 3 ])
# => @[:a 1 :b 2 :c 3]
(interleave [:a :b :c ] (range 3 ))
# => @[:a 0 :b 1 :c 2]
(interleave [:a :b :c ] (range 2 ))
# => @[:a 0 :b 1]
(struct ;(interleave [:a :b :c ] [1 2 3 ]))
# {:c 3 :a 1 :b 2}
(table ;(interleave [:a :b :c ] [1 2 3 ]))
# @{:c 3 :a 1 :b 2} # This turns the current REPL session (environment) with all bindings, including synced closures,
# into a loadable or compilable image.
# It is the same as e.g.: `janet -c test.janet test.jimage` after (spit "test.janet" (currenv))
(spit "test.jimage" (make-image (curenv )))(string/format "%f" (os/clock )) # => "1627746483.991000" (map hash [nil true false -1 0 3.14 :a "foo" [1 2 3 ] {:a 1 } (fn [])])
# => @[0 1 0 -1074790400 0 1244950043 -2107123988 -1254832049 -2021739803 -1395744911 53378043] (defmacro timeit [& body ]
# generate unique symbols to use in the macro so they can't conflict with anything used in `body`
(with-syms [$t0 $t1 ]
~(do
(def $t0 (os/clock :monotonic :double ))
(do ,;body )
(def $t1 (os/clock :monotonic :double ))
(- $t1 $t0 ))))
(def time-taken (timeit (os/sleep 0.5 )))
(printf "Took %.3f seconds" time-taken )
(os/cryptorand 1 ) # => @"\n"
(os/cryptorand 1 ) # => @"<"
(os/cryptorand 1 ) # => @"\xC0"
(os/cryptorand 1 ) # => @"\x89"
(os/cryptorand 8 ) # => @"\x87\x13\x99\x95\x10su\e"
(slice [:a :b :c :d ]) # => (:a :b :c :d)
(slice [:a :b :c :d ] 0 ) # => (:a :b :c :d)
(slice [:a :b :c :d ] 1 ) # => (:b :c :d)
(slice [:a :b :c :d ] 3 ) # => (:d)
(slice [:a :b :c :d ] 4 ) # => ()
(slice [:a :b :c :d ] 5 ) # error: index out of range
(slice [:a :b :c :d ] -1 ) # => ()
(slice [:a :b :c :d ] -2 ) # => (:d)
(slice [:a :b :c :d ] -4 ) # => (:b :c :d)
(slice [:a :b :c :d ] -5 ) # => (:a :b :c :d)
(slice [:a :b :c :d ] -6 ) # error: index out of range
(slice [:a :b :c :d ] 0 0 ) # => ()
(slice [:a :b :c :d ] 0 1 ) # => (:a)
(slice [:a :b :c :d ] 0 4 ) # => (:a :b :c :d)
(slice [:a :b :c :d ] -1 -1 ) # => ()
(slice [:a :b :c :d ] -2 -1 ) # => (:d)
(slice [:a :b :c :d ] -5 -1 ) # => (:a :b :c :d)
(slice [:a :b :c :d ] 1 0 ) # => ()
(slice [:a :b :c :d ] 4 0 ) # => ()
(slice [:a :b :c :d ] -1 -2 ) # => ()
(slice [:a :b :c :d ] -1 -5 ) # => ()
(var x 12 ) # => 12
(/= x 2 ) # => 6 (defn ls-sockets
"List the sockets in the current directory."
[]
(filter
(fn [fname ] (= :socket (os/stat fname :mode )))
(os/dir "." )))
path/delim # => ":" on Unix and Linux, ";" on Windows #!/usr/bin/env janet
# echo stdin to stdout.
(file/write stdout (file/read stdin :all ))
## Quadratic Formula
(defn qform
"Use the quadratic formula to solve for x. Returns all real solutions."
[a b c ]
(def det (- (* b b ) (* 4 a c )))
(def factor (/ 0.5 a ))
(cond
(neg? det ) []
(zero? det ) [(* factor (- b ))]
(let [root-det (math/sqrt det )]
[(* factor (- (- b ) root-det )) (* factor (+ (- b ) root-det ))])))
(qform 1 4 3 ) # -> (-3 -1)
# on a Core i5-4590:
(os/arch ) # => :x64
# on an Intel Atom N270:
(os/arch ) # => :x86
# on a raspberry pi:
(os/arch ) # => :arm # Find all bindings (in the current environment) with "push" in the name.
(doc "push" )
# Bindings:
#
# array/push buffer/push buffer/push-byte buffer/push-string
# buffer/push-word
#
# Dynamics:
#
#
#
# Use (doc sym) for more information on a binding.