This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sci4me's profile picture

Stream API: stream.lua

Started by sci4me, 20 March 2016 - 06:40 AM
sci4me #1
Posted 20 March 2016 - 07:40 AM
stream.lua is a stream API that I wrote, inspired by Java's Stream API. It basically allows you to create pipelines of operations that can be performed on sets of data. These sets of data can be created in various ways: arrays, functions, or you can even implement your own Stream.

I won't go into detailed explanation of what streams actually are or how they work.
Basically, the important concept to understand is that a stream is not a set of data. It is a set of operations that can be performed on a set of data.

So, finally, to the code.

To load the stream API:

local stream = dofile("stream.lua")

Now I'll just show a few simple examples of what you can do with streams:


stream.generate(function() return math.random() end)
	  .limit(5)
	  .forEach(print)
This will print out 5 random numbers.


stream.iterate(5, function(n) return n + 2 end)
	  .limit(10)
	  .peek(print)
	  .reduce(0, function(a, b) return a + b end)
This will create a stream with 10 elements that starts at 5 and counts up by 2.
It will print them. Then, the final result will be the sum of all the elements.

There is a LOT more functionality than I have shown here. The github repository (https://github.com/SciOS-CC/stream.lua) contains a listing of all functions as well as detailed documentation of each function.

The project is fully open source and licensed under MIT.

If you find a problem of a way to improve the project in any way, PLEASE report it as an issue on the github issue tracker instead of posting it here.

Finally, keep this in mind: I have yet to test this in ComputerCraft. I can't think of any reason it wouldn't work in CC however. Should be fine.

Let me know what ya'll think! Thanks!
Edited on 24 March 2016 - 05:56 PM
Bomb Bloke #2
Posted 20 March 2016 - 09:27 AM
CC forum keeps adding the />. i can't get it to go away. gg.

In the full post editor, disable emoticons.