Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js 37 try monad #48

Merged
merged 13 commits into from Oct 13, 2015
Merged

Js 37 try monad #48

merged 13 commits into from Oct 13, 2015

Conversation

jdesiloniz
Copy link

This PR contains our first basic implementation of the Try monad, with the following features:

  • It's based on the category theory Swiftz types, conforming to Pointed, Functor, Applicative and Monad.
  • Encapsulates Swift's throwable functions, by using the new do/try blocks in Swift 2.0. Encapsulated operations are evaluated. An operation result is stored in an Success type containing its value, or in a Failure type containing the catched exception.
  • It allows pattern matching to decompose it, and has utility methods like isSuccess, isFailure, getOrElse... to work with its values.
  • It implements the more important HOF with the same syntax (mostly) as in Scala: map, flatMap, filter, recover, recoverWith.
  • CONS: Because of the strict nature of the Swift data-type system, so far recover and recoverWith only allow to return instances of Try of the same generic type as the original instance on which the function is applied to.

This PR also includes a couple of modifications to PartialFunctions to allow some functionality to Try's recover and recoverWith functions.

Could you please review, @anamariamv? Also, it'd be nice to have your thoughts on this, @raulraja

Thanks!

@anamariamv
Copy link
Contributor

@raulraja could you review please? I reviewed it and I think that it's Ok.

@raulraja
Copy link

raulraja commented Oct 9, 2015

Looks great! Another handy OP here would be the catamorphism via fold. Not sure if Swiftz already implements a Foldable instance but the convenience method would be handy. Something in pseudo code like...

myTry<T> fold (fail = (ex) -> T , succ = (result) -> T)

@raulraja
Copy link

raulraja commented Oct 9, 2015

In any case this LGTM and we should consider adding our own foldable ops like the ones in Scalaz if SwiftZ doesn't already do so.

@jdesiloniz
Copy link
Author

@raulraja You're right. Swiftz includes Foldable. I'll take a look on how to conform Try to the fold methods, or to implement one of our own if Swiftz's ones don't make sense. I'm going to create a separate issue for this :). Thanks!

jdesiloniz pushed a commit that referenced this pull request Oct 13, 2015
@jdesiloniz jdesiloniz merged commit fbb0903 into master Oct 13, 2015
@jdesiloniz jdesiloniz deleted the js-37-TryMonad branch October 13, 2015 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants