Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Feb 01 2021 10:11
    @SystemFw banned @Hudsone_gitlab
  • Jan 31 2019 04:19
    404- forked
    404-/fs2
  • Jan 31 2019 03:01
    SethTisue commented #1232
  • Jan 30 2019 17:22
  • Jan 30 2019 13:45
  • Jan 30 2019 10:48
    pchlupacek commented #1406
  • Jan 30 2019 10:47
    pchlupacek commented #1406
  • Jan 30 2019 10:39
    pchlupacek commented #1407
  • Jan 30 2019 09:58
    lJoublanc commented #870
  • Jan 30 2019 09:42
    vladimir-popov commented #1407
  • Jan 30 2019 08:10
    vladimir-popov closed #1407
  • Jan 30 2019 08:10
    vladimir-popov commented #1407
  • Jan 29 2019 19:20
    SystemFw commented #1407
  • Jan 29 2019 19:20
    SystemFw commented #1407
  • Jan 29 2019 18:57
    SystemFw commented #1406
  • Jan 29 2019 17:47
    pchlupacek commented #1406
  • Jan 29 2019 17:42
    pchlupacek commented #1406
  • Jan 29 2019 17:39
    pchlupacek commented #1407
  • Jan 29 2019 17:39
    vladimir-popov edited #1407
  • Jan 29 2019 17:38
    vladimir-popov commented #1406
Gavin Bisesi
@Daenyth
You'll notice that most effect types lack such an instance, any IO monad can't because it represents a non-referentially transparent computation
Stream has /analogous/ operations to traverse, like evalMap and others, but the "leave stream" part needs to go through the .compile step
It's possible that Stream for F=Pure could have such an instance
but any general F couldn't
Ukonn Ra
@UkonnRa
So the only solution is to compile the Stream to a list if I want to "traverse" a Stream?
Michael Pilquist
@mpilquist
There's likely a better way to address the use case that stays within the world of streams. Could you describe more of your use case?
Fabio Labella
@SystemFw
@UkonnRa look into evalMap as well
Gavin Bisesi
@Daenyth
yeah, s.compile.toList is the appropriate way to get a list from a stream
Fabio Labella
@SystemFw

It's possible that Stream for F=Pure could have such an instance

Btw no, it couldn't. The issue is not effects (only), it's that a stream can be infinite

Ukonn Ra
@UkonnRa
My situation is just like the A code example part from the link. I have a StateT Stream and I want to chain them up
Fabio Labella
@SystemFw
evalMap should work
btw with Stream you can have stateful computations that don't require the use of StateT, but there is nothing wrong with doing what you're doing at first, so definitely keep exploring :)
Ukonn Ra
@UkonnRa
Yes, evalMap works, but in the StateT situation, the evalMap is a little ugly
Fabio Labella
@SystemFw
why?
genuine question
Gavin Bisesi
@Daenyth
@SystemFw aha I forgot that
Fabio Labella
@SystemFw
it should literally be the difference between
yourList.traverse(stateComputation) and yourStream.evalMap(stateComputation)
@Daenyth in fairness, the same can be said of compile.list :P
Gavin Bisesi
@Daenyth
yeah
but it's at least used in a monomorphic context
the traverse instance means that code could be calling traverse not knowing F=Stream and assuming finite-ness
and it kind of depends on your philosophical stance whether returning Nothing for a method impl satisfies the contract or not
Ukonn Ra
@UkonnRa
Well, let me try evalMap first, maybe I misunderstood something
Fabio Labella
@SystemFw
sure, feel free to post a scastie or a snippet if you're struggling further :)
Ukonn Ra
@UkonnRa
Well just like this piece of code, https://scastie.scala-lang.org/Nuz2i83yTnWs0IPtOUhPBg, I just have no idea what the evalMap is... Any advice?
Gavin Bisesi
@Daenyth
That evalMap does nothing useful
takes an element, wraps it in F[_], then evaluates the F to get the element back
All your StateT are using pure so it might be easier to skip the T and just use State
Fabio Labella
@SystemFw
@UkonnRa I thought you had a stream of elements, that you are operating on with StateT
if you just have three stateT computations, you can Stream.eval(first) ++ Stream.eval(second) ++ Stream.eval(third) but then why are you using Stream? Could just have first >> second >> third
in the code you have, you could make it work with evalMap(identity) btw (rather than pure)
if you want a slightly more meaningful example
try writing a Int => StateT that takes an Int and inc/decr by that int
then you can do Stream(1, -1, 2, 1, -1).evalMap(yourFunction) and see
or, in case you want to follow the example in stack overflow, List.fill(5)(yourStateT).sequence becomes Stream.repeatEval(yourStateT).take(5)
(or equivalently Stream.eval(yourStateT).repeat.take(5)
Ukonn Ra
@UkonnRa
Well, my real-world situation is like CQRS/ES, I have a bunch of Events in the DB, and sometime I have to read them out (into a Stream) and "rehydrate" it to the latest state.
The only solution I can figure out is like https://scastie.scala-lang.org/6eIjulhSRfqJRotHerKNaA
Which should do runS over and over again
Adam Rosien
@arosien
https://github.com/sloshy/fs2-es/ has some good patterns for ES in fs2
to re-use or steal
Gavin Bisesi
@Daenyth
@UkonnRa ES events may also be easier without StateT unless you already have such functions. Note that (init state) => List[Event] => (final state) is the same signature as foldLeft more or less, so you can do eventStream.fold(init)(update).compile.lastOrError
Ukonn Ra
@UkonnRa
Yes, I thought it would be interesting if using StateT, but maybe it is not suitable in this situation
Gavin Bisesi
@Daenyth
IMO State monad is often overkill
I generally never start with a monad transformer, I write the raw version first and use transformers where I see the opportunity to factor out repeated structure
Ukonn Ra
@UkonnRa
It would be nice to use traverse function with StateT, but unfortunatelly, Stream have no
Gavin Bisesi
@Daenyth
Traverse's signature is the same as evalMap
*almost
return type differs
G[A] => (A => F[B]) => F[G[B]]
Stream[F, A] => (A => F[B]) => Stream[F, B]