Rest.hx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package haxe;
  2. import lua.Lua.select;
  3. import lua.Table;
  4. import lua.PairTools.copy;
  5. import lua.TableTools.maxn;
  6. import lua.TableTools.pack;
  7. import lua.TableTools.unpack;
  8. import haxe.iterators.RestIterator;
  9. import haxe.iterators.RestKeyValueIterator;
  10. private typedef NativeRest<T> = Table<Int,T>;
  11. @:coreApi
  12. abstract Rest<T>(NativeRest<T>) {
  13. public var length(get, never):Int;
  14. inline function get_length():Int
  15. return maxn(this);
  16. @:from static public function of<T>(array:Array<T>):Rest<T> {
  17. return new Rest(Table.fromArray(array));
  18. }
  19. inline function new(table:Table<Int,T>):Void
  20. this = table;
  21. @:arrayAccess inline function get(index:Int):T
  22. return this[index + 1];
  23. @:to public function toArray():Array<T> {
  24. return Table.toArray(this);
  25. }
  26. public inline function iterator():RestIterator<T>
  27. return new RestIterator<T>(this);
  28. public inline function keyValueIterator():RestKeyValueIterator<T>
  29. return new RestKeyValueIterator<T>(this);
  30. public inline function append(item:T):Rest<T> {
  31. var result = copy(this);
  32. Table.insert(result, item);
  33. return new Rest(result);
  34. }
  35. public inline function prepend(item:T):Rest<T> {
  36. var result = copy(this);
  37. Table.insert(result, 1, item);
  38. return new Rest(result);
  39. }
  40. public function toString():String {
  41. return toArray().toString();
  42. }
  43. }