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

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

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

(map array?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    true      false        false         ]
indexed?cellularmitosisPlayground
# in the file ex.janet

(main [_ & args]
      (print "write something and I'll echo it")
      (def x (getline))
      (print x))

# in the terminal:
# $ janet ex.janet
# write something and I'll echo it
# bla <- you wrote this
# bla
getlineveqqqPlayground
# access tuple values

(def t [:a :b :c]) # test tuple

(first t)
# => :a

(last t)
# => :c

(slice t 0 2)
# => (:a :b)

# Index as function
(0 t)
# => :a

# Tuple as function
(t 0)
# => :a

# With destructuring
(let [[_ b _] t] 
    b)
# => :b

# With pattern matching
(match t
    [:a _ :d] (print "does not match")
    [:a b :c] (print b)
    _         (print "anything"))

# => :b
tupleleobmPlayground
(import joy)

# given a handler
(defn handle-func [request]
  (joy/text/plain (string "Hi " ((request :params) :name))))

# joy/defroutes will prepare routes
(joy/defroutes r [:get "/hi/:name" :handle-func])
# which you can pass to joy/app
(joy/server (joy/app {:routes r}) 9002)
# visit localhost:9002/hi/bob
joy/defroutesveqqqPlayground
# catch and propagate an error with fiber

(try
  (+ 1 nil)
  ([err fib]
   (propagate err fib)))
tryswlkrPlayground
# Interactively read a line from STDIN
(file/read stdin :line)

# Type abcd and then press ENTER
abcd

# Returns:
@"abcd\n"
stdinsemperosPlayground
(string/reverse "hello") # => "olleh"
string/reverseswlkrPlayground
(def f (generate [i :range [0 5]] (+ i i)))

(print (fiber/status f))
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (resume f)) 
(print (fiber/status f)) # -> :pending
(print (resume f)) 
(print (fiber/status f)) # -> :dead


# :new
# 0
# 2
# 4
# 6
# 8
# :pending
#
# :dead
generateleobmPlayground
(string/bytes "hello") # => (104 101 108 108 111)
string/bytesswlkrPlayground
(* 2 3)      # -> 6
(* 2 3.3)    # -> 6.6
(* 2.2 3.3)  # -> 7.26

(def pi 3.14159)
(* 2 pi)  # -> 6.28318

(* 2)  # -> 2
(*)    # -> 1

(* 2 3 4)             # -> 24
(apply * [2 3 4])     # -> 24
(* (splice [2 3 4]))  # -> 24
(* ;[2 3 4])          # -> 24
(def a [2 3 4])
(* ;a)                # -> 24
*cellularmitosisPlayground
(one? (math/next 1 math/inf))  # => false
math/nextcellularmitosisPlayground
# Print out the current time in Last-Modified header-compliant 
# form. (reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified)
(def weekdayMap [ "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" ])
(def monthMap ["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"])
(def dt (os/date (os/time) true))
(string/format "%s, %02d %s %04d %02d:%02d:%02d" 
     (weekdayMap (dt :week-day)) 
     (dt :month-day)
     (monthMap (dt :month))
     (dt :year)
     (dt :hours)
     (dt :minutes)
     (dt :seconds))
string/formatyumaikasPlayground
(defn f1 [s] (string "-" s "-"))
(defn f2 [s] (string "=" s "="))
(defn f3 [s] (string "#" s "#"))

(def f (juxt f1 f2 f3))

(f "x") # => ("-x-" "=x=" "#x#")
juxtuvtcPlayground
(seq [i :range [0 3]
      j :range [0 3]
      :let [c (string/format "%c" (+ 97 i))]
      :when (and (even? i) (even? j))]
  [(keyword c) j])
# => '@[(:a 0) (:a 2) (:c 0) (:c 2)]
seqsogaiuPlayground
(let [x false]
  (var y 0)
  (unless x
    (++ y)
    (++ y))
  y)
# => 2
unlesssogaiuPlayground