items.groupMapReduce(_.key)(_ => 1)(_ + _)
items.foldMap(item => Map(item.key -> 1))
Builder, which has no specific knowledge of maps for instance. So when you do
Map.newBuilder[String, Int]you end up with a
Builder[(String, Int), Map[String, Int]], which has no knowledge of maps specifically.
MapBuilderis just a mutable map that also has a mutable version of the method I explained above,
Buildersare intended for the stdlib itself, not really for users.
Mapwith the method you want.
java.util.Map, but I was specifically looking for something to build an immutable map, but avoiding the overhead. And before you say "the overhead might not matter", then yes, you're right, but I was looking for a way to do it none the less :)
groupMapReducewhich are optimized under the hood.
I have a naive question about using type class in scala 3. So I defined a
Functor and an
Applicative trait in the standard way
trait Functor[F[_]]: extension[T] (f: F[T]) def fmap[S](g: T => S): F[S] trait Applicative[A[_]] extends Functor[A] : def pure[S](s: S): A[S] extension[T] (a: A[T]) infix def <*>[S](f: A[T => S]): A[S]
Then I am trying to add given instance that needs some other applicative:
given[A[_]: Applicative]: Applicative[[T] =>> Foo[A, T]] with override def pure[T](t: T): Foo[A, T] = Foo(pure(t)) // ... rest omitted
Foo accepts a
A[T]. But the above code resolves
pure(t) to the
pure being defined, rather than the
pure available from
A[_]: Applicative. How can I provide a hint so resolution goes to the right function?
BTW, I wish I could use
map instead of
Functor. But doing that would prevent me from calling the standard
map method if I want to provide
given instances for, say
List. Is there a way to work around this?
given[A[_]](using ev: Applicative[A]): Applicative[[T] =>> Foo[A, T]] with override def pure[T](t: T): Foo[A, T] = Foo(ev.pure(t))
BTW, I wish I could use map instead of fmap for Functor. But doing that would prevent me from calling the standard map method if I want to provide given instances for, say List. Is there a way to work around this?