JanetDocsI'm feeling luckyGithub sign in

JanetDocs is a community documentation site for the Janet programming language

Loading...

Recent examples

(eachk [k v] {:a "a val" :b "b val" :c "c val"} (print k " - " v))
# prints c - c val
# prints a - a val
# prints b - b val
eachppepe
(eachk k [1 2 3] (print k))
# prints 0
# prints 1
# prints 2
# for indexed collections indices are printed  
eachkpepe
(eachk k {:a "a val" :b "b val" :c "c val"} (print k))
# prints c
# prints a
# prints b
eachkpepe
(each item [1 2 3 4] (print item))
# prints 1
# prints 2
# prints 3
# prints 4
# => nil
eachpepe
(let [len 8
      rand-string (string/join (map |(string/format "%02x" $)
                                    (os/cryptorand len)))]
  (= (length rand-string) (* 2 len))) # => true
os/cryptorandsogaiu
(os/execute
  @("/usr/bin/bash" "-c" "set")
  :e
  @{"SOME" "value"
    "OTHER" "one"})
# => 0

# execute bash and prints environment variables
# which contains SOME=value and Other=one
os/executegoldenHairDafo
(os/execute
  @("python" "-c" "print('Hello Janet'"))
  :p) 
# => 0

# execute python -c "print('Hello Janet') while
# searching path on the current path
os/executegoldenHairDafo
(defn output [x]
  (case x
    :a "a"
    "b"))

(output :a) # => "a"
(output "anything else") # => "b"
caseswlkr
(first [4 5 6])  # => 4
firstcellularmitosis
(flatten [1 [2 3 [4]] 5])  # => @[1 2 3 4 5]
flattencellularmitosis
> (eprin "there is a boo-boo on line " 33 "\n")
there is a boo-boo on line 33
nil
eprincellularmitosis
(update @[3 4 5] 1 dec)  # => @[3 3 5]
(update (update @[3 4 5] 1 dec) 2 inc)  # => @[3 3 6]
updatecellularmitosis
(get @"A"                      0)  # => 65 (0b01000001)
(get (buffer/bit-clear @"A" 0) 0)  # => 64 (0b01000000)
(get (buffer/bit-clear @"A" 6) 0)  # =>  1 (0b00000001)
buffer/bit-clearcellularmitosis
(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
compcellularmitosis
(any? [false false nil]) => nil
(any? [false false nil 1]) => 1
(any? [false false nil true]) => true
any?swlkr
(buffer/bit (buffer/new-filled 1 (chr "0")) 4)
# => true
buffer/bitsogaiu
(buffer/bit (buffer/new-filled 1 48) 4)
# => true
buffer/bitsogaiu
(buffer/bit @"0" 4)
# => true
buffer/bitsogaiu
(buffer/bit @"1" 0)
# => true
buffer/bitsogaiu
(buffer/format @"0 - 1 = " "%d" -1)
# => @"0 - 1 = -1"
buffer/formatsogaiu
(put-in  @{:a @{:aa 5} :b @{:bb 6}}  [:c]          9)  # => @{:a @{:aa 5} :b @{:bb 6} :c 9}
(put-in  @{:a @{:aa 5} :b @{:bb 6}}  [:a :aa]      9)  # => @{:a @{:aa 9} :b @{:bb 6}}
(put-in  @{:a @{:aa 5} :b @{:bb 6}}  [:a :cc :ddd] 9)  # => @{:a @{:cc @{:ddd 9} :aa 5} :b @{:bb 6}}

(put-in  @[:a :b @[:aa :bb :cc]]  [2 1] :zz )  # => @[:a :b @[:aa :zz :cc]]
(put-in  @[:a :b @[:aa :bb :cc]]  [5]   :z  )  # => @[:a :b @[:aa :bb :cc] nil nil :z]

(put-in  @[:a :b @[:aa :bb :cc]]  [4 0]   :zz )  # => @[:a :b @[:aa :bb :cc] nil @{0 :zz}]
(put-in  @[:a :b @[:aa :bb :cc]]  [4 :yy] :zz )  # => @[:a :b @[:aa :bb :cc] nil @{:yy :zz}]

put-incellularmitosis
(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"
putcellularmitosis
(next  [4 5 6]    )  # => 0
(next  [4 5 6]  0 )  # => 1
(next  [4 5 6]  1 )  # => 2
(next  [4 5 6]  2 )  # => nil

# note that dictionary keys are not necessarily in the same order
# as the corresponding literal.
(next  {:a 5 :b 6 :c 7}     )  # => :a
(next  {:a 5 :b 6 :c 7}  :a )  # => :c
(next  {:a 5 :b 6 :c 7}  :c )  # => :b
(next  {:a 5 :b 6 :c 7}  :b )  # => nil
nextcellularmitosis
(or true)        # => true
(or true true)   # => true
(or true false)  # => true

(or false 1 2)  # => 1
(or false 2 1)  # => 2

(or false nil)  # => nil
(or nil false)  # => false

# note that `or` does not behave as you might expect
# when used with `apply` and `splice`:
(or 1 2 3)             # => 1
(or (splice [1 2 3]))  # => (1 2 3)
(apply or [1 2 3])     # => (if 1 1 (if 2 2 3))
orcellularmitosis
(map false? [ false nil   true  0     1     42    'a    :a    "a"   [97]  {:a 42} (fn []) ])
# =>       @[ true  false false false false false false false false false false   false   ]
false?cellularmitosis
(map true? [ true nil   false 0     1     42    'a    :a    "a"   [97]  {:a 42} (fn []) ])
# =>      @[ true false false false false false false false false false false   false   ]
true?cellularmitosis
(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         ]
string?cellularmitosis
(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?cellularmitosis
(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         ]
symbol?cellularmitosis
(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)]      ]
pairscellularmitosis