-
Notifications
You must be signed in to change notification settings - Fork 216
sequences: created (bounds-check?) generic word #3148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
sequences: created (bounds-check?) generic word #3148
Conversation
|
In order to fix some weird build error, and also because it just made sense, I removed |
|
Could also use “integer check-instance”
|
|
I also wonder if a better solution is maybe a few more circular primitives in the circular vocabulary, rather that continuing to extend the sequences protocol. Perhaps, |
|
I'm not sure. I feel like making the sequence protocol more flexible (especially with very easy fixes like these) is better, because it doesn't make people use new words like |
|
I think it’s different enough that it might require different words.
But could you share some code examples that you hope to work, and how this fits in?
Thanks!
|
|
This will only help with things like { 1 2 3 } <circular> [ 0 8 ] dip subseq .
! prints { 1 2 3 1 2 3 1 2 }that example would throw an out-of-bounds error before { 1 2 3 } <circular> 7 [0..b] [ swap nth ] with map .
! prints { 1 2 3 1 2 3 1 2 }this example, which should be equivalent, worked before. I say that these should be equivalent because { 1 2 3 1 2 3 1 2 3 1 2 3 } [ 0 8 ] dip subseq .and { 1 2 3 1 2 3 1 2 3 1 2 3 } 7 [0..b] [ swap nth ] with map .are equivalent |
|
I sort of think circular should throw an error if trying to access an nth index outside of the underlying length. Partly this is because i've always thought about it as a ring buffer, not an infinite cycle of a sequence. And that leads to the idea of having circular have a start and a length, and repeat in cycles. Which is kind of like the Although, that doesn't have a So, things to change yes, but I'm unsure if allowing bounds check outside of length is a good idea... |
IN: scratchpad { 1 2 3 } 8 <cycles> >array .
{ 1 2 3 1 2 3 1 2 }
IN: scratchpad { 1 2 3 } 8 <cycles> 1 over circular>> change-circular-start >array .
{ 2 3 1 2 3 1 2 3 } |
|
Why shouldn't |
|
Because sequences aren’t infinite.
|
The creation of
(bounds-check?)fixes a minor problem with the implementation of certain virtual sequences (notablycircular), and increases the flexibility of the sequence protocol.Here's an example of where it could help:
circularhas an infinite length, but it also still needs to provide an implementation for thelengthword. It does this by returning the length of the underlying sequence.This created a problem with the use of words like
subseq, where users couldn't access indices outside of the underlying sequence. This happened becausecheck-sliceusedlengthto check if an index was valid.Now,
check-sliceusesbounds-check?(via the helper wordat-or-within-bounds?), which itself calls(bounds-check?)circularnow overrides(bounds-check?)to always return true, so you can always create subseqs or slices of it with any length(The reason
(bounds-check?)andbounds-check?are separate is because the latter is implemented as a generic word that dispatches on the numeric input. It seems like the only implementation of this isinteger, ensuring that the second input is always an integer. This should maybe be simplified, but theoretically, something other thanintegercould use this functionality, so it's still in for now)