Skip to content
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

feat: Add the Skip higher-order stream #72

Merged
merged 3 commits into from
Feb 4, 2025

Conversation

Hywan
Copy link
Collaborator

@Hywan Hywan commented Jan 24, 2025

This patch adds the Skip higher-order stream. Similarly to Head or Tail, it computes a limited view of the underlying ObservableVector's items. The view starts after count number of values are skipped, until the end. It must not be confused with Tail where Tail keeps the last values, while Skip skips the first values.

use eyeball_im::{ObservableVector, VectorDiff};
use eyeball_im_util::vector::VectorObserverExt;
use imbl::vector;
use stream_assert::{assert_closed, assert_next_eq, assert_pending};

// Our vector.
let mut ob = ObservableVector::<char>::new();
let (values, mut sub) = ob.subscribe().skip(3);

assert!(values.is_empty());
assert_pending!(sub);

// Append multiple values.
ob.append(vector!['a', 'b', 'c', 'd', 'e']);
// We get a `VectorDiff::Append` with the latest 2 values because the
// first 3 values have been skipped!
assert_next_eq!(sub, VectorDiff::Append { values: vector!['d', 'e'] });

// Let's recap what we have. `ob` is our `ObservableVector`,
// `sub` is the “limited view” of `ob`:
// | `ob`  | a b c d e |
// | `sub` | _ _ _ d e |
// “_” means the item has been skipped.

// Append multiple values.
ob.append(vector!['f', 'g']);
// We get a single `VectorDiff`.
assert_next_eq!(sub, VectorDiff::Append { values: vector!['f', 'g'] });

// Let's recap what we have:
// | `ob`  | a b c d e f g |
// | `sub` | _ _ _ d e f g |
//                     ^^^
//                     |
//                     `VectorDiff::Append { .. }`

// Insert a single value.
ob.insert(1, 'h');
// We get a single `VectorDiff::PushFront`. Indeed, `h` is inserted at
// index 1, so every value after that is shifted to the right, thus `c`
// “enters the view” via a `PushFront`.
assert_next_eq!(sub, VectorDiff::PushFront { value: 'c' });

// Let's recap what we have:
// | `ob`  | a h b c d e f g |
// | `sub` | _ _ _ c d e f g |
//                 ^
//                 |
//                 `VectorDiff::PushFront { .. }`

assert_pending!(sub);
drop(ob);
assert_closed!(sub);

@Hywan Hywan force-pushed the feat-im-util-skip branch 3 times, most recently from 777134d to bc8cadb Compare January 27, 2025 08:37
@Hywan Hywan marked this pull request as ready for review January 27, 2025 08:40
@@ -0,0 +1,2 @@
[default.extend-words]
skeep = "skeep"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not finding this word in a dictionary, what's going on here? ^^

Copy link
Collaborator Author

@Hywan Hywan Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, it's skip but I don't to use the same name as skip to highlight it does a difference thing. If you don't like that, I can fallback to skip, not a big deal after all. If you check the Skeep trait and its implementation, you'll notice that the skeep method on the Vector implementation tries to be smart than the Vector::skip method to avoid useless clones or splits.

@Hywan Hywan force-pushed the feat-im-util-skip branch from bc8cadb to c4bf107 Compare January 29, 2025 15:46
@Hywan Hywan force-pushed the feat-im-util-skip branch from c4bf107 to 23104ae Compare January 29, 2025 15:50
@Hywan
Copy link
Collaborator Author

Hywan commented Jan 29, 2025

(rebased on top of main)

Comment on lines +528 to +539
fn skeep(self, count: usize) -> Self {
match count {
// Skip 0 values, let's return all of them.
0 => self,

// Skip more values than `self` contains, let's return an empty `Vector`.
count if count >= self.len() => Vector::new(),

// Skip the first n values.
count => self.skip(count),
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not submit a PR to imbl to make the upstream skip more efficient like this? It even has a fixme in there about making it more efficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it then 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jneem/imbl#104 PR is here

@Hywan Hywan requested a review from jplatte February 4, 2025 07:24
@jplatte jplatte merged commit b4ca899 into jplatte:main Feb 4, 2025
6 checks passed
@Hywan
Copy link
Collaborator Author

Hywan commented Feb 4, 2025

Oh you merged it anyway? I'll open a follow-up PR as soon as imbl merges my patch.

@jplatte
Copy link
Owner

jplatte commented Feb 4, 2025

Yeah, I saw that it's a private API anyways. I figured it might take a while until imbl releases a new version with that improvement.

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