Commit 82275cdfd9549c558f2a4d32dc25d931d6672779
better examples
Dominic Tarr committed on 6/22/2016, 11:52:25 PMParent: 6dab0f0aee56ec3a23287a6df1a8be33b0bef4ca
Files changed
README.md | changed |
README.md | ||
---|---|---|
@@ -67,15 +67,14 @@ | ||
67 | 67 | The `read` function *must not* be called until the previous call has called back. |
68 | 68 | Unless, it is a call to abort the stream (`read(truthy, cb)`). |
69 | 69 | |
70 | 70 | ```js |
71 | -//a stream of 100 random numbers. | |
72 | -var random = function () { | |
73 | - var i = 100 | |
71 | +//a stream of random numbers. | |
72 | +function random (n) { | |
74 | 73 | return function (end, cb) { |
75 | 74 | if(end) return cb(end) |
76 | - //only read 100 times | |
77 | - if(i-- < 0) return cb(true) | |
75 | + //only read n times, then stop. | |
76 | + if(0<--n) return cb(true) | |
78 | 77 | cb(null, Math.random()) |
79 | 78 | } |
80 | 79 | } |
81 | 80 | |
@@ -91,9 +90,9 @@ | ||
91 | 90 | are reader streams. |
92 | 91 | |
93 | 92 | ```js |
94 | 93 | //read source and log it. |
95 | -var logger = function () { | |
94 | +function logger () { | |
96 | 95 | return function (read) { |
97 | 96 | read(null, function next(end, data) { |
98 | 97 | if(end === true) return |
99 | 98 | if(end) throw end |
@@ -107,9 +106,9 @@ | ||
107 | 106 | |
108 | 107 | Since these are just functions, you can pass them to each other! |
109 | 108 | |
110 | 109 | ```js |
111 | -var rand = random() | |
110 | +var rand = random(100) | |
112 | 111 | var log = logger() |
113 | 112 | |
114 | 113 | log(rand) //"pipe" the streams. |
115 | 114 | |
@@ -130,9 +129,9 @@ | ||
130 | 129 | That is, it's just a function that takes a `read` function, |
131 | 130 | and returns another `read` function. |
132 | 131 | |
133 | 132 | ```js |
134 | -var map = function (read, map) { | |
133 | +function map (read, map) { | |
135 | 134 | //return a readable function! |
136 | 135 | return function (end, cb) { |
137 | 136 | read(end, function (end, data) { |
138 | 137 | cb(end, data != null ? map(data) : null) |
@@ -320,4 +319,5 @@ | ||
320 | 319 | ## License |
321 | 320 | |
322 | 321 | MIT |
323 | 322 | |
323 | + |
Built with git-ssb-web