JanetDocsPlaygroundI'm feeling luckyGitHub sign in

JanetDocs is a community documentation site for the Janet programming language

Loading...

Recent examples

# Map over multiple structures -- Stops after the shortest is exhausted.
(map (fn [& args] ;args) [1] [1 2] [1 2 3])

# => @[(1 1 1)]
mapGrazfatherPlayground
# Print out the current time in Last-Modified header-compliant 
# form. (reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified)
(def weekdayMap [ "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" ])
(def monthMap ["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"])
(def dt (os/date (os/time) true))
(string/format "%s, %02d %s %04d %02d:%02d:%02d" 
     (weekdayMap (dt :week-day)) 
     (dt :month-day)
     (monthMap (dt :month))
     (dt :year)
     (dt :hours)
     (dt :minutes)
     (dt :seconds))
string/formatyumaikasPlayground
(def buf-bytes 12)

(var new-buffer (buffer/new buf-bytes)) #--> @""

(buffer/push new-buffer "hello, world") #--> @"hello, world"
buffer/pushMorganPetersonPlayground
(def new-bytes 512)

(var new-buffer (buffer/new new-bytes))

# --> @""
buffer/newMorganPetersonPlayground
(group-by 
  (fn [i] (i :label)) 
  [{:label 'A :value 4} {:label 'A :value 3} {:label 'B :value 5}])
# @{A @[{:label A :value 4} {:label A :value 3}] B @[{:label B :value 5}]}
group-byaquilaxPlayground
# complex matching, when you need to use predicates
# to determine a match
(def test {:a {:b 1} :c 2})
(match test
  ({:a a} (dictionary? a)) (print "is a dictionary"))
# --> "is a dictionary"

(match test
  ({:a a} (boolean? a)) (print "is a dictionary"))
# --> nil
matchllmIIPlayground
(printf "%s %s!" "Hello" "World") # Hello World!
printfaquilaxPlayground
(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 month :day day})))

(print (parse-date "2021-01-01")) # 1612137600
os/mktimeaquilaxPlayground
# 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"]
repeatsemperosPlayground
# Interactively read a line from STDIN
(file/read stdin :line)

# Type abcd and then press ENTER
abcd

# Returns:
@"abcd\n"
stdinsemperosPlayground
(net/address "0.0.0.0" 80) # => <core/socket-address 0x55CABA438E90>

(net/address "0.0.0.0" 8989) # => <core/socket-address 0x55CABA439980>

net/addressjgartePlayground
(peg/find-all ':d "Battery temperature: 40 °C")

# => @[21 22] indices containing digits

# :d (range "09")

# peg grammars are contained in a table @{} (mutable) located in boot.janet source file.

peg/find-alljgartePlayground
(peg/find ':a "Battery temperature: 40 °C")

# => 0 index of the first upper/lower case character

# :a (range "az" "AZ")
peg/findjgartePlayground
(peg/find ':s "Battery temperature: 40 °C")

# => 7 index of the first whitespace character

# :s (set " \t\r\n\0\f\v")

peg/findjgartePlayground
(loop [i :range [0 12]]
  (print i))

# => 0
# => 1
# => 2
# => 3
# => 4
# => 5
# => 6
# => 7
# => 8
# => 9
# => 10
# => 11
# => nil

loopjgartePlayground
(unmarshal @"hello world!") # => 104
unmarshaljgartePlayground
(assert :dude :what?) # => :dude

(assert :dude :dude) # => :dude

(assert (= :dude :what?) "Where's my car?")
# => error: Where's my car?
# =>  in _thunk [repl] (tailcall) on line 84, column 1

(assert (= :dude :dude) "Where's my car?") # => true

assertjgartePlayground
(flatten-into @[1 2 3] @[[4 5 6] @[7 8 9] @[10 11 12]]) # => @[1 2 3 4 5 6 7 8 9 10 11 12]
flatten-intojgartePlayground
(forv i 0 10
 (print "hello"))

# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => hello
# => nil
forvjgartePlayground
(one? :dude) # => false
one?jgartePlayground
(defmacro- =dude
 "sets any symbolic expression to the atom :what?"
 [a]
 ~(var a :what?)) # => <function =dude>

(=dude 2) # => :what?

(=dude :2) # => :what?

(=dude 2) # => :what?

(=dude "2") # => :what?

(=dude @"2") # => :what?

(=dude @[2]) # => :what?

(=dude (2)) # => :what?

(=dude @(2)) # => :what?

(=dude @{2 3}) # => :what?


(doc =dude)


# =>    macro
# =>    repl on line 59, column 1
# =>
# =>    (=dude a)


# => nil


# Macro expanding the =dude macro:

(macex (=dude 2)) # => :what?

(macex1 (=dude 2)) # => :what?

# Macros have to be quoted in order to expand

(macex ~(=dude 2)) # => (var a :what?)

(macex1 ~(=dude 2)) # => (var a :what?)

(macex ~(=dude @{2 3})) # => (var a :what?)
defmacro-jgartePlayground
(doc string)


# =>    cfunction
# =>    ../src/core/corelib.c on line 349, column 1
# =>
# =>    (string & xs)
# =>
# =>    Creates a string by concatenating the elements of xs together. If
# =>    an element is not a byte sequence, it is converted to bytes via
# =>    describe. Returns the new string.


# nil
docjgartePlayground
math/int32-max # => 2147483647
math/int32-maxjgartePlayground
(buffer "dude, " "where's " "my " "car?") # => @"dude, where's my car?"
bufferjgartePlayground
(buffer/bit-set @"hahaha" 3) # => @"hahaha"

(buffer/bit-set @"what" 3) # => @"\x7Fhat"

buffer/bit-setjgartePlayground
(buffer/bit-toggle @"dude" 3) # => @"lude"

(buffer/bit-toggle @"hahaha" 3) # => @"`ahaha"
buffer/bit-togglejgartePlayground
(sorted-by > @[1 2 3 4 5 6 7 8 9 10 11 12]) # => @[8 7 9 11 10 12 2 1 3 5 4 6]

(sorted-by < @[1 2 3 4 5 6 7 8 9 10 11 12]) # => @[8 7 9 11 10 12 2 1 3 5 4 6]

(sorted-by = @[1 2 3 4 5 6 7 8 9 10 11 12]) # => @[8 7 9 11 10 12 2 1 3 5 4 6]


sorted-byjgartePlayground
(janet/version 0) # => 49

(janet/version 1) # => 46

(janet/version 2) # => 49

(janet/version 3) # => 55

(janet/version 4) # => 46

(janet/version 5) # => 50
janet/versionjgartePlayground
(> 12 11 10 9 8 7 6 5 4 3 2 1) # => true

(> 12 11 10 9 8 7 6 5 4 3 2 9) # => false
>jgartePlayground
(os/realpath ".") # => "/home/jgarte"

(os/realpath "Downloads") # => "/home/jgarte/Downloads"
os/realpathjgartePlayground