Rest.hx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package haxe;
  2. import php.*;
  3. import haxe.iterators.RestIterator;
  4. import haxe.iterators.RestKeyValueIterator;
  5. private typedef NativeRest<T> = NativeIndexedArray<T>;
  6. @:coreApi
  7. @:semantics(value)
  8. abstract Rest<T>(NativeRest<T>) {
  9. public var length(get,never):Int;
  10. inline function get_length():Int
  11. return Global.count(this);
  12. @:from
  13. static public inline function of<T>(array:Array<T>):Rest<T>
  14. return new Rest(@:privateAccess array.arr);
  15. @:noDoc
  16. @:from
  17. static inline function ofNative<T>(array:NativeIndexedArray<T>):Rest<T>
  18. return new Rest(array);
  19. inline function new(a:NativeIndexedArray<T>):Void
  20. this = a;
  21. @:arrayAccess inline function get(index:Int):T
  22. return this[index];
  23. @:to public inline function toArray():Array<T>
  24. return @:privateAccess Array.wrap(this);
  25. public inline function iterator():RestIterator<T>
  26. return new RestIterator<T>(this);
  27. public inline function keyValueIterator():RestKeyValueIterator<T>
  28. return new RestKeyValueIterator<T>(this);
  29. public inline function append(item:T):Rest<T> {
  30. var result = this;
  31. result.push(item);
  32. return new Rest(result);
  33. }
  34. public inline function prepend(item:T):Rest<T> {
  35. var result = this;
  36. Global.array_unshift(result, item);
  37. return new Rest(result);
  38. }
  39. public function toString():String {
  40. return inline Boot.stringifyNativeIndexedArray(this);
  41. }
  42. }