Skip to content

Commit 2224840

Browse files
authored
Add every, some, reduce, and fill to README
1 parent d948446 commit 2224840

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ Now use this library's functions on an `Array`, `Iterable`, or `Iterator` and le
6161
| [`count`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#count) | Counts the number of elements that match the condition |
6262
| [`find` and `findIndex`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#find-and-findindex) | Finds the first element that matches the condition |
6363
| [`indexOf`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#indexOf) | Returns the index of the provided element |
64+
| [`every` and `some`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#every-and-some) | Check if some or all elements match the condition |
65+
| [`reduce`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#reduce) | Reduce to single value summed together using function |
6466
| [`asList` and `asVector`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#aslist-and-asvector) | Provides the result as a `haxe.ds.List` or `haxe.ds.Vector` |
6567
| [`concat`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#concat) | Appends another `Array`, `Iterable`, or even separate for-loop |
68+
| [`fill`](https://github.com/RobertBorghese/Haxe-MagicArrayTools#fill) | Fill a subsection or the entire `Array` with a value |
6669
---
6770

6871
# [Features]
@@ -374,6 +377,80 @@ EntitiesIterator.indexOf(World.FindPlayer(), 1, false);
374377

375378
 
376379

380+
### `every` and `some`
381+
382+
`every` returns `true` if every element returns `true` when passed to the provided callback. On the other hand, `some` returns `true` as long as at least one element passes.
383+
```haxe
384+
function every(callback: (T) -> Bool): Bool;
385+
function some(callback: (T) -> Bool): Bool;
386+
```
387+
```haxe
388+
[75, 7, 12, 93].every(_ > 0);
389+
390+
// |
391+
// V
392+
393+
{
394+
var result = true;
395+
for(it in [75, 7, 12, 93]) {
396+
if(it <= 0) {
397+
result = false;
398+
break;
399+
}
400+
}
401+
result;
402+
}
403+
```
404+
405+
```haxe
406+
(1...10).some(_ == 4);
407+
408+
// |
409+
// V
410+
411+
{
412+
var result = false;
413+
for(it in 1...10) {
414+
if(it == 4) {
415+
result = true;
416+
break;
417+
}
418+
}
419+
result;
420+
}
421+
```
422+
423+
&nbsp;
424+
425+
### `reduce`
426+
427+
`reduce` calls a function on every element to accumulate all the values. The returned value of the previous call is passed as the first argument; the second argument is the element being iterated on. The returned value of the final call is what `reduce` returns.
428+
```haxe
429+
function reduce(callback: (T, T) -> T): T;
430+
```
431+
```haxe
432+
["a", "b", "c", "d"].reduce((a, b) -> a + b);
433+
434+
// |
435+
// V
436+
437+
{
438+
var result = null;
439+
var _hasFoundValue = false;
440+
for(it in ["a", "b", "c", "d"]) {
441+
if(!_hasFoundValue) {
442+
_hasFoundValue = true;
443+
result = it;
444+
} else {
445+
result = result + it;
446+
};
447+
};
448+
result;
449+
}
450+
```
451+
452+
&nbsp;
453+
377454
### `asList` and `asVector`
378455

379456
These functions change the resulting data-structure to either be a `haxe.ds.List` or `haxe.ds.Vector`.
@@ -461,3 +538,48 @@ function concat(other: Array<T> | Iterable<T> | Iterator<T>): Array<T>;
461538
```
462539

463540
&nbsp;
541+
542+
### `fill`
543+
544+
`fill` fills the resulting `Array` with the provided value. A subsection can be filled using the second and third arguments.
545+
```haxe
546+
function fill(value: T, startIndex: Int = 0, endIndex: Int = this.length): Array<T>;
547+
```
548+
```haxe
549+
[1, 2, 3].fill(10);
550+
551+
// |
552+
// V
553+
554+
{
555+
var result = [];
556+
for(it in [1, 2, 3]) {
557+
var it2 = 10;
558+
result.push(it2);
559+
};
560+
result;
561+
}
562+
```
563+
```haxe
564+
(0...10).fill(999, 2, 8);
565+
566+
// |
567+
// V
568+
569+
{
570+
var result = [];
571+
var i = 0;
572+
for(it in (0 ... 10)) {
573+
var it2 = if((i >= 2) && (i < 8)) {
574+
999;
575+
} else {
576+
it;
577+
};
578+
result.push(it2);
579+
i++;
580+
};
581+
result;
582+
}
583+
```
584+
585+
&nbsp;

0 commit comments

Comments
 (0)