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

# Reading a file line by line, using loop's :iterate verb, and adding the line lengths

(with [fl (file/open "filepath")]
  (var sm 0)
  (loop [line :iterate (file/read fl :line)]
    (+= sm (length line)))
   sm) 
 

file/readMikeBellerPlayground
(zipcoll [:a :b :c] [1 2 3])  #=> @{:c 3 :a 1 :b 2}
zipcolluvtcPlayground
(defn ddup [ds ks val]
  (update-in ds ks
    (fn [x]
      (if (= nil x)
        @[val]
        (array/push x val)))))

(var a @{})
(ddup a [:a] 1)
(ddup a [:a] 2)
(ddup a [:a] 3)
# @{:a @[1 2 3]}

update-insbjaverPlayground
(def conn-chan (ev/thread-chan 1000))

(defn producer [no]
  (forever
    (ev/sleep 5)
    (print "Adding data from producer num:" no)
    (ev/give conn-chan (math/random))))

(defn consumer [no]
  (forever
    (ev/sleep 0.5)
    (def num (ev/take conn-chan))
    (print num ": Printing from consumer:" no)))

(defn main [& args]
  (ev/spawn-thread (producer 1))
  (ev/spawn-thread (consumer 1))
  (ev/spawn-thread (consumer 2))
  (ev/sleep 20)
  (print "exiting"))
ev/thread-chanGeo-7Playground
(- 1)
# => -1
-sogaiuPlayground
(def s (symbol/slice :hello 2 4))

(print (type s)) # symbol
(print s)        # ll
symbol/slicewrqPlayground
(reduce     + 1 [2 3 4])  # -> 10
(accumulate + 1 [2 3 4])  # -> @[3 6 10]
accumulatecellularmitosisPlayground
(os/shell "touch foo")
(os/stat "foo" :modified)  # => 1593836002
(os/touch "foo")
(os/stat "foo" :modified)  # => 1593836013
os/touchcellularmitosisPlayground
(math/acos 0.3)
1.2661036727795
math/acosbtbytesPlayground
(<= 1 2 3) # => true
(<= 1 2 1) # => false
<=pepePlayground
(keyword :)
# => :
keywordsogaiuPlayground
(map number? [nil   true  42   :a    "a"   []    {}    (fn []) ])
# =>        @[false false true false false false false false   ]
number?cellularmitosisPlayground
(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)]      ]
pairscellularmitosisPlayground
# 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
(defn square [x] (* x x))
(defn square-then-dec [x] ((comp dec square) x))
(defn dec-then-square [x] ((comp square dec) x))
(square-then-dec 3)  # => 8
(dec-then-square 3)  # => 4
compcellularmitosisPlayground