Coder Social home page Coder Social logo

split iterator in two? about itertools HOT 6 CLOSED

dev-ardi avatar dev-ardi commented on August 20, 2024
split iterator in two?

from itertools.

Comments (6)

scottmcm avatar scottmcm commented on August 20, 2024 3

That's actually my go-to example of a horrible API, particularly because the closures are in the opposite order from what everyone expects it to be (which is even worse in the Result version).

But it also shows how things like foo.map_or_else(|| local + 2, |y| local * y) end up not working because it tried to move local twice, whereas if you just write

if let Some(y) = foo { local + 2 } else { local * y }

it works just fine, and it's more direct anyway.

Basically, whenever you have two FnOnce closures where only one can be called, it works particularly badly, because the signature doesn't have a way to communicate that "not both" fact.

It's not as bad when it's multiple closures called multiple times each, but it's still a sign that it might not be a good abstraction.

from itertools.

scottmcm avatar scottmcm commented on August 20, 2024 2

I wouldn't bother adding that, because multi-callback things like that are usually bad.

That one would, for example, I think be better done with something like

.filter_map(|x| if x % 2 == 0 { Some(x) } else { println!("{x} was odd!"); None })

from itertools.

scottmcm avatar scottmcm commented on August 20, 2024 1

You're looking for https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.partition or https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.partition_map

It can't be done lazily.

from itertools.

dev-ardi avatar dev-ardi commented on August 20, 2024

It can't be done lazily.

Yeah you're right, I thought that it could be possible. What about something like

fn filter_callback<P, F>(self, predicate: P, callback: F) -> FilterCallback<Self, P, F>
where
    Self: Sized,
    P: FnMut(&Self::Item) -> bool,
    F: FnMut(&Self::Item)

To do something like

let iter = [1,2,3,4,5].iter().filter_callback(|x| (x % 2 == 0), |x| println!("{x} was odd!"));

from itertools.

dev-ardi avatar dev-ardi commented on August 20, 2024

Completely fair.

from itertools.

dev-ardi avatar dev-ardi commented on August 20, 2024

multi-callback things like that are usually bad.

What about things like Option::map_or_else?

from itertools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.