-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.janet
99 lines (82 loc) · 2.39 KB
/
lib.janet
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(def single-line-comment-styles
'{:slashes "//"
:dashes "--"
:hash "#"
:eol (choice "\n" "\r")
:line-comment (sequence :slashes (to :eol) :eol)
:main (capture :line-comment)})
(def multi-line-comment-styles
'{:opening-square-digraph "[-"
:closing-square-digraph "-]"
:opening-slash-digraph "/*"
:closing-slash-digraph "*/"
:comment-block (choice
(sequence
:opening-square-digraph
(to :closing-square-digraph)
:closing-square-digraph)
(sequence
:opening-slash-digraph
(to :closing-slash-digraph)
:closing-slash-digraph))
:main (capture :comment-block)})
(defn test-predicates
[predicates subject]
(map |($ subject) predicates))
(defn meets-all-criteria?
[predicates subject]
(all true?
(test-predicates predicates subject)))
(defn meets-any-criteria?
[predicates subject]
(truthy?
(any? (test-predicates predicates subject))))
(def not-nil? (complement nil?))
(defn has?
[pred ind]
(not-nil? (find pred ind)))
(defn has-equals?
``
Returns true if the indexable contains `val`, and false otherwise.
``
[val ind]
(has? |(= val $) ind))
(defn has-match?
``
Like `has-equals?` but instead of checking for equality, it checks to see if
an element satisfies `peg/match`.
``
[pat ind]
(has? |(peg/match pat $) ind))
(defn break-off
``
Named with regards to the imagery of physically "breaking off" a piece of
some particular item, like, say, a candy bar--splitting it into two separate
chunks; sort of a variable-sized head/tail split.
``
[x ind]
(default ind [])
[(take x ind) (drop x ind)])
(def none? (complement any?))
(defn any-of?
[xs subject]
(any? (map |(= $ subject) xs)))
(defn none-of?
[xs subject]
(none? (map |(= $ subject) xs)))
(defn either?
``
Checks if `subject` is equal to either `x` or `y`
``
[x y subject]
(any-of? [x y] subject))
(defn neither? [x y subject] (not (either? x y subject)))
(def tail (partial drop 1))
(defn hd-tl [x] [(first x) (when (not-nil? x) (tail x))])
(defn join-with
[sep & parts]
(string/join [;parts] sep))
(defn drop-from [x & inds]
(map |(drop x $) [;inds]))
(defn nil-or-empty? [x] (or (nil? x) (empty? x)))
(def not-nil-or-empty? (complement nil-or-empty?))