-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from armanbilge/feature/signal-monad
Add `Monad[Signal]`
- Loading branch information
Showing
5 changed files
with
226 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2022 Arman Bilge | ||
* | ||
* 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. | ||
*/ | ||
|
||
/* | ||
* Copyright (c) 2013 Functional Streams for Scala | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package calico | ||
package frp | ||
|
||
import cats.Monad | ||
import cats.StackSafeMonad | ||
import cats.data.OptionT | ||
import cats.effect.kernel.Concurrent | ||
import cats.syntax.all.* | ||
import fs2.Pull | ||
import fs2.Stream | ||
import fs2.concurrent.Signal | ||
|
||
given [F[_]: Concurrent]: Monad[Signal[F, _]] = new StackSafeMonad[Signal[F, _]]: | ||
def pure[A](a: A) = Signal.constant(a) | ||
|
||
override def map[A, B](siga: Signal[F, A])(f: A => B) = | ||
Signal.mapped(siga)(f) | ||
|
||
def flatMap[A, B](siga: Signal[F, A])(f: A => Signal[F, B]) = new: | ||
def get = siga.get.flatMap(f(_).get) | ||
def continuous = Stream.repeatEval(get) | ||
def discrete = siga.discrete.switchMap(f(_).discrete) | ||
|
||
override def ap[A, B](ff: Signal[F, A => B])(fa: Signal[F, A]) = | ||
new: | ||
def discrete: Stream[F, B] = | ||
nondeterministicZip(ff.discrete, fa.discrete).map(_(_)) | ||
def continuous: Stream[F, B] = Stream.repeatEval(get) | ||
def get: F[B] = ff.get.ap(fa.get) | ||
|
||
private def nondeterministicZip[A0, A1]( | ||
xs: Stream[F, A0], | ||
ys: Stream[F, A1] | ||
): Stream[F, (A0, A1)] = | ||
type PullOutput = (A0, A1, Stream[F, A0], Stream[F, A1]) | ||
|
||
val firstPull: OptionT[Pull[F, PullOutput, *], Unit] = for | ||
firstXAndRestOfXs <- OptionT(xs.pull.uncons1.covaryOutput[PullOutput]) | ||
(x, restOfXs) = firstXAndRestOfXs | ||
firstYAndRestOfYs <- OptionT(ys.pull.uncons1.covaryOutput[PullOutput]) | ||
(y, restOfYs) = firstYAndRestOfYs | ||
_ <- OptionT.liftF { | ||
Pull.output1[F, PullOutput]((x, y, restOfXs, restOfYs)): Pull[F, PullOutput, Unit] | ||
} | ||
yield () | ||
|
||
firstPull.value.void.stream.flatMap { (x, y, restOfXs, restOfYs) => | ||
restOfXs.either(restOfYs).scan((x, y)) { | ||
case ((_, rightElem), Left(newElem)) => (newElem, rightElem) | ||
case ((leftElem, _), Right(newElem)) => (leftElem, newElem) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2022 Arman Bilge | ||
* | ||
* 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 calico.frp | ||
|
||
import cats.data.NonEmptyList | ||
import cats.effect.IO | ||
import cats.effect.kernel.Resource | ||
import cats.effect.testkit.TestControl | ||
import cats.effect.testkit.TestInstances | ||
import cats.kernel.Eq | ||
import cats.laws.discipline.ApplicativeTests | ||
import cats.laws.discipline.MonadTests | ||
import cats.syntax.all.* | ||
import fs2.Stream | ||
import fs2.concurrent.Signal | ||
import fs2.concurrent.SignallingRef | ||
import munit.DisciplineSuite | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Arbitrary.arbitrary | ||
import org.scalacheck.Gen | ||
|
||
import scala.concurrent.duration.* | ||
|
||
class SignalSuite extends DisciplineSuite, TestInstances: | ||
|
||
override def scalaCheckTestParameters = | ||
if sys.props("java.vm.name").contains("Scala.js") then | ||
super.scalaCheckTestParameters.withMinSuccessfulTests(10).withMaxSize(10) | ||
else super.scalaCheckTestParameters | ||
|
||
case class TestSignal[A](events: NonEmptyList[(FiniteDuration, A)]) extends Signal[IO, A]: | ||
def discrete: Stream[IO, A] = Stream.eval(IO.realTime).flatMap { now => | ||
def go(events: NonEmptyList[(FiniteDuration, A)]): (A, List[(FiniteDuration, A)]) = | ||
events match | ||
case NonEmptyList((_, a), Nil) => (a, Nil) | ||
case NonEmptyList((t0, a0), tail @ ((t1, a1) :: _)) => | ||
if t1 > now then (a0, tail) | ||
else go(NonEmptyList.fromListUnsafe(tail)) | ||
|
||
val (current, remaining) = go(events) | ||
Stream.emit(current) ++ Stream.emits(remaining).evalMap { (when, a) => | ||
IO.realTime.map(when - _).flatMap(IO.sleep).as(a) | ||
} | ||
} | ||
def get = IO.never | ||
def continuous = Stream.never | ||
|
||
given [A: Arbitrary]: Arbitrary[Signal[IO, A]] = | ||
given Arbitrary[FiniteDuration] = Arbitrary(Gen.posNum[Byte].map(_.toLong.millis)) | ||
Arbitrary( | ||
for | ||
initial <- arbitrary[A] | ||
tail <- arbitrary[List[(FiniteDuration, A)]] | ||
events = tail.scanLeft(Duration.Zero -> initial) { | ||
case ((prevTime, _), (sleep, a)) => | ||
(prevTime + sleep) -> a | ||
} | ||
yield TestSignal(NonEmptyList.fromListUnsafe(events)) | ||
) | ||
|
||
given [A: Eq](using Eq[IO[List[(A, FiniteDuration)]]]): Eq[Signal[IO, A]] = Eq.by { sig => | ||
IO.ref(List.empty[(A, FiniteDuration)]).flatMap { ref => | ||
TestControl.executeEmbed( | ||
sig | ||
.discrete | ||
.evalMap(IO.realTime.tupleLeft(_)) | ||
.evalMap(x => ref.update(x :: _)) | ||
.compile | ||
.drain | ||
.timeoutTo(Long.MaxValue.nanos, IO.unit) | ||
) *> ref.get.map(_.distinctBy(_._2)) | ||
} | ||
} | ||
|
||
given Ticker = Ticker() | ||
|
||
// it is stack-safe, but expensive to test | ||
checkAll("Signal", MonadTests[Signal[IO, _]].stackUnsafeMonad[Int, Int, Int]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters