Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!
(mean [1 10 100 ]) # => 37
(mean []) # => nan (math/hypot 1 1 ) # => 1.41421
(math/sqrt 2 ) # => 1.41421
(var abc 123 )
(= 356 (with-vars [abc 456 ] (- abc 100 )))(peg/find-all ':d "hi 1 bye 2" ) # => @[3 9]
# due to 0 indexing, you will often want to add 1 to things:
(defn short-date [d ]
(let [{:year y :month mon :month-day d } d ]
(string y "-" (+ 1 mon ) "-" (+ 1 d ))))
# Makes os/date like 2025-12-25
(short-date (os//date ))
# From: https://codeberg.org/veqq/deforester/src/branch/master/deforester.janet
(defn- time-string
``Gives current time as ISO 8601 string: https://en.wikipedia.org/wiki/ISO_8601
This accounts for `os/date` 0-indexing month and days which are 1-indexed in ISO 8601.``
[]
(let [now (os/clock :realtime )
ms (math/floor (* 1000 (mod now 1 )))
{:year y :month mon :month-day d :hours h :minutes min :seconds s }
(os/date (math/floor now ))]
(string/format "%d-%02d-%02dT%02d:%02d:%02d.%03dZ" y (inc mon ) (inc d ) h min s ms )))(def [pipe-r pipe-w ] (os/pipe ))
(:write pipe-w "hello" )
(:read pipe-r 5 )
# => @"hello"
# send the string "hello" into the writable stream
# part and read it back from the readable one
# absolute value of negatives, ignore positives
(keep |(if (< $ 0 ) (- $ ) nil ) [-1 2 -3 -4 -5 6 ]) # -> @[1 3 4 5]
# halves each even number, ignores odds
(keep |(when (even? $ ) (/ $ 2 )) [1 2 8 7 -2 ]) # -> @[1 4 -1]
# Some tips for working with bytes and unicode:
(string/bytes "что-нибудь" ) #> (209 135 209 130 208 190 45 208 189 208 184 ...
(print (string/from-bytes 208 176 208 177 )) #> аб
(map string/from-bytes (string/bytes "что-нибудь" )) #> @["\xD1" "\x87" "\xD1" "\x82" "\xD0" "\xBE" ...
# Print renders "\xD1" "\x87" as ч, as unicode characters may have multiple bytes
# So use apply:
(apply print (map string/from-bytes
(string/bytes "что-нибудь" ))) #> что-нибудь (/ 2 )
# => 0.5
(def cc (ev/thread-chan 99 )) # Sets up a message channel.
(def ww (filewatch/new cc )) # Creates the watcher.
(filewatch/add ww "ftest.janet" :all ) # Watch for all changes.
(filewatch/add ww "fadd.janet" :modify ) # Only care if modified (Linux-specific flag).
(filewatch/listen ww ) # Start filewatcher listening for changes.
(pp (ev/take cc )) # Print info on first file to change.
(filewatch/unlisten ww ) # Stop caring about changes. (for i 0 5
(print i ))
# 0
# 1
# 2
# 3
# 4
# => nil (tabseq [i :in (range 97 100 )]
i (string/from-bytes i ))
# =>
@{97 "a"
98 "b"
99 "c" }
(last "hello" )
# => 111
# based on: https://codeberg.org/veqq/janetdocs/src/commit/54a964d1a35920af2655c4932a9b51379e8b9380/seed.janet#L16
(def core-api (all-bindings ))
(def alldocs @[])
(loop [b :in core-api ]
(when (not= "allbindings" (string b ))
(do
(def buf @"" )
(with-dyns [:out buf ]
(doc* b )
(array/push alldocs {:name (string b ) :docstring (string buf )})))))
(pp alldocs )(def a @[1 2 ])
(array/concat a 3 [4 5 ] @[6 7 ] [] @[] 8 )
a # => @[1 2 3 4 5 6 7 8]
(< ) # -> true
(< 1 ) # -> true
(< 1 2 ) # -> true
(< 2 1 ) # -> false
(< 1 2 3 ) # -> true
(< 1 3 2 ) # -> false
(map inc
(fiber/new |(each x (range 3 )
(yield x ))))
# => @[1 2 3]
(defmacro- =dude
"sets any symbolic expression to the atom :what?"
[a ]
~(var a :what? )) # => <function =dude>
(=dude 2 ) # => :what?
(=dude :2 ) # => :what?
(=dude 2 ) # => :what?
(=dude "2" ) # => :what?
(=dude @"2" ) # => :what?
(=dude @[2 ]) # => :what?
(=dude (2 )) # => :what?
(=dude [2 ]) # => :what?
(=dude @{2 3 }) # => :what?
(doc =dude )
# => macro
# => repl on line 59, column 1
# =>
# => (=dude a)
# => nil
# Macro expanding the =dude macro:
(macex (=dude 2 )) # => :what?
(macex1 (=dude 2 )) # => :what?
# Macros have to be quoted in order to expand
(macex ~(=dude 2 )) # => (var a :what?)
(macex1 ~(=dude 2 )) # => (var a :what?)
(macex ~(=dude @{2 3 })) # => (var a :what?)
(interpose 0 [1 ])
# -> @[1] (let [p @{:a 1 }
t @{:b 2 }]
(table/setproto t p )
[(get t :a ) (table/rawget t :a )])
# => '(1 nil)