-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add contains, collectFirst and mkString to NonEmptyList #2113
Comments
Contains would need an Eq and mkString should take a Show. |
I've needed |
I would prefer to add contains and mkstring to Foldable too, but we already passed the deadline for non bug bin compat breaking PRs... |
Yeah, I failed to think about that. 😞 To speak to the usefulness of the request, I have a syntax extension with the same three methods, and feel like a jerk for not creating a PR in time. |
|
@tpolecat actually Semigroups can be generalized in this way: if a+b is monoid, then a+z+b is a semigroup for all z. That’s one way to think about this: you are transforming the Monoid and then doing a combine. |
I ended up with def mkString[A](fa: F[A], prefix: String, delim: String, suffix: String)(implicit F: Functor[F], A: Show[A], M: Monoid[String]): String =
M.combine(prefix, M.combine(intercalate(F.map(fa)(A.show), delim), suffix))
def mkString[A](fa: F[A], delim: String)(implicit ev1: Functor[F], ev2: Show[A], M: Monoid[String]): String =
mkString(fa, M.empty, delim, M.empty) And def contains[A](fa: F[A])(v: A)(implicit ev: Eq[A]): Boolean =
exists(fa)(ev.eqv(_, v)) But it feels a bit strange. I couldn't find a use-case to abstract out the String. What do you think? |
Hey @rsoeldner, first of all thanks a lot for bringing this up! def foldSmash[A: Monoid](fa: F[A], prefix: A, delim: A, suffix: A): A =
prefix |+| intercalate(fa, delim) |+| suffix Not sure if these would be binary compatible, if so, we could easily bring them in for the next release :) |
I'm guessing that there aren't many instances that would actually override these values. So putting some of these methods in the |
@LukaJCB Thank you for the help. |
For this use-case we can always coerce to necessary type: val ints = List(1,2,3,4)
ints.map(_.show).foldSmash("prefix", "-", "suffix")
ints.map(YourMonoid.fromInt).foldSmash(prefix, delim, suffix) I think this is right balance between too generic and special implementations. |
@rsoeldner should this be closed out now that #2123 is merged? |
@ceedubs yes 👍 |
I would like to add these functionality to NonEmptyList, what do you think ?
I'm quite unsure about my mkString 😄
The text was updated successfully, but these errors were encountered: