Beta

FloatStream

Description:

FloatStream

Returns an reader function. This function should accept a single data parameter, and process it. Certain callbacks attached to the reader function will be called at particular times in same order the values are processed in. The callbacks are detailed below:

.received(string)

If provided, this callback should be called each time data is given to the reader. The data should be passed to the callback with no changes made.

var s = FloatStream();
s.received = function(data) { console.log("received: " + data); };

s("1 2 3");
// received: "1 2 3"
s("abc");
// received: "abc"

.complete(float)

If provided, this callback should be called whenever a float is deemed "complete." A float is deemed complete whenever is is ended by whitespace (a space, a new-line character, or a tab character). The resulting "completed" float should be passed to the complete callback.

If more than one complete float is found at any one time, each should be parsed and sent to the complete callback separately.

var s = FloatStream();
s.complete = function(float) { console.log("complete: " + float); };

s("1"); // the float has not be completed by a subsequent whitespace character
s(" "); // the previously sent float has been completed
// complete: 1
s("2 3\n"); // 2 completed floats
// complete: 2
// complete: 3

.possible(float)

If provided, this callback should be called when enough data has been passed into the reader to produce a float. This float should be passed to the possible callback. If more data changes what this value would be, the new value should be passed in a separate call. If the data does not change the value of the currently-streamed number, no additional call should be made.

Remember, numbers can be positive or negative, or even have exponents (1e3 == 1000). Any part of these should be parsed and sent to the possible callback whenever appropriate.

If more than one float is found at any given time, each should be parsed and sent to the possible callback separately. If a complete float is found without any streaming necessary, this should also be sent to the possible callback.

var s = FloatStream();
s.possible = function(float) { console.log("possible: " + float); };

s("1"); // stream started
// possible: 1
s("2 "); // stream continued and completed
// possible: 12
s("3."); // new float started
// possible: 3
s("141"); // stream continued
// possible: 3.141
s("000"); // stream continued 3.141000; no change
s(" "); // stream completed; no change

s("-0.3141e"); // stream started
// possible: -0.3141
s("1"); // stream continued, -0.3141e1
// possible: -3.141
s(" 2 4 "); // stream ended with no value change, 2 new completed floats
// possible: 2
// possible: 4

.error(data)

If provided, this callback should be called when data is invalid. The invalid data should be passed to the callback. Processing should continue, and any other callbacks calls should be made. Later errors should also be sent to the error callback.

var s = FloatStream();
s.error = function(error) { console.log("error: " + error); };

s("-"); // valid stream started
s("123"); // stream continued
s("abc\t1"); // invalid data, stream started
// error: -123abc
a(" d\ne f\t\t"); // invalid data
// error: d\ne f
Regular Expressions
Streams
Parsing
Fundamentals

Stats:

CreatedSep 27, 2015
PublishedSep 28, 2015
Warriors Trained40
Total Skips0
Total Code Submissions235
Total Times Completed8
JavaScript Completions8
Total Stars1
% of votes with a positive feedback rating50% of 2
Total "Very Satisfied" Votes1
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes1
Total Rank Assessments2
Average Assessed Rank
4 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • wthit56 Avatar
  • Voile Avatar
Ad