scalaz / ioeffect Goto Github PK
View Code? Open in Web Editor NEWAn effect monad for Scalaz 7.2
License: Other
An effect monad for Scalaz 7.2
License: Other
we can easily add https://gitlab.com/fommil/sbt-neojmh or sbt-jmh
but we need the perftests in the src/jmh
directory to be able to run then.
@jdegoes any chance of making them available so we can check for regressions?
Sorry about open here this issue, but to be honest there's no community mature enough in StackOverFlow to help on issues related with this library.
I'm trying to do an example of composition of Fibers, based in the for comprehension that you provide with fibboinachi example
I manage to make it work the fibbonachi, but if you execute this one is not returning anything.
most probably it will be something silly so sorry in advance XD!
@Test
def compositionOfFibersFeature(): Unit = {
println(s"Before ${Thread.currentThread().getName}")
def composition: IO[Throwable, String] = for {
fiber <- createIO("Business logic 1").fork
fiber1 <- createIO("Business logic 2").fork
v2 <- fiber1.join
v1 <- fiber.join
} yield v1 + v2
println(s"After: ${Thread.currentThread().getName}")
unsafePerformIO(composition)
}
private def createIO(sentence:String):IO[Throwable,String] = {
IO.point[Throwable, String](sentence)
.map(sentence => {
sentence.concat(s"$sentence ${Thread.currentThread().getName}").toUpperCase()
}).delay(1 second)
}
I´m learning the monad IO of scalaZ and I cannot understand how catchAll and catchSome operators works.
I was expecting so see a behave like the onError or onErrorrResumeNext of RxJava, but instead is not catching the throwable, and it´s just breaking the test and throwing the NullPointerException.
Here my two examples
@Test
def catchAllOperator(): Unit = {
val errorSentence: IO[Throwable, String] =
IO.point[Throwable, String](null)
.map(value => value.toUpperCase())
.catchAll(error => IO.fail(error))
println(unsafePerformIO(errorSentence))
}
And catchSome example
@Test
def catchSomeOperator(): Unit = {
val errorFunction = new PartialFunction[Throwable /*Entry type*/ , IO[Throwable, String] /*Output type*/ ] {
override def isDefinedAt(x: Throwable): Boolean = x.isInstanceOf[NullPointerException]
override def apply(v1: Throwable): IO[Throwable, String] = IO.point("Default value")
}
val errorSentence = IO.point[Throwable, String](null)
.map(value => value.toUpperCase())
.catchSome(errorFunction)
println(unsafePerformIO(errorSentence))
}
Any idea what I´m doing wrong?.
Regards
Readme currently reads:
def run(args: List[String]): IO[Void, ExitStatus] =
myAppLogic.attempt.map(_.fold(_ => 1)(_ => 0)).map(ExitStatus.ExitNow(_))
It should read:
def run(args: List[String]): IO[Void, ExitStatus] =
myAppLogic.attempt.map(_.fold(_ => 1, _ => 0)).map(ExitStatus.ExitNow(_))
we need somebody with the sonatype creds to add these envvars
SONATYPE_USERWORD
SONATYPE_PASSWORD
or to populate ~/.m2/credentials
as per https://gitlab.com/fommil/sbt-sensible/blob/master/src/main/scala/SonatypePlugin.scala
and to check that they are happy with the travis config.
Before doing that, I recommend renaming this repo to scalaz-ioeffect
.
// @xuwei-k do you have rights?
sbt makePom
cat target/scala-2.12/scalaz-ioeffect_2.12-0.0.1-SNAPSHOT.pom
<?xml version='1.0' encoding='UTF-8'?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.scalaz</groupId>
<artifactId>scalaz-ioeffect_2.12</artifactId>
<packaging>jar</packaging>
<description>scalaz-ioeffect</description>
<url>http://github.com/scalaz/effect</url>
<version>0.0.1-SNAPSHOT</version>
<licenses>
<license>
<name>BSD3</name>
<url>https://opensource.org/licenses/BSD-3-Clause</url>
<distribution>repo</distribution>
</license>
</licenses>
<name>scalaz-ioeffect</name>
<inceptionYear>2017</inceptionYear>
<organization>
<name>org.scalaz</name>
<url>http://github.com/scalaz/effect</url>
</organization>
<scm>
<url> Some(http://github.com/scalaz/effect) </url>
addSbtPlugin("com.fommil" % "sbt-sensible" % "2.4.1")
bug ? 🤔
https://gitlab.com/fommil/sbt-sensible/blob/36ed88c76e7b249571b951c8ac653ba66b5b00e7/src/main/scala/SonatypePlugin.scala#L131
/cc @fommil
when integrating with existing code, having the ability to create a Task
from some Future
thing is essential.
But we don't seem to have anything like this... can we please add this?
I'm trying to migrate my work codebase to scalaz.ioeffect
from cats-effect right now, so if there are any snippets that I can use as a workaround please do let me know.
note that cats has fromFuture[A](iofa: IO[Future[A]]): IO[A] = ...
which allows deciding if the future begins now or delayed.
I can't compile the example code provided in the README.
I get the following compilation error:
[error] /MyApp.scala:10: overriding method run in trait SafeApp of type (args: List[String])scalaz.ioeffect.IO[scalaz.ioeffect.Void, MyApp.ExitStatus];
[error] method run has incompatible type
[error] def run(args: List[String]): IO[Void, ExitStatus] =
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Feb 13, 2019 4:34:13 PM
And the following in IntelliJ:
Overriding type List[String] => IO[Void, MyApp.ExitStatus] does not conform to base type List[String] => IO[ioeffect.Void, SafeApp.this.ExitStatus]
Just to be clear, here is the example code from the README that I'm trying to run:
import scalaz.ioeffect.{IO, SafeApp}
import scalaz.ioeffect.console._
import java.io.IOException
object MyApp extends SafeApp {
def run(args: List[String]): IO[Void, ExitStatus] =
myAppLogic.attempt.map(_.fold(_ => 1, _ => 0)).map(ExitStatus.ExitNow(_))
def myAppLogic: IO[IOException, Unit] =
for {
_ <- putStrLn("Hello! What is your name?")
n <- getStrLn
_ <- putStrLn("Hello, " + n + ", good to meet you!")
} yield ()
}
I am using Scala 2.11.7 and Scalaz 7.2.27
we don't need to use Nothing
here, we have an encoding for this, see INil
in the 7.3 branch.
private final case object Later extends AsyncReturn[Nothing]
Also we can then do an exhaustive match instead of the case _ =>
I see 😱
I think we can only provide MonadPlus when E is Unit. Otherwise we can't differentiate between failures and the empty failure, or maybe our Plus logic needs to be more advanced.
I was kind of surprised not to find one of these, just a Monad
.
I'm using IO.syncThrowable
to invoke a legacy method that can throw an exception.
But even though that's supposed to capture the execption as a valid Task
error, not an unexpected exception, I'm still getting the stacktrace dumped to the screen with
scalaz.ioeffect.Errors$UnhandledError: An error was not handled by a fiber:
I can try to minimise it with an example if this is difficult to see by inspection.
So it seems something is going on wrong with par
implementation, when performed in isolation it seems to work but it starts having unpredictable behavior when performed multiple consecutive times, I realized about this since I have some property-based test with default sampling of 100 performing some IO using par
combinator.
You can find the test i'm talking about here: https://github.com/ktonga/scala-poc/blob/topic/parallel-io/src/test/scala/com/brickx/autoinvest/ProgramSpec.scala#L26
This tests used to run ok when IO
s were combined using apply2
from default Applicative
instance but started failing after I introduced the use of par
in this WIP PR: ktonga/scala-poc#6
I'll try to add a minimal example that reproduces the bug to this project for easy debugging.
Cheers.
specifically with https://github.com/sjrd/sbt-dynscalajs
because the regular scalajs plugin is just too painful.
https://twitter.com/paul_snively/status/987688873898729472 // @PaulAtBanno
We currently do not have a bimap
for IO
. As a bifunctor this is necessary for calling it a bifunctor. If someone wants to implement the new tag and case of the IO
runtime supporting it (for instance, as was done with the Map
and FlatMap
tags), that would be greatly appreciated.
OK, so here I thought race was straight-forward. Clearly I am doing something wrong...
import $ivy.`org.scalaz::scalaz-ioeffect:2.1.0`, scalaz._, ioeffect._, Scalaz._, scala.concurrent.duration._
object App extends SafeApp {
type Error = Exception
def run(args: List[String]): IO[Error, Unit] = {
val io1 = IO.sleep[Error](10.seconds).fork[Error]
val io2 = IO.point[Error, Unit]{ println("Hello"); Thread.sleep(500L) }.forever[Unit].fork[Error]
io1.race(io2).toUnit
}
App.main(Array.empty)
I was under the impression the two IO
would race each other and once the first one finishes (clearly the sleeper, since the other one is a forever
guy), the slower one is interrupted and will never be heard of again.
In the above code io2
is completely unfazed by the race and will keep on running forever. Can an IO.forever
not be interrupted?
Anyways, I changed things around a bit, thinking maybe one of the fibres is not allowed to be forked, and now I have this:
import $ivy.`org.scalaz::scalaz-ioeffect:2.1.0`, scalaz._, ioeffect._, Scalaz._, scala.concurrent.duration._
object App extends SafeApp {
type Error = Exception
def run(args: List[String]): IO[Error, Unit] = {
val io1: IO[Error, Unit] = IO.sleep[Error](10.seconds).fork[Error].toUnit
val io2: IO[Error, Unit] = IO.point(println("Hello")).forever[Unit]
io1.race(io2)
}
}
App.main(Array.empty)
a) The race lasts maybe 1 second, not 10, not forever
b) Sometimes I get a bunch of prints but usually only one
Here is essentially what I need (maybe there is a significantly better way to do this):
IO[Error, Unit]
(let's call them X
and Y
)X
runs forever (it's a streaming processor)Y
does a bunch of stuff, then completesX
to start, then Y
to start. Once Y
has completed, interrupt X
, collect results, done@jdegoes I'm wondering is it possible or will be possible in future to shift thread pools? (one thread pool for CPU intense load, another one for blocking operations, etc).
🤔 ?
$ wget https://oss.sonatype.org/content/repositories/releases/org/scalaz/scalaz-ioeffect_2.12/2.0.0/scalaz-ioeffect_2.12-2.0.0-javadoc.jar
$ unzip -Z scalaz-ioeffect_2.12-2.0.0-javadoc.jar
Archive: scalaz-ioeffect_2.12-2.0.0-javadoc.jar
Zip file size: 189 bytes, number of entries: 1
-rw---- 2.0 fat 25 bX defN 18-Apr-20 23:16 META-INF/MANIFEST.MF
1 file, 25 bytes uncompressed, 27 bytes compressed: -8.0%
it would be good to make this generally available, ideally after #7 is in place.
I wonder if perhaps we should remove the extends Monad
to be able to create instances for the scalaz mtl
We introduced this signature onto MonadError in scalaz/scalaz#1697
Also, it should be on the typeclass instance, not IO.
This is inconsistent with Functor.widen
final def widen[E2](implicit ev: E <~< E2): IO[E2, A]
how about .ewiden
? Consistent with .emap
I’m building an app using Scala Native, and pure FP IO is something I’d like to use. It would be great for scalaz-ioeffect
to support Scala Native 0.3 like scalaz-core
, scalaz-effect
, scalaz-iteratee
, etc. How difficult is this from a technical perspective?
rather that publish binaries, we should note that this is how to declare a source dependency
val ioeffect = ProjectRef(
uri("git://github.com/scalaz/ioeffect.git#1a3b397"),
"ioeffect"
)
and then .dependsOn(ioeffect)
instead of libraryDependencies
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.