[go: up one dir, main page]

JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!

Loading...

Random Examples

# https://en.wikipedia.org/wiki/Error_function
(math/erf 1)
# => 0.842701
math/erfsogaiuPlayground
(freeze @"Ho") #=> "Ho"
(freeze ["Ho"]) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepePlayground
(eval-string "(+ 1 2 3 4)") # -> 10
(eval-string ")") # -> parse error
(eval-string "(bloop)") # -> compile error
(eval-string "(+ nil nil)") # -> runtime error
eval-stringswlkrPlayground
(disasm (fn []) :bytecode)
# => @[(retn)]
disasmsogaiuPlayground
(not=        [1 1]   [1 1])  # => false
(not=        [1 1]   [2 3])  # => true
(not=        [1 1]  @[1 1])  # => true
(not=        [1 1]  @[2 3])  # => true
(not=       @[1 1]  @[1 1])  # => true
(not=       @[1 1]  @[2 3])  # => true

(deep-not=   [1 1]   [1 1])  # => nil
(deep-not=   [1 1]   [2 3])  # => true
(deep-not=   [1 1]  @[1 1])  # => true
(deep-not=   [1 1]  @[2 3])  # => true
(deep-not=  @[1 1]  @[1 1])  # => nil
(deep-not=  @[1 1]  @[2 3])  # => true
deep-not=cellularmitosisPlayground
(defn return-nil [x] nil)
(-?> 1 inc)                     # -> 2
(-?> 1 inc inc)                 # -> 3
(-?> 1 inc inc return-nil)      # -> nil
(-?> 1 inc inc return-nil inc)  # -> nil
-?>cellularmitosisPlayground
$ # trivial server which echo's to the console.
$ cat > srv.janet << EOF
#!/usr/bin/env janet

(defn handle-conn [conn]
  (print "new connection")
  (while true
    (def data (net/read conn 4096))
    (if (not data) (break))
    (prin data))
  (net/close conn)
  (print "connection closed"))

(print "starting server on 0.0.0.0:1234")
(net/server "0.0.0.0" 1234 handle-conn)
EOF
$ chmod +x srv.janet
$ ./srv.janet

----

$ # in another terminal:
$ echo hello | nc 0.0.0.0 1234
net/servercellularmitosisPlayground
(seq [v :in (coro
                (yield :hi)
                (yield :bye))]
    v)
# => @[:hi :bye]
coropepePlayground
(when-let [root   (math/sqrt 64) 
           unused (even? root)] 
         (printf "%d is even" root))
# -> "8 is even"
when-letfelixrPlayground
# When the :doc-color dynamic binding referenced by *doc-color* is truthy,
# the doc-format function replaces a minimal subset of Markdown markup with
# the corresponding ANSI escape codes.
#
# The following markup is supported:
# - *this will be underlined*
# - **this will be bold**
# - `(+ 1 2 3)` <- backticks for code
#
# You may be surprised by *underline* since the same markup is used to
# indicate italics in Markdown. This is likely a tradeoff for compatibility;
# historically, the italic attribute has not been widely supported by
# terminal emulators.
#
# The best way to see the effect of *doc-color* is try the following examples
# in the Janet REPL.

# By default, *doc-color* is enabled.
(print (doc-format "*underline*. **bold**. `(code)`."))

# Set the dynamic binding to a falsy value to disable doc-format's ANSI
# escape code substition.
(with-dyns [*doc-color* false]
  (print (doc-format "*underline*. **bold**. `(code)`.")))

# N.B.: At the time of writing, no docstrings in the core API take advantage of
# the bold or underline markup As a result, you may not see any difference in
# the doc formatting if your terminal theme uses the same hue for white and
# bright white (a few terminals that I tested on Linux make no distinction
# between the two colors in their default configuration).
*doc-color*quexxonPlayground
(buffer/bit @"1" 0)
# => true
buffer/bitsogaiuPlayground
(defn eval-string
  ``Evaluates a string in the current environment. If more control over the
  environment is needed, use `run-context`.``
  [str]
  (var state (string str))
  (defn chunks [buf _]
    (def ret state)
    (set state nil)
    (when ret
      (buffer/push-string buf str)
      (buffer/push-string buf "\n")))
  (var returnval nil)
  (run-context {:chunks chunks
                :on-compile-error (fn compile-error [msg errf &]
                                    (error (string "compile error: " msg)))
                :on-parse-error (fn parse-error [p x]
                                  (error (string "parse error: " (:error p))))
                :fiber-flags :i
                :on-status (fn on-status [f val]
                             (if-not (= (fiber/status f) :dead)
                               (error val))
                             (set returnval val))
                :source :eval-string})
  returnval)
run-contexttionisPlayground
(and true true)   # => true
(and true false)  # => false

(and true false nil)  # => false
(and true nil false)  # => nil

(and)       # => true
(and true)  # => true

(and 1)                  # => 1
(and 1 "hello")          # => "hello"
(and [false] 1 "world")  # => "world"

# note that `and` does not behave as you might expect
# when used with `apply` and `splice`:
(and 1 2 3)                 # => 3
(and (splice [1 2 3]))      # => (1 2 3)
(apply and [1 2 3])         # => (if 1 (if 2 3 2) 1)
(eval (apply and [1 2 3]))  # => 3

# if you need an `and` which you can feed to `apply`,
# you can use a reduce-based implementation:
(defn and2 [& xs]
  (reduce
    (fn [a b] (if (not a) a b))
    true xs))

# however, note that `and2` does not implement short-circuiting:
# janet:23:> (and false (do (print "hello") true))
# false
# janet:24:> (and2 false (do (print "hello") true))
# hello
# false

# alternatively, you can use `all` for this case.
(all truthy?  [true 0 1     'a :a "a" [] {}])  # => true
(all truthy?  [true 0 nil   'a :a "a" [] {}])  # => nil
(all truthy?  [true 0 false 'a :a "a" [] {}])  # => false
(apply (partial all truthy?) [[true 0 1 'a :a "a" [] {}]])  # => true
andcellularmitosisPlayground
(pp (all-bindings))
# => prints @[% %= * ... yield zero? zipcoll]

(def a "A")
(pp (all-bindings (curenv) true))
# => prints @[_ a] - only local bindings are listed
all-bindingspepePlayground
(defn slurp-lines [path]
  (string/split "\n" (slurp path)))
string/splitcellularmitosisPlayground