Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Soren
@srnb_gitlab
Is this much easier with CachedResource?
Gavin Bisesi
@Daenyth
No. You'd need to read my (not quite trivial) implementation and write your own class that has the semantics you want
Perhaps have a look at chris davenport's keypool WIP package for Resource pooling
My implementation is for taking one resource that works and holding the lifecycle open an unknown amount of time concurrently
you want to take N potential resources and find the one that works
if you can accept operation as a parameter, and just call use, it's much easier
if you need to find the working one and then go run the whole rest of your application... that is less fun
Oleg Pyzhcov
@oleg-py
Can't you compile fs2.Stream to a Resource? Like
Stream.emits(inputs)
    .evalMap(resourceFor(input).attempt)
    .dropWhile(_.isLeft)
    .head.compile.resource.lastOrError
Gavin Bisesi
@Daenyth
True... that still just feels really.. weird. to me
Oleg Pyzhcov
@oleg-py
Why? o_o
Gavin Bisesi
@Daenyth
.evalMap(resourceFor(input).attempt).collectFirst { case Right(r) => r }.compile /shrug
idk
just seems like it shouldn't work. I guess the types say it should
That imperative thinking still kicks me sometimes :)
Ah no, here's why
resourceFor(input).attempt => Resource[F, Either[Throwable, A]]
or compile error
Is Resource a MonadError?
in order to know whether acquire fails, you have to try
that means you need to call either use or allocated
Oleg Pyzhcov
@oleg-py
If F is, then Resource[F] is too
No, you can just evalMap it to a tuple :-P
Rob Norris
@tpolecat
@kubukoz yes, Alternative, sorry
Gavin Bisesi
@Daenyth
@oleg-py wouldn't the attempt then make that non-failing and compile returns the first one always whether or not it worked?
Jakub Kozłowski
@kubukoz
I'm not sure now, maybe foldMapK would do it @tpolecat
and that only requires MonoidK IIRC
Fabio Labella
@SystemFw
you want Stream.resource to get a Stream[F, A] from Resource[F, A]
Oleg Pyzhcov
@oleg-py
^ this
Rob Norris
@tpolecat
@kubukoz you need to be able to point the values into F and you need Applicative for that
Jakub Kozłowski
@kubukoz
ah. but you need to wrap in the result F, so Applicative... Alternative
yeah
Gavin Bisesi
@Daenyth
Stream.emits(inputs).flatMap(i => Stream.resource(resourceFor(i)).attempt).collectFirst { case Right(res) => res }.compile.resource.lastOrError ?
Oleg Pyzhcov
@oleg-py
Something like that
I don't have a repl/ide to check at hand
Gavin Bisesi
@Daenyth
that's really cool if that works
Fabio Labella
@SystemFw
actually the main issue here is finding the correct use of collect/take and so on
well, not the main issue but it's trickier than you think
you can look at retry to see what you need to do
with collectFirst if they all fail the stream never terminates
Oleg Pyzhcov
@oleg-py
Why? The seed is finite collection
Fabio Labella
@SystemFw
ah you're right
I'm used to it being some repeatEval/unfold of sorts :)
but basically what you have is good for List[F[A]] => ... but not for Stream[F, A] =>... when you can't assume finiteness anymore
Oleg Pyzhcov
@oleg-py
.take(999) // good enough
Fabio Labella
@SystemFw
you need to use takeThrough(!cond).last roughly
Soren
@srnb_gitlab
:dizzy_face: so what should I use?
Gavin Bisesi
@Daenyth
my last one should work for finite list inputs
Fabio Labella
@SystemFw
yep
Gavin Bisesi
@Daenyth
but it should also work if you swap my collectFirst with fabio's takeThrough.last
and his works for non-finite inputs