stacycurl / pimpathon

Adds useful methods to scala & java classes.
Apache License 2.0
35 stars 9 forks source link

GTL[A].seqFold(B)((B, A) => Option[B]): Option[B] #193

Closed stacycurl closed 9 years ago

stacycurl commented 9 years ago

Is seqMap the right name for the existing method ?

stacycurl commented 9 years ago

Another scenario: I want to tee values into an accumulator and use that to terminate the builder.

def seqScanLeft[B, Acc, To](f: A ⇒ B, zero: Acc)(p: Acc ⇒ B ⇒ Boolean, step: Acc ⇒ B ⇒ Acc)(implicit cbf: CanBuildFrom[Nothing, B, To]): Option[To]

or def seqScanLeft[B, Acc, To](z: B, Acc)(op: (B, A, Acc) => Option[(B, Acc)])(implicit cbf: CBF[Nothing, B, To]): Option[To]

fommil commented 9 years ago

I find this type signature to be very confusing.

stacycurl commented 9 years ago

It's a left fold that can exit (with None) at any step with the result being None, i.e. you only get Some if op always return Some.

Scalaz has a more general method def foldLeftM[G[_], A, B](fa: F[A], z: B)(f: (B, A) => G[B])(implicit M: Monad[G]): G[B] which when G == Option is seqFold.

Any suggestions what it could be called that might be less confusing ? exitEarlyFold, foldLeftOption, mapOption ? I'm happy to rename them, I've never been happy with their names.

seqFold is now used to implement seqMap which itself was only recently generalised from List to GTL; previously seqMap was implemented in terms of a private method apoMap which implemented (I think) an apomorphism (http://stackoverflow.com/questions/11350341/how-can-i-interleave-elements-of-2-lists-in-scala), a fold that can exit early with a complete result (unlike with no result for seqFold).

fommil commented 9 years ago

how about foldLeftWithEarlyExit and some docs?

If scalaz has something, is it really needed in pimpathon? It feels like this is veering into "niche use" territory, which dilutes the value of the core pimpathon library (which IMHO contains only a few easily understandable pimps).

stacycurl commented 9 years ago

I needed seqFold for List[A].asMap.withUniqueKeys & didn't want to add back scalaz into core, it doesn't necessarily have to be public though (apoMap never was).

SeqMap was originally added because Rory added something similar to starling.

Do the unit tests make the pimps any more understandable ? I'm thinking of some approach that can gen docs from them.

On 18 February 2015 at 09:23, Sam Halliday notifications@github.com wrote:

how about foldLeftWithEarlyExit and some docs?

If scalaz has something, is it really needed in pimpathon? It feels like this is veering into "niche use" territory, which dilutes the value of the core pimpathon library (which IMHO contains only a few easily understandable pimps).

— Reply to this email directly or view it on GitHub https://github.com/stacycurl/pimpathon/issues/193#issuecomment-74834770.

Stacy

stacycurl commented 9 years ago

I needed seqFold for List[A].asMap.withUniqueKeys & didn't want to add back scalaz into core, it doesn't necessarily have to be public though (apoMap never was).

SeqMap was originally added because Rory added something similar to starling.

I feel the release notes were a start in making pimpathon more understandable, next I plan to create some table of contents, starting with the sorted concatenated release notes & then adding explanations and examples (hopefully generated from tests) to them.

I'm very happy if others get value from pimpathon but I want to continue to experiment with data structure transformations (probably the least understandable bit) that aren't served by a type-class approach (or not served well). Even scalaz has some of these (they have a MapOps & others with methods that don't depend on type classes). One person's niche is another playground (mine), hopefully with better documentation (and/or method renaming) these won't interfere.

On 18 February 2015 at 09:23, Sam Halliday notifications@github.com wrote:

how about foldLeftWithEarlyExit and some docs?

If scalaz has something, is it really needed in pimpathon? It feels like this is veering into "niche use" territory, which dilutes the value of the core pimpathon library (which IMHO contains only a few easily understandable pimps).

— Reply to this email directly or view it on GitHub https://github.com/stacycurl/pimpathon/issues/193#issuecomment-74834770.

Stacy

fommil commented 9 years ago

I don't personally need the docs because I know how its structured and what the philosophy is, but to appeal to people coming to the project an overview (mission statement maybe?) and some examples would be good.