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
Arulselvan Madhavan
@ArulselvanMadhavan
Thanks @SystemFw I wish I had a working emacs setup to get suggestions on methods
Fabio Labella
@SystemFw
tell me about it :P
Mark Tomko
@mtomko
Is there a pattern for doing something like this:
  type Thingy
  def x[F[_]: Concurrent]: Resource[F, MVar[F, Thingy]] =
    for {
      sem <- Resource.liftF(Semaphore[F](1L))
      mvar <- Resource.liftF(MVar.empty[F, Thingy])
      fiber <- // construct a fiber that uses the semaphore and mvar and cancels when the resource is closed
    } yield mvar
I maybe don't need the semaphore, actually. but I do want the mvar to be updated periodically and safely, until the resource is closed.
yeah, the mvar is enough that I don't need semaphore, when I started this I wasn't using an mvar...
The fiber would be doing something like Stream.repeatEval(update(mvar)).metered(5.minutes)
Mark Tomko
@mtomko
oh, maybe it's just Resource.make(???)(_.cancel), duh
Fabio Labella
@SystemFw
@mtomko since you're using fs2 anyway, look into hold and compile.resource instead
Mark Tomko
@mtomko
okay, thanks!
Fabio Labella
@SystemFw
do you use the MVar for reading only in x? (from the user point of view)
Mark Tomko
@mtomko
no, someone else will read it.
Fabio Labella
@SystemFw
yeah that's what I mean
but will they write to it as well? or it's just the update that does that?
Mark Tomko
@mtomko
ah
Nobody else will update it
Fabio Labella
@SystemFw
yeah, pretty typical pattern
do you have an initial "default" value that you can give to Thingy? if you do, then hold will fit your use case perfectly. If you don't, you can use most of the same tools that are under hold (concurrently mostly) and then compile.resource to have a Resource
Mark Tomko
@mtomko
We don't easily have a default initial value, at least not without some side effects.
Fabio Labella
@SystemFw
actually we have holdResource as well to save some characters
Mark Tomko
@mtomko
yeah, I just noticed that.
I also realized that MVar isn't quite right, because we're not waiting for someone to take a value out.
This is really just a way to monitor some external condition and compute a value when that condition changes
Fabio Labella
@SystemFw
mm ok
Mark Tomko
@mtomko
and we want to make the computed value available (within the context of an F[_])
Fabio Labella
@SystemFw
the tools that holdResource is made of should be of help
Mark Tomko
@mtomko
great, I'm coming to the end of my day, but I'll read that code over and try to reframe things.
Thanks!
Fabio Labella
@SystemFw
you just need to figure out what is the right concurrent structure to hold that
Mark Tomko
@mtomko
oh, like Deferred (because there's no initial value) and Ref
Fabio Labella
@SystemFw
yeah, MVar might actually work here, and it's a simple enough case that it should be ok to use
and if it does, you can literally copy the definition of holdResource replacing SignallingRef with MVar
Mark Tomko
@mtomko
well, I think I was wrong about MVar. I must have confused it with Ref. I don't want a 1-element queue, because there's no guarantee that anyone will take - so I don't want to block on put if it's full.
Fabio Labella
@SystemFw
yeah, you could do it with a proper MVar update, which we don't have (because it's tough to use in most other cases which aren't this)
but basically if you wanted to use MVar for this
your update should take and then put, whereas your read side should read
Mark Tomko
@mtomko
yeah, I was debating that, too. Is that better than using Ref?
Fabio Labella
@SystemFw
with Ref[F, A] you won't be able to say "wait for the first element", and you'd need to pass a first element, at which point you could have used hold
you can do it with Ref[F, VerySimpleThingy[A]]
where VerySimpleThingy is a simple state machine with Deferred like the ones I described in my talk
Mark Tomko
@mtomko
got it. presumably with MVar I'd need to tryTake inside update, so that I didn't block if I had a new value to publish.
Fabio Labella
@SystemFw
basically FirstCall(wait: Deferred[F, A]) | SubsequentCalls(get: A)
no, you want take
Mark Tomko
@mtomko
I'll go back though - was that the talk about concurrency primitives in fs2, maybe NE Scala 2019?
Fabio Labella
@SystemFw
I have a better version, that one was misrecorded
ah, actually you're right about tryTake
what works with the first call does not for the next ones
another example of why MVar is tricky
Mark Tomko
@mtomko
Thanks! I was at NE Scala for the original but it's been a while. I'll watch the other talk and also study the documentation a bit. Thanks for the help!
Fabio Labella
@SystemFw
the Ref + Deferred version is pretty simple though