Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kemsky/stream
ActionScript collection library with modern functionality
https://github.com/kemsky/stream
actionscript adobe-air adobe-flash adobe-flex collections functional-programming
Last synced: about 2 months ago
JSON representation
ActionScript collection library with modern functionality
- Host: GitHub
- URL: https://github.com/kemsky/stream
- Owner: kemsky
- License: mit
- Created: 2015-10-03T13:17:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-20T15:35:22.000Z (almost 9 years ago)
- Last Synced: 2024-08-04T05:04:45.369Z (5 months ago)
- Topics: actionscript, adobe-air, adobe-flash, adobe-flex, collections, functional-programming
- Language: ActionScript
- Homepage:
- Size: 491 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-actionscript-sorted - stream - ActionScript collection library with modern functionality (Utilities / Other Utilities)
README
# stream [![Build Status](https://api.travis-ci.org/kemsky/stream.svg)](https://travis-ci.org/kemsky/stream)
###ActionScript collection library - StreamBasically stream is an array wrapper that adds many useful methods and properties (`filter`, `iterate`, `map`, `fold`, `flatMap`, `first`, `second` ... , `last`, `empty` etc.).
Inspired by Javascript and Ruby arrays, Scala collections.
Complete documentation, unit tests with 100% code coverage, Travis CI integration (automatic testing, asdoc and coverage report generation).
See [latest release](https://github.com/kemsky/stream/releases/latest).
## Creating Stream
```as3
var s:Stream = $(1, 2, 3);
var s:Stream = $([1, 2, 3]);
var s:Stream = $(new ArrayCollection([1, 2, 3]));
var s:Stream = $(new ArrayList([1, 2, 3]));
var s:Stream = $(new Stream([1, 2, 3]));var s:Stream = Stream.from([1, 2, 3]);
var s:Stream = Stream.of(1, 2, 3);//All expressions are equivalent to:
var s:Stream = new Stream([1, 2, 3])//Create from iterable object
var obj:Object = {name1: "first", name2: "second"};
var s:Stream = Stream.from(obj);
trace(s);
//prints Stream{Stream{"name1", "first"}, Stream{"name2", "second"}}//create stream from object property names or property values
var s:Stream = Stream.fromKeys(obj);
trace(s);
//prints Stream{"name1", "name2"}var s:Stream = Stream.fromValues(obj);
trace(s);
//prints Stream{"first", "second"}//create from XMLList or XML
var s:Stream = $(12);
var s:Stream = Stream.from(12);
trace(s);
//prints Stream{1, 2}
```## Iteration and element access
Stream extends Proxy class and provides the same iteration capabilities as standard Array:
```as3
var s:Stream = $(1, 2, 3);
for each (var item:* in s)
{
trace(item);
}for (var i:int = 0; i < s.length; i++)
{
trace(s[i]);
trace(s.getItem(i));
}
//prints 1, 2, 3//iterator
var values:Iterator = s.iterator();
while (values.hasNext)
{
var n:Number = values.next();
//values.item;
//values.index;
//values.remove();
//values.end();
}
//or use standard for each loop
for each (var item:Number in values)
{
//values.item;
//values.index;
//values.remove();
//values.end();
}//set item at index 0
s.setItem(5, 0);
s[0] = 5;
s.first = 5;
//last item also has index -1, item before last -2 and etc. (Ruby-like)
s[-3] = 5;//remove item from Stream
delete s[0];
s.removeItem(0);
```
*Stream is about 10x slower when accessed by index (`[index]`) and it seems to be Proxy overhead.
If you need better performance (3x slower than Array) use methods to access Stream items: `getItem(index)` and `setItem(value, index)`.*## Convert Stream to any collection
```as3
var s:Stream = Stream.of(1, 2, 3);var collection:ArrayCollection = s.collection();
var array:Array = s.array();
var vector:Vector. = s.vector();
var list:ArrayList = s.list();
```## Array-like methods
Stream has all methods(every, forEach, map, some, slice, splice, push, pop etc.) that standard Array has:
```as3
var s:Stream = $(1, 2, 3);
s.forEach(function(item:Number):void
{
trace(item);
});
//or
s.forEach(function(item:Number, index:uint):void
{
trace(item);
});
//or
s.forEach(function(item:Number, index:uint, stream:Stream):void
{
trace(item);
});
//prints 1, 2, 3
```## foldLeft, foldRight methods
Javascript Array has `reduce` method which is quite similar.
```as3
var sum2:Number = $(0, 1, 2, 3, 4).foldRight(function (prev:Number, current:Number):Number
{
return prev + current;
}, 10);
trace(sum2);
//prints 20
```## flatMap, flatten
```as3
var s:Stream = new Stream([1, 2, 3], $(4, 5, 6), new ArrayCollection([7, 8, 9]));
var mapped:Stream = s.flatMap(function(item:*):*
{
//this is nop callback, used just for example
//you can use short form: s.flatten();
return item;
});
trace(mapped);
//prints 1, 2, 3, 4, 5, 6, 7, 8, 9
```## Various filtering methods
```as3
//filter out null, NaN or undefined items
var s:Stream = Stream.of(null, NaN, undefined).compact();
trace(s, s.length);
//prints Stream{}, 0public class Item
{
public var name:String;
public var price:Number;
}var item1:Item = new Item("1", 1);
var item2:Item = new Item("2", 2);
var s:Stream = $(item1, item2);// 1. using custom callback
var result:Stream = s.filter(function(item:Item):Boolean{
return item.price > 1;
});// 2. using provided global functions(can compare Boolean, Number, Date, XML, String types)
// supports nested properties i.e. member("prop.prop.prop")
var result:Stream = s.filter(gt(member("price"), 1));// 3. using Proxy magick and global functions
var result:Stream = s.price(gt(_, 1));//all three provide identical results
```## E4X descendant accessor operator `..`
Short alternative to `map` method:
```as3
var item1:Item = new Item("car", 1);
var item2:Item = new Item("truck", 2);
var s:Stream = $(item1, item2);var prices:Stream = s..price;
trace(prices);
//prints 1,2
```## Many other handy methods and properties
Stream can be converted to Object, Dictionary or custom class (using `group` method).
```as3
var item1:Item = new Item("1", 1);
var item2:Item = new Item("2", 2);
var s:Stream = $(item1, item2);var d:Dictionary = s.dictionary("name");
trace(d["1"], item1);
//prints first itemtrace(s.second);
//prints second itemtrace(s.unique);
//prints truetrace(s.contains(item1));
//prints truetrace(s.count(function(item:Item):Boolean
{
return item.price > 1;
}));
//or
trace(s.count(gt(member("price"), 1)));
//prints 1
```See also: `group`,`partition`,`fill`,`find`,`findIndex`, `drop`, `zip`, `zipWithIndex`, `skip` etc.
## Build
You can build library using **Intelij Idea** or **Apache Maven**.
- Compile `mvn compile -Dtag.name=<...>`
- Run tests `mvn test -DflashPlayer.command=""`
- Generate coverage report `mvn flexmojos:coverage-report -DflashPlayer.command=""`
- Generate documentation: `mvn flexmojos:asdoc`