These are chat archives for typelevel/cats

10th
Dec 2017
Luka Jacobowitz
@LukaJCB
Dec 10 2017 00:43
I wouldn’t be averse to that
I do think they should go in a different package
I.e. cats.collection or something of the sort
Adelbert Chang
@adelbertc
Dec 10 2017 01:03
ive wanting Streaming a couple of times
Adelbert Chang
@adelbertc
Dec 10 2017 01:13
Luka Jacobowitz
@LukaJCB
Dec 10 2017 01:13
Pretty sure that’s IO.apply
Adelbert Chang
@adelbertc
Dec 10 2017 01:15
no ExecutorService though
i recall something about not having ExcecutionContext anywhere in cats-effect intentiaonlly?
Luka Jacobowitz
@LukaJCB
Dec 10 2017 01:16
I don’t think cats-effect offers that
Adelbert Chang
@adelbertc
Dec 10 2017 01:16
and concurrency would be provided by FS2 or something?
Luka Jacobowitz
@LukaJCB
Dec 10 2017 01:16
Yeah
Adelbert Chang
@adelbertc
Dec 10 2017 01:16
i guess my question then becomes whats the FS2 version of Task.apply
Luka Jacobowitz
@LukaJCB
Dec 10 2017 01:17
Well, it works somewhat differently AFAIK, you do not need to specify the ExecutionContext when capturing synchronous effects
You’d specify it when doing something with concurrency, like fs2.async.start
Fabio Labella
@SystemFw
Dec 10 2017 01:19
@adelbertc the cats-effect version of apply
is IO.shift >> IO.apply(...)
Adelbert Chang
@adelbertc
Dec 10 2017 01:19
o_O
Fabio Labella
@SystemFw
Dec 10 2017 01:19
if you look at Task.apply
it does Task.fork(Task.delay(...))
Adelbert Chang
@adelbertc
Dec 10 2017 01:20
oh interesting
Fabio Labella
@SystemFw
Dec 10 2017 01:20
where fork it's a misnomer
because it's submitting to an ec
Adelbert Chang
@adelbertc
Dec 10 2017 01:20
interesting interesting
Fabio Labella
@SystemFw
Dec 10 2017 01:20
but not actually fork-ing as in asynchronously starting stuff
for this ^^^ you need fs2
either fork (which does what it says in fs2) or start
Adelbert Chang
@adelbertc
Dec 10 2017 01:21
gotcha
makes sense
Fabio Labella
@SystemFw
Dec 10 2017 01:21
but if you only want the equivalent of Task.apply, the shift >> apply thing will do
Adelbert Chang
@adelbertc
Dec 10 2017 01:21
thank you!
Fabio Labella
@SystemFw
Dec 10 2017 01:21
no problem :)
that being said though
it was generally considered a bad idea that Task.apply would always submit to the ec on creation
that's what Task.delay was encouraged
IO.apply is the equivalent fo Task.delay
Adelbert Chang
@adelbertc
Dec 10 2017 01:34
right
Fabio Labella
@SystemFw
Dec 10 2017 01:34
and you have a generic operation call shift
IO.shift, which takes an implicit ec
and moves the subsequent computations to it, until another call to shift moves it somewhere else
so, creation of sync actions and ec submission has been separated
Adelbert Chang
@adelbertc
Dec 10 2017 01:35
interesting
i seem to recall there used to be shift method on IO
Fabio Labella
@SystemFw
Dec 10 2017 01:35
that was changed
it had confusing semantics
you can see an explanation of that in the original an IO monad for cats blog post
with the whole shift.shift idiom
now shift is simpler to understand
"submit everything that follows to ec"
Rob Norris
@tpolecat
Dec 10 2017 01:42
I need to figure out how to shift back.
I don’t want the whole continuation to be on specialized pool X. I want to do my stuff and then shift back to the caller.
Fabio Labella
@SystemFw
Dec 10 2017 01:43
that's easy :)
IO.shift(blockingJDBC) *> doobieQuery *> IO.shift
Rob Norris
@tpolecat
Dec 10 2017 01:44
How? You don’t have a source context to shift into. All you have is a thread and you don’t know where it came from.
Fabio Labella
@SystemFw
Dec 10 2017 01:44
assuming your normal ec is implicit
well, the function that does the shift and shift back needs two execution contexts as arguments, that's true
Rob Norris
@tpolecat
Dec 10 2017 01:45
Right.
Implicit ec is unforgivable.
/me waves arms around
Fabio Labella
@SystemFw
Dec 10 2017 01:48
:)
anyway yeah, there's no notion of "the current ec"
otoh, shift is the only function (excluding fromFuture ofc) that mentions ec in cats-effect
you don't need an ec to create an IO (not even an async one), nor to run it
Rob Norris
@tpolecat
Dec 10 2017 01:49
Indeed.
Fabio Labella
@SystemFw
Dec 10 2017 01:50
there's a bunch in fs2 though, since we provide some concurrency
Adelbert Chang
@adelbertc
Dec 10 2017 01:53
gotcha
im in the midst of converting a scalaz-stream library into FS2
Fabio Labella
@SystemFw
Dec 10 2017 01:53
that being said, it might be worth shifting at the start of your program from the current thread to the main pool (normally Implicits.global)
just so you always have a notion of "the initial pool" that you can shift back to
ah, cool
well, hopefully it all goes smoothly, but happy to help either here or in the fs2 channel if you are unsure about anything
I'd also suggest you go to the milestone version directly (0.10-M9 currently)
Adelbert Chang
@adelbertc
Dec 10 2017 01:56
yep
will do!
asaadza
@asaadza
Dec 10 2017 02:27
I was thinking about experimenting with something like IORef for shared state, eg a bit of the app state that is updated based on external events (like button clicks or changes in weather or ...), but also read elsewhere (eg updating a UI or sending notification emails or ...), without using FRP or anything else that requires extra libraries beyond cats or scalaz, for now. So, the requirement is shared state that is also mutable, eg global mutable state. Is there already some suitable structure for an idiomatic solution in cats for such use-cases?
Rob Norris
@tpolecat
Dec 10 2017 02:30
fs2 has a Ref type.
Async mutable cell
Functional UI programming in Scala is unsolved, mostly because the underlying libraries are all complete garbage.
asaadza
@asaadza
Dec 10 2017 02:32
@tpolecat - ;) . I'll take a look, thanks. Is there a reason why cats-effect does not support something like that?
For example, it would be useful in scala-js, for dom events.
Cody Allen
@ceedubs
Dec 10 2017 02:33
(separate conversation) I just opened #2089 to discuss removing OneAnd please weigh in if you are someone who would like to see it stick around.
Rob Norris
@tpolecat
Dec 10 2017 02:33
Not sure. @SystemFw does Ref rely on a bunch of other fs2 stuff?
Qi Wang
@Qi77Qi
Dec 10 2017 02:37
IO.shift(blockingJDBC) > doobieQuery > IO.shift
what’s this blockingJDBC look like usually? :]
Michael Pilquist
@mpilquist
Dec 10 2017 02:39
Ref and Promise can both be ported to cats-effect directly - no dependencies on Stream or other fs2 constructs
asaadza
@asaadza
Dec 10 2017 02:39
... looking ahead, do you envisage libraries like fs2 and monocle supporting both scalaz and cats for the foressable future? TBH, I thought FS2 was scalaz only (since it was called scalaz-stream) until I noticed a mention in the cats readme?
Andi Miller
@andimiller
Dec 10 2017 02:39
is there some kind of flatten for Validated? where Validated[NEL[String], Validated[NEL[String], A]] can turn into Validated[NEL[String], A] and combine the NELs?
Michael Pilquist
@mpilquist
Dec 10 2017 02:40
fs2 directly depends on cats and supports scalaz through shims
asaadza
@asaadza
Dec 10 2017 02:41
@mpilquist - OK. It also gives cats a good workout in the field then.
Fabio Labella
@SystemFw
Dec 10 2017 02:41
@tpolecat Like Michael said, both Ref and Promise can in principle be ported to cats-effect. In practice, I'd like to see some more usage first, since it is but one concurrency scheme that you can envision for cats-effect
Cody Allen
@ceedubs
Dec 10 2017 02:41
@Qi77Qi it would be something like stmt.executeQuery(query) with lots of other garbage for creating the statement, adding a stmt.close finalizer, etc. All of the stuff that Doobie does for you with a much better API :)
Qi Wang
@Qi77Qi
Dec 10 2017 02:41
@ceedubs oh I was wondering about which thread pool should it be
was trying to remember what we were using for blockingIO
Cody Allen
@ceedubs
Dec 10 2017 02:44
@andimiller you can do something like _.andThen(identity). But there would never be a case where it could combine the NELs, because if the “outer” Validated is an Invalid then there would be no “inner” Validated to combine with
Fabio Labella
@SystemFw
Dec 10 2017 02:44
@Qi77Qi there's a gist by Daniel (Spiewak) summarising his advice on thread pool management
Qi Wang
@Qi77Qi
Dec 10 2017 02:45
@SystemFw do you have the gist? :smile:
Andi Miller
@andimiller
Dec 10 2017 02:45
oh good point
Fabio Labella
@SystemFw
Dec 10 2017 02:45
let me dig it up for you
Qi Wang
@Qi77Qi
Dec 10 2017 02:45
thank you @SystemFw !
Fabio Labella
@SystemFw
Dec 10 2017 02:45
Qi Wang
@Qi77Qi
Dec 10 2017 02:46
:thumbsup: thanks!
Cody Allen
@ceedubs
Dec 10 2017 02:46
@andimiller if you have a structure like that, then it probably means that you should have used something like andThen a little earlier in your code to avoid the nested validations
Fabio Labella
@SystemFw
Dec 10 2017 02:47
to answer your more general question, you can't easily know if something is blocking or not unfortunately :( you have to rely on api docs, or common knowledge.
For example, in fs2 nothing is blocking (when we use blocking, it's semantic blocking, but no actual threads are being blocked). Otoh, in JDBC everything is
Andi Miller
@andimiller
Dec 10 2017 02:47
I'm trying to chain effects onto the Validated, so I'm doing a .map { IO { blah } }.sequence
Cody Allen
@ceedubs
Dec 10 2017 02:47
though there are certain times when structures like that tend to come up
Andi Miller
@andimiller
Dec 10 2017 02:48
the effect makes it a little bit awkward to work with the Validated :(
Cody Allen
@ceedubs
Dec 10 2017 02:49
@andimiller pretty much any time that you have . map(…).sequence you should be able to change it to .traverse(…)
Andi Miller
@andimiller
Dec 10 2017 02:49
that is also a great point, thanks :)
Fabio Labella
@SystemFw
Dec 10 2017 02:49
other things to consider are using Nested with IO and Validated
Cody Allen
@ceedubs
Dec 10 2017 02:49
I don’t think that solves the other issue, but it’s a useful tidbit :)
Fabio Labella
@SystemFw
Dec 10 2017 02:50
or EitherT (using parMapN and co when you want accumulation rather than shortcircuiting)
Cody Allen
@ceedubs
Dec 10 2017 02:50
:thumbsup:
harrydevnull
@harrydevnull
Dec 10 2017 04:06

i have naive question

case class PersonWithAddress(name:String,age:Int,address: Option[Address])

case class Person(name:String,age:Int)
case class Address(firstLine:String)

val pl = List(
  Person("alpha",31),
  Person("beta",32),
  Person("gamma",33),
  Person("delta",34)
)

val addresses = Map(
  "alpha" ->"1stline",
  "beta"->"2ndline",
  "gamma"->"3rd line",
  "delta"->"4th line"
)

I have a list of persons and map of address I need to combine them as List[PersonwithAddress] classes . how would i do using monoids?

Andi Miller
@andimiller
Dec 10 2017 04:09
a monoid would be used to combine multiple people into one person
harrydevnull
@harrydevnull
Dec 10 2017 04:18

ah ok! sorry I am pretty new to functional programming, I am not sure the exact verbiage to indicate what I want.

val  o =pl.map(i=>PersonWithAddress(i.name,i.age,addresses.get(i.name)))

this is the way I got it; are there any better ways of doing it cats?

Rob Norris
@tpolecat
Dec 10 2017 04:29
Looks good to me.
Alexander Konovalov
@alexknvl
Dec 10 2017 04:48

a monoid would be used to combine multiple people into one person

Monoid[Frankenstein]

zero_coder
@kostonstyle
Dec 10 2017 11:43
I have a monadic recursion and want to know, if it is thread safe or not:
  private def pool[A]
  (consumer: => Consumer[String, String])(cb: Vector[KkConsumerRecord] => IO[A]): IO[Unit] = {
    val records: ConsumerRecords[String, String] = consumer.poll(Long.MaxValue)
    val converted = records.iterator().asScala.map(rec => {
      KkConsumerRecord(rec.key(),
        rec.value(),
        rec.offset(),
        rec.partition(),
        rec.topic(),
        rec.timestamp())
    })

    val vec = converted.foldLeft(Vector.empty[KkConsumerRecord]) { (b, a) =>
      a +: b
    }

    cb(vec).flatMap(_ => pool(consumer)(cb))
  }
Could someone please, tell me if it is thread safe or not?
Jose C
@jmcardon
Dec 10 2017 23:23
the thread safety of your code has nothing to do with IO, it has to do with what you're manipulating. IO is just how you're suspending the side effects.
if that code is not thread safe, wrapping it in IO will not make it thread safe