janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.43k stars 221 forks source link

Add `lengthable?` #1280

Closed primo-ppcg closed 11 months ago

primo-ppcg commented 11 months ago

Related: #1279

Check if x is a bytes, indexed, or dictionary.


- Speed up `keys`, `values`, and `pairs`, while allowing non-lengthable types
- Allow non-lengthable types for `reverse`
- Minor speed improvement for `reverse!`
- Updates some docstrings missed in previous PRs
sogaiu commented 11 months ago

Various tests didn't turn up anything here :+1:

Some manual trials:

$ ./build/janet
Janet 1.30.0-4cb1f616 linux/x64/gcc - '(doc)' for help

repl:1:> (lengthable? :a)
true

repl:2:> (lengthable? "a")
true

repl:3:> (lengthable? 1)
false

repl:4:> (lengthable? [:x :y])
true

repl:5:> (lengthable? (peg/compile '':a))
false

repl:6:> (lengthable? {:x 1})
true