Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Sep 14 18:08
    @SethTisue banned @discjock:matrix.org
  • Sep 12 20:37
    @SethTisue banned @manuelzamoratetherbtcusa:matrix.org
  • Sep 06 14:29
    @SethTisue banned @white_hat_hacker:minds.com
  • Sep 06 14:29
    @SethTisue banned @alexchole:matrix.org
  • Aug 31 17:03
    @SethTisue banned @andersonwatts7:matrix.org
  • Jul 19 20:37
    @SethTisue banned @tonyobanon
  • Jan 02 23:58
    @SethTisue banned @fakubishes:nerdsin.space
  • Dec 15 2021 05:01
    som-snytt commented #12516
  • Dec 15 2021 04:38
    SethTisue edited #1312
  • Dec 15 2021 04:38
    SethTisue opened #2273
  • Dec 15 2021 04:31
    jackkoenig opened #12516
  • Dec 15 2021 04:29
    SethTisue edited #1312
  • Dec 15 2021 04:28
    SethTisue edited #1312
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    scala-jenkins milestoned #9831
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    SethTisue opened #9831
  • Dec 15 2021 03:35
    som-snytt commented #11339
  • Dec 15 2021 03:27
    som-snytt labeled #12494
  • Dec 15 2021 03:07
    SethTisue edited #1312
Luis Miguel Mejía Suárez
@BalmungSan
@megri you can do this:
items.foldLeft(Map.empty[K, V]) {
  case (acc, item) =>
    acc.updatedWith(key = item.key) {
      case Some(n) => Some(n + 1)
      case None => Some(0)
    }
}
No need to mutate.
Also, this seems it is doing some kind of word-count or something similar, right?
You should be able to just:
items.groupMapReduce(_.key)(_ => 1)(_ + _)
Bonus: If you have cats in scope, you can just do:
items.foldMap(item => Map(item.key -> 1))
megri
@megri
@BalmungSan Thanks, but I wanted a builder version specifically. I'm just coding for fun and part of that is exploring the standard library. Not having a way to build a map stumped me and I figured I had to be missing something, but maybe it's just an omission..
I think the underlying issue is that all collection builders return a base trait 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.
Luis Miguel Mejía Suárez
@BalmungSan
@megri Well if you add a new key-value pair whose key was already present it overwrite the previous key.
Also, under the hood a MapBuilder is just a mutable map that also has a mutable version of the method I explained above,
Anyways, Builders are intended for the stdlib itself, not really for users.
So that is why there is no specific builder for Map with the method you want.
In any case, you should not wonder for this kind of low level details.
megri
@megri

Anyways, Builders are intended for the stdlib itself, not really for users.

I find that statement questionable.

And I understand that I can overwrite an already existing key with a new one. What I'm specifically looking for is a way to do it without recomputing the whole map every time I change a key. I know I can use something like mutable.Map or 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 :)
Luis Miguel Mejía Suárez
@BalmungSan
mutable.Map.updateWith
Or again, just look for something higher level like groupMapReduce which are optimized under the hood.
Matt Hicks
@darkfrog26
Anyone needing help migrating away from log4j to Scribe, just let me know, I'd be happy to help. ;)
lambook
@lambook
i'm looking for a library to handle HEX operations (hex String to byte array etc..), i have this library org.apache.commons.codec, is there any core scala library to do the same thing?
ritschwumm
@ritschwumm
@lambook i don't think the core lib has it - but imho that's one of the things that are too simple to be worth adding a third part dependency.
lambook
@lambook

@lambook i don't think the core lib has it - but imho that's one of the things that are too simple to be worth adding a third part dependency.

is this an official library ? i m looking for a library that is widely used

johannes karoff
@cornerman
Hi, I am trying to write a macro in scala 3, where I need to list all abstract methods of a trait - so I want to ignore all implemented member methods.
I am using TypeTree.of[T].memberMethods, but do not know how to filter.
ritschwumm
@ritschwumm
@lambook no, not at all. it's my personal collection of useful things. it's not even published to a public maven repo.
Hanns Holger Rutz
@Sciss

If I have

def augmentString (x: String) : Any = ()

can I disable the compiler warning:

parameter value x in method augmentString is never used

by annotating this method somehow?

Luis Miguel Mejía Suárez
@BalmungSan
@Sciss AFAIK you can do: def augmentString (@unused x: String) : Any = ()
Hanns Holger Rutz
@Sciss
@BalmungSan thanks, excellent
Luis Miguel Mejía Suárez
@BalmungSan
Did that worked?
Hanns Holger Rutz
@Sciss
yes
Hanns Holger Rutz
@Sciss
errrr, @unused doesn't cross compile in Scala 2.12. But it's part of scala-collection-compat.
Luis Miguel Mejía Suárez
@BalmungSan
:sweat_smile:
Mark T. Kennedy
@mtk
anyone else having problems reaching docs.scala-lang.org? it's mapped to icvm0053.epfl.ch (128.178.218.78) and traceroute dies at the first epfl.ch host it reaches.
Luis Miguel Mejía Suárez
@BalmungSan
Mark T. Kennedy
@mtk
thanks!
go through with this and make it runnable
johannes karoff
@cornerman
To answer my own question about finding out which member method are abstract (not implemented) in a macro, you can use: TypeTree.of[T].memberMethods.filter(_.flags.is(Flags.Deferred))
Edrick de Guzman
@edrick.deguzman_gitlab
Hello everyone, I would like to ask for some guidance/insight regarding create a jar file/package for my scala test project? I currently don't have a main class since we use test instead of main. Can anyone point me to the right direction?
Luis Miguel Mejía Suárez
@BalmungSan
@edrick.deguzman_gitlab care to elaborate?
Why do you want to create a jar if you don't have a main?
Who is going to be your user?
tgeng
@tgeng

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

where 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 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?

Luis Miguel Mejía Suárez
@BalmungSan

@tgeng

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?

WDYM?

2 replies
Luis Miguel Mejía Suárez
@BalmungSan
@tgeng please don't use threads.
Second, that is not a valid Functor.
At least not one represented using typeclasses.
You need the F parameter.
Ah well no.
Forget about it.
I am not used to extension methods.