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

# Since table/clone produces a shallow copy and composing freeze and thaw makes everything mutable/immutable
(defn deep-clone [ds]
  (prewalk
    |(cond
       (table? $) (table/clone $)
       (array? $) (array/slice $)
       (buffer? $) (buffer/slice $)
       $)
  ds))
# CalebF wrote this: https://janet.zulipchat.com/#narrow/channel/409517-help/topic/table.2Fclone.20doesn.27t.20make.20a.20deep.20copy.3F/near/617675442
prewalkveqqqPlayground
(string/split "," "bruce,scott,tiger,woods")
# => @["bruce" "scott" "tiger" "woods"]
string/splitsogaiuPlayground
(map int?  [ nil   true  0    1    3.14   (math/trunc 3.14)  (band 1.1 2.2) ])
# =>      @[ false false true true false  true               true           ]
int?cellularmitosisPlayground
(defn inc [x] (+ x 1))
(defn inv [x] (* x -1))
(defn sq  [x] (* x x))

(-> 2 inc)             # -> 3
(-> 2 inc inc)         # -> 4
(-> 2 inc inc inv)     # -> -4
(-> 2 inc inc inv sq)  # -> 16
->cellularmitosisPlayground
(string? "hello")   # => true
(string? @"hello")  # => false
(string? :hello)    # => false
string?cellularmitosisPlayground
[1 2 3]                  # => (1 2 3)
(tuple 1 2 3)            # => (1 2 3)
(tuple (splice [1 2 3])  # => (1 2 3)
(tuple ;[1 2 3])         # => (1 2 3)
tuplecellularmitosisPlayground
(defmacro inner [x] ~(do [,x (* ,x ,x)]))
(defmacro outer [n] (map |~(inner ,$0) (range n)))

# Hints:
#
# * Quote the argument.
# * Because it's quoted, the argument can have undefined symbols.
# * Compare the result of `macex` with `macex1`.
# * If needed, print the result with `pp`.
#
(macex '(outer 10))
macex4kbytePlayground
# note, if running a server from the repl, you need to (quit) your repl.

# in a terminal:
# $ while true; do date | nc 0.0.0.0 1234 -w 1; sleep 1; done

# in a janet repl:
(net/server "0.0.0.0" 1234
  (fn [conn]
    (prin (net/read conn 4096))
    (net/close conn)))
# output doesn't actually start until you (quit) your repl's fiber:
(quit)
net/servercellularmitosisPlayground
(/ 0)
# => inf
/sogaiuPlayground
# 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.
spitcellularmitosisPlayground
(map  |($ {:a 7 :b 8} )   [  keys      values    kvs             pairs                 ])
# =>                     @[  @[:a :b]  @[7 8]    @[:a 7 :b 8]    @[(:a 7) (:b 8)]      ]

(map  |($ [4 5 6] )       [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1 2]  @[4 5 6]  @[0 4 1 5 2 6]  @[(0 4) (1 5) (2 6)]  ]

(map  |($ 'ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ :ab )           [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]

(map  |($ "ab" )          [  keys      values    kvs             pairs                 ])
# =>                     @[  @[0 1]    @[97 98]  @[0 97 1 98]    @[(0 97) (1 98)]      ]
keyscellularmitosisPlayground
(map idempotent?  [ nil  true 97   'a   :a   "a"  ])
# =>             @[ true true true true true true ]

(map idempotent?  [  [97]   @[97]  {0 97}  @{0 97}  ])
# =>             @[  false  false  false   false    ]

(map idempotent?  [  (fn [])  even?  loop  file/open  ])
# =>             @[  true     true   true  true       ]

(map idempotent?  [  (file/temp)  (fiber/new (fn []))  ])
# =>             @[  true         true                 ]
idempotent?cellularmitosisPlayground
(map bytes?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ true  true  true   true   false    false     false        false         ]

(map symbol?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ true  false false  false  false    false     false        false         ]

(map keyword?    [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false true  false  false  false    false     false        false         ]

(map string?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false true   false  false    false     false        false         ]

(map buffer?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  true   false    false     false        false         ]
string?cellularmitosisPlayground
(math/pow 10 3)   # => 1000
(math/cbrt 1000)  # => 10
math/cbrtcellularmitosisPlayground
(if-let [x true 
         y (not x)]
  :a
  :b)
# => :b
if-letsogaiuPlayground