HNHacker News
TopNewBestAskShowJobs

JSoto

50 karma · joined January 1, 2013

submissionscomments
JSoto··on Twitter launches Fabric mobile app for developers
iOS engineer who worked on the Fabric app here :) I gave a talk about this topic at a Swift conference last year, if you wanna check that out! https://www.youtube.com/watch?v=Ent6LJDIB3I
JSoto··on Functor and Monad in Swift
Oh yeah that's a good point! (and a really interesting optimization that can be applied to immutable values)
JSoto··on Functor and Monad in Swift
I agree that it would be a little cleaner, because it would express the intention better (the fact that you're simply giving out the same error). However, the type would be wrong:

Yeah, this is correct. This would be wrong:

    func map<U>(f: T -> U) -> Result<U> {
        switch self {
        case let .Value(value):
            return Result<U>.Value(Box(f(value.unbox)))
        case let .Error(error):
            // self is Result<T>, but the return type must be Result<U>
            return self
        }
    }
As for efficiency: like you mention, enum are value types. For this reason, they're passed by copy! So even returning self would return a copy of self, not the same instance :)
JSoto··on Functor and Monad in Swift
Yeah, this is correct. This would be wrong:

    func map<U>(f: T -> U) -> Result<U> {
        switch self {
        case let .Value(value):
            return Result<U>.Value(Box(f(value.unbox)))
        case let .Error(error):
            // self is Result<T>, but the return type must be Result<U>
            return self
        }
    }
JSoto··on Functor and Monad in Swift
Thanks!

I think that would make for another interesting article. I purposely ignored those details to make sure I kept my article more practical and less academic :) I also didn't mention the unit function of Monads (is that equivalent to one of the laws that you refer to?)