by

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
How do I dropWhile a List[F[T]] based on F[T] => F[Boolean]?
A solution using sequencing is not what I want because I don't want to evaluate the F[T]s later in the list
Gavin Bisesi
@Daenyth
@srnb_gitlab I recommend fs2
Oleg Pyzhcov
@oleg-py
or manual recursion
Gavin Bisesi
@Daenyth
val lst: List[F[T]] = ???
fs2.Stream.emits(lst)  // Stream[Pure, F[T]]
  .evalMap(identity)   // Stream[F, T]
  .evalMap(t => cond(t).map(b => (t, b))) // Stream[F, (T, B)]
  .dropWhile(tb => tb._2)
  .map(_._1) // Stream[F, T]
Oleg Pyzhcov
@oleg-py
def dropWhileM[F[_]: Monad, A](list: List[F[A]])(fn: F[A] => F[Boolean]): F[List[F[A]]] = list match {
  case fa :: fas => fn(fa).ifM( dropWhileM(fas)(fn), list.pure[F] )
  case _ => list.pure[F]
}
wonder why you'd want F[A] => F[Boolean] instead of A => F[Boolean] tho
Luis Martinez
@luis3m
or even A => Boolean I'd say
Oleg Pyzhcov
@oleg-py
Well, that's quite common. "filename => file.exists" needs to go through an effect type
Luis Martinez
@luis3m
right
Oleg Pyzhcov
@oleg-py
Hmm...
def dropWhileM[F[_]: Monad, A](list: List[F[A]])(fn: F[A] => F[Boolean]): F[List[F[A]]] = {
  list.tails.toStream
    .findM { _.headOption.existsM(fn).map(!_) }
    .map(_.orEmpty)
}
Gavin Bisesi
@Daenyth
needs moar tailRecM
Ivano Pagano
@ivanopagano
any way to generically build a Traversable instance from a collection ?
e.g. I have a List[A] and would like to build a T[A] where there's a Traversable[T]
this is some form of CanBuildFrom, semantically, yet I would rather avoid doing it procedurally
but I guess I need to be able to "unfold" rather than "fold" as in traverse
owww, sorry, I meant Traverse[T] of course
Gavin Bisesi
@Daenyth
I'm not sure there's a typeclass describing unfold
Ivano Pagano
@ivanopagano
I guess not, indeed
I guess this means that I may stop searching
Rob Norris
@tpolecat
List is already traversable … what is your intent?
Gavin Bisesi
@Daenyth
@tpolecat I think he wants def unfoldWhatever[F[_]: Foldable, G[_]: ?Unfoldable?, A](as: F[A]): G[A] ( @ivanopagano is that right)
something something recursion schemes?
Greg Pfeil
@sellout
đź‘€
Rob Norris
@tpolecat
Haha
We don’t have such a thing but you can do it with Foldable and MonoidK
JsonFn
@JsonFn
When trying Applicative example e.g. Applicative[List].product(List(3), List(4)).flatten It throws No implicit view available from (Int, Int) => scala.collection.GenTraversableOnce[B]. How can I create GenTraversableOnce? The only way is to create a new such instance? for instance new GenTraversableOnce { ... }
Fabio Labella
@SystemFw
that's a red herring
Rob Norris
@tpolecat
What is your intent? The product is a List of pairs of Int.
Fabio Labella
@SystemFw
there no implicit views needed in cats, you must be getting flatten from collections
Rob Norris
@tpolecat
If you want a list of Int you can flatMap the pairs to lists.
Fabio Labella
@SystemFw
random info: Applicative cannot flatten, you need monad. you can use syntax and do (List(3), List(4)).product
or mapN and many other things
JsonFn
@JsonFn
@SystemFw no wonder. my fault. thanks for the advice.
Soren
@srnb_gitlab

wonder why you'd want F[A] => F[Boolean] instead of A => F[Boolean] tho

@oleg-py I'm checking the state of whether creating a Resource[F, ?] failed or not

So I want to use dropWhileF(_.attemptT.isLeft)
then take the head on it
Jakub Kozłowski
@kubukoz

We don’t have such a thing but you can do it with Foldable and MonoidK

MonoidK or Alternative?

Soren
@srnb_gitlab
@Daenyth On the usage of fs2, how should I get the head from the stream? Is calling compile in the middle of my program ok?
Jakub Kozłowski
@kubukoz
it's fine (stream.head.compile.lastOrError should do it)
Gavin Bisesi
@Daenyth
Sure. You can .head.compile.last and get F[Option[A]]
Jakub Kozłowski
@kubukoz
or that yeah
Gavin Bisesi
@Daenyth
But if your F[A] is a resource you don't want this
because you've lost release
Jakub Kozłowski
@kubukoz
I think he doesn't have a resource since there's a List[A] mentioned forget it, I can't read again today
Soren
@srnb_gitlab
Yeah, I need the first Resource[F, Connection[F]] that doesn't encounter an error
Gavin Bisesi
@Daenyth
that's a bit trickier
and when I say a bit... massively
uh
how about