-
Notifications
You must be signed in to change notification settings - Fork 51
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
Reverse iteration #76
Comments
Just to give an example use case: Let's say we need to find the first number in a List that is greater that 10, but searching from the back to the front. Here is how we could write it using import * as L from "list";
function firstOverTenFromBack(list: List<number>): number | null {
for (const num of list.reverse()) {
if (num > 10) {
return num;
}
}
return null;
}
const myList = L.from([1, 5, 20, 16, 4, 5, 11, 2, 5]);
console.log(firstOverTenFromBack(myList)); // 11 But this is very inefficient when the List is large. |
This is a great idea and relates slightly to #75. What are you actually most interested in a I'd like to add both but I'd also like to work on the most "important" one first. |
I personally would be interested in a reverse iterator, so I can use it with regular for loops. My personal style is to try to use for loops when possible, rather than a functional style, in order to keep the code familiar for other javascript programmers. |
Then you should not use Also reversing the list should be |
Maybe I am wrong. Looks like the underlying data structure is a JS array... |
The underlying data structure is a relaxed radix balanced tree. Since the data structure is symmetric it actually is possible to reverse it in
I will be happy to add a backwards iterator. But, the alternatives to |
What do you think about the name for (const element of backwards(myList)) {
...
} |
that |
@benny-medflyt I've finally gotten around to add the |
This is now release as part of Thanks a lot for the feature request @benny-medflyt and for your patience 😉 |
I can't find a way to loop over a List backwards. The only way seems to be to use
reverse
, but this is very inefficient for large lists.There is
reduceRight
/foldr
, but those don't allow for early termination.I think we need a function
(Similar to C++
rbegin
/rend
)The text was updated successfully, but these errors were encountered: