Welcome, I'm happy to see you here! Feel free to add a happy example, the more the merrier!
(try
(int/u64 "18446744073709551616" )
([e ] e ))
# => "bad u64 initializer: string" (defn spit-lines [path lines ]
(spit path (string/join lines "\n" )))(unmarshal @"hello world!" ) # => 104
# 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 )(get [10 11 12 13 ] 0 ) # => 10
(get {:a 10 :b 20 } :a ) # => 10
(get [10 11 12 ] 99 ) # => nil
(get [10 11 12 ] -1 ) # => nil
(get [10 11 12 ] -2 ) # => nil
(get {:a 1 } :z ) # => nil
(map (fn [x ] (get x 0 )) [ 'a :a "a" [97 ] @[97 ] {0 97 } @{0 97 } ])
# => @[ 97 97 97 97 97 97 97 ]
(map truthy? [ nil false true 0 1 'a :a "a" [] {} (fn []) ])
# => @[ false false true true true true true true true true true ]
# note that 'not' works as an implementation of 'falsey?'
(map not [ nil false true 0 1 'a :a "a" [] {} (fn []) ])
# => @[ true true false false false false false false false false false ]
(sandbox :fs ) # disable access to the sandbox
(os/mkdir "lol" )
# error: operation forbidden by sandbox
# in os/mkdir [src/core/os.c] on line 2146
# in thunk [repl] (tail call) on line 30, column 1 (def kvpairs [[:x 1 ] [:y 2 ]])
(table ;(flatten kvpairs )) # => @{:x 1 :y 2} # This turns the current REPL session (environment) with all bindings, including synced closures,
# into a loadable or compilable image.
# It is the same as e.g.: `janet -c test.janet test.jimage` after (spit "test.janet" (currenv))
(spit "test.jimage" (make-image (curenv )))(buffer/bit-toggle @"dude" 3 ) # => @"lude"
(buffer/bit-toggle @"hahaha" 3 ) # => @"`ahaha"
# Due to: https://github.com/swlkr/janetdocs/issues/50
(defn parse-date
"Parses YYYY-MM-DD date format to time"
[date ]
(let [[year month day ] (map scan-number (string/split "-" date ))]
(os/mktime {:year year :month (dec month ) :month-day (dec day )})))
(print (parse-date "2021-01-01" )) # 1609459200
(print (os/strftime "%c" (parse-date "2021-01-01" ))) # Fri Jan 1 00:00:00 2021 (os/which ) # => :macos # note, if running a server from the repl, you need to (quit) your repl.
# in a terminal:
# $ while true; do date | nc 0.0.0.0 1234 -w 1; sleep 1; done
# in a janet repl:
(net/server "0.0.0.0" 1234
(fn [conn ]
(prin (net/read conn 4096 ))
(net/close conn )))
# output doesn't actually start until you (quit) your repl's fiber:
(quit )
## Quadratic Formula
(defn qform
"Use the quadratic formula to solve for x. Returns all real solutions."
[a b c ]
(def det (- (* b b ) (* 4 a c )))
(def factor (/ 0.5 a ))
(cond
(neg? det ) []
(zero? det ) [(* factor (- b ))]
(let [root-det (math/sqrt det )]
[(* factor (- (- b ) root-det )) (* factor (+ (- b ) root-det ))])))
(qform 1 4 3 ) # -> (-3 -1)
(->> (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 (assertf true "this sentence is %n" false )
# => true
# janet 1.10.1
(array/remove @[1 2 3 ] 0 ) # => @[2 3]
(array/remove @[1 2 3 ] 1 ) # => @[1 3]
(array/remove @[1 2 3 ] 2 ) # => @[1 2]
(array/remove @[1 2 3 ] 3 ) # => @[1 2 3]
(array/remove @[1 2 3 ] 4 ) # error: index out of range
(array/remove @[1 2 3 ] -1 ) # => @[1 2 3]
(array/remove @[1 2 3 ] -2 ) # => @[1 2]
(array/remove @[1 2 3 ] -3 ) # => @[1 3]
(array/remove @[1 2 3 ] -4 ) # => @[2 3]
(array/remove @[1 2 3 ] -5 ) # error: index out of range
(array/remove @[1 2 3 4 ] 1 1 ) # => @[1 3 4]
(array/remove @[1 2 3 4 ] 1 2 ) # => @[1 4]
(array/remove @[1 2 3 4 ] 1 3 ) # => @[1]
(array/remove @[1 2 3 4 ] 1 4 ) # => @[1]
(array/remove @[1 2 3 4 ] 1 99 ) # => @[1]
(string/repeat "moshi" 2 ) # => "moshimoshi" (map math/abs [-2.9 -2.1 2.1 2.9 ]) # => @[ 2.9 2.1 2.1 2.9 ]
(map math/floor [-2.9 -2.1 2.1 2.9 ]) # => @[ -3 -3 2 2 ]
(map math/ceil [-2.9 -2.1 2.1 2.9 ]) # => @[ -2 -2 3 3 ]
(map math/round [-2.9 -2.1 2.1 2.9 ]) # => @[ -3 -2 2 3 ]
(map math/trunc [-2.9 -2.1 2.1 2.9 ]) # => @[ -2 -2 2 2 ]
(when-with [f (file/open "nofile.exists" )]
(print "Realy?" ))
# it just returns nil