Skip to content
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 Iterant.interleave #524

Merged
merged 6 commits into from Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion AUTHORS
Expand Up @@ -35,4 +35,7 @@ Ryo Fukumuro
https://github.com/rfkm

Ian McIntosh
https://github.com/cranst0n
https://github.com/cranst0n

Denys Zadorozhnyi
https://github.com/greenhat
20 changes: 20 additions & 0 deletions monix-tail/shared/src/main/scala/monix/tail/Iterant.scala
Expand Up @@ -1507,6 +1507,26 @@ sealed abstract class Iterant[F[_], A] extends Product with Serializable {
final def tail(implicit F: Sync[F]): Iterant[F, A] =
IterantTail(self)(F)

/** Lazily interleaves two iterants together, starting with the first
* element from `self`.
*
* The length of the result will be the shorter of the two
* arguments.
*
* Example: {{{
* val lh = Iterant[Task].of(11, 12)
* val rh = Iterant[Task].of(21, 22, 23)
*
* // Yields 11, 21, 12, 22
* lh.interleave(rh)
* }}}
*
* @param rhs is the other iterant to interleave the source with (the
* right hand side)
*/
final def interleave[B >: A](rhs: Iterant[F, B])(implicit F: Sync[F]): Iterant[F, B] =
IterantInterleave(self, rhs)(F)

/** Converts this `Iterant` into an `org.reactivestreams.Publisher`.
*
* Meant for interoperability with other Reactive Streams
Expand Down
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2014-2018 by The Monix Project Developers.
* See the project homepage at: https://monix.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package monix.tail.internal

import cats.effect.Sync
import monix.tail.Iterant


private[tail] object IterantInterleave {
def apply[F[_], A, B >: A](lh: Iterant[F, A], rh: Iterant[F, B]) (implicit F: Sync[F]): Iterant[F, B] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small comment — you don't need the subtyping relationship to avoid covariance issues here, i.e. you don't need a B type parameter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or in other words, it's OK to have it on the method in Iterant, but not on this function here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexandru If I remove B in apply:

def apply[F[_], A](lh: Iterant[F, A], rh: Iterant[F, A]) (implicit F: Sync[F]): Iterant[F, A]

I get error:

[error] monix/monix-tail/shared/src/main/scala/monix/tail/Iterant.scala:1528:23: type mismatch;
[error]  found   : monix.tail.Iterant[F,A]
[error]  required: monix.tail.Iterant[F,B]
[error] Note: A <: B, but class Iterant is invariant in type A.
[error] You may wish to define A as +A instead. (SLS 4.5)
[error]     IterantInterleave(self, rhs)(F)
[error]                       ^

Do you want to remove B from Iterant.interleave?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, true, Iterant is invariant for now as a design decision having to do with covariance not mixing well with higher kinds, but you can use self.upcast[B] in that call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexandru Thanks! Done. I saw upcast but did not think of using it here.

rh
}
}
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2014-2018 by The Monix Project Developers.
* See the project homepage at: https://monix.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package monix.tail

import cats.effect.Sync
import monix.eval.Coeval
import monix.execution.internal.Platform
import org.scalacheck.Test
import org.scalacheck.Test.Parameters

object IterantInterleaveSuite extends BaseTestSuite {
override lazy val checkConfig: Parameters = {
if (Platform.isJVM)
Test.Parameters.default.withMaxSize(256)
else
Test.Parameters.default.withMaxSize(32)
}

private def naiveImp[F[_], A, B >: A](lh: Iterant[F, A], rh: Iterant[F, B])
(implicit F: Sync[F]): Iterant[F, B] =
lh.zip(rh).flatMap { case (a, b) => Iterant[F].pure(a) ++ Iterant[F].pure(b) }

test("naiveImp smoke test") { implicit s =>
assertEquals(naiveImp(
Iterant[Coeval].of(11, 12),
Iterant[Coeval].of(21, 22, 23)).toListL.value,
List(11, 21, 12, 22))
assertEquals(naiveImp(
Iterant[Coeval].of(11, 12),
Iterant[Coeval].of(21)).toListL.value,
List(11, 21))
}

// TODO equivalence test with naive imp

// TODO various scenarios (stop on error, etc.)

}