Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
(varglobal "smile" false )
# => nil
smile
# => false
(set smile true )
# => smile
smile
# => true
(dyn 'smile )
# => @{:ref @[true]}
(defn walker
`Simple walker function, that prints non-sequential
members of the form or prints "Sequence" and walks
recursively sequential members of the tree.`
[form ]
(if (or (indexed? form ) (dictionary? form ))
(do (print "Sequence" )
(walk walker form ))
(print form )))
(walk walker [[[[0 1 3 ]] 16 7 [3 [3 5 ]] 3 4 ] 1 [3 4 ]])
# Prints
# Sequence
# Sequence
# Sequence
# 0
# 1
# 3
# 16
# 7
# Sequence
# 3
# Sequence
# 3
# 5
# 3
# 4
# 1
# Sequence
# 3
# 4
(def a @[1 2 ])
(array/pop a ) # => 2
a # => @[1]
(array/pop a ) # => 1
a # => @[]
(array/pop a ) # => nil
a # => @[]
(->> (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 (def b @"" )
(xprint b "HOHOHO" )
# => nil
b
# => @"HOHOHO\n" (get (os/environ ) "HOME" ) # => "/Users/cell"
(os/getenv "HOME" ) # => "/Users/cell"
(var x 12 ) # => 12
(/= x 2 ) # => 6 [1 2 3 ] # => (1 2 3)
(tuple/type [1 2 3 ]) # => :parens
(tuple/brackets 1 2 3 ) # => [1 2 3]
(tuple/type (tuple/brackets 1 2 3 )) # => :brackets
# Reading a file line by line, using loop's :iterate verb, and adding the line lengths
(with [fl (file/open "filepath" )]
(var sm 0 )
(loop [line :iterate (file/read fl :line )]
(+= sm (length line )))
sm )
(protect
(if (> (math/random ) 0.42 )
(error "Good luck" )
"Bad luck" ))
# => randomly returns:
# (false "Good luck")
# or
# (true "Bad luck") (defn spit-lines [path lines ]
(spit path (string/join lines "\n" )))# janet 1.10.1
(= :a :a ) # => true
(= :a "a" ) # => false
(= :a ":a" ) # => false
(= "a" "a" ) # => true
(= "a" @"a" ) # => false
(= @"a" @"a" ) # => false
(= [1 2 ] [1 2 ]) # => true
(= [1 2 ] @[1 2 ]) # => false
(= @[1 2 ] @[1 2 ]) # => false
(= {:a 1 } {:a 1 }) # => true
(= {:a 1 } @{:a 1 }) # => false
(= @{:a 1 } @{:a 1 }) # => false
(= (fn []) (fn [])) # => false
(peg/replace ~(sequence :s (thru "t" ))
" duck"
"smiling cat sleeps" )
# => @"smiling duck sleeps" (pp (all-bindings ))
# => prints @[% %= * ... yield zero? zipcoll]
(def a "A" )
(pp (all-bindings (curenv ) true ))
# => prints @[_ a] - only local bindings are listed (defn greet-me [] (print "hey programmer!" ))
(defn greet-stranger [] (print "hey stranger!" ))
(varfn greet [] (greet-me ))
(greet ) # prints "hey programmer!"
(varfn greet [] (greet-stranger ))
(greet ) # prints "hey stranger!"
# kind of analogous to (def greet (fn [] (some-thunk))), but with built-in
# sugar to automatically update 'greet' if varfn is called with a name
# already bound