Skip to content

Commit

Permalink
Reorganize Decoded monadic instance
Browse files Browse the repository at this point in the history
- Implement `map`, `apply`, `flatMap` as instance methods. This mimics
  the behavior found elsewhere.
- Move the operator definitions into Decoded.swift for visibility
  • Loading branch information
gfontenot committed Mar 27, 2015
1 parent 5cc79d3 commit 76f2aa4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
6 changes: 0 additions & 6 deletions Argo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
EABDF6921A9CD4EA00B6CC83 /* types.plist in Resources */ = {isa = PBXBuildFile; fileRef = EABDF68E1A9CD4EA00B6CC83 /* types.plist */; };
EABDF6941A9CD4FC00B6CC83 /* PListDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EABDF6931A9CD4FC00B6CC83 /* PListDecodingTests.swift */; };
EABDF6951A9CD4FC00B6CC83 /* PListDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EABDF6931A9CD4FC00B6CC83 /* PListDecodingTests.swift */; };
EAC59A0F1A9F74C500BEB5CC /* DecodedMonad.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC59A0E1A9F74C500BEB5CC /* DecodedMonad.swift */; };
EAC59A101A9F74C500BEB5CC /* DecodedMonad.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC59A0E1A9F74C500BEB5CC /* DecodedMonad.swift */; };
EAD8BF621AA0DD3900A11963 /* Box.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = EAEE2F4B1A9FCC9F00DA2846 /* Box.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
EAD8BF631AA0DD4100A11963 /* Box.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = EAEE2F4D1A9FCCAA00DA2846 /* Box.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
EAD9FAF619D0F7900031E006 /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAD9FAF519D0F7900031E006 /* Operators.swift */; };
Expand Down Expand Up @@ -185,7 +183,6 @@
EABDF68D1A9CD4EA00B6CC83 /* PListFileReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PListFileReader.swift; sourceTree = "<group>"; };
EABDF68E1A9CD4EA00B6CC83 /* types.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = types.plist; sourceTree = "<group>"; };
EABDF6931A9CD4FC00B6CC83 /* PListDecodingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PListDecodingTests.swift; sourceTree = "<group>"; };
EAC59A0E1A9F74C500BEB5CC /* DecodedMonad.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DecodedMonad.swift; sourceTree = "<group>"; };
EAD9FACF19D0EAB50031E006 /* Argo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Argo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
EAD9FAD319D0EAB50031E006 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
EAD9FADA19D0EAB60031E006 /* ArgoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ArgoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -346,7 +343,6 @@
isa = PBXGroup;
children = (
EAD9FAF519D0F7900031E006 /* Operators.swift */,
EAC59A0E1A9F74C500BEB5CC /* DecodedMonad.swift */,
);
path = Operators;
sourceTree = "<group>";
Expand Down Expand Up @@ -686,7 +682,6 @@
files = (
F87EB6AA1ABC5F1300E3B0AB /* Decoded.swift in Sources */,
F87EB6AE1ABC5F1300E3B0AB /* JSONDecodable.swift in Sources */,
EAC59A0F1A9F74C500BEB5CC /* DecodedMonad.swift in Sources */,
F87EB6A41ABC5F1300E3B0AB /* decode.swift in Sources */,
F87EB6AC1ABC5F1300E3B0AB /* JSON.swift in Sources */,
F87EB6A61ABC5F1300E3B0AB /* flatReduce.swift in Sources */,
Expand Down Expand Up @@ -726,7 +721,6 @@
files = (
F87EB6AB1ABC5F1300E3B0AB /* Decoded.swift in Sources */,
F87EB6AF1ABC5F1300E3B0AB /* JSONDecodable.swift in Sources */,
EAC59A101A9F74C500BEB5CC /* DecodedMonad.swift in Sources */,
F87EB6A51ABC5F1300E3B0AB /* decode.swift in Sources */,
F87EB6AD1ABC5F1300E3B0AB /* JSON.swift in Sources */,
F87EB6A71ABC5F1300E3B0AB /* flatReduce.swift in Sources */,
Expand Down
30 changes: 0 additions & 30 deletions Argo/Operators/DecodedMonad.swift

This file was deleted.

45 changes: 45 additions & 0 deletions Argo/Types/Decoded.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Box
import Runes

public enum Decoded<T> {
case Success(Box<T>)
Expand All @@ -20,3 +21,47 @@ public enum Decoded<T> {
}
}
}

public extension Decoded {
func map<U>(f: T -> U) -> Decoded<U> {
switch self {
case let .Success(box): return .Success(Box(f(box.value)))
case let .MissingKey(string): return .MissingKey(string)
case let .TypeMismatch(string): return .TypeMismatch(string)
}
}

func apply<U>(f: Decoded<T -> U>) -> Decoded<U> {
switch f {
case let .Success(box): return box.value <^> self
case let .MissingKey(string): return .MissingKey(string)
case let .TypeMismatch(string): return .TypeMismatch(string)
}
}

func flatMap<U>(f: T -> Decoded<U>) -> Decoded<U> {
switch self {
case let .Success(box): return f(box.value)
case let .MissingKey(string): return .MissingKey(string)
case let .TypeMismatch(string): return .TypeMismatch(string)
}
}
}

public func pure<A>(a: A) -> Decoded<A> {
return .Success(Box(a))
}

// MARK: Monadic Operators

public func >>-<A, B>(a: Decoded<A>, f: A -> Decoded<B>) -> Decoded<B> {
return a.flatMap(f)
}

public func <^><A, B>(f: A -> B, a: Decoded<A>) -> Decoded<B> {
return a.map(f)
}

public func <*><A, B>(f: Decoded<A -> B>, a: Decoded<A>) -> Decoded<B> {
return a.apply(f)
}

0 comments on commit 76f2aa4

Please sign in to comment.