Skip to content

Commit 0fcbda2

Browse files
authored
Merge pull request #1017 from Techcable/feature/helper-func-contains
Add `contains?` helper function to boot.janet
2 parents 14e33c2 + e78a3d1 commit 0fcbda2

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/boot/boot.janet

+41
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,47 @@
11961196
(def kw (keyword prefix (slice alias 1 -2)))
11971197
~(def ,alias :dyn ,;more ,kw))
11981198

1199+
1200+
(defn contains-key?
1201+
```Checks if a collection contains the specified key.
1202+
1203+
Semantically equivalent to `(not (nil? (get collection key)))`.
1204+
1205+
Arrays, tuples, and buffer types (string, buffer, keyword, symbol) are all indexed by integer keys.
1206+
For those types, this function simply checks if the index is less than the length.
1207+
1208+
If this function succeeds, then a call to `(in collection key)` is guarenteed
1209+
to succeed as well.
1210+
1211+
Note that tables or structs (dictionaries) never contain null keys```
1212+
[collection key]
1213+
(not (nil? (get collection key))))
1214+
1215+
(defn contains?
1216+
```Checks if a collection contains the specified value.
1217+
1218+
This supports any iterable type by way of the `next` function.
1219+
This includes buffers, dictionaries, arrays, fibers, and possibly abstract types.
1220+
1221+
For tables and structs, this checks the values, not the keys.
1222+
For arrays, tuples (and any other iterable type), this simply checks if any of the values are equal.
1223+
1224+
For buffer types (strings, buffers, keywords), this checks if the specified byte is present.
1225+
This is because, buffer types (strings, keywords, symbols) are simply sequences, with byte values.
1226+
This means they will also work with `next` and `index-of`.
1227+
1228+
However, it also means this function will not check for substrings, only integer bytes (which could be unexpected).
1229+
In other words is `(contains? "foo bar" "foo")` is always false, because "foo" is not an integer byte
1230+
If you want to check for a substring in a buffer, then use `(truthy? (string/find substr buffer))`,
1231+
or just `(if (string/find substr buffer) then else)`
1232+
1233+
In general this function has O(n) performance, since it requires iterating over all the values.
1234+
1235+
Note that tables or structs (dictionaries) never contain null values```
1236+
[collection val]
1237+
(not (nil? (index-of val collection))))
1238+
1239+
11991240
(defdyn *defdyn-prefix* ``Optional namespace prefix to add to keywords declared with `defdyn`.
12001241
Use this to prevent keyword collisions between dynamic bindings.``)
12011242
(defdyn *out* "Where normal print functions print output to.")

test/suite0010.janet

+68
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,74 @@
3333
(assert (= nil (index-of (chr "a") "")) "index-of 9")
3434
(assert (= nil (index-of 10 @[])) "index-of 10")
3535
(assert (= nil (index-of 10 @[1 2 3])) "index-of 11")
36+
# NOTE: These is a motivation for the contains? and contains-key? functions below
37+
38+
# returns false despite key present
39+
(assert (= false (index-of 8 {true 7 false 8})) "index-of corner key (false) 1")
40+
(assert (= false (index-of 8 @{false 8})) "index-of corner key (false) 2")
41+
# still returns null
42+
(assert (= nil (index-of 7 {false 8})) "index-of corner key (false) 3")
43+
44+
# contains?
45+
(assert (= false (contains? [] "foo")) "contains? 1")
46+
(assert (= true (contains? [4 7 1 3] 4)) "contains? 2")
47+
(assert (= false (contains? [4 7 1 3] 22)) "contains? 3")
48+
(assert (= false (contains? @[1 2 3] 4)) "contains? 4")
49+
(assert (= true (contains? @[:a :b :c] :a)) "contains? 5")
50+
(assert (= false (contains? {} :foo)) "contains? 6")
51+
(assert (= true (contains? {:a :A :b :B} :A)) "contains? 7")
52+
(assert (= true (contains? {:a :A :b :B} :A)) "contains? 7")
53+
(assert (= true (contains? @{:a :A :b :B} :A)) "contains? 8")
54+
(assert (= true (contains? "abc" (chr "a"))) "contains? 9")
55+
(assert (= false (contains? "abc" "1")) "contains? 10")
56+
# weird true/false corner cases, should align with "index-of corner key {k}" cases
57+
(assert (= true (contains? {true 7 false 8} 8)) "contains? corner key (false) 1")
58+
(assert (= true (contains? @{false 8} 8)) "contains? corner key (false) 2")
59+
(assert (= false (contains? {false 8} 7)) "contains? corner key (false) 3")
60+
61+
# contains-key?
62+
(do
63+
(var test-contains-key-auto 0)
64+
(defn test-contains-key [col key expected &keys {:name name}]
65+
``Test that contains-key has the outcome `expected`, and that if
66+
the result is true, then ensure (in key) does not fail either``
67+
(assert (boolean? expected))
68+
(default name (string "contains-key? " (++ test-contains-key-auto)))
69+
(assert (= expected (contains-key? col key)) name)
70+
(if
71+
# guarenteed by `contains-key?` to never fail
72+
expected (in col key)
73+
# if `contains-key?` is false, then `in` should fail (for indexed types)
74+
#
75+
# For dictionary types, it should return nil
76+
(let [[success retval] (protect (in col key))]
77+
(def should-succeed (dictionary? col))
78+
(assert
79+
(= success should-succeed)
80+
(string/format
81+
"%s: expected (in col key) to %s, but got %q"
82+
name (if expected "succeed" "fail") retval)))))
83+
84+
(test-contains-key [] 0 false) # 1
85+
(test-contains-key [4 7 1 3] 2 true) # 2
86+
(test-contains-key [4 7 1 3] 22 false) # 3
87+
(test-contains-key @[1 2 3] 4 false) # 4
88+
(test-contains-key @[:a :b :c] 2 true) # 5
89+
(test-contains-key {} :foo false) # 6
90+
(test-contains-key {:a :A :b :B} :a true) # 7
91+
(test-contains-key {:a :A :b :B} :A false) # 8
92+
(test-contains-key @{:a :A :b :B} :a true) # 9
93+
(test-contains-key "abc" 1 true) # 10
94+
(test-contains-key "abc" 4 false) # 11
95+
# weird true/false corner cases
96+
#
97+
# Tries to mimic the corresponding corner cases in contains? and index-of,
98+
# but with keys/values inverted
99+
#
100+
# in the first two cases (truthy? (get val col)) would have given false negatives
101+
(test-contains-key {7 true 8 false} 8 true :name "contains-key? corner value (false) 1")
102+
(test-contains-key @{8 false} 8 true :name "contains-key? corner value (false) 2")
103+
(test-contains-key @{8 false} 7 false :name "contains-key? corner value (false) 3"))
36104

37105
# Regression
38106
(assert (= {:x 10} (|(let [x $] ~{:x ,x}) 10)) "issue 463")

0 commit comments

Comments
 (0)