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

# opens a file named filename for writing, and writes Hello World!
(def f (file/open "filename" :w))
(file/write f "Hello World!")
(file/flush f)
(file/close f)
file/writeterminalcommandPlayground
(-> 1 (+ 2) (+ 3))  # -> 6
->cellularmitosisPlayground
(map bytes?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ true  true  true   true   false    false     false        false         ]

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

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

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

(map buffer?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  true   false    false     false        false         ]
keyword?cellularmitosisPlayground
(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
(= 1 1)  # => true
(= 1 2)  # => false

(= 1.1 1.1)  # => true
(= 1.1 1.2)  # => false

(= 1 1.0)  # => true

# these are representations of two different IEEE-754 64-bit buckets:
(= 
 1.0000000000000001
 1.0000000000000002)  # => false

# these are two representations of the same IEEE-754 64-bit bucket:
(= 
 1.00000000000000001
 1.00000000000000002)  # => true
=cellularmitosisPlayground
(let [a 1 b 2 c 3] (+ a b c))  # => 6

(let
  [a 1
   b (+ a 1)
   c (+ b 1)]
  (+ a b c))  # => 6
letcellularmitosisPlayground
(math/rng-uniform (math/rng 0))
# => 0.487181
math/rng-uniformsogaiuPlayground
(count even? [1 2 3 4 5])  # => 2

(count  (fn [x] (> x 3))  [1 2 3 4 5])  # => 2
(count         |(> $ 3)   [1 2 3 4 5])  # => 2

(count  (fn [x] (truthy? x))  [nil false true 42 :a "foo"])  # => 4
(count         |(truthy? $)   [nil false true 42 :a "foo"])  # => 4

(var f even?)
(count f [1 2 3 4 5])  # => 2
(set f odd?)
(count f [1 2 3 4 5])  # => 3

(map  (fn [f] (count f [1 2 3 4 5]))  [even? odd?])  # => @[2 3]
(map         |(count $ [1 2 3 4 5])   [even? odd?])  # => @[2 3]
countcellularmitosisPlayground
# janet 1.10.1

(array/remove @[1 2 3] 0)  # => @[2 3]
(array/remove @[1 2 3] 1)  # => @[1 3]
(array/remove @[1 2 3] 2)  # => @[1 2]
(array/remove @[1 2 3] 3)  # => @[1 2 3]
(array/remove @[1 2 3] 4)  # error: index out of range

(array/remove @[1 2 3] -1)  # => @[1 2 3]
(array/remove @[1 2 3] -2)  # => @[1 2]
(array/remove @[1 2 3] -3)  # => @[1 3]
(array/remove @[1 2 3] -4)  # => @[2 3]
(array/remove @[1 2 3] -5)  # error: index out of range

(array/remove @[1 2 3 4] 1 1)   # => @[1 3 4]
(array/remove @[1 2 3 4] 1 2)   # => @[1 4]
(array/remove @[1 2 3 4] 1 3)   # => @[1]
(array/remove @[1 2 3 4] 1 4)   # => @[1]
(array/remove @[1 2 3 4] 1 99)  # => @[1]
array/removecellularmitosisPlayground
(partition 2 [:a 1 :b 2 :c 3])
# => @[(:a 1) (:b 2) (:c 3)]
partitionsogaiuPlayground
(min-of [2 3 4 5])         # => 2
(min-of {:a 3 :b 5 :c 1})  # => 1
(min-of ["word" 1 nil 2])  # => 1
min-ofsnltdPlayground
(let [c0 (ev/chan)
      c1 (ev/chan 1)]
  [(ev/capacity c0) (ev/capacity c1)])
# => '(0 1)
ev/capacitysogaiuPlayground
(os/shell "uptime > /tmp/uptime.txt")  # => 0
(slurp "/tmp/uptime.txt")
# => @"22:33  up 5 days,  9:34, 15 users, load averages: 1.93 1.74 1.59\n"
(os/rm "/tmp/uptime.txt")  # => nil
os/shellcellularmitosisPlayground
(take-while number? 
            (fiber/new
              |(each x [1 2 3 :hi]
                 (yield x))))
# => @[1 2 3]
take-whilesogaiuPlayground
(os/time)  # => 1593837535
os/timecellularmitosisPlayground