Commit 9e3584c7f2ccc9625546b554598ed80ccf660d0d
headers
Dominic Tarr committed on 3/22/2013, 8:40:05 AMParent: 139b6be7f1e796c60b1d5442a0e52e46373a5928
Files changed
README.md | changed |
README.md | ||
---|---|---|
@@ -8,8 +8,26 @@ | ||
8 | 8 | data is pulled out of the source stream, into the destination. |
9 | 9 | |
10 | 10 | `pull-stream` is an minimal take on pull streams. |
11 | 11 | |
12 | +## Examples | |
13 | + | |
14 | +What if implementing a stream was this simple: | |
15 | + | |
16 | +### Pipeable | |
17 | + | |
18 | +``` js | |
19 | +var pipeable = require('pull-stream').pipeable | |
20 | + | |
21 | +var createStream = pipeable(function (readable) { | |
22 | + return function read (end, cb) { | |
23 | + readable(end, cb) | |
24 | + } | |
25 | +}) | |
26 | +``` | |
27 | + | |
28 | +### Readable & Reader vs. Readable & Writable | |
29 | + | |
12 | 30 | Instead of a readable stream, and a writable stream, there is a `readable` stream, |
13 | 31 | and a `reader` stream. |
14 | 32 | |
15 | 33 | the readable stream is just a function, that may be called many times, |
@@ -52,8 +70,10 @@ | ||
52 | 70 | that do input _and_ output. |
53 | 71 | |
54 | 72 | Simple! |
55 | 73 | |
74 | +### Duplex | |
75 | + | |
56 | 76 | ``` js |
57 | 77 | var map = function (readable, map) { |
58 | 78 | //return a readable function! |
59 | 79 | return function (end, cb) { |
@@ -65,8 +85,10 @@ | ||
65 | 85 | ``` |
66 | 86 | |
67 | 87 | join them together! |
68 | 88 | |
89 | +### function composition style "pipe" | |
90 | + | |
69 | 91 | ``` js |
70 | 92 | logger( |
71 | 93 | map(randomReadable, function (e) { |
72 | 94 | return Math.round(e * 1000) |
@@ -75,8 +97,10 @@ | ||
75 | 97 | |
76 | 98 | That is good -- but it's kinda weird, because we are used to left to right syntax |
77 | 99 | for streams... `ls | grep | wc -l` |
78 | 100 | |
101 | +### pipeability | |
102 | + | |
79 | 103 | So, we want to pass in the `readable` and `reader` function! |
80 | 104 | It needs to be that order, so that it reads left to right. |
81 | 105 | |
82 | 106 | A basic duplex function would look like this: |
@@ -117,8 +141,11 @@ | ||
117 | 141 | ``` |
118 | 142 | |
119 | 143 | The `reader` stream is the same as before! |
120 | 144 | |
145 | + | |
146 | +### Left-to-Right pipe | |
147 | + | |
121 | 148 | Now PIPE THEM TOGETHER! |
122 | 149 | |
123 | 150 | ``` js |
124 | 151 | randomReader2 (multiply) (logger) |
Built with git-ssb-web