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

(defn greet-me [] (print "hey programmer!"))
(defn greet-stranger [] (print "hey stranger!"))

(varfn greet [] (greet-me))
(greet) # prints "hey programmer!"
(varfn greet [] (greet-stranger))
(greet) # prints "hey stranger!"

# kind of analogous to (def greet (fn [] (some-thunk))), but with built-in
# sugar to automatically update 'greet' if varfn is called with a name
# already bound
varfnwrqPlayground
(string/join @["alice" "bob" "eve"] "\t")
# => "alice\tbob\teve"
string/joinsogaiuPlayground
(spit "/tmp/hello.sh" "#!/bin/bash\necho 'Hello from Bash!'\n")
(os/chmod "/tmp/hello.sh" "rwx------")
(os/setenv "PATH" (string (os/getenv "PATH") ":/tmp"))
(os/shell "hello.sh")
os/setenvcellularmitosisPlayground
(drop 1 "smile")
# => "mile"
dropsogaiuPlayground
(one? :dude) # => false
one?jgartePlayground
(take-while number? @[1 2 3 "4" 5 6 7 8 9 "10" 11 "12"]) # => (1 2 3)
take-whilejgartePlayground
(seq [v :in (coro
                (yield :hi)
                (yield :bye))]
    v)
# => @[:hi :bye]
coropepePlayground
(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
#!/usr/bin/env janet
# echo stdin to stdout.
(file/write stdout (file/read stdin :all))
file/writecellularmitosisPlayground
(empty? [])
# => true
empty?sogaiuPlayground
(os/cwd)  # => "/Users/cell/tmp"
(os/getenv "PWD")  # => "/Users/cell/tmp"
(get (os/environ) "PWD")  # => "/Users/cell/tmp"
os/cwdcellularmitosisPlayground
(dyn :pretty-format)
# => "%.20Q"
dynsogaiuPlayground
(last [1 1 2 3 5 8])
# => 8
lastsogaiuPlayground
$ # 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
Math. Seedrandom(3206)
math/seedrandomMonif2009Playground