by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Fabio Labella
@SystemFw
which is why you don't need to explicitly declare a Functor for your algebra
class Functor f where
   fmap :: (a -> b) -> f a -> f b

data Coyoneda f a where
   Fmap :: (a -> b) -> f b -> Coyoneda f a
in Scala, for a while we were literally doing Free[Coyoneda[F, ?], A] (with a FreeC type synonym)
until it got folded in, and then it ended up looking like Operational
Rob Norris
@tpolecat
Free of Coyoneda was the :fire: for a long time. It really opened things up.
Billzabob
@Billzabob
That's a lot to take in but I love when I get something new to learn about from here. One more question: Is there an implementation of a rose tree with the appropriate cats type-class instances somewhere or should I just try and roll my own?
Fabio Labella
@SystemFw
@Billzabob funnily enough, that's Cofree
using List as the pattern functor
Billzabob
@Billzabob
@SystemFw Interesting how often things seem to tie together like that.
Fabio Labella
@SystemFw
yet another way to look at Free (especially evident in the first version) is as a tree with a Functorful f of children, and values at the leaves (the Pure constructor is Leaf a, and Roll is the branch, with f children). If you look at Cofree, that's also a Tree with values at the branches instead, and again whose structure is determined by f, and that's a Rose Tree
data Free f a = Leaf a | Node f (Free f a)
data Cofree f a = Node a (f (Cofree f a))
type Rose = Cofree []
-- replace and you get
data Rose a = Node a [Rose a]
Igor Postanogov
@ipostanogov

Hi!

If I have Map[String, Int] & f: Int => ValidatedNec[String, Bool], I can get Map[String, ValidatedNec[String, Bool]] via mapValues.
Is there any way to get ValidatedNec[String, Map[String, Bool]] from it?

Oleg Pyzhcov
@oleg-py
import alleycats.std.map._ and then traverse?
Igor Postanogov
@ipostanogov
@oleg-py, it works, thanks!
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