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  |($ {: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
(>= 4 2)
# => true 

# multiple values
(>= 4 4 3 2 1 1)
# => true 

# with array in apply call
(apply >= [4 4 3 2 1 1])
# => true 

# with array in splice form
(>= ;[4 4 3 2 1 1])
# => true 
>=leobmPlayground
(map tuple ["a" "b" "c" "d"] [1 2 3 4] "abcd")

# => @[("a" 1 97) ("b" 2 98) ("c" 3 99) ("d" 4 100)]
mapfelixrPlayground
(map function? [ even?  (fn [])  |($)  file/read  ->   ])
# =>          @[ true   true     true  false      true ]
function?cellularmitosisPlayground
(buffer/bit @"1" 0)
# => true
buffer/bitsogaiuPlayground
(map dictionary? [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         true          ]

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

(map table?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     false        true          ]
dictionary?cellularmitosisPlayground
(math/log (* math/e math/e))  # => 2
(math/log2 256)               # => 8
(math/log10 1000)             # => 3
math/log2cellularmitosisPlayground
(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
# Due to: https://github.com/swlkr/janetdocs/issues/50

(defn parse-date
  "Parses YYYY-MM-DD date format to time"
  [date]
  (let [[year month day] (map scan-number (string/split "-" date))]
    (os/mktime {:year year :month (dec month) :month-day (dec day)})))

(print (parse-date "2021-01-01")) # 1609459200
(print (os/strftime "%c" (parse-date "2021-01-01"))) # Fri Jan  1 00:00:00 2021
os/mktimeveqqqPlayground
(update-in @{:a 1} [:a] (fn [x] (+ 1 x)))
# @{:a 2}
update-insbjaverPlayground
# 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
(string/slice "hello")    # => "hello"

(string/slice "hello" 0)  # => "hello"
(string/slice "hello" 1)  # => "ello"
(string/slice "hello" 4)  # => "o"
(string/slice "hello" 5)  # => ""

(string/slice "hello" -1)  # => ""
(string/slice "hello" -2)  # => "o"
(string/slice "hello" -5)  # => "ello"
(string/slice "hello" -6)  # => "hello"

(string/slice "hello" 1 1)  # => ""
(string/slice "hello" 1 2)  # => "e"
(string/slice "hello" 1 5)  # => "ello"

(string/slice "hello" -5 -4)  # => "e"
(string/slice "hello" -6 -4)  # => "he"

(string/slice "hello"  2  1)  # => ""
(string/slice "hello" -1 -2)  # => ""
string/slicecellularmitosisPlayground
[1 2 3]                  # => (1 2 3)
(tuple 1 2 3)            # => (1 2 3)
(tuple (splice [1 2 3])  # => (1 2 3)
(tuple ;[1 2 3])         # => (1 2 3)
tuplecellularmitosisPlayground
(peg/find-all ':d "Battery temperature: 40 °C")

# => @[21 22] indices containing digits

# :d (range "09")

# peg grammars are contained in a table @{} (mutable) located in boot.janet source file.

peg/find-alljgartePlayground
# many terminals will truncate long data, use this if you need to see/copypaste it:
(defn full-print `Print a large DS without truncation` [ds] (print (string/format "%m" ds)))
ppveqqqPlayground