Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> The second approach is open for extension - it allows you to write new functions on old datatypes.

I prefer to just generalize the function (make it generic, leverage traits/typeclasses) tbh.

> Probably for lack of > weird operators like <$>, <*>, $, or >>=

Nope btw. I mean, maybe? I don't know Haskell well enough to say. The answer that I was looking for here is a specific Rust idiosyncrasy. It doesn't allow you to import `std::iter::Iterator::collect` on its own. It's an associated function, and needs to be qualified. (So you need to write `Iterator::collect` at the very least.)



> It doesn't allow you to import `std::iter::Iterator::collect` on its own. It's an associated function, and needs to be qualified.

You probably noticed, but it should become a thing in RFC 3591: https://github.com/rust-lang/rust/issues/134691

So it does kind of work on current nightly:

  #![feature(import_trait_associated_functions)]
  
  use std::iter::Iterator::{filter, map, collect};

  fn get_ids2(data: Vec<Widget>) -> Vec<Id> {
      collect(map(filter(Vec::into_iter(data), |w| w.alive), |w| w.id))
  }  

  fn get_ids3(data: impl Iterator<Item = Widget>) -> Vec<Id> {
      collect(map(filter(data, |w| w.alive), |w| w.id))
  }


Oh, interesting! Thank you, I did not know about that, actually.




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

Search: