diff --git a/SwiftShell/File.swift b/SwiftShell/File.swift index d223167..1cc51f0 100644 --- a/SwiftShell/File.swift +++ b/SwiftShell/File.swift @@ -30,11 +30,16 @@ extension File: ReadableStreamType { public func lines() -> SequenceOf { return split(delimiter: "\n")(stream: self) } + + public func writeTo(inout target: Target) { + target.write(self.read()) + } + } -extension File: WriteableStreamType { +extension File: WriteableStreamType { - public func write (text: String) { + public func write (text: String) { writeData(text.dataUsingEncoding(streamencoding, allowLossyConversion:false)!) } diff --git a/SwiftShell/Pipes.swift b/SwiftShell/Pipes.swift index 2d67acf..4236e98 100644 --- a/SwiftShell/Pipes.swift +++ b/SwiftShell/Pipes.swift @@ -12,8 +12,36 @@ infix operator |> { precedence 50 associativity left } public func |> (lhs: T, rhs: T -> U) -> U { return rhs(lhs) } -/* -public func | (lhs: T, rhs:((T,V) -> U, V)) -> U { + +/* crashes the compiler (beta 6) +/** + Sequence |> (sorted, {<}) + + leads to + + sorted( Sequence, {<}) +*/ +public func |> (lhs: T, rhs:((T,V) -> U, V)) -> U { return rhs.0(lhs, rhs.1 ) } +*/ + +/* crashes the compiler (beta 6) +/** + Print one stream to another. + readablestream |> writablestream +*/ +public func |> (lhs: Streamable, inout rhs: OutputStreamType) { + // lhs.writeTo(&rhs) + // rhs.write(lhs) + print(lhs, &rhs) +} +*/ + +/* crashes the compiler (beta 6) +public func |> (lhs: ReadableStreamType, inout rhs: WriteableStreamType) { + // lhs.writeTo(&rhs) + // rhs.write(lhs) + print(lhs, &rhs) +} */ \ No newline at end of file diff --git a/SwiftShell/Stream.swift b/SwiftShell/Stream.swift index 1422df2..3c6a034 100644 --- a/SwiftShell/Stream.swift +++ b/SwiftShell/Stream.swift @@ -10,11 +10,12 @@ import Foundation public var streamencoding = NSUTF8StringEncoding -public protocol ReadableStreamType { +public protocol ReadableStreamType : Streamable { func readSome() -> String? func read() -> String func lines() -> SequenceOf + func writeTo(inout target: Target) } public protocol WriteableStreamType : OutputStreamType { @@ -32,7 +33,7 @@ public func stream(text: String) -> ReadableStreamType { return pipe.fileHandleForReading } -public func stream ( closureclosure:() -> () -> String? ) -> NSFileHandle { +public func stream ( closureclosure:() -> () -> String? ) -> ReadableStreamType { let closure = closureclosure() let pipe = NSPipe() @@ -41,12 +42,9 @@ public func stream ( closureclosure:() -> () -> String? ) -> NSFileHandle { if let text = closure() { filehandle.write(text) } else { - // why won't these work? (beta 6) - // filehandle.closeStream() - // input.closeStream() + // why won't this work? (beta 6) // filehandle.writeabilityHandler = nil - // this will surely create a reference cycle, but it crashes when input is declared unowned (beta 6) input.writeabilityHandler = nil } } diff --git a/SwiftShellTests/Stream_Tests.swift b/SwiftShellTests/Stream_Tests.swift index 6141e0b..7078734 100644 --- a/SwiftShellTests/Stream_Tests.swift +++ b/SwiftShellTests/Stream_Tests.swift @@ -25,7 +25,6 @@ class Stream_Tests: XCTestCase { return "this is it" } else { return nil - } } }) @@ -35,4 +34,27 @@ class Stream_Tests: XCTestCase { func testStreamFromArray () { XCTAssertEqual( stream(["item 1","item 2"]).read(), "item 1item 2") } + + func streams () -> (WriteableStreamType, ReadableStreamType) { + let pipe = NSPipe() + return (pipe.fileHandleForWriting, pipe.fileHandleForReading) + } + + func testPrintStreamToStream () { + var (writable, readable) = streams() + let input = stream("this goes in") + + // doesn't work in beta 6 (see Pipes.Swift) + // stream("this goes in") |> writable + + // nor does this. Damn you beta 6 + // print(stream("this goes in") , &writable) + // input.writeTo(&writable) + + writable.write(input.read()) + + XCTAssertEqual(readable.readSome()!, "this goes in") + + } + }