Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
(reduce2 + [1 2 3 ]) # -> 6
(accumulate2 + [1 2 3 ]) # -> @[1 3 6] (get-in {:a {:cc 2 } :b {:dd 3 }} [:a ] 42 ) # => {:cc 2}
(get-in {:a {:cc 2 } :b {:dd 3 }} [:a :cc ] 42 ) # => 2
(get-in {:a {:cc 2 } :b {:dd 3 }} [0 ] 42 ) # => 42
(get-in {:a {:cc 2 } :b {:dd 3 }} [:z ] 42 ) # => 42
# repeat is about _side-effects_, since it returns nil.
# You'll need to use a mutable collection if you want to
# use repeat to add repeated items to a collection:
(var coll @["Y" "Z" ]) #=> @["Y" "Z"]
(repeat 3 (array/push coll "A" )) #=> nil
coll
# Returns:
@["Y" "Z" "A" "A" "A" ]
# You can use map if the whole collection consists
# of these repetitions:
(map (fn [_ ] "A" ) (range 3 ))
# Returns:
@["A" "A" "A" ](next [4 5 6 ] ) # => 0
(next [4 5 6 ] 0 ) # => 1
(next [4 5 6 ] 1 ) # => 2
(next [4 5 6 ] 2 ) # => nil
# note that dictionary keys are not necessarily in the same order
# as the corresponding literal.
(next {:a 5 :b 6 :c 7 } ) # => :a
(next {:a 5 :b 6 :c 7 } :a ) # => :c
(next {:a 5 :b 6 :c 7 } :c ) # => :b
(next {:a 5 :b 6 :c 7 } :b ) # => nil
(table ;(kvs (struct :a 1 :b 2 )))
# => @{:a 1 :b 2} (defn output [x ]
(case x
:a "a"
"b" ))
(output :a ) # => "a"
(output "anything else" ) # => "b" (->> (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 (string/trim " foo " ) # => "foo"
(string/trim "_!_foo_!_" "_!" ) # => "foo" In order to use :fresh , write:
(import path :fresh true )# From the REPL, always nil
repl> (dyn *current-file* )
nil
# As an expression to the CLI, always nil
$ janet -e '(pp (dyn *current-file* ))'
nil
# Given the file /tmp/janet/test.janet with the following contents:
(pp (dyn *current-file* ))
# Note how the value reflects the given file path
~ $ janet /tmp/janet/test.janet
"/tmp/janet/test.janet"
/tmp/janet $ janet ./test.janet
"./test.janet"
/tmp/janet $ janet test.janet
"test.janet"
/tmp/janet $ janet ../janet/test.janet
"../janet/test.janet"
# The following are equivalent - the first is preferred.
# The key insight is that dynamic bindings are just keyword/value
# pairs in the current fiber's environment table.
(dyn *current-file* )
(dyn :current-file )
((curenv ) *current-file* )
((curenv ) :current-file )(map int? [ nil true 0 1 3.14 (math/trunc 3.14 ) (band 1.1 2.2 ) ])
# => @[ false false true true false true true ] # see https://janet-lang.org/docs/abstract_machine.html
(def plus10
(asm
'{
:arity 1
:bytecode @[(ldi 1 10 ) # $1 = 10
(add 2 0 1 ) # $2 = $0 + $1
(ret 2 )]})) # return $2
(plus10 1 ) # -> 11 # If your tooling or the compiler doesn't believe a symbol will be bound e.g.:
(def a b ) # if b e.g. gets bound by some macro or pushed directly to the env
# You can wrap it with compwhen on a bool:
(var- bla false )
(compwhen bla
(def a b ))
# Whatever defines b should then set bla to true
# From: https://codeberg.org/veqq/declarative-dsls/src/branch/master/src/declarative-dsls/logic.janet # Demonstrate file/flush -- tail %flushtest.csv to see new
# entries appended as the program runs. Otherwise, entries
# wouldn't be visible until file was closed. @oofoe
(def fp (file/open "flushtest.csv" :wb ))
(file/write fp "Timestamp,Fiducial\n" )
(for i 0 5
(print "-- Writing " i )
(file/flush (file/write fp (string (os/time ) "," i "\n" )))
(os/sleep (* 5 (math/random ))))
(file/close fp )(def a @[])
(array/push a 1 ) # => @[1]
a # => @[1]
(array/push a 2 ) # => @[1 2]
a # => @[1 2]