JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!

Loading...

Random Examples

(reduce2     + [1 2 3])  # -> 6
(accumulate2 + [1 2 3])  # -> @[1 3 6]
accumulate2cellularmitosisPlayground
(zipcoll [:a :b :c] [1 2 3])  #=> @{:c 3 :a 1 :b 2}
zipcolluvtcPlayground
(let [a 1]
 (let [b a]
  b)) # => 1
letjgartePlayground
(def record @{:recipient @{:name "Bob" :age 60} :sender @{:name "Alice" :age 21}})

(put-in
  record
  [:sender :age] 
  26)

# @{:recipient @{:name "Bob" :age 60} :sender @{:name "Alice" :age 26}}

(put-in
  record
  [:recipient] 
  @{:name "Carl"})

# @{:recipient @{:name "Carl"} :sender @{:name "Alice" :age 26}}
put-infelixrPlayground
(last 'hello)
# => 111
lastsogaiuPlayground
(let [c0 (ev/chan)
      c1 (ev/chan 1)]
  [(ev/capacity c0) (ev/capacity c1)])
# => '(0 1)
ev/capacitysogaiuPlayground
# with by function 

(sorted [1 -2 2 3 9 -10] >)  #@[9 3 2 1 -2 -10]
sortedleobmPlayground
(defn dataframe 
  ``Create an empty dataframe with the given columns``
  [& columns]
  (tabseq [c :in columns] c @[]))
tabseqveqqqPlayground
(def [stdin-r stdin-w] (os/pipe))
(def [stdout-r stdout-w] (os/pipe))

# write the input that will be sent to sed
(:write stdin-w "hello world 1\nhello world 2")
(:close stdin-w)

(os/execute
  ["sed" "s|world|janet|g"]
  :px
  # the program reads from :in and writes to :out
  {:in stdin-r :out stdout-w})

(:read stdout-r math/int32-max)
# => @"hello janet 1\nhello janet 2"

# feed two lines to sed, which replaces "world"
# with "janet", and read the modified results back
os/executeYohananDiamondPlayground
(os/cwd)  # => "/Users/cell/tmp"
(os/getenv "PWD")  # => "/Users/cell/tmp"
(get (os/environ) "PWD")  # => "/Users/cell/tmp"
os/cwdcellularmitosisPlayground
(math/atan2 0 0)
# => 0
math/atan2sogaiuPlayground
(defn- parse-timestamp
  ``Parse a date or datetime string into a unix epoch int``
  [s]
  (if (string/find " " s)
    # datetime: "yyyy-MM-dd HH:mm"
    (let [[date-part time-part] (string/split " " s)
          [y m d]               (string/split "-" date-part)
          [hh mm]               (string/split ":" time-part)]
      (os/mktime {:year         (scan-number y)
                  :month        (scan-number m)
                  :month-day    (scan-number d)
                  :hours        (scan-number hh)
                  :minutes      (scan-number mm)
                  :seconds      0}))
    # date: "yyyy-MM-dd"
    (let [[y m d] (string/split "-" s)]
      (os/mktime {:year      (scan-number y)
                  :month     (scan-number m)
                  :month-day (scan-number d)
                  :hours 0 :minutes 0 :seconds 0}))))
os/mktimeveqqqPlayground
(print
  (prompt :a 
          (for i 0 1000000000000 
            (print (string "i=" i))
            (if (= i 2) 
                (return :a 10)))))
# output:
# i=0
# i=1
# i=2
# 10
returnfelixrPlayground
(map length  [  'a  :a  "a"  [97]  @[97]  {0 97}  @{0 97}  ])
# =>        @[  1   1   1    1     1      1       1        ]
lengthcellularmitosisPlayground
(->> "X"
     (string "a" "b")
     (string "c" "d")
     (string "e" "f"))  # => "efcdabX"
->>uvtcPlayground
(def h ["a" "b" :c]) # => ("a" "b" :c)

(find (fn [a] (= "a" a)) h) # => "a"
findfaywongPlayground
# Note that the earlier sort func is executed later

(update @{:data @[:cherry :orange]} :data (comp sort |(array/push $ :apple)))
# => @{:data @[:apple :cherry :orange]}
compveqqqPlayground
(empty? {})
# => true
empty?sogaiuPlayground
(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?cellularmitosisPlayground
(-)
# => 0
-sogaiuPlayground