-
-
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 Foldable.intercalate #1520
Add Foldable.intercalate #1520
Conversation
Current coverage is 92.41% (diff: 100%)@@ master #1520 diff @@
==========================================
Files 246 246
Lines 3739 3743 +4
Methods 3618 3622 +4
Messages 0 0
Branches 121 121
==========================================
+ Hits 3455 3459 +4
Misses 284 284
Partials 0 0
|
Thanks @peterneyens. This looks good to me. The string example is the main way I could see myself using this in the real world. Out of curiosity, did you have any other use-cases in mind? If we add this, should we add something like |
I added |
3a4c479
to
b859ea3
Compare
@peterneyens would you be willing to add a test for |
Actually I guess the sbt-doctest examples are pretty legitimate tests. 👍 from me. |
* res0: String = a-b-c | ||
* scala> Foldable[Vector].intercalate(Vector(1,2,3), 1) | ||
* res1: Int = 8 | ||
* }}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not clear what we should expect if there is only 1 item in the foldable (nor is it tested). I would expect no additional item is added. I think that is what the code does but can you add a doc test example as well as a test?
@@ -67,6 +67,14 @@ abstract class FoldableCheck[F[_]: Foldable](name: String)(implicit ArbFInt: Arb | |||
fa.reduceRightOption((x, ly) => ly.map(x - _)).value should === (list.reduceRightOption(_ - _)) | |||
} | |||
} | |||
|
|||
test("intercalate") { | |||
forAll { (fa: F[Int], a: Int) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test might be clearer if you tested against mkString. Basically implement mkString with intercalate.
Also, using a commutative monoid here (on Int) weakens the test. Using String, which is not commutative would make it stronger.
Good points @johnynek. |
@peterneyens yeah I need it for generating SQL … I also did a more general thing that takes start/end brackets as well as the delimiter, which generalizes In any case 👍 for this, with test improvements as suggested above. |
👍 after test improvements. |
Thanks for the pointers @johnynek
|
755f9e9
to
d02699e
Compare
+1 on this. |
Let's do this |
one bummer about this: using it to write The simplest thing would be to use |
@johnynek, so something like the following ? def intercalate[A](fa: F[A], a: A)(implicit A: Monoid[A]): A =
combineAll(intersperseList(toList(fa), a))
private def intersperseList[A](xs: List[A], x: A): List[A] = {
val bld = List.newBuilder[A]
val it = xs.iterator
if (it.hasNext) {
bld += it.next
while(it.hasNext) {
bld += x
bld += it.next
}
}
bld.result
}
// Reducible
def intercalate1[A](fa: F[A], a: A)(implicit A: Semigroup[A]): A =
reduce(intersperseList(toList(fa), a)) |
Exactly.
…On Mon, Jan 9, 2017 at 08:15 Peter Neyens ***@***.***> wrote:
@johnynek <https://github.com/johnynek>, so something like the following ?
def intercalate[A](fa: F[A], a: A)(implicit A: Monoid[A]): A =
combineAll(intersperseList(toList(fa), a))
private def intersperseList[A](xs: List[A], x: A): List[A] = {
val bld = List.newBuilder[A]
val it = xs.iterator
if (it.hasNext) {
bld += it.next
while(it.hasNext) {
bld += x
bld += it.next
}
}
bld.result
}
// Reducible def intercalate1[A](fa: F[A], a: A)(implicit A: Semigroup[A]): A =
reduce(intersperseList(toList(fa), a))
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1520 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAEJdggEg7EnzO0mQ-maRCPnVmPQvOCeks5rQnkjgaJpZM4LcrW->
.
|
Add an element between all the existing elements while folding.