JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

Community documentation for Janet

Supported Modules

Welcome, I'm happy to see you here! Feel free to pick a function and add a happy example, the more the merrier!

Loading...

Random Examples

(import spork/charts :as c)
(import spork/gfx2d :as g)
(g/save "bla.png" (c/plot-line-graph
                   :canvas (g/blank 100 100)
                   :data {:timestamp [1 2 3 40 5 60]
                          :temperature-1 [75.1 75.2 75.4 75.5 75.5 75.4]
                          :temperature-2 [55.1 55.4 55.7 60.0 60.4 60.9]}
                   :x-column :timestamp
                   :y-column [:temperature-1 :temperature-2]))

# then you can look at bla.png
spork/charts/plot-line-graphveqqqPlayground
(let [a 1]
 (let [b a]
  b)) # => 1
letjgartePlayground
(map dictionary? [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         true          ]

(map struct?     [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     true         false         ]

(map table?      [ 'ab   :ab   "ab"   @"ab"  [97 98]  @[97 98]  {0 97 1 98}  @{0 97 1 98}  ])
# =>            @[ false false false  false  false    false     false        true          ]
table?cellularmitosisPlayground
(parse "(+ 4 5)") # => (+ 4 5)
(type (parse "(+ 4 5)")) # => :tuple
parseskuzzymigletPlayground
(<)        # -> true
(< 1)      # -> true
(< 1 2)    # -> true
(< 2 1)    # -> false
(< 1 2 3)  # -> true
(< 1 3 2)  # -> false
<cellularmitosisPlayground
(/ 1 2 3)
# => 0.166667
/sogaiuPlayground

# sh/$'s contents are quasiquoted, allowing direct or string arguments
# so you need to unquote , variables:

(def out (file/open "trust-db.txt" :w))
(sh/$ "gpg" "--export-ownertrust" > ,out) # > requires an opened file object
(file/close out)

# note how > requires an opened file object
(with [out (file/open "trust-db.txt" :w)]
  (sh/$ gpg --export-ownertrust > ,out))
sh/$veqqqPlayground
(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
(ffi/context "/usr/lib64/libSDL2.so")

(ffi/defbind SDL_CreateWindow :ptr
    [title :string
     x :int
     y :int
     w :int
     h :int
     flags :uint32])

(ffi/defbind SDL_Delay :void [ms :uint32])
(ffi/defbind SDL_DestroyWindow :void [window :ptr])
(ffi/defbind SDL_Quit :void [])
(def SDL_WINDOW_SHOWN 0x00000004)

(defn main [&]
  (def window (SDL_CreateWindow "Hello world!" 0 0 640 480 SDL_WINDOW_SHOWN))
  (SDL_Delay 4000)
  (SDL_DestroyWindow window)
  (SDL_Quit))
ffi/defbindAndriamanitraPlayground
(empty? {})
# => true
empty?sogaiuPlayground
# Walk from the API is defined using a case 

(defn walk
  `Iterate over the values in ast and apply f
  to them. Collect the results in a data structure. If ast is not a
  table, struct, array, or tuple,
  returns form.`
  [f form]
  (case (type form)
    :table (walk-dict f form)
    :struct (table/to-struct (walk-dict f form))
    :array (walk-ind f form)
    :tuple (let [x (walk-ind f form)]
             (if (= :parens (tuple/type form))
               (tuple/slice x)
               (tuple/brackets ;x)))
    form))
casepingiunPlayground
# 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
(chr "a")  # => 97
chrcellularmitosisPlayground
(peg/match ~{:main (capture (some :S))}
           "hello world")
# => @["hello"]
peg/matchsogaiuPlayground
(pp (all-bindings))
# => prints @[% %= * ... yield zero? zipcoll]

(def a "A")
(pp (all-bindings (curenv) true))
# => prints @[_ a] - only local bindings are listed
all-bindingspepePlayground