Simplifying Future[Option[_]] with Monad Transformers
Companion code to article Practical Monads: Dealing with Futures of Options
Simplifying Future[Option[_]] with Monad Transformers
Simplifying Future[Option[_]] with Monad Transformers
Companion code to article Practical Monads: Dealing with Futures of Options
Not an issue with your code, but a slight change leads to a compiler error which I cannot understand or fix. Perhaps you can shed some light on it.
adding types inside the for comprehension fails to compile:
val summed = for {
resultOne: Int <- one
resultTwo: Int <- two
} yield {
resultOne + resultTwo
}
The compiler error is: value filter is not a member of com.example.monadT.OptionTransformer[scala.concurrent.Future,Int] [error] resultOne: Int <- one
while this compiles and runs:
val summed = for {
resultOne <- one
resultTwo <- two
} yield {
resultOne + resultTwo
}
How is filter
involved here? How would I fix it and still keep explicit types?
Again, a slight change to the code leads to a compilation failure which I dont quite understand:
runs fine:
val one: OptionTransformer[Future, Int] = OptionTransformer(Future(Option(1)))
fails to compile:
val one: OptionTransformer[Future, Int] = OptionTransformer(Future(Some(1)))
[error]... no type parameters for method apply: (value: T[Option[A]])(implicit m: com.example.monadT.Monad[T])com.example.monadT.OptionTransformer[T,A] in object OptionTransformer exist so that it can be applied to arguments (scala.concurrent.Future[Some[Int]])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : scala.concurrent.Future[Some[Int]]
[error] required: ?T[Option[?A]]
[error] val one: OptionTransformer[Future, Int] = OptionTransformer(Future(Some(1)))
A method returning Future(Some(1))
does compile and run:
val getFO: (Int) => Future[Option[Int]] = (in) => Future.successful(Some(in + 1))
...
resultOne <- OptionTransformer(getFO(0))
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.