Permalink
Browse files

Add Runes+Result.swift

  • Loading branch information...
kareman committed Mar 27, 2015
1 parent 6142452 commit c49709d9bb17291fac6b82a0fe136d6d10e1bd9f
Showing with 101 additions and 0 deletions.
  1. +12 −0 FootlessParser.xcodeproj/project.pbxproj
  2. +89 −0 source/General/Runes+Result.swift
@@ -15,6 +15,7 @@
BAC6CE5E1AC5B621002246E5 /* Runes.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BAC6CE5A1AC5B51D002246E5 /* Runes.framework */; };
BAC6CE601AC5B669002246E5 /* LlamaKit.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = BAC6CE591AC5B51D002246E5 /* LlamaKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
BAC6CE611AC5B669002246E5 /* Runes.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = BAC6CE5A1AC5B51D002246E5 /* Runes.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
+ BAC6CE671AC5BBB3002246E5 /* Runes+Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAC6CE661AC5BBB3002246E5 /* Runes+Result.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -49,6 +50,7 @@
BAC6B2311A61D92B005643D9 /* FootlessParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FootlessParserTests.swift; sourceTree = "<group>"; };
BAC6CE591AC5B51D002246E5 /* LlamaKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = LlamaKit.framework; path = Carthage/Build/Mac/LlamaKit.framework; sourceTree = "<group>"; };
BAC6CE5A1AC5B51D002246E5 /* Runes.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Runes.framework; path = Carthage/Build/Mac/Runes.framework; sourceTree = "<group>"; };
+ BAC6CE661AC5BBB3002246E5 /* Runes+Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Runes+Result.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -96,6 +98,7 @@
BAC6B2211A61D92B005643D9 /* source */ = {
isa = PBXGroup;
children = (
+ BAC6CE651AC5BBB3002246E5 /* General */,
BAC6B2221A61D92B005643D9 /* Supporting Files */,
);
path = source;
@@ -151,6 +154,14 @@
name = Mac;
sourceTree = "<group>";
};
+ BAC6CE651AC5BBB3002246E5 /* General */ = {
+ isa = PBXGroup;
+ children = (
+ BAC6CE661AC5BBB3002246E5 /* Runes+Result.swift */,
+ );
+ path = General;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXHeadersBuildPhase section */
@@ -258,6 +269,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ BAC6CE671AC5BBB3002246E5 /* Runes+Result.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,89 @@
+//
+// Created by Gordon Fontenot (https://github.com/gfontenot) on 22.01.15.
+// From https://github.com/thoughtbot/Runes/blob/a2d22e2a761d4284deb30318930bd7ab3382c47f/Source/Result.swift
+//
+
+import LlamaKit
+import Runes
+
+/**
+ map a function over a result
+
+ - If the value is .Failure, the function will not be evaluated and this will return the failure
+ - If the value is .Success, the function will be applied to the unwrapped value
+
+ :param: f A transformation function from type T to type U
+ :param: a A value of type Result<T, E>
+
+ :returns: A value of type Result<U, E>
+*/
+public func <^><T, U, E>(f: T -> U, a: Result<T, E>) -> Result<U, E> {
+ return a.map(f)
+}
+
+/**
+ apply a function from a result to a result
+
+ - If the function is .Failure, the function will not be evaluated and this will return the error from the function result
+ - If the value is .Failure, the function will not be evaluated and this will return the error from the passed result value
+ - If both the value and the function are .Success, the unwrapped function will be applied to the unwrapped value
+
+ :param: f A result containing a transformation function from type T to type U
+ :param: a A value of type Result<T, E>
+
+ :returns: A value of type Result<U, E>
+*/
+public func <*><T, U, E>(f: Result<(T -> U), E>, a: Result<T, E>) -> Result<U, E> {
+ return a.apply(f)
+}
+
+/**
+ flatMap a function over a result
+
+ - If the value is .Failure, the function will not be evaluated and this will return the failure
+ - If the value is .Success, the function will be applied to the unwrapped value
+
+ :param: f A transformation function from type T to type Result<U, E>
+ :param: a A value of type Result<T, E>
+
+ :returns: A value of type Result<U, E>
+*/
+public func >>-<T, U, E>(a: Result<T, E>, f: T -> Result<U, E>) -> Result<U, E> {
+ return a.flatMap(f)
+}
+
+@availability(*, unavailable, message="function (T -> U) does not return Result<U, E>, perhaps you meant f <^> val")
+public func >>-<T, U, E>(a: Result<T, E>, f: T -> U) -> Result<U, E> {
+ return a.map(f)
+}
+
+/**
+ Wrap a value in a minimal context of .Success
+
+ :param: a A value of type T
+
+ :returns: The provided value wrapped in .Success
+*/
+public func pure<T, E>(a: T) -> Result<T, E> {
+ return success(a)
+}
+
+extension Result {
+ /**
+ apply a function from a result to self
+
+ - If the function is .Failure, the function will not be evaluated and this will return the error from the function result
+ - If self is .Failure, the function will not be evaluated and this will return the error from self
+ - If both self and the function are .Success, the unwrapped function will be applied to self
+
+ :param: f A result containing a transformation function from type T to type U
+
+ :returns: A value of type Result<U, E>
+ */
+ func apply<U>(f: Result<(T -> U), E>) -> Result<U, E> {
+ switch f {
+ case let .Success(fx): return map(fx.unbox)
+ case let .Failure(e): return failure(e.unbox)
+ }
+ }
+}

1 comment on commit c49709d

@kareman

This comment has been minimized.

Show comment
Hide comment
@kareman

kareman May 10, 2015

Owner

Discussed briefly here.

Owner

kareman commented on c49709d May 10, 2015

Discussed briefly here.

Please sign in to comment.