Skip to content

Commit

Permalink
Merge pull request #100 from TinkoffCreditSystems/minor_fixes
Browse files Browse the repository at this point in the history
Add summon to Execute, add putErr and putErrLn to Console, scalafmt f…
  • Loading branch information
Odomontois authored Jan 9, 2020
2 parents 03fe998 + 931033e commit 23e8c68
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 39 deletions.
2 changes: 1 addition & 1 deletion concurrent/src/main/scala/tofu/concurrent/Atom.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package tofu.concurrent
import cats.{Applicative, Functor, Monad}
import cats.{Applicative, Functor}
import cats.effect.Sync
import cats.effect.concurrent.Ref
import tofu.Guarantee
Expand Down
3 changes: 1 addition & 2 deletions concurrent/src/main/scala/tofu/concurrent/QVar.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package tofu.concurrent
import cats.effect.concurrent.MVar
import cats.effect.{Concurrent, Sync}
import cats.{Applicative, Functor}
import cats.Applicative
import tofu.Guarantee
import tofu.concurrent.QVar.QVarByMVar
import tofu.higherKind.{RepresentableK, derived}
import tofu.optics.Contains
import tofu.syntax.monadic._

/** a middleground between cats.concurrent.MVar and zio.Queue.bounded(1) */
Expand Down
28 changes: 13 additions & 15 deletions core/src/main/scala/tofu/Execute.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package tofu

import cats.effect.{Async, ContextShift}
import tofu.syntax.monadic._

import tofu.syntax.monadic._
import scala.concurrent.{ExecutionContext, Future}

import simulacrum.typeclass

@typeclass
trait Execute[F[_]] {
def executionContext: F[ExecutionContext]

Expand All @@ -13,20 +16,15 @@ trait Execute[F[_]] {
def deferFuture[A](f: => Future[A]): F[A] = deferFutureAction(_ => f)
}

object Execute extends ExecuteInstance

sealed trait ExecuteInstance {
object Execute {
final implicit def asyncExecute[F[_]](
implicit ec: ExecutionContext,
implicit
ec: ExecutionContext,
cs: ContextShift[F],
asyncF: Async[F]
): Execute[F] =
new Execute[F] {
def async: Async[F] = asyncF

override def executionContext: F[ExecutionContext] = ec.pure[F]

def deferFutureAction[A](f: ExecutionContext => Future[A]): F[A] =
Async.fromFuture(asyncF.delay(f(ec)))
}
}
): Execute[F] = new Execute[F] {
def executionContext: F[ExecutionContext] = ec.pure[F]
def deferFutureAction[A](f: ExecutionContext => Future[A]): F[A] =
Async.fromFuture(asyncF.delay(f(ec)))
}
}
38 changes: 28 additions & 10 deletions core/src/main/scala/tofu/common/Console.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package tofu.common

import java.io.{BufferedReader, PrintStream}

import scala.{Console => ScalaConsole}

import cats.effect.Sync
import simulacrum.typeclass
import tofu.higherKind
import tofu.higherKind.RepresentableK

import scala.io.StdIn

@typeclass
trait Console[F[_]] {
def readStrLn: F[String]

def putStr(s: String): F[Unit]
def putStrLn(s: String): F[Unit]

def readStrLn: F[String]
def putErr(err: String): F[Unit]
def putErrLn(err: String): F[Unit]
}

object Console {
def putStr[F[_]](s: String)(implicit C: Console[F]): F[Unit] = C.putStr(s)
def putStrLn[F[_]](s: String)(implicit C: Console[F]): F[Unit] = C.putStrLn(s)
def readStrLn[F[_]](implicit C: Console[F]): F[String] = C.readStrLn
def readStrLn[F[_]: Console]: F[String] = Console[F].readStrLn

def putStr[F[_]: Console](s: String): F[Unit] = Console[F].putStr(s)
def putStrLn[F[_]: Console](s: String): F[Unit] = Console[F].putStrLn(s)

def putErr[F[_]: Console](e: String): F[Unit] = Console[F].putErr(e)
def putErrLn[F[_]: Console](e: String): F[Unit] = Console[F].putErrLn(e)

implicit val representableKInstance: RepresentableK[Console] = higherKind.derived.genRepresentableK[Console]

implicit def syncInstance[F[_]](implicit F: Sync[F]): Console[F] = new Console[F] {
def putStr(s: String): F[Unit] = F.delay(print(s))
def putStrLn(s: String): F[Unit] = F.delay(println(s))
def readStrLn: F[String] = F.delay(StdIn.readLine())
implicit def syncInstance[F[_]: Sync]: Console[F] = instance(ScalaConsole.in, ScalaConsole.out, ScalaConsole.err)

def instance[F[_]: Sync](in: BufferedReader, out: PrintStream, err: PrintStream): Console[F] = new Console[F] {
def readStrLn: F[String] = Sync[F].delay(in.readLine())

def putStr(s: String): F[Unit] = Sync[F].delay(out.print(s))
def putStrLn(s: String): F[Unit] = Sync[F].delay(out.println(s))

def putErr(e: String): F[Unit] = Sync[F].delay(err.print(e))
def putErrLn(e: String): F[Unit] = Sync[F].delay(err.println(e))
}
}
10 changes: 6 additions & 4 deletions higherKindCore/src/main/scala/tofu/syntax/monadic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object monadic extends TupleSemigroupalSyntax with ApplicativeSyntax with MonadS
def tupleRight[B](b: B)(implicit F: Functor[F]): F[(A, B)] = F.tupleRight(fa, b)
}

implicit class TofuSemigroupalOps[F[_], A](private val fa: F[A]) extends AnyVal {
implicit final class TofuSemigroupalOps[F[_], A](private val fa: F[A]) extends AnyVal {
def product[B](fb: F[B])(implicit F: Semigroupal[F]): F[(A, B)] = F.product(fa, fb)
}

Expand All @@ -25,9 +25,11 @@ object monadic extends TupleSemigroupalSyntax with ApplicativeSyntax with MonadS
def <*>(fa: F[A])(implicit F: Apply[F]): F[B] = F.ap(fab)(fa)
}

implicit class TofuApplicativeOps[F[_], A](private val condition: Boolean) extends AnyVal {
def whenOpt(fa: =>F[A])(implicit F: Applicative[F]): F[Option[A]] = if (condition) F.map(fa)(Some(_)) else F.pure(None)
def unlessOpt(fa: =>F[A])(implicit F: Applicative[F]): F[Option[A]] = if (condition) F.pure(None) else F.map(fa)(Some(_))
implicit final class TofuApplicativeOps[F[_], A](private val condition: Boolean) extends AnyVal {
def whenOpt(fa: => F[A])(implicit F: Applicative[F]): F[Option[A]] =
if (condition) F.map(fa)(Some(_)) else F.pure(None)
def unlessOpt(fa: => F[A])(implicit F: Applicative[F]): F[Option[A]] =
if (condition) F.pure(None) else F.map(fa)(Some(_))
}

implicit final class TofuApplyFunc2Ops[F[_], A, B, C](private val fab: F[(A, B) => C]) extends AnyVal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tofu.logging
package derivation

import cats.Show
import magnolia.{CaseClass, Magnolia, SealedTrait, TypeName}
import magnolia.{CaseClass, Magnolia, SealedTrait}
import org.manatki.derevo.Derivation

object loggable extends Derivation[Loggable] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package tofu.logging
import cats.Functor
import tofu.syntax.monadic._

import scala.reflect.ClassTag

trait LogsVOps[I[_], F[_]]
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import cats.{Applicative, Functor, ~>}
import cats.effect.{CancelToken, Fiber}
import tofu.internal.CachedMatcher
import tofu.lift.Unlift
import tofu.optics.{Contains, Subset}
import tofu.syntax.functionK
import tofu.optics.Contains
import tofu.syntax.functionK.funK
import tofu.zioInstances.ZioTofuInstance.convertFiber
import zio.clock.Clock
Expand Down

0 comments on commit 23e8c68

Please sign in to comment.