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

# create a channel without buffer, default is 0
(def channel (ev/chan))

(ev/spawn
  (ev/sleep 5)
  (ev/give channel "Hard work is done!"))

(print "do anything")
(for i 0 5
  (print i)
  (ev/sleep 0.5))

(print (ev/take channel)) # blocks here, until there is a result in the channel
(print "done")

ev/chanleobmPlayground
(array 1 2.3 :a "foo" true nil [] {} (fn []))
# => @[1 2.3 :a "foo" true nil () {} <function 0x7FB2A3F02170>]
arraycellularmitosisPlayground
(cmp 1.0 2)
# => -1
cmpsogaiuPlayground
(merge {:a 1 :b 2})

#=> @{:a 1 :b 2} 
# good way to convert struct to table
mergepepePlayground
# channel that can be used for communication between os threads
(def chan (ev/thread-chan 10))

# one thread for sending a message
(ev/do-thread
  (def msg "hi")
  (print "sending: " msg)
  (ev/give chan msg))

# another thread for receiving a message
(ev/do-thread
  (print "received: " (ev/take chan)))

# expected output
#
# sending: hi
# received: hi
ev/do-threadsogaiuPlayground
(table/to-struct @{:a 1}) # => {:a 1}
table/to-structswlkrPlayground
(in  [10 11 12 13]  0   42 )  # => 10
(in  {:a 10 :b 20}  :a  42 )  # => 10

(in  [10 11 12]  99 42 )  # error
(in  [10 11 12]  -1 42 )  # error
(in  [10 11 12]  -2 42 )  # error
(in  {:a 1}      :z 42 )  # => 42

(map  (fn [x] (in x 0 42))   [  'a  :a  "a"  [97]  @[97]  {0 97}  @{0 97}  {:a 1} ])
# =>                        @[  97  97  97   97    97     97      97       42     ]
incellularmitosisPlayground
(count even? [1 2 3 4 5])  # => 2

(count  (fn [x] (> x 3))  [1 2 3 4 5])  # => 2
(count         |(> $ 3)   [1 2 3 4 5])  # => 2

(count  (fn [x] (truthy? x))  [nil false true 42 :a "foo"])  # => 4
(count         |(truthy? $)   [nil false true 42 :a "foo"])  # => 4

(var f even?)
(count f [1 2 3 4 5])  # => 2
(set f odd?)
(count f [1 2 3 4 5])  # => 3

(map  (fn [f] (count f [1 2 3 4 5]))  [even? odd?])  # => @[2 3]
(map         |(count $ [1 2 3 4 5])   [even? odd?])  # => @[2 3]
countcellularmitosisPlayground
# with by function 

(sorted [1 -2 2 3 9 -10] >)  #@[9 3 2 1 -2 -10]
sortedleobmPlayground
(reduce string "ha" ["ha" "ha" "ha" "ha"]) # => "hahahahaha"

(accumulate string "ha" ["ha" "ha" "ha" "ha"]) # => @["haha" "hahaha" "hahahaha" "hahahahaha"]
accumulatejgartePlayground
(def name "Joe")
(when (= name "Joe") "Hello Joe")
# "Hello Joe"


(when (not= 1 2) "not-equal")
# "not-equal"
whenleobmPlayground
(string/find-all "duck" "duck duck duck goose!") # => @[0 5 10]
string/find-allsogaiuPlayground
(label result
  (each x [0 1 2 3]
    (when (= x 3)
      (print "reached the end"))
    (when (= x 2)
      (return result 8))))
# => 8
labelsogaiuPlayground
(invert {:a 1 :b 2 :c 3})
# => @{3 :c 1 :a 2 :b}
invertsogaiuPlayground
(let [buf @"hello"]
  (buffer/blit buf "zany world" -1 4))
# =>
@"hello world"
buffer/blitsogaiuPlayground