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

(filter (partial string/has-prefix? "z") (all-bindings))  # => @[zero? zipcoll]
all-bindingscellularmitosisPlayground
(defn capitalize [str]
  (string (string/ascii-upper (string/slice str 0 1)) (string/slice str 1)))
string/ascii-upperveqqqPlayground
(from-pairs [[:hello "world"] [:foo "bar"]]) 

# => @{:foo "bar" :hello "world"}
from-pairsjgartePlayground
# https://en.wikipedia.org/wiki/Error_function
(math/erf 1)
# => 0.842701
math/erfsogaiuPlayground
(peg/find-all ~(capture :d)
              "hi 0 bye 1")
# => @[3 9]
peg/find-allsogaiuPlayground
(dyn 'defn)
# => @{:source-map ("boot.janet" 12 1) :value <function defn> :doc "(defn name & more)\n\nDefine a function. Equivalent to (def name (fn name [args] ...))." :macro true}
dynsogaiuPlayground
# janet 1.10.1

(all pos? [1 2 3])     # => true
(all pos? [1 2 3 -4])  # => false
(all pos? [1 2 3 0])   # => false

(all (partial string/has-prefix? "a") ["aa" "ab"])       # => true
(all (partial string/has-prefix? "a") ["aa" "ab" "bb"])  # => false

(all truthy? [1 2])              # => true
(all truthy? [1 2 3])            # => true
(all truthy? [1 2 nil 3])        # => false
(all truthy? [1 false 2 nil 3])  # => false

(all (fn [x] x) [1 2])              # => 2
(all (fn [x] x) [1 2 3])            # => 3
(all (fn [x] x) [1 2 nil 3])        # => nil
(all (fn [x] x) [1 false 2 nil 3])  # => false
allcellularmitosisPlayground
(eachk k [1 2 3] (print k))
# prints 0
# prints 1
# prints 2
# for indexed collections indices are printed  
eachkpepePlayground
(table ;(kvs (struct :a 1 :b 2)))
# => @{:a 1 :b 2}
tablesogaiuPlayground
(neg? -42)  # => true
(map neg? [-1 0 1])  # => @[true false false]
neg?cellularmitosisPlayground
(do
  (def coll @[])
  (forv i 0 9
    (array/push coll i)
    (+= i 2))
  coll)
# => @[0 3 6]
forvsogaiuPlayground
(invert "yo")
# => @{111 1 121 0}
invertsogaiuPlayground
(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   ]
math/floorcellularmitosisPlayground
# 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
# Upgrade mutates, but I wasn't sure whether it'd matter using the pure sorted or mutative sort. So I
# did a simple test.

(import spork)
# create data before test
(def d @{:data (range 10000000 0 -1)})
(spork/test/timeit (update d :data sort)) # 4.32930135726929 seconds and 87.4 MB

(def d @{:data (range 10000000 0 -1)}) # 87.4 MB
(spork/test/timeit (update d :data sorted)) # 4.49482655525208 seconds and 167 MB

# Where did those memory numbers come from? With only the data, the Janet process 
# uses 87.4 MB. Timewise, they take the same amount of time but on starting, sorted
# prepares memory equivalent to its input. To check ram within Janet:
(def pid (os/getpid)) # => "/proc/367537/statm"
(def statm-path (string "/proc/" pid "/statm")) # => 367537
(slurp statm-path) # => @"40444 40038 710 82 0 39426 0\n"

# Collecting garbage:
(gccollect)
(slurp statm-path) # => @"20912 20503 695 82 0 19894 0\n"
spork/test/timeitveqqqPlayground