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

(drop 1 [1 1 2 3 5 8])
# => '(1 2 3 5 8)
dropsogaiuPlayground
(in  [10 11 12 13]  0   42 )  # => 10
(in  {:a 10 :b 20}  :a  42 )  # => 10

(in  [10 11 12]  99 42 )  # error
(in  [10 11 12]  -1 42 )  # error
(in  [10 11 12]  -2 42 )  # error
(in  {:a 1}      :z 42 )  # => 42

(map  (fn [x] (in x 0 42))   [  'a  :a  "a"  [97]  @[97]  {0 97}  @{0 97}  {:a 1} ])
# =>                        @[  97  97  97   97    97     97      97       42     ]
incellularmitosisPlayground
Math. Seedrandom(3206)
math/seedrandomMonif2009Playground
(keyword/slice "some crazy keyword" 11 -1)
# returns :keyword 
keyword/slicepepePlayground
(os/date)
# => {:month 6 :dst false :year-day 185 :seconds 38 :minutes 44 :week-day 6 :year 2020 :hours 4 :month-day 3}
os/datecellularmitosisPlayground
(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
(group-by 
  (fn [i] (i :label)) 
  [{:label 'A :value 4} {:label 'A :value 3} {:label 'B :value 5}])
# @{A @[{:label A :value 4} {:label A :value 3}] B @[{:label B :value 5}]}
group-byaquilaxPlayground
(os/time)  # => 1593838384
(os/date)  # => {:month 6 :dst false :year-day 185 :seconds 8 :minutes 53 :week-day 6 :year 2020 :hours 4 :month-day 3}
(os/mktime (os/date))  # => 1593838390
os/mktimecellularmitosisPlayground
(keyword)
# => :
keywordsogaiuPlayground
(from-pairs [[:hello "world"] [:foo "bar"]]) 

# => @{:foo "bar" :hello "world"}
from-pairsjgartePlayground
(put  @{:a 4 :b 5}  :c 6         )  # => @{:a 4 :b 5 :c 6}
(put  @{:a 4 :b 5}  :b nil       )  # => @{:a 4}
(put  @{:a 4 :b 5}  :z nil       )  # => @{:a 4 :b 5}

(put  @[:a :b :c]   0  :z        )  # => @[:z :b :c]
(put  @[:a :b :c]   1  nil       )  # => @[:a nil :c]

(put  @[:a :b :c]   5  :d        )  # => @[:a :b :c nil nil :d]

(put  @"hello"      0  "z"       )  # error: can only put integers in buffers
(defn ord [ch] (first (string/bytes ch)))
(ord "z")  # => 122
(put  @"hello"      0  122       )  # => @"zello"
(put  @"hello"      0  (ord "z") )  # => @"zello"
(put  @"hello"      8  (ord "y") )  # => @"hello\0\0\0y"
putcellularmitosisPlayground
(scan-number "111" 2)
# => 7
scan-numbersogaiuPlayground
(array/pop (range 12)) # => 11
array/popjgartePlayground
(def error-levels {:red 3 :orange 2 :yellow 1})

# Create a new prototype object called ErrorProto with one method, :compare
(def ErrorProto
  @{:level nil

    # Returns -1, 0, 1 for x < y, x = y, x > y respectively.
    :compare (fn [self other]
               (let [lx (error-levels (self :level))
                     ly (error-levels (other :level))]
                 (cond
                   (< lx ly) -1
                   (> lx ly) 1
                   :else 0)))})

# Factory function to create new Error objects 
(defn make-error
  [level]
  (table/setproto @{:level level} ErrorProto))


(def err-red (make-error :red))
(def err-yell (make-error :yellow))
(def err-orange (make-error :orange))

# calls of the polymorphic compare function
(compare err-red err-orange) # 1
(compare err-orange err-red) # -1
(compare err-red err-red)    # 0

# These following functions call internally 
# the polymorphic compare function, but return a boolean value
(compare> err-red err-orange)  # true
(compare> err-yell err-orange) # false
(compare= err-yell err-yell)   # true

#-------------------------------------------------------------------------------
# sort the objects with compare> and compare<

(def errors-unsorted @[err-red err-yell err-orange])

# ascending order
(sort errors-unsorted compare<) 
# => @[@{:level :yellow} @{:level :orange} @{:level :red}]

# descending order
(sort errors-unsorted compare>) 
# => @[@{:level :red} @{:level :orange} @{:level :yellow}]

# without compare
(sort-by |(error-levels ($ :level)) errors-unsorted)
# => @[@{:level :yellow} @{:level :orange} @{:level :red}]

# Note!!!, the following does not work as expected. 
# sort alone does not automatically use the compare function (the comparator)
(sort errors-unsorted) # result is not sorted!
compareleobmPlayground
# Flatten single level of nested collections

(def foo [ [[1 2] [3 4]]   
           [[5 6] [7 8]] ])

(apply array/concat @[] foo) # => @[(1 2) (3 4) (5 6) (7 8)]
array/concatmaradPlayground