javisoto / blog-posts Goto Github PK
View Code? Open in Web Editor NEWMy blog posts published in www.javiersoto.me
My blog posts published in www.javiersoto.me
Nice article Javi. I just came across a scenario where I would like to have a flatMap on optional, and I realized there isn't one.
Therefore, in the swift standard library Optional is current a Functor but not a Monad right?
I'm thinking this might be a great use-case to point out for a practical benefit having a monad.
You already encourage people to come up with their own flatMap for Optional in your article, which is great, but maybe you could also mention that there actually is no flatMap for optional, and you could use an extension to turn Optional into a Monad using your own implementation of flatMap.
Here is a trivial example of when you may want a flatMap in optional. The nesting comes when you are using an Optional return value within your map function.
func squareRoot(number: Int) -> Int? {
let root = Int(sqrt(Float(number)))
return root * root == number ? root : nil
}
let optionalInput: Int? = 4
let optionalOutput = optionalInput.map { squareRoot($0) }
println (optionalOutput)
//=> Optional(Optional(2))
if let unwrappedOutput = optionalOutput {
println(unwrappedOutput)
//=> Optional(2)
}
In the Monads section of the post, there is a code snippet where you define flatten, and inside there is a reference to innerResult
- is this a typo? Or where is this coming from? Maybe this is some magic swift thing I am not familiar with? Very informative post, but this is tripping me up a bit. (I'm just starting to learn Swift, so maybe this is a language feature I haven't quite grasped yet.)
In the earlier example, we used the transformation function to return another value, but what if we wanted to use it to return a new Result object?
func map<U>(f: T -> U) -> Result<U>
What is the practical reason for which it returns Result<U>
instead of U
? We wrap U
inside Result
then flatten
it, to get U
again?
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.