Hacker News new | past | comments | ask | show | jobs | submit login

How difficult is it to add a "zero cost abstraction" like the ones used here? For example, what would it take to make this compile:

    for window in buffer.sliding_window(coefficients.len()) {
        let prediction = coefficients.iter()
                                     .zip(window)
                                     .map(|(&c, &s)| c * s as i64)
                                     .sum::<i64>() >> qlp_shift;
        let delta = buffer[i];
        buffer[i] = prediction as i32 + delta;
    }
with sliding_window returning an iterator over slices of the buffer?



"Zero cost abstraction" to me means a completely "static" abstraction, i.e. there are no runtime mechanisms necessary to implement the abstraction.

Your sliding window is just a series of windows, so there is no reason why it couldn't be compiled in the "most" efficient way. In fact it probably does, just try it out by implementing the sliding_window as an iterable.

However there's always this problem with cleverness: It gets harder and harder to read and maintain. Also http://wiki.c2.com/?SufficientlySmartCompiler


There is a `windows` iterator.

https://doc.rust-lang.org/std/primitive.slice.html#method.wi...

However, your code would not work as-is, since the window borrows the buffer, so `buffer[i]` cannot be written to. Further, there is no `windows_mut` to let you write to the end of the window, because that would let you get multiple mutable references to a given element.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: