Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Sep 05 2019 14:43
    @typelevel-bot banned @jdegoes
  • Jan 31 2019 21:17
    codecov-io commented #484
  • Jan 31 2019 21:08
    scala-steward opened #484
  • Jan 31 2019 18:19
    andywhite37 commented #189
  • Jan 31 2019 02:41
    kamilongus starred typelevel/cats-effect
  • Jan 30 2019 00:01
    codecov-io commented #483
  • Jan 29 2019 23:51
    deniszjukow opened #483
  • Jan 29 2019 23:37
  • Jan 29 2019 23:22
  • Jan 29 2019 20:26
    Rui-L starred typelevel/cats-effect
  • Jan 29 2019 18:01
    jdegoes commented #480
  • Jan 29 2019 17:04
    thomaav starred typelevel/cats-effect
  • Jan 28 2019 17:43
    asachdeva starred typelevel/cats-effect
  • Jan 28 2019 07:12
    alexandru commented #480
  • Jan 28 2019 05:45
    codecov-io commented #482
  • Jan 28 2019 05:35
    daron666 opened #482
  • Jan 27 2019 13:56
    codecov-io commented #481
  • Jan 27 2019 13:46
    lrodero opened #481
  • Jan 27 2019 05:47
    codecov-io commented #460
  • Jan 27 2019 05:37
    codecov-io commented #460
Luis Miguel Mejía Suárez
@BalmungSan
@valdo404 you may have better luck in the discord server: https://discord.gg/JDQSt72z
ayeo
@ayeo
Hi all, I want to control the bulb using stream of signals. Each signal from the stream just toggle the bulb. I have no idea how to connect the pieces togheter. I have been trying to use for-comprehension but without any success. I have something like this at the moment.
  val signal1 =  Stream.repeatEval(producer("A", 100.millis))
  val signal2 =  Stream.repeatEval(producer("B", 200.millis))
  val signal: Stream[IO, String] = signal1.merge(signal2)

  class Bulb(ref: Ref[IO, Int]) {
    def toggle(): IO[Unit] = IO.println(("toggle")) >> IO.unit //todo
  }

  def pipes(bulb: Bulb): Pipe[IO, String, String] = in =>
    in.evalMap(signal => bulb.toggle() >> IO("Toggled"))


  val ioBulb: IO[Bulb] = for {
    r <- Ref[IO].of(0)
    b = new Bulb(r)
  } yield b
gvolpe
@gvolpe:matrix.org
[m]

ayeo (ayeo) assuming every toggle sets the internal Ref to either 1 or 0, you can do something like this.

object Main extends IOApp.Simple:
  val signal1 = Stream.emit[IO, String]("A").metered(100.millis)
  val signal2 = Stream.emit[IO, String]("B").metered(200.millis)

  def run: IO[Unit] = 
    Bulb.make[IO].flatMap { bulb =>
      signal1
        .merge(signal2)
        .evalMap(_ => IO.println("toggle") *> bulb.toggle)
        .interruptAfter(5.seconds)
        .compile
        .drain
    }

trait Bulb[F[_]]:
  def toggle: F[Unit]

object Bulb:
  def make[F[_]: Functor: Ref.Make]: F[Bulb[F]] =
    Ref[F].of(0).map { ref => 
      new:
        def toggle: F[Unit] = ref.update(x => if x == 0 then 1 else 0)
    }

Sleeps and timeouts don't work so well in the browser but you can see the compiling version here: https://scastie.scala-lang.org/NXdBGJLZTwyJeFh3tsgIwg

yanly007
@yanly007
Hello everyone,
Sorry if noob question, but I don't understand why using an instance of Async[F] where F is an effect monad, basically gives us access to the methods in MonadError too.
Async doesn't seem to extends MonadError...
Does Async have MonadError instances available regardless of what F is?
Luis Miguel Mejía Suárez
@BalmungSan
Hi @yanly007, first this channel is mostly dead, switch to discord.
Second, Async is a subtype of MonadError both in CE2 and in CE3.
yanly007
@yanly007
Thanks for the answer Luis!
CameronDiver
@CameronDiver
Hey, would anyone be able to help me understand some better ways to compose IO actions with cats?
Currently I've got something very akin to javascripts "callback hell", and there has to be a better way
I tried for example to use Either and map through assuming that the right bias will help, but it seems map keeps the value within the IO context
CameronDiver
@CameronDiver
basically any usage examples that have 3-4 io actions that depend on each being composed would be amazingly helpful
Luis Miguel Mejía Suárez
@BalmungSan
for? Or just plain flatMap?
CameronDiver
@CameronDiver
flatMap still propagates Either/Option etc though, so if I have something returning an IO[Either[A,B]], flatMap will pass Either[A,B] to the callback
so I've still got to do the standard unwrapping
let me show you the terrible code I've managed to cobble together
  def registerIntegration(req: Request[IO], auth: String) = {
    // First check that we have the correct payload
    req
      .as[RegisterIntegrationPayload]
      .option
      .flatMap({
        case None => BadRequest("Malformed request body")
        case Some(payload) => {
          // Check that the user referenced exists
          repository
            .getUser(payload.userId)
            .flatMap({
              case None => BadRequest("No user found")
              case Some(_) => {
                // Unwrap the lightning auth header
                authStringToLightningAuth(auth) match {
                  case Left(st) => BadRequest(st)
                  case Right((user, pass)) => {
                    // Make sure we have the correct authentication
                    LightningAdServer
                      .getAccount(user, pass)
                      .flatMap({
                        case Left(error) =>
                          InternalServerError(
                            s"Lightning ad server failed: $error"
                          )
                        case Right(accountName) => {
                          // Mark that the user is lightning registered
                          repository
                            .markUserLightningRegistered(payload.userId)
                            .flatMap(registered =>
                              if (registered)
                                Ok("Registered for lightning integration")
                              else
                                InternalServerError(
                                  "Failed to register for lightning integration"
                                )
                            )
                        }
                      })
                  }
                }
              }
            })
        }
      })
  }
it seems to me that I should be able to avoid this amount of identation in idiomatic scala/cats
but, I've no idea how :/
Luis Miguel Mejía Suárez
@BalmungSan
@CameronDiver So two things that may help.
Use EitherT or OptionT
(make sure to just use one, like convert all Eithers into Option or vice versa)
And then remember that foo.flatMap(x => x.bar.flatMap(y => ???)) is the same as foo.flatMap(x => x.bar).flatMap(y => ???)
i.e. you can always unnest flatMap calls.
The for syntax may help with that.
CameronDiver
@CameronDiver
The unnested flatMap calls is huge, I didn't notice that! I'll check out how to use EitherT/OptionT too. Really appreciate the help!
Naftoli Gugenheim
@nafg
You cannot always unnest flatMaps easily. For instance if y => ??? depended on x then to unnest you'd have to return a tuple to pass x along with the result of x.bar
tpetillot
@tpetillot
Hi folks! Would it be possible to convert A => F[B] to F[A => B]? I'm definitely missing something.
Naftoli Gugenheim
@nafg
@tpetillot try discord
tpetillot
@tpetillot
@nafg done, thanks!
antunonline
@antunonline
Hi guys, I'm playing with Cats Fibers and having hard time to find some information regarding the integration of Fibers and JVM NIO; specifically do Fibers block when calling NIO operations or are they somehow integrated with such calls and automatically poll/select on file/socket descriptors?
Or is such construct orthogonal to Fibers and user needs to provide such functionality?
湿垃圾 == 菜狗
@djx314
Hi guys, for the past two days, I've iterated my code by times. I find a simple way to do something like macwire in cats use IO or Resource. First it only works in Scala3. Finally I choose cats-effect-cps, It becomes more maintainable and intuitive. And than we can do the same thing in zio.RIO, zio.ZManaged, Future because the support of cats-effect-cps. I organized my code on macwire issue Further in Scala3.2 it can be
图片.png
Mario Siegenthaler
@msiegenthaler
Hey, I'm having some weird issues with my application just completely locking up after some time. It's all cats effect 3.3.13 (http4s server that makes calls to some old-school blocking apis [keycloak client wrapped with IO.interuptibleMany]). What I observed is that I seem to loose all cats-effect compute threads (io-compute). At the beginning I have two, then some compute-blocker in addition, then when it works I consistently see only the io-scheduler and no other io-* threads..
1 reply
Tomas Klubal
@blondacz
Hi all, why is the WorkStealingThreadPool#shutdown() calling Thread.currentThread.interrupt()? It causes the calling thread to die, which might not be desirable.
Abdulkhamid Muminov
@homycdev
Hi all. Is there any resource expect official documentation to learn the cats-effect library and cats data structures? Somehow the official docs are quite hard to grasp it for the functional newbee, or I'd call imperative rookie :D Thanks in advance!
Luis Miguel Mejía Suárez
@BalmungSan
@homycdev hey, you may get more help in Discord but check the resources and examples I have here: https://github.com/pslcorp/programs-as-values
Joesan
@joesan
What are the correct imports that I should use to be able to use the Id Monad?
I have cats-core version 2.8.0 as a dependency in my project, and have the import cats._
but my IDE still could not find the Id Monad
josiah_sama
@josiah_sama:matrix.org
[m]

I'm trying to use parReduceMapA on a List[Set[MyType]] where MyType has a Semigroup instance defined for it, but I'm getting the error:
could not find implicit value for parameter T: cats.Reducible[List]

The code is using tagless final, so it looks kinda like this

object MyService {
  def impl[F[_]: Spawn: Parallel]: MyService[F] = new MyService[F] {
    private def otherFunc(param1: String, param2: Set[MyType]): List[Set[MyType]] = ???

    def myFunc(param1: String, param2: Set[MyType]): F[List[Set[MyType]]] =
      for {
        myVal <- otherFunc(param1, param2).parReduceMapA(Applicative[F].pure)
        ... etc
josiah_sama
@josiah_sama:matrix.org
[m]
Okay, figured this out: List can be empty, so requires a Monoid, and parFoldMapA operates on the Monoid instance, which is why that works and parReduceMapA does not work because it requires a non-empty container since it operates on Semigroup