Skip to content

Conversation

@LeoMehraban
Copy link
Contributor

The creation of (bounds-check?) fixes a minor problem with the implementation of certain virtual sequences (notably circular), and increases the flexibility of the sequence protocol.

Here's an example of where it could help:

circular has an infinite length, but it also still needs to provide an implementation for the length word. 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 because check-slice used length to check if an index was valid.
Now, check-slice uses bounds-check? (via the helper word at-or-within-bounds?), which itself calls (bounds-check?)
circular now overrides (bounds-check?) to always return true, so you can always create subseqs or slices of it with any length

(The reason (bounds-check?) and bounds-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 is integer, ensuring that the second input is always an integer. This should maybe be simplified, but theoretically, something other than integer could use this functionality, so it's still in for now)

@LeoMehraban
Copy link
Contributor Author

In order to fix some weird build error, and also because it just made sense, I removed (bounds-check?) and fully replaced it with bounds-check?, which now throws an error if not called with an integer

@mrjbq7
Copy link
Member

mrjbq7 commented Oct 19, 2025 via email

@mrjbq7
Copy link
Member

mrjbq7 commented Oct 20, 2025

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, circular-find, circular-subseq?, etc.

@LeoMehraban
Copy link
Contributor Author

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 circular-map, and it allows this functionality to be used by other sequence protocol implementations.

@mrjbq7
Copy link
Member

mrjbq7 commented Oct 21, 2025 via email

@LeoMehraban
Copy link
Contributor Author

This will only help with things like subseq, where I'd say it is similar enough to warrant the same words:

{ 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

@mrjbq7
Copy link
Member

mrjbq7 commented Oct 21, 2025

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 <cycles> word in sequences.repeating.

Although, that doesn't have a start offset...

So, things to change yes, but I'm unsure if allowing bounds check outside of length is a good idea...

@mrjbq7
Copy link
Member

mrjbq7 commented Oct 21, 2025

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 }

@LeoMehraban
Copy link
Contributor Author

Why shouldn't <circular> be an infinite cycle of a sequence? I thought that was kinda the point

@mrjbq7
Copy link
Member

mrjbq7 commented Oct 21, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants