ReadableStreamType inherits from Streamable.

Also tried out and commented stuff that does not work in beta 6.
This commit is contained in:
Kare Morstol
2014-08-23 21:02:08 +02:00
parent e63494b68b
commit ed34a689ec
4 changed files with 64 additions and 11 deletions
+7 -2
View File
@@ -30,11 +30,16 @@ extension File: ReadableStreamType {
public func lines() -> SequenceOf <String >{
return split(delimiter: "\n")(stream: self)
}
public func writeTo<Target : OutputStreamType>(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)!)
}
+30 -2
View File
@@ -12,8 +12,36 @@ infix operator |> { precedence 50 associativity left }
public func |> <T,U>(lhs: T, rhs: T -> U) -> U {
return rhs(lhs)
}
/*
public func | <T,U,V>(lhs: T, rhs:((T,V) -> U, V)) -> U {
/* crashes the compiler (beta 6)
/**
Sequence |> (sorted, {<})
leads to
sorted( Sequence, {<})
*/
public func |> <T,U,V>(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 |> <U>(lhs: Streamable, inout rhs: OutputStreamType) {
// lhs.writeTo(&rhs)
// rhs.write(lhs)
print(lhs, &rhs)
}
*/
/* crashes the compiler (beta 6)
public func |> <U>(lhs: ReadableStreamType, inout rhs: WriteableStreamType) {
// lhs.writeTo(&rhs)
// rhs.write(lhs)
print(lhs, &rhs)
}
*/
+4 -6
View File
@@ -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 <String >
func writeTo<Target : OutputStreamType>(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
}
}
+23 -1
View File
@@ -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")
}
}