Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!
(- ;(range 10 ))
# => -45 (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 (peg/find ':s "Battery temperature: 40 °C" )
# => 7 index of the first whitespace character
# :s (set " \t\r\n\0\f\v")
(update @{:data @[:cherry :orange ]} :data (comp sorted |(array/push $ :apple )))
# => {:data @[:apple :cherry :orange]}
# note that all data structures must be mutable with @
(def c @{:size :small :roast :dark :add-ins @[:soy :almond :soy ]})
(defn add! [coffee extra ]
(update coffee :extras (comp sort |(array/push $ extra ))))
(add! c :chocolate )# sorted allocates the full memory of its input
(def d2 (range 10000000 0 -1 )) # 87mb
(sorted d2 ) # now 167 mb (assertf true "this sentence is %n" false )
# => true
(when-let [root (math/sqrt 64 )
unused (even? root )]
(printf "%d is even" root ))
# -> "8 is even" (string/bytes "hello" ) # => (104 101 108 108 111) (all-dynamics )
# => @[:args :err-color :executable :peg-grammar :pretty-format :syspath]
(setdyn :fixed true )
(all-dynamics )
# => @[:args :err-color :executable :fixed :peg-grammar :pretty-format :syspath] (bxor 2r011 2r110 )
# => 5
(any? "" )
# => nil
(any? "Hello" )
# => 72
(any? @"Hello" )
# => 72
(any? @[])
# => nil
(any? [])
# => nil
(any? {})
# => nil
(any? @{})
# => nil
(any? @[1 2 3 4 ])
# => 1
(any? @{:a 2 :b 3 })
# => 2
(string/trim " foo " ) # => "foo"
(string/trim "_!_foo_!_" "_!" ) # => "foo" # 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 # Expanding the example from https://janetdocs.org/core-api/stdin
# We use argparse to provide automatically add flags
# --help comes for free, try it!
(import spork/argparse )
(defn main [& ]
(let [opts (argparse/argparse "Sum numbers from files, stdin or data."
"multiply" {:kind :option :short "m" :default "1" :map scan-number :help "Multiply the result" }
"verbose" {:kind :flag :help "Print the input first" }
:default {:kind :accumulate })
_ (unless opts (os/exit 0 )) # lest it crash on help etc.
args (or (opts :default ) []) # give empty? empty array of :default is nil
data (cond (empty? args ) (file/read stdin :all )
(os/stat (first args )) (slurp (first args ))
(string/join args " " ))
total (* (opts "multiply" ) (sum (flatten (parse-all data ))))]
(if (opts "verbose" ) (print "Data: " data ))
(print total )))(keyword "" )
# => : (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 ] (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 ]
(compare> 1 0 ) # => true
(compare> 10 9.9 ) # => true
(compare> :a :b ) # => false
(compare> :b :a ) # => true (peg/find-all ':d "hi 1 bye 2" ) # => @[3 9]
(->> (all-bindings )
(keep (fn [sym ]
(when (string/has-prefix? "peg/" (string sym ))
(string sym ))))
sort )
# =>
@["peg/compile"
"peg/find"
"peg/find-all"
"peg/match"
"peg/replace"
"peg/replace-all" ]
# from https://github.com/sogaiu/margaret/blob/master/tutorials/tutorial.janet