Hacker News new | ask | show | jobs
by steveklabnik 3321 days ago
You can get rid of the second map using itertools, which has join for iterators, incidentally.

I feel like you can get rid of the inner one too but forget the right combinator.

1 comments

Off topic - If only that worked.

    use self::itertools::join;
    use self::itertools::Itertools;
    ....
    s.lines()
        .map(|bline| UnicodeSegmentation::graphemes(bline, true)    
            .collect::<Vec<&str>>())
        .join(|line| wordwrapline(&line, maxline, maxword),"\n")
error[E0061]: this function takes 1 parameter but 2 parameters were supplied.

Rust is picking the wrong version of "join". There's one in Iter with one parameter and one in Itertools with two parameters. Haven't figured out how to get the one from Itertools yet. The obvious syntax, ".Itertools::join(...)" gets "error: expected `<`, found `join`".

Without either function overloading or member function qualification, how do you do this?

    use self::itertools::join;
This is the free function version; leave it out. I haven't tried this myself, but I'd bet that's what's going on; https://docs.rs/itertools/0.5.6/itertools/trait.Itertools.ht... is different from https://docs.rs/itertools/0.5.6/itertools/fn.join.html, though.
You are getting the join from itertools. There's no join on iterators in the standard library, only on &[T] (which is why the collect is needed).

A quick search of itertools docs indicates there's no join method in it that takes two parameters, only a method that takes a &str like the slice one in std, and a freestanding version of it that takes the iterator as the first argument (instead of as the method's self) and the &str as the second.

Based on reading that documentation, I think steve's original comment was meant to say "second collect", not "second map".