1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 01:37:19 +00:00

Remove documentation on type error from index-of

Three reasons:
1. This same behavior is not documented on the `next` function
2. This function does not throw the error directly,
   it only throws an error because `next` does.
3. Following the same idea as the previous commit, this behavior is
   more or less implementation-defined for nonsensical types
  > In dynamic languages, the usual idea is garbage in, garbage out.

Various other documentation cleanup.
This commit is contained in:
Techcable 2022-08-26 20:38:57 -07:00
parent a20612478e
commit c099ec05ee
No known key found for this signature in database
GPG Key ID: D7B98ADFF827CD17

View File

@ -1069,9 +1069,7 @@
(defn index-of
``Find the first key associated with a value x in a data structure, acting like a reverse lookup.
Will not look at table prototypes.
Returns `dflt` if not found.
This will throw an error if `ind` is not iterable.``
Returns `dflt` if not found.``
[x ind &opt dflt]
(var k (next ind nil))
(var ret dflt)
@ -1219,18 +1217,16 @@
This includes buffers, dictionaries, arrays, fibers, and possibly abstract types.
For tables and structs, this checks the values, not the keys.
For arrays, tuples (and any other iterable type), this simply checks if any of the values are eqyak.
For arrays, tuples (and any other iterable type), this simply checks if any of the values are equal.
For buffer types (strings, buffers, keywords), this checks if the specified byte is present.
This is because, buffer types (strings, keywords, symbols) are iterable types, with byte values.
This is because, buffer types (strings, keywords, symbols) are simply sequences, with byte values.
This means they will also work with `next` and `index-of`.
However, it also means this function will not check for substrings, only integer bytes.
However, it also means this function will not check for substrings, only integer bytes (which could be unexpected).
In other words is `(contains? "foo bar" "foo")` is always false, because "foo" is not an integer byte
If you want to check for a substring in a buffer, then use `(not (nil? (string/find substr buffer)))`
If the type is not iterable, this will return false.
This is in contrast to `index-of` and `next`, which will throw a type error.
If you want to check for a substring in a buffer, then use `(truthy? (string/find substr buffer))`,
or just `(if (string/find substr buffer) then else)`
In general this function has O(n) performance, since it requires iterating over all the values.