Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!
(buffer/bit @"0" 4 )
# => true
(keyword : )
# => : (var a 2 )
a # => 2
(dec a ) # => 1
a # => 2
(-- a ) # => 1
a # => 1
# foo.txt is a file with contents "hello\nworld\n".
(slurp "foo.txt" ) # => @"hello\nworld\n"
(string/split "\n" (slurp "foo.txt" )) # => @["hello" "world" ""]
(defn slurp-lines [path ]
(string/split "\n" (slurp path )))
(slurp-lines "foo.txt" ) # => @["hello" "world" ""]
(string/join @["hello" "world" "" ] "\n" ) # => "hello\nworld\n"
(spit "foo2.txt" (string/join @["hello" "world" "" ] "\n" ))
# The contents of foo.txt and foo2.txt are now identical.
(defn spit-lines [path lines ]
(spit path (string/join lines "\n" )))
(spit-lines "foo3.txt" (slurp-lines "foo.txt" ))
# The contents of foo.txt and foo3.txt are now identical.
(for i 0 5
(print i ))
# 0
# 1
# 2
# 3
# 4
# => nil (defn eval-string
``Evaluates a string in the current environment. If more control over the
environment is needed, use `run-context`.``
[str ]
(var state (string str ))
(defn chunks [buf _ ]
(def ret state )
(set state nil )
(when ret
(buffer/push-string buf str )
(buffer/push-string buf "\n" )))
(var returnval nil )
(run-context {:chunks chunks
:on-compile-error (fn compile-error [msg errf & ]
(error (string "compile error: " msg )))
:on-parse-error (fn parse-error [p x ]
(error (string "parse error: " (:error p ))))
:fiber-flags :i
:on-status (fn on-status [f val ]
(if-not (= (fiber/status f ) :dead )
(error val ))
(set returnval val ))
:source :eval-string })
returnval )
(when-with [f (file/open "nofile.exists" )]
(print "Realy?" ))
# it just returns nil (parse "[:a :b :c]" ) # => [:a :b :c] (-> "X"
(string "a" "b" )
(string "c" "d" )
(string "e" "f" )) # => "Xabcdef" #1) start janet normally, work in the repl, encounter something to debug:
(debug )
# debug:
# in thunk [repl] (tail call) on line 4, column 1
# nil
# repl:5:>
#2) because the -d flag was missing on start, debug is skipping back out into the repl. now i really DO want to debug here and now, without restarting the vm:
(dyn *debug* )
# => nil
#3) we can just set the flag ourself and thus activate the debugger:
(setdyn *debug* :anything_truthy )
(debug )
# debug:
# in thunk [repl] (tail call) on line 2, column 1
# entering debug[1] - (quit) to exit
# debug[1]:1:> (def s (symbol/slice :hello 2 4 ))
(print (type s )) # symbol
(print s ) # ll
(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
(defn inc [x ] (+ x 1 ))
(defn inv [x ] (* x -1 ))
(defn sq [x ] (* x x ))
(-> 2 inc ) # -> 3
(-> 2 inc inc ) # -> 4
(-> 2 inc inc inv ) # -> -4
(-> 2 inc inc inv sq ) # -> 16
(cmp 1.0 2 )
# => -1
(seq [i :range [0 3 ]
j :range [0 3 ]
:let [c (string/format "%c" (+ 97 i ))]
:when (and (even? i ) (even? j ))]
[(keyword c ) j ])
# => '@[(:a 0) (:a 2) (:c 0) (:c 2)]