Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Aly
@aly:scuttlebug.space
[m]

Is there a sort of scan-traverse? I.e. I have a

val start: Int
val str: String
val f: (Int, Char) => Option[Int]

and I want

val end: Option[Int]
Looking at Hoogle, this is foldM
How do I run it on a String? Efficiently at least
Oleg Pyzhcov
@oleg-py

you don't. Efficiently, at least.

You can get the Foldable[Iterable] instance from alleycats and do (str: Iterable[Char]).foldM(start)(f). I'm pretty sure that's gonna box every char tho

Christopher Davenport
@ChristopherDavenport
It really depends on the string. If it's a byte array backed string you are always going to see some performance loss at the char level.
If it's char array backed you can in a limited fashion rely on the approach above at limited cost.
Aly
@aly:scuttlebug.space
[m]
It's a string that I'm getting from fs2.text.utf8Decode
Oleg Pyzhcov
@oleg-py
aren't java.lang.String always char-array backed?
Aly
@aly:scuttlebug.space
[m]
So I could change the combinator that I'm using there if necessary
Christopher Davenport
@ChristopherDavenport
No, there are char array and byte array strings.
Technically different, but effectively the same.
Oleg Pyzhcov
@oleg-py
oh
depends on the jre I guess :D
Christopher Davenport
@ChristopherDavenport
I think you can just operate on the string. The performance hit as long as you are efficient in a single pass is normally not noticed
It's if you do backtracking it can hurt.
Cats-parse I highly recommend based on what you are presently describing.
Oleg Pyzhcov
@oleg-py
you'll have to be boxing characters to pass them to Function2 in the first place as well
Aly
@aly:scuttlebug.space
[m]
Ok, I'll just use a while loop with charAt then, and some vars
RE cats-parse: I'm writing a lexer generator that takes tmLanguage files and generates lexers because I don't want to have to maintain both a VSCode language file and a cats-parse grammar
Christopher Davenport
@ChristopherDavenport
You are doing parsing, so that library does it efficiently and is nice to work with.
Oleg Pyzhcov
@oleg-py
there goes the wisdom of benchmark first :D
Aly
@aly:scuttlebug.space
[m]

The while loop is easier to write :^)

(Eventually I'll see if the lexer is slow enough that I need to actually work on it)

Christopher Davenport
@ChristopherDavenport
Never start with that, imo. The boxing cost on single pass will probably not be noticeable. So start there.
Oleg Pyzhcov
@oleg-py
especially if your characters are mostly ASCII :D
Aly
@aly:scuttlebug.space
[m]
I already have code for the while loop :P
Hmm, Antimirov also does a while loop for this
Christopher Davenport
@ChristopherDavenport
Depends, in library code I often end up in a while loop, but application code and more complex grammars FP generally wins.
Aly
@aly:scuttlebug.space
[m]
Yeah, this is going in generated code
zeroexcuses
@zeroexcuses
Is anyone here familiar with https://www.jetbrains.com/mps/ ? It looks like an amazing DSL building toolkit, but I'm not sure why it's almost unheard of.
Ty Scales
@tscales
Hello. I have a List[A] and a f: A => F[Boolean]. I would like to filter my list and I'm curious if there is a better way than what i've come up with:

for {
  bools <- myList.traverse(f(_))          //F[List[Boolean]]
  boolsAndVals = bools zip myList   //F[List(Boolean, A)]
}  yield boolsAndVals.filter(_._1)
Luis Miguel Mejía Suárez
@BalmungSan
It seems you can use myList.filterA(f)
But not sure if that behaves as you would expect.
Another option, if you have fs2 in scope, would be: Stream.emits(myList).evalFilter(f).compile.toList
Ty Scales
@tscales
filterA is exactly what i was needing. Thanks!
Ahmet Turk
@ahmet_turk_gitlab
Hi all, I have following effect F[_] : Async.
I want to call an service which returns F[Data] and I want to recover errors. There suppose to be some sort of handleError or recover kind of thing that I could not find it. Am I missing something?
Alexander Van Hecke
@alexandervanhecke
you can take a look at the attempt, recover and recoverWith methods (make sure you have cats.implicits._ or cats.syntax.all._ imported)
ie service.myMethod.attempt will give you an F[Either[Throwable, Data]]
Ahmet Turk
@ahmet_turk_gitlab
@alexandervanhecke I found that intellij is having problem to discover those methods these days. I never know them by heart. Intellij would help me and insert the imports but that functionality does not work for me at the moment :) Anyway. at least I know that I am not crazy and those methods are actually exists :)
Rob Norris
@tpolecat
Huh. They may have closed the copy loophole in Scala.
bblfish
@bblfish:matrix.org
[m]
some code to try it out
scala> final case class SfInt private(val long: Long) extends AnyVal   
object SfInt {
     def apply(l: Long): Option[SfInt] = if l > 0 then Some(new SfInt(l)) else None
}
scala> 
scala>  SfInt(4).map(x=>x.copy(long=-5))
1 |SfInt(4).map(x=>x.copy(long=-5))
  |                ^^^^^^
  |method copy cannot be accessed as a member of (x : SfInt) from module class rs$line$7$.
Alex Kozlenkov
@prova
Heads up for a improved (on herding-cats) UMP examples gist: https://gist.github.com/prova/3b76f61783b5089b66ad90c5e925278f
bblfish
@bblfish:matrix.org
[m]
thanks @ole
Jack Viers
@jackcviers
@ahmet_turk_gitlab You can always also do ala carte imports, too: the methods you're intrested in are in cats.syntax.applicativeError._ and cats.syntax.monadError._. For most cats things you can always import cats.syntax.<lowerCamelCase>._. And Intellij should pop up completions after you type 'cats.syntax.', whereupon you can just scroll through the list looking for somethingError. Alternatively, since Async extends MonadError, you should also be able to do Async[F].handleErrorWith(yourcurrentAsyncCode){ ... errorHandlingGoesHere ... }, and the IDE will help you by displayng all the methods on Async[F]. When you don't have the global cats.implicits._ or the correct ala carte imports imported, the IDE is correct that F doesn't have any methods named .handleErrorWith. Another thing to do when your IDE is failing you is go to the API Docs and search for your method: https://typelevel.org/cats/api/?search=handleErrorWith.
bblfish
@bblfish:matrix.org
[m]
Has anyone worked on a typesafe crypto library? The current libraries all depend on strings, which require deep knowledge of cryptography to understand.
bblfish
@bblfish:matrix.org
[m]
:point_up: Edit: thanks @oleg
Martijn
@martijnhoekstra:matrix.org
[m]
@Daenyth is the resident cryptodog I think
Rohan Sircar
@rohan-sircar
bblfish
@bblfish:matrix.org
[m]
that looks interesting, though it has not had much work for 1.5 years on it, and not that much documentation. It would be great to have feedback on it from @Daenyth :-)
bblfish
@bblfish:matrix.org
[m]
I was needing a good crypto library for the my implementation of the HTTP Bis working groups Signing Http Messages spec co-operating-systems/Reactive-SoLiD#13.
bblfish
@bblfish:matrix.org
[m]
:point_up: Edit: I was needing a good crypto library for co-operating-systems/Reactive-SoLiD#13 of the HTTP Bis working groups Signing Http Messages spec.