Skip to content

Commit

Permalink
Merge pull request #25 from CodaFi/big-merge
Browse files Browse the repository at this point in the history
Update to Swift 2.0
  • Loading branch information
CodaFi committed Sep 12, 2015
2 parents 6a97548 + 5ccfac2 commit bb7a67b
Show file tree
Hide file tree
Showing 26 changed files with 218 additions and 345 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ DerivedData
*.hmap
*.ipa
*.xcuserstate
*.xcscmblueprint
Cartfile.resolved
Carthage/

# CocoaPods
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "Carthage/Checkouts/Swiftz"]
path = Carthage/Checkouts/Swiftz
url = https://github.com/typelift/Swiftz.git
[submodule "Carthage/Checkouts/SwiftCheck"]
path = Carthage/Checkouts/SwiftCheck
url = https://github.com/typelift/SwiftCheck.git
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
language: objective-c
osx_image: beta-xcode6.3
osx_image: xcode7

before_install:
- gem install xcpretty-travis-formatter --no-rdoc --no-ri --no-document --quiet

install:
- git submodule update -i --recursive

script:
- xctool -project Concurrent.xcodeproj -scheme Concurrent -sdk macosx ONLY_ACTIVE_ARCH=NO clean build test
- xctool -project Concurrent.xcodeproj -scheme Concurrent-iOS -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO clean build test
- set -o pipefail && xcodebuild -project Concurrent.xcodeproj -scheme Concurrent -sdk macosx ONLY_ACTIVE_ARCH=NO clean build test | xcpretty -c -f `xcpretty-travis-formatter`
- set -o pipefail && xcodebuild -project Concurrent.xcodeproj -scheme Concurrent-iOS -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO clean build test | xcpretty -c -f `xcpretty-travis-formatter`
3 changes: 1 addition & 2 deletions Cartfile.private
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
github "typelift/Swiftz"
github "typelift/SwiftCheck"
github "typelift/SwiftCheck"
2 changes: 0 additions & 2 deletions Cartfile.resolved

This file was deleted.

