Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
a pipeline generator
//Example 11 - 1. A pipeline of generators // A generator to yield the lines of the string s one at a time. // Note that we don't use s.split(), because that would process the entire // string at once, allocating an array, and we want to be lazy instead. debugger; function eachline(s) { let p; while ((p = s.indexOf('\n')) != -1) { yield s.substring(0, p); s = s.substring(p + 1); } if (s.length > 0) yield s;//Example 11 - 1. A pipeline of generators // A generator to yield the lines of the string s one at a time. // Note that we don't use s.split(), because that would process the entire // string at once, allocating an array, and we want to be lazy instead. function eachline(s) { let p; while ((p = s.indexOf('\n')) != -1) { yield s.substring(0, p); s = s.substring(p + 1); } if (s.length > 0) yield s; } // A generator function that yields f(x) for each element x of the iterable i function map(i, f) { for (let x in i) yield f(x); } // A generator function that yields the elements of i for which f(x) is true function select(i, f) { for (let x in i) { if (f(x)) yield x; } } // Start with a string of text to process debugger; let text = " #comment \n \n hello \nworld\n quit \n unreached \n"; // Now build up a pipeline of generators to process it. // First, break the text into lines let lines = eachline(text); // Next, trim whitespace from the start and end of each line let trimmed = map(lines, function (line) { return line.trim(); }); // Finally, ignore blank lines and comments let nonblank = select(trimmed, function (line) { return line.length > 0 && line[0] != "#" }); // Now pull trimmed and filtered lines from the pipeline and process them, // stopping when we see the line "quit". for (let line in nonblank) { if (line === "quit") break; print(line); } } // A generator function that yields f(x) for each element x of the iterable i function map(i, f) { for (let x in i) yield f(x); } // A generator function that yields the elements of i for which f(x) is true function select(i, f) { for (let x in i) { if (f(x)) yield x; } } // Start with a string of text to process debugger; let text = " #comment \n \n hello \nworld\n quit \n unreached \n"; // Now build up a pipeline of generators to process it. // First, break the text into lines let lines = eachline(text); // Next, trim whitespace from the start and end of each line let trimmed = map(lines, function (line) { return line.trim(); }); // Finally, ignore blank lines and comments let nonblank = select(trimmed, function (line) { return line.length > 0 && line[0] != "#" }); // Now pull trimmed and filtered lines from the pipeline and process them, // stopping when we see the line "quit". for (let line in nonblank) { if (line === "quit") break; print(line); }
run
|
edit
|
history
|
help
0
Futbal
p
JS exam part 2 - Circumference
JavaScript - Looping thru strings
prime consecutive numbers
Triangle
Functions
nuBuilder - Calc precision
ECMAScript5 - js
JS exam part 2 - Missing Number