summaryrefslogtreecommitdiff
path: root/dot_config/nvim/fnl/toast/core.fnl
blob: 54b0455beb603afa0e9352abb81eee8dd35b876c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(local {: insert} (require :toast.table))

(fn dec [n]
  "Decrement n by 1."
  (- n 1))
(fn inc [n]
  "Increment n by 1."
  (+ n 1))
(fn drop [n xs]
  "Returns a table of all but the first n elements in xs."
  (let [out []]
   (each [i v (ipairs xs)]
    (when (> i n) (insert out v)))
   out))
(fn first [xs]
  "The first element in a sequential table."
  (. xs 1))
(fn last [xs]
  "The last element in a sequential table."
  (. xs (length xs)))
(fn group [n xs]
  "Group elements in xs in groups of n.
   Extra elements at the end will sit in the final group.
   For example, (group 2 [1 2 3 4 5]) results in [[1 2] [3 4] [5]]."
  (let [ll #(length (last $))
        donext #(= (ll $) n)]
   (accumulate [out [[]]
                _ v (ipairs xs)]
    (do (when (donext out)
         (insert out []))
        (insert (last out) v)
        out))))
(fn map [f xs]
  "Returns a sequential table consisting of the result of apply f to every item in xs."
  (accumulate [out []
               _ v (ipairs xs)]
   (let [mapped (f v)]
    (insert out (if (= 0 (select :# mapped))
                     nil
                     mapped)))))

{: dec
 : inc
 : drop
 : group
 : first
 : last}