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

skipping #18

Open
nilsbecker opened this issue May 29, 2019 · 6 comments
Open

skipping #18

nilsbecker opened this issue May 29, 2019 · 6 comments

Comments

@nilsbecker
Copy link

i have not found a way to take only every nth element -- is this possible? have i missed it? more generally, one could think of a list of 'step lengths' or 'skip lengths' to apply cyclically as one moves over the generator, i.e. [0;2;1] means 'take next element, then skip two and take one, then skip one and take one, then take the next element, etc.,..'

@nilsbecker
Copy link
Author

right now i am using chunks together with Array.get which seems not ideal.

@c-cube
Copy link
Owner

c-cube commented May 29, 2019

I think skip would be useful (it's pretty simple to write I think, with a tiny state machine). The more general version makes me more dubious, it seems hard to explain and rarely useful. Skip could look like:

let skip n gen =
  let i = ref n in
  let rec next() =
    match gen() with
    | None -> None
    | Some x when !i = 1 ->
      i := n; Some x
    | Some _ ->
      decr i; next()
  in next

@nilsbecker
Copy link
Author

i would agree that the simple one is probably enough in >90% of the cases. the more general one just occurred to me by imagining a scenario like "i need successive pairs which occur every 7 elements" or something like that. however, then one could probably just use chunks and pay for array construction. another way of implementing general skipping would be to zip together two generators; one for the jump lengths and another one for the value stream. again, probably not widely useful.

@nilsbecker
Copy link
Author

nilsbecker commented May 29, 2019

about naming: skip 4 to me means that every 5th element should be taken. so one has to be careful; maybe skip is not a good name, despite its ubiquity

@c-cube
Copy link
Owner

c-cube commented May 30, 2019

take_every ~nth:5 ?

@nilsbecker
Copy link
Author

that's very clear i find. one could argue that take_every 5 is already enough but i guess that's taste

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

No branches or pull requests

2 participants