2 changes: 1 addition & 1 deletion Carthage/Checkouts/SwiftCheck
Submodule SwiftCheck updated 46 files
+5 −1 .gitignore
+3 −0 .gitmodules
+6 −4 .travis.yml
+1 −0 Cartfile.private
+1 −0 Cartfile.resolved
+1 −0 Carthage/Checkouts/Operadics
+21 −0 LICENSE
+62 −20 README.md
+39 −0 SwiftCheck.podspec
+82 −36 SwiftCheck.xcodeproj/project.pbxproj
+4 −1 SwiftCheck.xcodeproj/xcshareddata/xcschemes/SwiftCheck-iOS.xcscheme
+4 −1 SwiftCheck.xcodeproj/xcshareddata/xcschemes/SwiftCheck.xcscheme
+490 −104 SwiftCheck/Arbitrary.swift
+95 −30 SwiftCheck/Check.swift
+0 −73 SwiftCheck/Combinators.swift
+248 −70 SwiftCheck/Gen.swift
+1 −1 SwiftCheck/Info.plist
+21 −0 SwiftCheck/Lattice.swift
+225 −185 SwiftCheck/Modifiers.swift
+338 −263 SwiftCheck/Property.swift
+82 −58 SwiftCheck/Random.swift
+31 −20 SwiftCheck/Rose.swift
+60 −27 SwiftCheck/State.swift
+0 −10 SwiftCheck/SwiftCheck.h
+0 −122 SwiftCheck/SwiftCheck.playground/Contents.swift
+0 −3 SwiftCheck/SwiftCheck.playground/Sources/SupportCode.swift
+0 −4 SwiftCheck/SwiftCheck.playground/contents.xcplayground
+392 −298 SwiftCheck/Test.swift
+47 −9 SwiftCheck/TestOperators.swift
+32 −17 SwiftCheck/Testable.swift
+61 −0 SwiftCheck/Witness.swift
+10 −10 SwiftCheckTests/BooleanIdentitySpec.swift
+79 −0 SwiftCheckTests/ComplexSpec.swift
+12 −2 SwiftCheckTests/DiscardSpec.swift
+42 −34 SwiftCheckTests/GenSpec.swift
+1 −1 SwiftCheckTests/Info.plist
+19 −12 SwiftCheckTests/ModifierSpec.swift
+33 −0 SwiftCheckTests/PropertySpec.swift
+9 −9 SwiftCheckTests/ShrinkSpec.swift
+12 −21 SwiftCheckTests/SimpleSpec.swift
+54 −0 SwiftCheckTests/TestSpec.swift
+715 −0 Tutorial.playground/Contents.swift
+4 −0 Tutorial.playground/contents.xcplayground
+1 −1 Tutorial.playground/playground.xcworkspace/contents.xcworkspacedata
+0 −0 Tutorial.playground/timeline.xctimeline
+10 −0 Tutorial.xcworkspace/contents.xcworkspacedata
1 change: 0 additions & 1 deletion Carthage/Checkouts/Swiftz
Submodule Swiftz deleted from 2998fe
105 changes: 17 additions & 88 deletions Concurrent.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -62,6 +62,8 @@
ReferencedContainer = "container:Concurrent.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
Expand All @@ -71,6 +73,7 @@
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0610"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -62,6 +62,8 @@
ReferencedContainer = "container:Concurrent.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
Expand All @@ -71,6 +73,7 @@
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
22 changes: 15 additions & 7 deletions Concurrent/Chan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public struct Chan<A> {

/// Reads a value from the channel.
public func read() -> A {
return self.readEnd.modify { readEnd in
let item : ChItem<A> = readEnd.read()
return (item.stream(), item.val())
do {
return try self.readEnd.modify { readEnd in
let item : ChItem<A> = readEnd.read()
return (item.stream(), item.val())
}
} catch _ {
fatalError("Fatal: Could not modify read head.")
}
}

Expand All @@ -46,7 +50,7 @@ public struct Chan<A> {

/// Writes a list of values to a channel.
public func writeList(xs : [A]) {
xs.map({ self.write($0) })
xs.forEach(self.write)
}

/// Returns whether the channel is empty.
Expand All @@ -55,9 +59,13 @@ public struct Chan<A> {
/// concurrent computations, this may change out from under you without warning, or even by the
/// time it can be acted on. It is better to use one of the direct actions above.
public var isEmpty : Bool {
return self.readEnd.withMVar { r in
let w = r.tryRead()
return w == nil
do {
return try self.readEnd.withMVar { r in
let w = r.tryRead()
return w == nil
}
} catch _ {
fatalError("Fatal: Could not determine emptiness; read of underlying MVar failed.")
}
}

Expand Down
11 changes: 4 additions & 7 deletions Concurrent/Concurrent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// Copyright (c) 2014 TypeLift. All rights reserved.
//

import Swiftz

public typealias ThreadID = pthread_t

public func myTheadID() -> ThreadID {
Expand All @@ -21,11 +19,10 @@ public func forkIO(@autoclosure(escaping) io : () -> ()) -> ThreadID {
})
}

/// Forks a thread and calls the given function when the thread is about to terminate with either
/// a value or an exception.
public func forkFinally<A>(@autoclosure(escaping) io : () -> A, finally : Either<Exception, A> -> ()) -> ThreadID {
return mask({ (let restore : A -> A) -> ThreadID in
return forkIO(finally(try(restore(io()))))
/// Forks a computation onto a new thread and returns its thread ID.
public func forkIO(io : () -> ()) -> ThreadID {
return CONCRealWorld.forkWithStart({
return io()
})
}

Expand Down
82 changes: 0 additions & 82 deletions Concurrent/Exception.swift

This file was deleted.

103 changes: 52 additions & 51 deletions Concurrent/Future.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,87 +6,88 @@
// Copyright (c) 2014 TypeLift. All rights reserved.
//

import Swiftz

public struct Future<A> {
let threadID : MVar<Optional<ThreadID>>
let cResult : IVar<Result<A>>
let finalizers : MVar<[Result<A> -> ()]>
let completionLock : IVar<Optional<A>>
let finalizers : MVar<[Optional<A> -> ()]>

private init(_ threadID : MVar<Optional<ThreadID>>, _ cResult : IVar<Result<A>>, _ finalizers : MVar<[Result<A> -> ()]>) {
private init(_ threadID : MVar<Optional<ThreadID>>, _ cOptional : IVar<Optional<A>>, _ finalizers : MVar<[Optional<A> -> ()]>) {
self.threadID = threadID
self.cResult = cResult
self.completionLock = cOptional
self.finalizers = finalizers
}

public func read() -> Result<A> {
return self.cResult.read()
public func read() -> Optional<A> {
return self.completionLock.read()
}

public func then(finalize : Result<A> -> ()) -> Future<A> {
let ma : Optional<Result<A>> = self.finalizers.modify { sTodo in
let res = self.cResult.tryRead()
if res == nil {
return (sTodo + [finalize], res)
public func then(finalize : Optional<A> -> ()) -> Future<A> {
do {
let ma : Optional<Optional<A>> = try self.finalizers.modify { sTodo in
let res = self.completionLock.tryRead()
if res == nil {
return (sTodo + [finalize], res)
}
return (sTodo, res)
}
return (sTodo, res)
}

switch ma {
case .None:
return self
case .Some(let val):
self.runFinalizerWithResult(val)(finalize)
return self

switch ma {
case .None:
return self
case .Some(let val):
self.runFinalizerWithOptional(val)(finalize)
return self
}
} catch _ {
fatalError("Fatal: Could not read underlying MVar to spark finalizers.")
}
}

private func complete(r : Result<A>) -> Result<A> {
return self.cResult.tryPut(r) ? r : self.cResult.read()
private func complete(r : Optional<A>) -> Optional<A> {
return self.completionLock.tryPut(r) ? r : self.completionLock.read()
}

private func runFinalizerWithResult<A>(val : Result<A>) -> (Result<A> -> ()) -> ThreadID {
private func runFinalizerWithOptional<A>(val : Optional<A>) -> (Optional<A> -> ()) -> ThreadID {
return { todo in forkIO(todo(val)) }
}
}

public func forkFutures<A>(ios : [() -> A]) -> Chan<Result<A>> {
let c : Chan<Result<A>> = Chan()
let ps = ios.map({ forkFuture($0) }).map({ f in f.then({ c.write($0) }) })
public func forkFutures<A>(ios : [() -> A]) -> Chan<Optional<A>> {
let c : Chan<Optional<A>> = Chan()
ios.map(forkFuture).forEach { f in f.then(c.write) }
return c
}

public func forkFuture<A>(io : () -> A) -> Future<A> {
let msTid : MVar<Optional<ThreadID>> = MVar()
let result : IVar<Result<A>> = IVar()
let msTodo : MVar<[Result<A> -> ()]> = MVar(initial: [])
public func forkFuture<A>(io : () throws -> A) -> Future<A> {
let msTid = MVar<ThreadID?>()
let lock : IVar<A?> = IVar()
let msTodo : MVar<[A? -> ()]> = MVar(initial: [])

let p = Future(msTid, result, msTodo)
let act : dispatch_block_t = {
let p = Future(msTid, lock, msTodo)
let act : () -> () = {
p.threadID.put(.Some(myTheadID()))

let val : Result<A> = { r in
let res : Result<A> = r.toResult({ e in return NSError(domain: "", code: 0, userInfo: [ NSLocalizedDescriptionKey : e.description ]) })
return p.complete(res)
}(try(io()))

switch val {
case .Error(let err):
return error("")
case .Value(_):
return ()
}
_ = p.complete({
do {
return try io()
} catch _ {
return nil
}
}())
}

let process : dispatch_block_t = {
let paranoid = Result<A>.error(NSError(domain: "", code: 0, userInfo: nil))

p.threadID.modify_(const(.None))
let paranoid : A? = nil
p.threadID.modify_({ _ in .None })
let val = p.complete(paranoid)
let sTodo = p.finalizers.swap([])
let _ = sTodo.map(p.runFinalizerWithResult(val))
sTodo.forEach { _ in
let _ = p.runFinalizerWithOptional(val)
}
}

let thr = forkIO(finally(act())(then: process()))
_ = forkIO {
act()
process()
}
return p
}
6 changes: 5 additions & 1 deletion Concurrent/IChan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public struct IChan<A> {
/// If the same head has been written to more than once, this function will throw an exception.
public func write(x : A) -> IChan<A> {
let ic = IChan()
self.ivar.put((x, ic))
do {
try self.ivar.put((x, ic))
} catch _ {
fatalError("Fatal: Accidentally wrote to the same IChan head twice.")
}
return ic
}

Expand Down
Loading

0 comments on commit bb7a67b

Please sign in to comment.