Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!
(keyword "" )
# => : (> 12 11 10 9 8 7 6 5 4 3 2 1 ) # => true
(> 12 11 10 9 8 7 6 5 4 3 2 9 ) # => false (def a @[[42 "c" ] [68 "b" ] [31 "d" ] [27 "a" ]])
(sort a (fn [a b ]
(< (a 1 ) (b 1 ))))
(pp a ) # @[(27 "a") (68 "b") (42 "c") (31 "d")] (forever
(print (os/time ))
(ev/sleep 1 ))
# => epoch clocks, prints epoch timestamp every second (bnot 255 ) # => -256
# nested iteration
(loop [a :in [100 200 300 ]
b :in [1 2 3 ]]
(print (+ a b )))
# 101
# 102
# 103
# 201
# 202
# 203
# 301
# 302
# 303 (os/shell "uptime > /tmp/uptime.txt" ) # => 0
(slurp "/tmp/uptime.txt" )
# => @"22:33 up 5 days, 9:34, 15 users, load averages: 1.93 1.74 1.59\n"
(os/rm "/tmp/uptime.txt" ) # => nil (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 }))))(struct :a 1 :b 2 ) # => {:a 1 :b 2} (math/atan2 0 0 )
# => 0
(buffer/bit @"1" 0 )
# => true
(eval-string "(+ 1 2 3 4)" ) # -> 10
(eval-string ")" ) # -> parse error
(eval-string "(bloop)" ) # -> compile error
(eval-string "(+ nil nil)" ) # -> runtime error (def a @[1 2 ])
(array/concat a 3 [4 5 ] @[6 7 ] [] @[] 8 )
a # => @[1 2 3 4 5 6 7 8]
(defn get-time-str []
(let [{:hours h :minutes m :seconds s } (os/date )]
(string h ":" m ":" s )))
(get-time-str ) => "23:18:16" (unmarshal @"hello world!" ) # => 104
(map indexed? [ 'ab :ab "ab" @"ab" [97 98 ] @[97 98 ] {0 97 1 98 } @{0 97 1 98 } ])
# => @[ false false false false true true false false ]
(map tuple? [ 'ab :ab "ab" @"ab" [97 98 ] @[97 98 ] {0 97 1 98 } @{0 97 1 98 } ])
# => @[ false false false false true false false false ]
(map array? [ 'ab :ab "ab" @"ab" [97 98 ] @[97 98 ] {0 97 1 98 } @{0 97 1 98 } ])
# => @[ false false false false false true false false ] (def s (symbol/slice :hello 2 4 ))
(print (type s )) # symbol
(print s ) # ll
# To make a package conditionally export from another:
# A package can see whether another module was already imported,
# and conditionally act and e.g. import it and rebind/export some symbols:
(var- bla false )
(compwhen (not (some |(string/has-suffix? "dataframes.janet" $ ) (keys module/cache )))
(import declarative-dsls/dataframes :as "no" :export false )
(set bla true ))
(compwhen bla
(def print-as-table no/print-as-table )
(def select no/select )
(def populate-df! no/populate-df! )
(def rows->dataframe no/rows->dataframe )
(def dataframe->rows no/dataframe->rows ))
# From: https://codeberg.org/veqq/declarative-dsls/src/branch/master/src/declarative-dsls/logic.janet (map number? [nil true 42 :a "a" [] {} (fn []) ])
# => @[false false true false false false false false ]
# Match enables polymorphic functions:
(defn fac [& vars ]
(match vars
[0 ] 1
[x ] (* x (fac (- x 1 )))))
# from https://janet.guide/control-flow/
(defn calculate [expr ]
(match expr
[:add x y ] (+ x y )
[:subtract x y ] (- x y )
[:multiply x y ] (* x y )
([:divide x y ] (= y 0 )) (error "division by zero!" )
[:divide x y ] (/ x y )))
(calculate [:subtract 5 10 ])
# Note, it checks prefixes, so you must order things this way:
(match [1 2 ]
[x y z ] "three elements"
[x y ] "two elements"
[x ] "one element"
[] "no elements" )