Coder Social home page Coder Social logo

blog-posts's People

Contributors

ben-g avatar fillito avatar javisoto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

ben-g harryprayiv

blog-posts's Issues

Please explain more about the intention of Monad?

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?

innerResult?

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.)

Optional is not a Monad?

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)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.