Skip to content

Commit

Permalink
Merge pull request #1677 from patseev/series/3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored Feb 13, 2021
2 parents 4b0ad0a + 34cf98d commit 129ca14
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
case Left(value) => IO.pure(value)
}

def timed: IO[(FiniteDuration, A)] =
Clock[IO].timed(this)

def product[B](that: IO[B]): IO[(A, B)] =
flatMap(a => that.map(b => (a, b)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ package object syntax {
object temporal extends kernel.syntax.GenTemporalSyntax
object async extends kernel.syntax.AsyncSyntax
object resource extends kernel.syntax.ResourceSyntax
object clock extends kernel.syntax.ClockSyntax
}
8 changes: 8 additions & 0 deletions kernel/shared/src/main/scala/cats/effect/kernel/Clock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ trait Clock[F[_]] extends ClockPlatform[F] {

// lawless (unfortunately), but meant to represent current (when sequenced) system time
def realTime: F[FiniteDuration]

/**
* Returns an effect that completes with the result of the source together
* with the duration that it took to complete.
*/
def timed[A](fa: F[A]): F[(FiniteDuration, A)] =
applicative.map3(monotonic, fa, monotonic)((startTime, a, endTime) =>
(endTime.minus(startTime), a))
}

object Clock {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ trait GenTemporal[F[_], E] extends GenConcurrent[F, E] with Clock[F] {
val timeoutException = raiseError[A](ev(new TimeoutException(duration.toString)))
timeoutTo(fa, duration, timeoutException)
}

}

object GenTemporal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ trait AllSyntax
with GenConcurrentSyntax
with AsyncSyntax
with ResourceSyntax
with ClockSyntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2020-2021 Typelevel
*
* 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 cats.effect.kernel.syntax

import cats.effect.kernel.Clock

import scala.concurrent.duration.FiniteDuration

trait ClockSyntax {
implicit def clockOps[F[_], A](wrapped: F[A]): ClockOps[F, A] =
new ClockOps(wrapped)
}

final class ClockOps[F[_], A] private[syntax] (private val wrapped: F[A]) extends AnyVal {

def timed(implicit F: Clock[F]): F[(FiniteDuration, A)] =
F.timed(wrapped)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ package object syntax {
object temporal extends GenTemporalSyntax
object async extends AsyncSyntax
object resource extends ResourceSyntax
object clock extends ClockSyntax
}
11 changes: 11 additions & 0 deletions kernel/shared/src/test/scala/cats/effect/kernel/SyntaxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,15 @@ class SyntaxSpec extends Specification {
result: Resource[F, A]
}
}

def clockSyntax[F[_], A](target: F[A])(implicit F: Clock[F]) = {
import syntax.clock._

Clock[F]: F.type

{
val result = target.timed
result: F[(FiniteDuration, A)]
}
}
}

0 comments on commit 129ca14

Please sign in to comment.