|
| 1 | +module src/lib/stream_input |
| 2 | + |
| 3 | +import stream |
| 4 | +import src/lib/csv |
| 5 | +import queue |
| 6 | +import scanner |
| 7 | +import io/error |
| 8 | +import io/filesystem |
| 9 | +import io/time |
| 10 | + |
| 11 | +def pushToPullStream[T] {pushStr: () => Unit / { emit[T], stop } } {pullStr: () => Unit / { read[T] } } = { |
| 12 | + def q = emptyQueue[T]() |
| 13 | + var running = true |
| 14 | + try pushStr() with emit[T] { |
| 15 | + def emit(v: T) = { |
| 16 | + q.pushBack(v) |
| 17 | + resume(()) |
| 18 | + } |
| 19 | + } with stop { |
| 20 | + running = false |
| 21 | + } |
| 22 | + try pullStr() with read[T] { |
| 23 | + while (running) { |
| 24 | + val v = q.popFront() |
| 25 | + v match { |
| 26 | + case Some(value) => {resume { unbox value }} |
| 27 | + case None() => {} |
| 28 | + } |
| 29 | + } |
| 30 | + resume { do stop() } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Parse string to double (not in standard library???) |
| 35 | +extern def toDouble(s: String) at {}: Double = |
| 36 | + js "parseFloat(${s})" |
| 37 | + |
| 38 | +def csvFeed(path: String, columnName: String, delayMs: Int): Unit / { emit[Double], Exception[IOError], Exception[WrongFormat] } = { |
| 39 | + readFileUTF8(path) { |
| 40 | + returning::scanner[Char, Unit] { |
| 41 | + try { |
| 42 | + getColumnOf(columnName) { |
| 43 | + decodeCsv() |
| 44 | + } |
| 45 | + } with emit[String] { s => |
| 46 | + do emit(s.toDouble) |
| 47 | + time::sleep(delayMs) |
| 48 | + resume(()) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +def csvFeedStr(csvStr: String, columnName: String, delayMs: Int): Unit / { emit[Double], Exception[WrongFormat] } = { |
| 55 | + feed(csvStr) { |
| 56 | + returning::scanner[Char, Unit] { |
| 57 | + try { |
| 58 | + getColumnOf(columnName) { |
| 59 | + decodeCsv() |
| 60 | + } |
| 61 | + } with emit[String] { s => |
| 62 | + do emit(s.toDouble) |
| 63 | + time::sleep(delayMs) |
| 64 | + resume(()) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +def csvFeedPull(path: String, columnName: String, delayMs: Int) {body: () => Unit / { read[Double], Exception[IOError], Exception[WrongFormat] } }: Unit / { Exception[IOError], Exception[WrongFormat] } = { |
| 71 | + pushToPullStream[Double] { csvFeed(path, columnName, delayMs) } { body() } |
| 72 | +} |
| 73 | + |
| 74 | +def csvFeedStrPull(csvStr: String, columnName: String, delayMs: Int) {body: () => Unit / { read[Double], Exception[WrongFormat] } }: Unit / { Exception[WrongFormat] } = { |
| 75 | + pushToPullStream[Double] { csvFeedStr(csvStr, columnName, delayMs) } { body() } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +namespace examples { |
| 80 | + def example1(): Unit = { |
| 81 | + with on[WrongFormat].panic() |
| 82 | + |
| 83 | + val csv = "name,age\nJohn,42\nJoe,10\nJolene,27" |
| 84 | + |
| 85 | + try csvFeedStr(csv, "age", 500) with emit[Double] { v => |
| 86 | + println("Received value: " ++ v.show()) |
| 87 | + resume(()) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + def example2(): Unit = { |
| 92 | + with on[IOError].panic() |
| 93 | + with on[WrongFormat].panic() |
| 94 | + |
| 95 | + try csvFeed("src/test/testdata.csv", "value", 500) with emit[Double] { v => |
| 96 | + println("Received value: " ++ v.show()) |
| 97 | + resume(()) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + def example3(): Unit = { |
| 102 | + with on[IOError].panic() |
| 103 | + with on[WrongFormat].panic() |
| 104 | + with boundary |
| 105 | + csvFeedPull("src/test/testdata.csv", "value", 500) { |
| 106 | + while (true) { |
| 107 | + println("Waiting to read value...") |
| 108 | + val v = do read[Double]() |
| 109 | + println("Received value: " ++ v.show()) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + def main(): Unit = { |
| 115 | + example2() |
| 116 | + } |
| 117 | +} |
0 commit comments