-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'khanlou/master' into cocoapods
# Conflicts: # Promise.xcodeproj/project.pbxproj # README.md
- Loading branch information
Showing
13 changed files
with
480 additions
and
81 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Promise.playground/Pages/Zip Functions Generator.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//: [Previous](@previous) | ||
|
||
/*: | ||
|
||
# Zip Functions Generator | ||
|
||
This page generates `zip` functions for 3-*N* parameters. They all build on the | ||
two parameter variant, with `zip` for *N* parameters always delegating work | ||
to one with *N*-1 parameters. | ||
|
||
*/ | ||
|
||
import Foundation | ||
|
||
func types(_ n: Int) -> String { | ||
return (1...n).map { "T\($0)" }.joined(separator: ", ") | ||
} | ||
|
||
|
||
func createZip(_ n: Int) -> String { | ||
var output: String = "/// Zips \(n) promises of different types into a single Promise whose\n" | ||
output.append("/// type is a tuple of \(n) elements.\n") | ||
output.append("public static func zip<") | ||
output.append(types(n)) | ||
output.append(">(") | ||
output.append((1...n-1).map { "_ p\($0): Promise<T\($0)>, " }.joined()) | ||
output.append("_ last: Promise<T\(n)>) -> Promise<(\(types(n)))> {\n") | ||
|
||
output.append("return Promise<(\(types(n)))>(work: { (fulfill: @escaping ((\(types(n)))) -> Void, reject: @escaping (Error) -> Void) in\n") | ||
|
||
output.append("let zipped: Promise<(\(types(n-1)))> = zip(") | ||
output.append((1...n-1).map { "p\($0)" }.joined(separator: ", ")) | ||
output.append(")\n\n") | ||
|
||
output.append("func resolver() -> Void {\n") | ||
output.append("if let zippedValue = zipped.value, let lastValue = last.value {\n") | ||
output.append("fulfill((") | ||
output.append((0...n-2).map { "zippedValue.\($0), " }.joined()) | ||
output.append("lastValue))\n}\n}\n") | ||
|
||
output.append("zipped.then({ _ in resolver() }, reject)\n") | ||
output.append("last.then({ _ in resolver() }, reject)\n") | ||
|
||
output.append("})\n}") | ||
|
||
return output | ||
} | ||
|
||
func createZips(_ n: Int) -> String { | ||
return (3...n).map(createZip).joined(separator: "\n\n") | ||
} | ||
|
||
print(createZips(6)) | ||
|
||
|
||
//: [Next](@next) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Promise.xcodeproj/xcshareddata/xcschemes/Promise macOS.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "0830" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "52F3C1BC1ECE503D0028CA53" | ||
BuildableName = "Promise Mac" | ||
BlueprintName = "Promise Mac" | ||
ReferencedContainer = "container:Promise.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
language = "" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
</Testables> | ||
<AdditionalOptions> | ||
</AdditionalOptions> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
language = "" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "52F3C1BC1ECE503D0028CA53" | ||
BuildableName = "Promise Mac" | ||
BlueprintName = "Promise Mac" | ||
ReferencedContainer = "container:Promise.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
<AdditionalOptions> | ||
</AdditionalOptions> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "52F3C1BC1ECE503D0028CA53" | ||
BuildableName = "Promise Mac" | ||
BlueprintName = "Promise Mac" | ||
ReferencedContainer = "container:Promise.xcodeproj"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.