(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
| #!/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -i -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk | |
| import Foundation | |
| class JSON { | |
| struct Path: Printable { | |
| enum Element { | |
| case Index(Int) | |
| case Key(String) |
A result type (based on swiftz) that can represent either an error or success:
enum Result<X, T> {
case Err(() -> X)
case Ok(() -> T)
}
Now we need a way to chain multiple results together without lots of nesting of if statements -- or exceptions. To do so, we can define a new bind (result, next) operator (implementation borrowed from swiftz) that operates on Result types (a.k.a flatMap or >>=):
Err, the result is immediately returned.| import Darwin | |
| let isLittleEndian = Int(OSHostByteOrder()) == OSLittleEndian | |
| let htons = isLittleEndian ? _OSSwapInt16 : { $0 } | |
| let htonl = isLittleEndian ? _OSSwapInt32 : { $0 } | |
| let htonll = isLittleEndian ? _OSSwapInt64 : { $0 } | |
| let ntohs = isLittleEndian ? _OSSwapInt16 : { $0 } | |
| let ntohl = isLittleEndian ? _OSSwapInt32 : { $0 } | |
| let ntohll = isLittleEndian ? _OSSwapInt64 : { $0 } |
| #import <Foundation/Foundation.h> | |
| #import "PASConstants.h" | |
| #import "PASViewController.h" | |
| @interface PASHideDetailAnimator : NSObject <UIViewControllerAnimatedTransitioning> | |
| @end | |
| @implementation PASHideDetailAnimator |
| enum Either<A, B> { | |
| case Left(A) | |
| case Right(B) | |
| } | |
| func isLeft<A,B>(it : Either<A,B>) -> Bool { | |
| switch it { case .Left: return true; case .Right: return false } | |
| } | |
| func isRight<A,B>(it : Either<A,B>) -> Bool { |
| operator infix ^= { | |
| } | |
| operator infix *= { | |
| } | |
| operator infix * { | |
| } | |
| operator infix % { |
| //! Monad chain macro in Rust | |
| //! | |
| //! A typical usage is to avoid nested match expressions. | |
| //! | |
| //! Supports: | |
| //! - Pattern matching | |
| //! - Or expressions (A | B => ...) | |
| //! - Guarded statements (x if <cond> => ...) | |
| #[feature(macro_rules)]; |
| //! Chain macro in Rust | |
| //! | |
| //! A typical usage is to avoid nested match expressions. | |
| //! | |
| //! Supports: | |
| //! - Pattern matching | |
| //! - Or expressions (A | B => ...) | |
| //! - Guarded statements (x if <cond> => ...) | |
| //! - Implicit else (requires all arms to return same type) | |