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

(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
(first [4 5 6])  # => 4
firstcellularmitosisPlayground
(for i 0 5 
     (print i))
 
# 0
# 1
# 2
# 3
# 4

# => nil
forleobmPlayground
(map inc
     (fiber/new |(each x (range 3)
                   (yield x))))
# => @[1 2 3]
fiber/newsogaiuPlayground
(get-in  {:a {:cc 2} :b {:dd 3}}  [:a]     42)  # => {:cc 2}
(get-in  {:a {:cc 2} :b {:dd 3}}  [:a :cc] 42)  # => 2

(get-in  {:a {:cc 2} :b {:dd 3}}  [0]  42)  # => 42
(get-in  {:a {:cc 2} :b {:dd 3}}  [:z] 42)  # => 42
get-incellularmitosisPlayground
(def h ["a" "b" :c]) # => ("a" "b" :c)

(find (fn [a] (= "a" a)) h) # => "a"
findfaywongPlayground
(put (table) :a 1) # => @{:a 1}
putsogaiuPlayground
(os/clock)  # => 1.59384e+09
os/clockcellularmitosisPlayground
(peg/find ':d "Battery temperature: 40 °C")
# => 21 index of the first number
peg/findpepePlayground
(freeze @"Ho") #=> "Ho"
(freeze ["Ho"]) #=> ("Ho")
(freeze @["Ho"]) #=> ("Ho")
(freeze @{"Ho" "Ho"}) #=> {"Ho" "Ho"}
freezepepePlayground
(array/slice @[1 2 3] 0 0)  # => @[]
(array/slice @[1 2 3] 0 1)  # => @[1]
(array/slice @[1 2 3] 0 2)  # => @[1 2]
(array/slice @[1 2 3] 0 3)  # => @[1 2 3]
(array/slice @[1 2 3] 0 4)  # error: index out of range

(array/slice @[1 2 3] 1 1)  # => @[]
(array/slice @[1 2 3] 1 2)  # => @[2]

(array/slice @[1 2 3] 0 -1)  # => @[1 2 3]
(array/slice @[1 2 3] 0 -2)  # => @[1 2]
(array/slice @[1 2 3] 0 -3)  # => @[1]
(array/slice @[1 2 3] 0 -4)  # => @[]
(array/slice @[1 2 3] 0 -5)  # error: index out of range
array/slicecellularmitosisPlayground
# Match enables polymorphic functions:
(defn fac [& vars]
  (match vars
    [0] 1
    [x] (* x (fac (- x 1)))))

# from https://janet.guide/control-flow/
(defn calculate [expr]
  (match expr
    [:add x y] (+ x y)
    [:subtract x y] (- x y)
    [:multiply x y] (* x y)
    ([:divide x y] (= y 0)) (error "division by zero!")
    [:divide x y] (/ x y)))

(calculate [:subtract 5 10])

# Note, it checks prefixes, so you must order things this way:
(match [1 2]
  [x y z] "three elements"
  [x y] "two elements"
  [x] "one element"
  [] "no elements")
matchveqqqPlayground
(def f (file/temp))
(file/write f "ok stuff")
(file/seek f :set 0)
(file/read f :all)

# N.b. file/temp's file is not accessible outside of the process
file/tempveqqqPlayground
(flatten-into @[1 2 3] @[[4 5 6] @[7 8 9] @[10 11 12]]) # => @[1 2 3 4 5 6 7 8 9 10 11 12]
flatten-intojgartePlayground
(some even? [1 3 5 7 11 18])
# =>
true
somesogaiuPlayground