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

# Reading a file line by line, using loop's :iterate verb, and adding the line lengths

(with [fl (file/open "filepath")]
  (var sm 0)
  (loop [line :iterate (file/read fl :line)]
    (+= sm (length line)))
   sm) 
 

file/readMikeBellerPlayground
(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)]      ]
valuescellularmitosisPlayground
(string/trim " foo ")  # => "foo"
(string/trim "_!_foo_!_" "_!")  # => "foo"
string/trimcellularmitosisPlayground
# 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
path/delim # => ":" on Unix and Linux, ";" on Windows
spork/path/delimclementiPlayground
# excess elements of the longer list are discarded
(interleave [1] 
            [2 4] 
            [3 6])
# -> @[1 2 3]
interleaveKrasjetPlayground
(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
not=cellularmitosisPlayground
(defn walker
  `Simple walker function, that prints non-sequential 
   members of the form or prints "Sequence" and walks 
   recursively sequential members of the tree.` 
  [form] 
  (if (or (indexed? form) (dictionary? form))
    (do (print "Sequence")
        (walk walker form))
    (print form)))

(walk walker [[[[0 1 3]] 16 7 [3 [3 5]] 3 4] 1 [3 4]])

# Prints
# Sequence
# Sequence
# Sequence
# 0
# 1
# 3
# 16
# 7
# Sequence
# 3
# Sequence
# 3
# 5
# 3
# 4
# 1
# Sequence
# 3
# 4
walkpepePlayground
# catch and propagate an error with fiber

(try
  (+ 1 nil)
  ([err fib]
   (propagate err fib)))
tryswlkrPlayground
(peg/find-all ':d "hi 1 bye 2") # => @[3 9]
peg/find-allpepePlayground
(array 1 2.3 :a "foo" true nil [] {} (fn []))
# => @[1 2.3 :a "foo" true nil () {} <function 0x7FB2A3F02170>]
arraycellularmitosisPlayground
(distinct [1 1 2 3])  # => @[1 2 3]

(distinct "hello")  # => @[104 101 108 111]
(string/from-bytes (splice (distinct "hello")))  # => "helo"
distinctcellularmitosisPlayground
# From the REPL, always nil
repl> (dyn *current-file*)
nil

# As an expression to the CLI, always nil
$ janet -e '(pp (dyn *current-file*))'
nil

# Given the file /tmp/janet/test.janet with the following contents:
(pp (dyn *current-file*))

# Note how the value reflects the given file path
~ $ janet /tmp/janet/test.janet
"/tmp/janet/test.janet"

/tmp/janet $ janet ./test.janet
"./test.janet"

/tmp/janet $ janet test.janet
"test.janet"

/tmp/janet $ janet ../janet/test.janet
"../janet/test.janet"

# The following are equivalent - the first is preferred.
# The key insight is that dynamic bindings are just keyword/value
# pairs in the current fiber's environment table.
(dyn *current-file*)
(dyn :current-file)
((curenv) *current-file*)
((curenv) :current-file)
*current-file*quexxonPlayground
(interpose 0 [1])
# -> @[1]
interposeKrasjetPlayground
(math/pow 10 3)   # => 1000
(math/cbrt 1000)  # => 10
math/cbrtcellularmitosisPlayground