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

mitems with start #579

Closed
juancarlospaco opened this issue Feb 8, 2021 · 5 comments
Closed

mitems with start #579

juancarlospaco opened this issue Feb 8, 2021 · 5 comments

Comments

@juancarlospaco
Copy link
Collaborator

juancarlospaco commented Feb 8, 2021

Working on improving the random.sampleBuffer, I made this change:

proc sampleBuffer*[T](r: var Rand, buffer: var openArray[T]; alphabet: set[T]; start = 0.Natural) =
  # for c in buffer.mitems: c = sample(r, alphabet)              # OLD code.
  for i in start .. buffer.len: buffer[i] = sample(r, alphabet)  # See "start".

I need a mitems but that takes a start: Natural argument, but no such overload exists,
would be very nice because mitems tends to be used for in-place good performance loops!

https://nim-lang.github.io/Nim/iterators.html#mitems.i%2Carray%5BIX%2CT%5D

What if I have a gigabyte sized iterable but only want to iterate only the last 9 items ?.

  • Add an overload to mitems that takes 1 extra argument start: Natural.
  • Or a range 0..9 being start..end, but with the start at least is Ok.

The code of mitems is not massively complex, should be easy to implement.

iterator mitems*[T](a: var openArray[T]): var T {.inline.} =
  var i = 0
  while i < len(a):
    yield a[i]
    inc(i)

Not only performance, but consistency and correctness of the code,
switching mitems with for i in start..end and back again is just wrong.

@timotheecour
Copy link
Owner

but openArray can be sliced (modulo some backend specific bugs, see nim-lang#15952); assuming nim-lang#15952) is fixed, doesnt' a.toOpenArray(start, end) subsume your proposal?

@juancarlospaco
Copy link
Collaborator Author

juancarlospaco commented Feb 9, 2021

toOpenArray is iterator and {.inline.} ?. 🤔

@timotheecour
Copy link
Owner

timotheecour commented Feb 9, 2021

instead of:

proc sampleBuffer*[T](r: var Rand, buffer: var openArray[T]; alphabet: set[T]; start = 0.Natural) = ...
sampleBuffer(r, buf, alpha, start = 2)

use:

proc sampleBuffer*[T](r: var Rand, buffer: var openArray[T]; alphabet: set[T]) = ...
sampleBuffer(r, buf.toOpenArray(2, buf.high), alpha)

@juancarlospaco
Copy link
Collaborator Author

juancarlospaco commented Feb 9, 2021

Then this bug is kinda fixed.

  • But would be nice to document that, on the mitems documentation.

@timotheecour
Copy link
Owner

But would be nice to document that, on the mitems documentation.

not sure it belongs to mitems documentation, toOpenarray likewise can be used in other contexts (eg items and plenty other places). But we should definitely fix nim-lang#15952

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

No branches or pull requests

2 participants