
แรงบันดาลใจจาก Python - ออกแบบมาสำหรับ PHP
Itertools ทำให้คุณเป็นซุปเปอร์สตาร์ซ้ำโดยการจัดหาเครื่องมือสองประเภท:
ตัวอย่างเครื่องมือวนซ้ำ
foreach (Multi:: zip ([ ' a ' , ' b ' ], [ 1 , 2 ]) as [ $ letter , $ number ]) {
print ( $ letter . $ number ); // a1, b2
}สตรีมตัวอย่างเครื่องมือวนซ้ำ
$ result = Stream:: of ([ 1 , 1 , 2 , 2 , 3 , 4 , 5 ])
-> distinct () // [1, 2, 3, 4, 5]
-> map ( fn ( $ x ) => $ x ** 2 ) // [1, 4, 9, 16, 25]
-> filter ( fn ( $ x ) => $ x < 10 ) // [1, 4, 9]
-> toSum (); // 14 ฟังก์ชั่นทั้งหมดทำงานในคอลเลกชัน iterable :
array (ประเภท)Generator (ประเภท)Iterator (อินเตอร์เฟส)Traversable (อินเตอร์เฟส)| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
chain | ห่วงโซ่หลาย iTerables เข้าด้วยกัน | Multi::chain($list1, $list2) |
zip | วนซ้ำหลายคอลเลกชันพร้อมกันจนกว่าตัววนซ้ำที่สั้นที่สุดจะเสร็จสิ้น | Multi::zip($list1, $list2) |
zipEqual | วนคอลเลกชันหลายคอลเลกชันที่มีความยาวเท่ากันพร้อมกันข้อผิดพลาดหากความยาวไม่เท่ากัน | Multi::zipEqual($list1, $list2) |
zipFilled | วนซ้ำหลายคอลเลกชันโดยใช้ค่าฟิลเลอร์หากความยาวไม่เท่ากัน | Multi::zipFilled($default, $list1, $list2) |
zipLongest | วนซ้ำหลายคอลเลกชันพร้อมกันจนกว่าตัววนซ้ำที่ยาวที่สุดจะเสร็จสิ้น | Multi::zipLongest($list1, $list2) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
chunkwise | ย้ำโดยชิ้น | Single::chunkwise($data, $chunkSize) |
chunkwiseOverlap | วนซ้ำโดยชิ้นส่วนที่ทับซ้อนกัน | Single::chunkwiseOverlap($data, $chunkSize, $overlapSize) |
compress | กรององค์ประกอบที่ไม่ได้เลือก | Single::compress($data, $selectors) |
compressAssociative | กรององค์ประกอบโดยคีย์ที่ไม่ได้เลือก | Single::compressAssociative($data, $selectorKeys) |
dropWhile | วางองค์ประกอบในขณะที่เพรดิเคตเป็นจริง | Single::dropWhile($data, $predicate) |
filter | ตัวกรองสำหรับองค์ประกอบที่ภาคแสดงเป็นจริง | Single::filterTrue($data, $predicate) |
filterTrue | ตัวกรองสำหรับองค์ประกอบความจริง | Single::filterTrue($data) |
filterFalse | ตัวกรองสำหรับองค์ประกอบเท็จ | Single::filterFalse($data) |
filterKeys | ตัวกรองสำหรับคีย์ที่ภาคแสดงเป็นจริง | Single::filterKeys($data, $predicate) |
flatMap | ฟังก์ชั่นแผนที่ลงในรายการและผลลัพธ์ที่แบน | Single::flaMap($data, $mapper) |
flatten | การทำซ้ำหลายมิติแบบแบน | Single::flatten($data, [$dimensions]) |
groupBy | ข้อมูลกลุ่มโดยองค์ประกอบทั่วไป | Single::groupBy($data, $groupKeyFunction, [$itemKeyFunc]) |
limit | ย้ำถึงขีด จำกัด | Single::limit($data, $limit) |
map | ฟังก์ชั่นแผนที่ลงในแต่ละรายการ | Single::map($data, $function) |
pairwise | ย้ำคู่ที่ทับซ้อนกันอย่างต่อเนื่อง | Single::pairwise($data) |
reindex | คีย์ Reindex ของคีย์-ค่า iterable | Single::reindex($data, $reindexer) |
repeat | ทำซ้ำรายการหลายครั้ง | Single::repeat($item, $repetitions) |
reverse | ทำซ้ำองค์ประกอบตามลำดับย้อนกลับ | Single::reverse($data) |
skip | ย้ำหลังจากข้ามองค์ประกอบ | Single::skip($data, $count, [$offset]) |
slice | สกัดชิ้นส่วนของการวนซ้ำ | Single::slice($data, [$start], [$count], [$step]) |
string | ย้ำอักขระของสตริง | Single::string($string) |
takeWhile | องค์ประกอบวนซ้ำในขณะที่ภาคแสดงเป็นความจริง | Single::takeWhile($data, $predicate) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
count | นับตามลำดับตลอดไป | Infinite::count($start, $step) |
cycle | ปั่นจักรยานผ่านคอลเลกชัน | Infinite::cycle($collection) |
repeat | ทำซ้ำรายการตลอดไป | Infinite::repeat($item) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
choice | การเลือกแบบสุ่มจากรายการ | Random::choice($list, $repetitions) |
coinFlip | การพลิกเหรียญแบบสุ่ม (0 หรือ 1) | Random::coinFlip($repetitions) |
number | ตัวเลขสุ่ม | Random::number($min, $max, $repetitions) |
percentage | เปอร์เซ็นต์สุ่มระหว่าง 0 ถึง 1 | Random::percentage($repetitions) |
rockPaperScissors | มือรองกระดาษแบบสุ่ม | Random::rockPaperScissors($repetitions) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
frequencies | การกระจายความถี่ของข้อมูล | Math::frequencies($data, [$strict]) |
relativeFrequencies | การกระจายความถี่สัมพัทธ์ของข้อมูล | Math::relativeFrequencies($data, [$strict]) |
runningAverage | เรียกใช้การสะสมเฉลี่ย | Math::runningAverage($numbers, $initialValue) |
runningDifference | เรียกใช้การสะสมที่แตกต่างกัน | Math::runningDifference($numbers, $initialValue) |
runningMax | เรียกใช้การสะสมสูงสุด | Math::runningMax($numbers, $initialValue) |
runningMin | เรียกใช้การสะสมขั้นต่ำ | Math::runningMin($numbers, $initialValue) |
runningProduct | การสะสมผลิตภัณฑ์ | Math::runningProduct($numbers, $initialValue) |
runningTotal | เรียกใช้การสะสมทั้งหมด | Math::runningTotal($numbers, $initialValue) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
distinct | ย้ำเฉพาะรายการที่แตกต่างกัน | Set::distinct($data) |
distinctBy | ย้ำเฉพาะรายการที่แตกต่างกันโดยใช้ตัวเปรียบเทียบที่กำหนดเอง | Set::distinct($data, $compareBy) |
intersection | จุดตัดของ iterables | Set::intersection(...$iterables) |
intersectionCoercive | สี่แยกที่มีการบีบบังคับประเภท | Set::intersectionCoercive(...$iterables) |
partialIntersection | จุดตัดบางส่วนของ iterables | Set::partialIntersection($minCount, ...$iterables) |
partialIntersectionCoercive | สี่แยกบางส่วนที่มีการบีบบังคับประเภท | Set::partialIntersectionCoercive($minCount, ...$iterables) |
symmetricDifference | ความแตกต่างของ iterables แบบสมมาตร | Set::symmetricDifference(...$iterables) |
symmetricDifferenceCoercive | ความแตกต่างแบบสมมาตรกับการบีบบังคับประเภท | Set::symmetricDifferenceCoercive(...$iterables) |
union | สหภาพของ iterables | Set::union(...$iterables) |
unionCoercive | สหภาพกับการบีบบังคับประเภท | Set::unionCoercive(...$iterables) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
asort | ย้ำคอลเลกชันที่จัดเรียงไว้เพื่อรักษากุญแจ | Sort::asort($data, [$comparator]) |
sort | ย้ำคอลเลกชันที่จัดเรียง | Sort::sort($data, [$comparator]) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
readCsv | การแยกไฟล์ CSV ทีละบรรทัด | File::readCsv($fileHandle) |
readLines | ย้ำบรรทัดไฟล์ทีละบรรทัด | File::readLines($fileHandle) |
| ตัววนซ้ำ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
tee | วนซ้ำซ้ำ | Transform::tee($data, $count) |
toArray | แปลงสภาพเป็นอาร์เรย์ | Transform::toArray($data) |
toAssociativeArray | แปลงสภาพเป็นอาร์เรย์ที่เชื่อมโยงกัน | Transform::toAssociativeArray($data, [$keyFunc], [$valueFunc]) |
toIterator | แปลง ITERABLE เป็นตัววนซ้ำ | Transform::toIterator($data) |
| สรุป | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
allMatch | จริงถ้ารายการทั้งหมดเป็นจริงตามภาคแสดง | Summary::allMatch($data, $predicate) |
allUnique | จริงถ้ารายการทั้งหมดไม่ซ้ำกัน | Summary::allUnique($data, [$strict]) |
anyMatch | จริงถ้ารายการใดเป็นจริงตามภาคแสดง | Summary::anyMatch($data, $predicate) |
arePermutations | จริงถ้า iterables เป็น permutations ของกันและกัน | Summary::arePermutations(...$iterables) |
arePermutationsCoercive | จริงถ้า iterables เป็นปริศนาของกันและกันด้วยการบีบบังคับประเภท | Summary::arePermutationsCoercive(...$iterables) |
exactlyN | จริงถ้ารายการ n เป็นจริงตามภาคแสดง | Summary::exactlyN($data, $n, $predicate) |
isEmpty | จริงถ้า iterable ไม่มีรายการ | Summary::isEmpty($data) |
isPartitioned | จริงถ้าแบ่งพาร์ติชันด้วยรายการที่เป็นจริงตามภาคแสดงก่อนอื่น | Summary::isPartitioned($data, $predicate) |
isSorted | จริงถ้ามีการจัดเรียงซ้ำ | Summary::isSorted($data) |
isReversed | จริงถ้าเรียงลำดับย้อนกลับเรียงลำดับ | Summary::isReversed($data) |
noneMatch | จริงถ้าไม่มีรายการที่เป็นจริงตามภาคแสดง | Summary::noneMatch($data, $predicate) |
same | จริงถ้า iterables เหมือนกัน | Summary::same(...$iterables) |
sameCount | จริงถ้า iterables มีความยาวเท่ากัน | Summary::sameCount(...$iterables) |
| ลดลง | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
toAverage | ค่าเฉลี่ยขององค์ประกอบ | Reduce::toAverage($numbers) |
toCount | ลดความยาวของการวนซ้ำ | Reduce::toCount($data) |
toFirst | ลดลงเป็นค่าแรก | Reduce::toFirst($data) |
toFirstAndLast | ลดลงเป็นค่าแรกและสุดท้าย | Reduce::toFirstAndLast($data) |
toLast | ลดค่าสุดท้าย | Reduce::toLast() |
toMax | ลดองค์ประกอบที่ใหญ่ที่สุด | Reduce::toMax($numbers, [$compareBy]) |
toMin | ลดองค์ประกอบที่เล็กที่สุด | Reduce::toMin($numbers, [$compareBy]) |
toMinMax | ลดลงเป็นอาร์เรย์ของขอบเขตบนและล่าง | Reduce::toMinMax($numbers, [$compareBy]) |
toNth | ลดมูลค่าที่ตำแหน่งที่ n | Reduce::toNth($data, $position) |
toProduct | ลดลงในผลิตภัณฑ์ขององค์ประกอบของมัน | Reduce::toProduct($numbers) |
toRandomValue | ลดลงเป็นค่าสุ่มจากการวนซ้ำ | Reduce::toRandomValue($data) |
toRange | ลดความแตกต่างของค่าสูงสุดและค่าต่ำสุด | Reduce::toRange($numbers) |
toString | ลดลงเป็นสตริงที่เข้าร่วม | Reduce::toString($data, [$separator], [$prefix], [$suffix]) |
toSum | ลดผลรวมขององค์ประกอบ | Reduce::toSum($numbers) |
toValue | ลดมูลค่าโดยใช้ callable roducer | Reduce::toValue($data, $reducer, $initialValue) |
| แหล่งที่มา | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
of | สร้างสตรีมจากการวนซ้ำ | Stream::of($iterable) |
ofCoinFlips | สร้างกระแสของการพลิกเหรียญแบบสุ่ม | Stream::ofCoinFlips($repetitions) |
ofCsvFile | สร้างสตรีมจากไฟล์ CSV | Stream::ofCsvFile($fileHandle) |
ofEmpty | สร้างสตรีมที่ว่างเปล่า | Stream::ofEmpty() |
ofFileLines | สร้างสตรีมจากบรรทัดของไฟล์ | Stream::ofFileLines($fileHandle) |
ofRandomChoice | สร้างสตรีมของการเลือกแบบสุ่ม | Stream::ofRandomChoice($items, $repetitions) |
ofRandomNumbers | สร้างกระแสของตัวเลขสุ่ม (จำนวนเต็ม) | Stream::ofRandomNumbers($min, $max, $repetitions) |
ofRandomPercentage | สร้างกระแสสุ่มเปอร์เซ็นต์ระหว่าง 0 ถึง 1 | Stream::ofRandomPercentage($repetitions) |
ofRange | สร้างกระแสของช่วงตัวเลข | Stream::ofRange($start, $end, $step) |
ofRockPaperScissors | สร้างกระแสของมือรองกระดาษหิน | Stream::ofRockPaperScissors($repetitions) |
| การดำเนินการ | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
asort | เรียงลำดับแหล่งที่มาของการดูแลรักษาคีย์ | $stream->asort([$comparator]) |
chainWith | แหล่งที่มาของห่วงโซ่ที่มีอยู่ด้วยการให้ iterables เข้าด้วยกันในการวนซ้ำครั้งเดียว | $stream->chainWith(...$iterables) |
compress | บีบอัดแหล่งที่มาโดยการกรองข้อมูลที่ไม่ได้เลือก | $stream->compress($selectors) |
compressAssociative | บีบอัดแหล่งที่มาโดยการกรองคีย์ที่ไม่ได้เลือก | $stream->compressAssociative($selectorKeys) |
chunkwise | ย้ำโดยชิ้น | $stream->chunkwise($chunkSize) |
chunkwiseOverlap | วนซ้ำโดยชิ้นส่วนที่ทับซ้อนกัน | $stream->chunkwiseOverlap($chunkSize, $overlap) |
distinct | กรององค์ประกอบ: วนเฉพาะรายการที่ไม่ซ้ำกันเท่านั้น | $stream->distinct([$strict]) |
distinctBy | ตัวกรององค์ประกอบ: วนเฉพาะรายการที่ไม่ซ้ำกันโดยใช้ตัวเปรียบเทียบที่กำหนดเอง | $stream->distinct($compareBy) |
dropWhile | วางองค์ประกอบจากแหล่งที่วนซ้ำในขณะที่ฟังก์ชั่นเพรดิเคตเป็นจริง | $stream->dropWhile($predicate) |
filter | ตัวกรองสำหรับองค์ประกอบเฉพาะที่ฟังก์ชันภาคแสดงเป็นจริง | $stream->filterTrue($predicate) |
filterTrue | ตัวกรองสำหรับองค์ประกอบความจริงเท่านั้น | $stream->filterTrue() |
filterFalse | ตัวกรองสำหรับองค์ประกอบที่เป็นเท็จเท่านั้น | $stream->filterFalse() |
filterKeys | ตัวกรองสำหรับคีย์ที่ฟังก์ชันเพรดิเคตเป็นจริง | $stream->filterKeys($predicate) |
flatMap | ฟังก์ชั่นแผนที่ลงบนองค์ประกอบและผลลัพธ์ที่แบน | $stream->flatMap($function) |
flatten | สตรีมหลายมิติที่แบน | $stream->flatten($dimensions) |
frequencies | การกระจายความถี่ | $stream->frequencies([$strict]) |
groupBy | จัดกลุ่มแหล่งที่มาของ ITERABLE โดยองค์ประกอบข้อมูลทั่วไป | $stream->groupBy($groupKeyFunction, [$itemKeyFunc]) |
infiniteCycle | ปั่นจักรยานผ่านองค์ประกอบของแหล่งที่มาที่ซ้ำได้ตามลำดับตลอดไป | $stream->infiniteCycle() |
intersectionWith | ตัดกันแหล่งที่มาของมันและให้ iterables | $stream->intersectionWith(...$iterables) |
intersection CoerciveWith | ตัดกันแหล่งที่มาของ itersable และให้ iterables กับประเภทการบีบบังคับ | $stream->intersectionCoerciveWith(...$iterables) |
limit | จำกัด การวนซ้ำของสตรีม | $stream->limit($limit) |
map | ฟังก์ชั่นแผนที่ลงบนองค์ประกอบ | $stream->map($function) |
pairwise | ส่งคืนองค์ประกอบคู่จากแหล่งที่มาที่ iterable | $stream->pairwise() |
partialIntersectionWith | ตัดแหล่งที่มาของการทำซ้ำบางส่วนและให้ iterables | $stream->partialIntersectionWith( $minIntersectionCount, ...$iterables) |
partialIntersection CoerciveWith | ตัดแหล่งที่มาของ itersable บางส่วนและให้ iterables ที่มีประเภทการบีบบังคับ | $stream->partialIntersectionCoerciveWith( $minIntersectionCount, ...$iterables) |
reindex | คีย์ Reindex ของสตรีมค่าคีย์ | $stream->reindex($reindexer) |
relativeFrequencies | การกระจายความถี่สัมพัทธ์ | $stream->relativeFrequencies([$strict]) |
reverse | องค์ประกอบย้อนกลับของสตรีม | $stream->reverse() |
runningAverage | สะสมค่าเฉลี่ยการทำงาน (ค่าเฉลี่ย) เหนือแหล่งที่วนซ้ำ | $stream->runningAverage($initialValue) |
runningDifference | สะสมความแตกต่างของการทำงานเหนือแหล่งที่วนซ้ำ | $stream->runningDifference($initialValue) |
runningMax | สะสมสูงสุดที่กำลังทำงานอยู่เหนือแหล่งที่วนซ้ำ | $stream->runningMax($initialValue) |
runningMin | สะสม MIN ที่กำลังวิ่งผ่านแหล่งที่มาของ iterable | $stream->runningMin($initialValue) |
runningProduct | สะสมผลิตภัณฑ์ที่กำลังดำเนินอยู่เหนือแหล่งที่ซ้ำ | $stream->runningProduct($initialValue) |
runningTotal | สะสมแหล่งที่ทำงานทั้งหมดผ่านแหล่งที่วนซ้ำ | $stream->runningTotal($initialValue) |
skip | ข้ามองค์ประกอบบางอย่างของสตรีม | $stream->skip($count, [$offset]) |
slice | แยกชิ้นของลำธาร | $stream->slice([$start], [$count], [$step]) |
sort | เรียงลำดับสตรีม | $stream->sort([$comparator]) |
symmetricDifferenceWith | ความแตกต่างแบบสมมาตรของแหล่งที่มาที่ซ้ำและได้รับ iterables | $this->symmetricDifferenceWith(...$iterables) |
symmetricDifference CoerciveWith | ความแตกต่างแบบสมมาตรของแหล่งที่ซ้ำกันและให้ iterables ที่มีประเภทการบีบบังคับ | $this->symmetricDifferenceCoerciveWith( ...$iterables) |
takeWhile | องค์ประกอบกลับจากแหล่งที่วนซ้ำตราบใดที่ภาคแสดงเป็นจริง | $stream->takeWhile($predicate) |
unionWith | Union of Stream กับ Iterables | $stream->unionWith(...$iterables) |
unionCoerciveWith | Union of Stream with Iterables ที่มีการบีบบังคับประเภท | $stream->unionCoerciveWith(...$iterables) |
zipWith | ทำซ้ำแหล่งที่มาซ้ำ ๆ กับคอลเลกชันที่ทำซ้ำอีกชุดพร้อมกัน | $stream->zipWith(...$iterables) |
zipEqualWith | ทำซ้ำแหล่งที่มาซ้ำ ๆ กับคอลเลกชันที่มีความยาวเท่ากันอีกครั้งพร้อมกัน | $stream->zipEqualWith(...$iterables) |
zipFilledWith | ทำซ้ำแหล่งที่มาซ้ำกับคอลเลกชัน iterable อื่นโดยใช้ฟิลเลอร์เริ่มต้น | $stream->zipFilledWith($default, ...$iterables) |
zipLongestWith | ทำซ้ำแหล่งที่มาซ้ำ ๆ กับคอลเลกชันที่ทำซ้ำอีกชุดพร้อมกัน | $stream->zipLongestWith(...$iterables) |
| การทำงานของเทอร์มินัล | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
allMatch | ส่งคืนจริงถ้ารายการทั้งหมดในสตรีมจับคู่เพรดิเคต | $stream->allMatch($predicate) |
allUnique | ส่งคืนจริงถ้ารายการทั้งหมดในสตรีมไม่ซ้ำกัน | $stream->allUnique([$strict]]) |
anyMatch | ส่งคืนจริงหากรายการใด ๆ ในสตรีมตรงกับเพรดิเคต | $stream->anyMatch($predicate) |
arePermutationsWith | ส่งคืนจริงหากการเปลี่ยนกระแสของ iterables ทั้งหมดของสตรีม | $stream->arePermutationsWith(...$iterables) |
arePermutationsCoerciveWith | ส่งคืนจริงหากการเรียงลำดับ iterables ทั้งหมดของสตรีมที่มีการบีบบังคับประเภท | $stream->arePermutationsCoerciveWith(...$iterables) |
exactlyN | ส่งคืนจริงถ้ารายการ n เป็นจริงตามภาคแสดง | $stream->exactlyN($n, $predicate) |
isEmpty | ส่งคืนจริงถ้าสตรีมไม่มีรายการ | $stream::isEmpty() |
isPartitioned | ส่งคืนจริงหากแบ่งพาร์ติชันด้วยรายการที่แท้จริงตามภาคแสดงก่อนอื่น | $stream::isPartitioned($predicate) |
isSorted | ส่งคืนจริงถ้าสตรีมเรียงลำดับตามลำดับจากน้อยไปมาก | $stream->isSorted() |
isReversed | ส่งคืนจริงถ้าสตรีมเรียงลำดับตามลำดับที่ย้อนกลับ | $stream->isReversed() |
noneMatch | ส่งคืนจริงหากไม่มีรายการในสตรีมตรงข้ามเพรดิเคต | $stream->noneMatch($predicate) |
sameWith | ส่งคืนจริงถ้าสตรีมและคอลเลกชันทั้งหมดเหมือนกัน | $stream->sameWith(...$iterables) |
sameCountWith | ส่งคืนจริงถ้าสตรีมและคอลเลกชันทั้งหมดมีความยาวเท่ากัน | $stream->sameCountWith(...$iterables) |
| การทำงานของเทอร์มินัล | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
toAverage | ลดสตรีมเป็นค่าเฉลี่ยของรายการ | $stream->toAverage() |
toCount | ลดสตรีมให้มีความยาว | $stream->toCount() |
toFirst | ลดสตรีมเป็นค่าแรก | $stream->toFirst() |
toFirstAndLast | ลดสตรีมเป็นค่าแรกและค่าสุดท้าย | $stream->toFirstAndLast() |
toLast | ลดสตรีมเป็นค่าสุดท้าย | $stream->toLast() |
toMax | ลดสตรีมเป็นค่าสูงสุด | $stream->toMax([$compareBy]) |
toMin | ลดสตรีมเป็นค่าต่ำสุด | $stream->toMin([$compareBy]) |
toMinMax | ลดสตรีมเป็นอาร์เรย์ของขอบเขตบนและล่าง | $stream->toMinMax([$compareBy]) |
toNth | ลดสตรีมเป็นค่าที่ตำแหน่งที่ n | $stream->toNth($position) |
toProduct | ลดสตรีมไปยังผลิตภัณฑ์ของรายการ | $stream->toProduct() |
toString | ลดสตรีมไปยังสตริงที่เข้าร่วม | $stream->toString([$separator], [$prefix], [$suffix]) |
toSum | ลดสตรีมเป็นผลรวมของรายการ | $stream->toSum() |
toRandomValue | ลดสตรีมเป็นค่าสุ่มภายใน | $stream->toRandomValue() |
toRange | ลดสตรีมถึงความแตกต่างของค่าสูงสุดและขั้นต่ำ | $stream->toRange() |
toValue | ลดสตรีมเช่นฟังก์ชั่น array_reduce () | $stream->toValue($reducer, $initialValue) |
| การทำงานของเทอร์มินัล | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
toArray | ส่งคืนอาร์เรย์ขององค์ประกอบสตรีม | $stream->toArray() |
toAssociativeArray | ส่งคืนแผนที่ค่าคีย์ขององค์ประกอบสตรีม | $stream->toAssociativeArray($keyFunc, $valueFunc) |
tee | ส่งคืนอาร์เรย์ของสตรีมที่เหมือนกันหลายสาย | $stream->tee($count) |
| การทำงานของเทอร์มินัล | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
callForEach | ดำเนินการผ่านฟังก์ชั่นในแต่ละรายการ | $stream->callForEach($function) |
print | print แต่ละรายการในสตรีม | $stream->print([$separator], [$prefix], [$suffix]) |
printLn | print แต่ละรายการบนบรรทัดใหม่ | $stream->printLn() |
toCsvFile | เขียนเนื้อหาของสตรีมไปยังไฟล์ CSV | $stream->toCsvFile($fileHandle, [$headers]) |
toFile | เขียนเนื้อหาของสตรีมไปยังไฟล์ | $stream->toFile($fileHandle) |
| การดำเนินการดีบัก | คำอธิบาย | รหัสตัวอย่าง |
|---|---|---|
peek | มองดูแต่ละองค์ประกอบระหว่างการดำเนินการสตรีม | $stream->peek($peekFunc) |
peekStream | มองไปที่สตรีมทั้งหมดระหว่างการดำเนินการ | $stream->peekStream($peekFunc) |
peekPrint | มองดูแต่ละองค์ประกอบโดยการพิมพ์ระหว่างการดำเนินการ | $stream->peekPrint() |
peekPrintR | มองดูแต่ละองค์ประกอบโดยการพิมพ์ระหว่างการดำเนินการ | $stream->peekPrintR() |
printR | print_r แต่ละรายการ | $stream->printR() |
varDump | var_dump แต่ละรายการ | $stream->varDump() |
เพิ่มไลบรารีลงในไฟล์ composer.json ในโครงการของคุณ:
{
"require" : {
"markrogoyski/itertools-php" : " 1.* "
}
}ใช้นักแต่งเพลงเพื่อติดตั้งไลบรารี:
$ php composer.phar installนักแต่งเพลงจะติดตั้ง iTertools ภายในโฟลเดอร์ผู้ขายของคุณ จากนั้นคุณสามารถเพิ่มไฟล์. php ต่อไปนี้เพื่อใช้ไลบรารีด้วยการโหลดอัตโนมัติ
require_once __DIR__ . ' /vendor/autoload.php ' ;อีกทางเลือกหนึ่งใช้นักแต่งเพลงบนบรรทัดคำสั่งที่ต้องการและติดตั้ง iTertools:
$ php composer.phar require markrogoyski/itertools-php:1.*
ฟังก์ชั่นทั้งหมดทำงานในคอลเลกชัน iterable :
array (ประเภท)Generator (ประเภท)Iterator (อินเตอร์เฟส)Traversable (อินเตอร์เฟส) ห่วงโซ่หลาย iTerables เข้าด้วยกันเป็นลำดับต่อเนื่องเดียว
Multi::chain(iterable ...$iterables)
use IterTools Multi ;
$ prequels = [ ' Phantom Menace ' , ' Attack of the Clones ' , ' Revenge of the Sith ' ];
$ originals = [ ' A New Hope ' , ' Empire Strikes Back ' , ' Return of the Jedi ' ];
foreach (Multi:: chain ( $ prequels , $ originals ) as $ movie ) {
print ( $ movie );
}
// 'Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith', 'A New Hope', 'Empire Strikes Back', 'Return of the Jedi'วนซ้ำหลายคอลเลกชันที่วนซ้ำพร้อมกัน
Multi::zip(iterable ...$iterables)
use IterTools Multi ;
$ languages = [ ' PHP ' , ' Python ' , ' Java ' , ' Go ' ];
$ mascots = [ ' elephant ' , ' snake ' , ' bean ' , ' gopher ' ];
foreach (Multi:: zip ( $ languages , $ mascots ) as [ $ language , $ mascot ]) {
print ( " The { $ language } language mascot is an { $ mascot } . " );
}
// The PHP language mascot is an elephant.
// ...ZIP ทำงานกับอินพุตที่ทำซ้ำได้หลายตัว-ไม่ จำกัด เพียงสอง
$ names = [ ' Ryu ' , ' Ken ' , ' Chun Li ' , ' Guile ' ];
$ countries = [ ' Japan ' , ' USA ' , ' China ' , ' USA ' ];
$ signatureMoves = [ ' hadouken ' , ' shoryuken ' , ' spinning bird kick ' , ' sonic boom ' ];
foreach (Multi:: zip ( $ names , $ countries , $ signatureMoves ) as [ $ name , $ country , $ signatureMove ]) {
$ streetFighter = new StreetFighter ( $ name , $ country , $ signatureMove );
}หมายเหตุ: สำหรับความยาวที่ไม่สม่ำเสมอการวนซ้ำจะหยุดลงเมื่อการทำซ้ำที่สั้นที่สุดหมดลง
วนซ้ำหลายคอลเลกชันที่มีความยาวเท่ากันพร้อมกัน
โยน LengthException ถ้าความยาวไม่เท่ากันหมายความว่าอย่างน้อยหนึ่งตัววนซ้ำสิ้นสุดลงก่อนที่คนอื่น ๆ
Multi::zipEqual(iterable ...$iterables)
use IterTools Multi ;
$ letters = [ ' A ' , ' B ' , ' C ' ];
$ numbers = [ 1 , 2 , 3 ];
foreach (Multi:: zipEqual ( $ letters , $ numbers ) as [ $ letter , $ number ]) {
// ['A', 1], ['B', 2], ['C', 3]
}วนซ้ำหลายคอลเลกชันที่วนซ้ำพร้อมกันโดยใช้ค่าฟิลเลอร์เริ่มต้นหากความยาวไม่เท่ากัน
Multi::zipFilled(mixed $filler, iterable ...$iterables)
use IterTools Multi ;
$ default = ' ? ' ;
$ letters = [ ' A ' , ' B ' ];
$ numbers = [ 1 , 2 , 3 ];
foreach (Multi:: zipFilled ( $ default , $ letters , $ numbers ) as [ $ letter , $ number ]) {
// ['A', 1], ['B', 2], ['?', 3]
}วนซ้ำหลายคอลเลกชันที่วนซ้ำพร้อมกัน
Multi::zipLongest(iterable ...$iterables)
สำหรับความยาวที่ไม่สม่ำเสมอ iterables ที่หมดแล้วจะสร้าง null สำหรับการทำซ้ำที่เหลือ
use IterTools Multi ;
$ letters = [ ' A ' , ' B ' , ' C ' ];
$ numbers = [ 1 , 2 ];
foreach (Multi:: zipLongest ( $ letters , $ numbers ) as [ $ letter , $ number ]) {
// ['A', 1], ['B', 2], ['C', null]
}องค์ประกอบกลับเป็นชิ้นขนาดที่แน่นอน
Single::chunkwise(iterable $data, int $chunkSize)
ขนาดก้อนต้องมีอย่างน้อย 1
use IterTools Single ;
$ movies = [
' Phantom Menace ' , ' Attack of the Clones ' , ' Revenge of the Sith ' ,
' A New Hope ' , ' Empire Strikes Back ' , ' Return of the Jedi ' ,
' The Force Awakens ' , ' The Last Jedi ' , ' The Rise of Skywalker '
];
foreach (Single:: chunkwise ( $ movies , 3 ) as $ trilogy ) {
$ trilogies [] = $ trilogy ;
}
// [
// ['Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith'],
// ['A New Hope', 'Empire Strikes Back', 'Return of the Jedi'],
// ['The Force Awakens', 'The Last Jedi', 'The Rise of Skywalker]'
// ]ส่งคืนชิ้นส่วนที่ทับซ้อนกันขององค์ประกอบ
Single::chunkwiseOverlap(iterable $data, int $chunkSize, int $overlapSize, bool $includeIncompleteTail = true)
use IterTools Single ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
foreach (Single:: chunkwiseOverlap ( $ numbers , 3 , 1 ) as $ chunk ) {
// [1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]
}บีบอัดซ้ำโดยการกรองข้อมูลที่ไม่ได้เลือก
Single::compress(string $data, $selectors)
use IterTools Single ;
$ movies = [
' Phantom Menace ' , ' Attack of the Clones ' , ' Revenge of the Sith ' ,
' A New Hope ' , ' Empire Strikes Back ' , ' Return of the Jedi ' ,
' The Force Awakens ' , ' The Last Jedi ' , ' The Rise of Skywalker '
];
$ goodMovies = [ 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 ];
foreach (Single:: compress ( $ movies , $ goodMovies ) as $ goodMovie ) {
print ( $ goodMovie );
}
// 'A New Hope', 'Empire Strikes Back', 'Return of the Jedi', 'The Force Awakens'บีบอัดซ้ำโดยการกรองคีย์ที่ไม่ได้เลือก
Single::compressAssociative(string $data, array $selectorKeys)
use IterTools Single ;
$ starWarsEpisodes = [
' I ' => ' The Phantom Menace ' ,
' II ' => ' Attack of the Clones ' ,
' III ' => ' Revenge of the Sith ' ,
' IV ' => ' A New Hope ' ,
' V ' => ' The Empire Strikes Back ' ,
' VI ' => ' Return of the Jedi ' ,
' VII ' => ' The Force Awakens ' ,
' VIII ' => ' The Last Jedi ' ,
' IX ' => ' The Rise of Skywalker ' ,
];
$ originalTrilogyNumbers = [ ' IV ' , ' V ' , ' VI ' ];
foreach (Single:: compressAssociative ( $ starWarsEpisodes , $ originalTrilogyNumbers ) as $ episode => $ title ) {
print ( " $ episode : $ title " . PHP_EOL );
}
// IV: A New Hope
// V: The Empire Strikes Back
// VI: Return of the Jediวางองค์ประกอบจากการทำซ้ำในขณะที่ฟังก์ชั่นเพรดิเคตเป็นจริง
เมื่อฟังก์ชั่นเพรดิเคตส่งกลับเท็จหนึ่งครั้งองค์ประกอบที่เหลือทั้งหมดจะถูกส่งคืน
Single::dropWhile(iterable $data, callable $predicate)
use IterTools Single ;
$ scores = [ 50 , 60 , 70 , 85 , 65 , 90 ];
$ predicate = fn ( $ x ) => $ x < 70 ;
foreach (Single:: dropWhile ( $ scores , $ predicate ) as $ score ) {
print ( $ score );
}
// 70, 85, 65, 90ตัวกรององค์ประกอบจากองค์ประกอบที่กลับมาเป็นเพียงการกลับมาเท่านั้นที่ฟังก์ชั่นเพรดิเคตนั้นเป็นจริง
Single::filter(iterable $data, callable $predicate)
use IterTools Single ;
$ starWarsEpisodes = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ];
$ goodMoviePredicate = fn ( $ episode ) => $ episode > 3 && $ episode < 8 ;
foreach (Single:: filter ( $ starWarsEpisodes , $ goodMoviePredicate ) as $ goodMovie ) {
print ( $ goodMovie );
}
// 4, 5, 6, 7กรององค์ประกอบจากการทำซ้ำเท่านั้นองค์ประกอบที่กลับมาซึ่งเป็นความจริง
Single::filterTrue(iterable $data)
use IterTools Single ;
$ reportCardGrades = [ 100 , 0 , 95 , 85 , 0 , 94 , 0 ];
foreach (Single:: filterTrue ( $ reportCardGrades ) as $ goodGrade ) {
print ( $ goodGrade );
}
// 100, 95, 85, 94ตัวกรององค์ประกอบจากการวนซ้ำเท่านั้นองค์ประกอบที่ส่งคืนซึ่งฟังก์ชั่นเพรดิเคตเป็นเท็จ
หากไม่มีการจัดเตรียมภาคแสดงค่าบูลีนของข้อมูลจะถูกใช้
Single::filterFalse(iterable $data, callable $predicate)
use IterTools Single ;
$ alerts = [ 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 ];
foreach (Single:: filterFalse ( $ alerts ) as $ noAlert ) {
print ( $ noAlert );
}
// 0, 0, 0, 0ตัวกรององค์ประกอบจากการทำซ้ำเท่านั้นองค์ประกอบที่ส่งคืนซึ่งคีย์ฟังก์ชันเพรดิเคตนั้นเป็นจริง
Single::filterKeys(iterable $data, callable $predicate)
use IterTools Single ;
$ olympics = [
2000 => ' Sydney ' ,
2002 => ' Salt Lake City ' ,
2004 => ' Athens ' ,
2006 => ' Turin ' ,
2008 => ' Beijing ' ,
2010 => ' Vancouver ' ,
2012 => ' London ' ,
2014 => ' Sochi ' ,
2016 => ' Rio de Janeiro ' ,
2018 => ' Pyeongchang ' ,
2020 => ' Tokyo ' ,
2022 => ' Beijing ' ,
];
$ summerFilter = fn ( $ year ) => $ year % 4 === 0 ;
foreach (Single:: filterKeys ( $ olympics , $ summerFilter ) as $ year => $ hostCity ) {
print ( " $ year : $ hostCity " . PHP_EOL );
}
// 2000: Sydney
// 2004: Athens
// 2008: Beijing
// 2012: London
// 2016: Rio de Janeiro
// 2020: Tokyoแมปฟังก์ชั่นเฉพาะองค์ประกอบของการวนซ้ำแล้วแบนผลลัพธ์
Single::flatMap(iterable $data, callable $mapper)
use IterTools Single ;
$ data = [ 1 , 2 , 3 , 4 , 5 ];
$ mapper = fn ( $ item ) => [ $ item , - $ item ];
foreach (Single:: flatMap ( $ data , $ mapper ) as $ number ) {
print ( $ number . ' ' );
}
// 1 -1 2 -2 3 -3 4 -4 5 -5แบนการวนซ้ำหลายมิติ
Single::flatten(iterable $data, int $dimensions = 1)
use IterTools Single ;
$ multidimensional = [ 1 , [ 2 , 3 ], [ 4 , 5 ]];
$ flattened = [];
foreach (Single:: flatten ( $ multidimensional ) as $ number ) {
$ flattened [] = $ number ;
}
// [1, 2, 3, 4, 5]ข้อมูลกลุ่มโดยองค์ประกอบข้อมูลทั่วไป
Single::groupBy(iterable $data, callable $groupKeyFunction, callable $itemKeyFunction = null)
$groupKeyFunction กำหนดคีย์ในการจัดกลุ่มองค์ประกอบโดย$itemKeyFunction อนุญาตให้ดัชนีที่กำหนดเองภายในสมาชิกแต่ละกลุ่ม use IterTools Single ;
$ cartoonCharacters = [
[ ' Garfield ' , ' cat ' ],
[ ' Tom ' , ' cat ' ],
[ ' Felix ' , ' cat ' ],
[ ' Heathcliff ' , ' cat ' ],
[ ' Snoopy ' , ' dog ' ],
[ ' Scooby-Doo ' , ' dog ' ],
[ ' Odie ' , ' dog ' ],
[ ' Donald ' , ' duck ' ],
[ ' Daffy ' , ' duck ' ],
];
$ charactersGroupedByAnimal = [];
foreach (Single:: groupBy ( $ cartoonCharacters , fn ( $ x ) => $ x [ 1 ]) as $ animal => $ characters ) {
$ charactersGroupedByAnimal [ $ animal ] = $ characters ;
}
/*
'cat' => [
['Garfield', 'cat'],
['Tom', 'cat'],
['Felix', 'cat'],
['Heathcliff', 'cat'],
],
'dog' => [
['Snoopy', 'dog'],
['Scooby-Doo', 'dog'],
['Odie', 'dog'],
],
'duck' => [
['Donald', 'duck'],
['Daffy', 'duck'],
*/ย้ำถึงขีด จำกัด
หยุดแม้ว่าจะมีข้อมูลมากขึ้นหากถึงขีด จำกัด
Single::limit(iterable $data, int $limit)
use IterTools Single ;
$ matrixMovies = [ ' The Matrix ' , ' The Matrix Reloaded ' , ' The Matrix Revolutions ' , ' The Matrix Resurrections ' ];
$ limit = 1 ;
foreach (Single:: limit ( $ matrixMovies , $ limit ) as $ goodMovie ) {
print ( $ goodMovie );
}
// 'The Matrix' (and nothing else)แมปฟังก์ชั่นลงบนแต่ละองค์ประกอบ
Single::map(iterable $data, callable $function)
use IterTools Single ;
$ grades = [ 100 , 99 , 95 , 98 , 100 ];
$ strictParentsOpinion = fn ( $ g ) => $ g === 100 ? ' A ' : ' F ' ;
foreach (Single:: map ( $ grades , $ strictParentsOpinion ) as $ actualGrade ) {
print ( $ actualGrade );
}
// A, F, F, F, Aส่งคืนคู่ที่ทับซ้อนกันอย่างต่อเนื่อง
ส่งคืนเครื่องกำเนิดไฟฟ้าที่ว่างเปล่าหากคอลเลกชันที่ได้รับมีองค์ประกอบน้อยกว่า 2 องค์ประกอบ
Single::pairwise(iterable $data)
use IterTools Single ;
$ friends = [ ' Ross ' , ' Rachel ' , ' Chandler ' , ' Monica ' , ' Joey ' , ' Phoebe ' ];
foreach (Single:: pairwise ( $ friends ) as [ $ leftFriend , $ rightFriend ]) {
print ( "{ $ leftFriend } and { $ rightFriend }" );
}
// Ross and Rachel, Rachel and Chandler, Chandler and Monica, ...ทำซ้ำรายการ
Single::repeat(mixed $item, int $repetitions)
use IterTools Single ;
$ data = ' Beetlejuice ' ;
$ repetitions = 3 ;
foreach (Single:: repeat ( $ data , $ repetitions ) as $ repeated ) {
print ( $ repeated );
}
// 'Beetlejuice', 'Beetlejuice', 'Beetlejuice'คีย์ Reindex ของคีย์-ค่า iterable โดยใช้ฟังก์ชั่นดัชนี
Single::reindex(string $data, callable $indexer)
use IterTools Single ;
$ data = [
[
' title ' => ' Star Wars: Episode IV – A New Hope ' ,
' episode ' => ' IV ' ,
' year ' => 1977 ,
],
[
' title ' => ' Star Wars: Episode V – The Empire Strikes Back ' ,
' episode ' => ' V ' ,
' year ' => 1980 ,
],
[
' title ' => ' Star Wars: Episode VI – Return of the Jedi ' ,
' episode ' => ' VI ' ,
' year ' => 1983 ,
],
];
$ reindexFunc = fn ( array $ swFilm ) => $ swFilm [ ' episode ' ];
$ reindexedData = [];
foreach (Single:: reindex ( $ data , $ reindexFunc ) as $ key => $ filmData ) {
$ reindexedData [ $ key ] = $ filmData ;
}
// [
// 'IV' => [
// 'title' => 'Star Wars: Episode IV – A New Hope',
// 'episode' => 'IV',
// 'year' => 1977,
// ],
// 'V' => [
// 'title' => 'Star Wars: Episode V – The Empire Strikes Back',
// 'episode' => 'V',
// 'year' => 1980,
// ],
// 'VI' => [
// 'title' => 'Star Wars: Episode VI – Return of the Jedi',
// 'episode' => 'VI',
// 'year' => 1983,
// ],
// ]ย้อนกลับองค์ประกอบของการวนซ้ำ
Single::reverse(iterable $data)
use IterTools Single ;
$ words = [ ' Alice ' , ' answers ' , ' your ' , ' questions ' , ' Bob ' ];
foreach (Single:: reverse ( $ words ) as $ word ) {
print ( $ word . ' ' );
}
// Bob questions your answers Aliceข้ามองค์ประกอบ N ในการทำซ้ำหลังจากชดเชยออฟเซ็ตเสริม
Single::skip(iterable $data, int $count, int $offset = 0)
use IterTools Single ;
$ movies = [
' The Phantom Menace ' , ' Attack of the Clones ' , ' Revenge of the Sith ' ,
' A New Hope ' , ' The Empire Strikes Back ' , ' Return of the Jedi ' ,
' The Force Awakens ' , ' The Last Jedi ' , ' The Rise of Skywalker '
];
$ prequelsRemoved = [];
foreach (Single:: skip ( $ movies , 3 ) as $ nonPrequel ) {
$ prequelsRemoved [] = $ nonPrequel ;
} // Episodes IV - IX
$ onlyTheBest = [];
foreach (Single:: skip ( $ prequelsRemoved , 3 , 3 ) as $ nonSequel ) {
$ onlyTheBest [] = $ nonSequel ;
}
// 'A New Hope', 'The Empire Strikes Back', 'Return of the Jedi'สกัดชิ้นส่วนของการวนซ้ำ
Single::slice(iterable $data, int $start = 0, int $count = null, int $step = 1)
use IterTools Single ;
$ olympics = [ 1992 , 1994 , 1996 , 1998 , 2000 , 2002 , 2004 , 2006 , 2008 , 2010 , 2012 , 2014 , 2016 , 2018 , 2020 , 2022 ];
$ winterOlympics = [];
foreach (Single:: slice ( $ olympics , 1 , 8 , 2 ) as $ winterYear ) {
$ winterOlympics [] = $ winterYear ;
}
// [1994, 1998, 2002, 2006, 2010, 2014, 2018, 2022]ย้ำตัวละครแต่ละตัวของสตริง
Single::string(string $string)
use IterTools Single ;
$ string = ' MickeyMouse ' ;
$ listOfCharacters = [];
foreach (Single:: string ( $ string ) as $ character ) {
$ listOfCharacters [] = $ character ;
}
// ['M', 'i', 'c', 'k', 'e', 'y', 'M', 'o', 'u', 's', 'e']องค์ประกอบที่ส่งคืนจากการวนซ้ำตราบใดที่ภาคแสดงเป็นจริง
หยุดการวนซ้ำทันทีที่เพรดิเคตกลับมาเป็นเท็จแม้ว่าองค์ประกอบอื่น ๆ ในภายหลังจะกลับมาจริงในที่สุด (แตกต่างจากตัวกรองกรอง)
Single::takeWhile(iterable $data, callable $predicate)
use IterTools Single ;
$ prices = [ 0 , 0 , 5 , 10 , 0 , 0 , 9 ];
$ isFree = fn ( $ price ) => $ price == 0 ;
foreach (Single:: takeWhile ( $ prices , $ isFree ) as $ freePrice ) {
print ( $ freePrice );
}
// 0, 0 นับตามลำดับตลอดไป
Infinite::count(int $start = 1, int $step = 1)
use IterTools Infinite ;
$ start = 1 ;
$ step = 1 ;
foreach (Infinite:: count ( $ start , $ step ) as $ i ) {
print ( $ i );
}
// 1, 2, 3, 4, 5 ...วนผ่านองค์ประกอบของคอลเลกชันตามลำดับตลอดไป
Infinite::cycle(iterable $iterable)
use IterTools Infinite ;
$ hands = [ ' rock ' , ' paper ' , ' scissors ' ];
foreach (Infinite:: cycle ( $ hands ) as $ hand ) {
RockPaperScissors:: playHand ( $ hand );
}
// rock, paper, scissors, rock, paper, scissors, ...ทำซ้ำรายการตลอดไป
Infinite::repeat(mixed $item)
use IterTools Infinite ;
$ dialogue = ' Are we there yet? ' ;
foreach (Infinite:: repeat ( $ dialogue ) as $ repeated ) {
print ( $ repeated );
}
// 'Are we there yet?', 'Are we there yet?', 'Are we there yet?', ... สร้างการเลือกแบบสุ่มจากอาร์เรย์ของค่า
Random::choice(array $items, int $repetitions)
use IterTools Random ;
$ cards = [ ' Ace ' , ' King ' , ' Queen ' , ' Jack ' , ' Joker ' ];
$ repetitions = 10 ;
foreach (Random:: choice ( $ cards , $ repetitions ) as $ card ) {
print ( $ card );
}
// 'King', 'Jack', 'King', 'Ace', ... [random]สร้างการพลิกเหรียญแบบสุ่ม (0 หรือ 1)
Random::coinFlip(int $repetitions)
use IterTools Random ;
$ repetitions = 10 ;
foreach (Random:: coinFlip ( $ repetitions ) as $ coinFlip ) {
print ( $ coinFlip );
}
// 1, 0, 1, 1, 0, ... [random]สร้างตัวเลขสุ่ม (จำนวนเต็ม)
Random::number(int $min, int $max, int $repetitions)
use IterTools Random ;
$ min = 1 ;
$ max = 4 ;
$ repetitions = 10 ;
foreach (Random:: number ( $ min , $ max , $ repetitions ) as $ number ) {
print ( $ number );
}
// 3, 2, 5, 5, 1, 2, ... [random]สร้างเปอร์เซ็นต์สุ่มระหว่าง 0 ถึง 1
Random::percentage(int $repetitions)
use IterTools Random ;
$ repetitions = 10 ;
foreach (Random:: percentage ( $ repetitions ) as $ percentage ) {
print ( $ percentage );
}
// 0.30205562629132, 0.59648594775233, ... [random]สร้างมือกรรไกรกระดาษหินแบบสุ่ม
Random::rockPaperScissors(int $repetitions)
use IterTools Random ;
$ repetitions = 10 ;
foreach (Random:: rockPaperScissors ( $ repetitions ) as $ rpsHand ) {
print ( $ rpsHand );
}
// 'paper', 'rock', 'rock', 'scissors', ... [random] ส่งคืนการกระจายความถี่ของข้อมูล
Math::frequencies(iterable $data, bool $strict = true): Generator
ค่าเริ่มต้นกับการเปรียบเทียบประเภทที่เข้มงวด กำหนดให้เข้มงวดกับเท็จสำหรับการเปรียบเทียบการบีบบังคับประเภท
use IterTools Math ;
$ grades = [ ' A ' , ' A ' , ' B ' , ' B ' , ' B ' , ' C ' ];
foreach (Math:: frequencies ( $ grades ) as $ grade => $ frequency ) {
print ( " $ grade : $ frequency " . PHP_EOL );
}
// A: 2, B: 3, C: 1ส่งคืนการกระจายความถี่สัมพัทธ์ของข้อมูล
Math::relativeFrequencies(iterable $data, bool $strict = true): Generator
ค่าเริ่มต้นกับการเปรียบเทียบประเภทที่เข้มงวด กำหนดให้เข้มงวดกับเท็จสำหรับการเปรียบเทียบการบีบบังคับประเภท
use IterTools Math ;
$ grades = [ ' A ' , ' A ' , ' B ' , ' B ' , ' B ' , ' C ' ];
foreach (Math:: relativeFrequencies ( $ grades ) as $ grade => $ frequency ) {
print ( " $ grade : $ frequency " . PHP_EOL );
}
// A: 0.33, B: 0.5, C: 0.166สะสมค่าเฉลี่ยที่ทำงานอยู่เหนือรายการตัวเลข
Math::runningAverage(iterable $numbers, int|float $initialValue = null)
use IterTools Math ;
$ grades = [ 100 , 80 , 80 , 90 , 85 ];
foreach (Math:: runningAverage ( $ grades ) as $ runningAverage ) {
print ( $ runningAverage );
}
// 100, 90, 86.667, 87.5, 87สะสมความแตกต่างของการวิ่งผ่านรายการตัวเลข
Math::runningDifference(iterable $numbers, int|float $initialValue = null)
use IterTools Math ;
$ credits = [ 1 , 2 , 3 , 4 , 5 ];
foreach (Math:: runningDifference ( $ credits ) as $ runningDifference ) {
print ( $ runningDifference );
}
// -1, -3, -6, -10, -15ระบุค่าเริ่มต้นที่เป็นตัวเลือกเพื่อนำไปสู่ความแตกต่างของการทำงาน
use IterTools Math ;
$ dartsScores = [ 50 , 50 , 25 , 50 ];
$ startingScore = 501 ;
foreach (Math:: runningDifference ( $ dartsScores , $ startingScore ) as $ runningScore ) {
print ( $ runningScore );
}
// 501, 451, 401, 376, 326สะสมค่าสูงสุดที่ทำงานอยู่เหนือรายการตัวเลข
Math::runningMax(iterable $numbers, int|float $initialValue = null)
use IterTools Math ;
$ numbers = [ 1 , 2 , 1 , 3 , 5 ];
foreach (Math:: runningMax ( $ numbers ) as $ runningMax ) {
print ( $ runningMax );
}
// 1, 2, 2, 3, 5สะสมขั้นต่ำที่รันอยู่เหนือรายการตัวเลข
Math::runningMin(iterable $numbers, int|float $initialValue = null)
use IterTools Math ;
$ numbers = [ 3 , 4 , 2 , 5 , 1 ];
foreach (Math:: runningMin ( $ numbers ) as $ runningMin ) {
print ( $ runningMin );
}
// 3, 3, 2, 2, 1สะสมผลิตภัณฑ์ที่ทำงานอยู่เหนือรายการตัวเลข
Math::runningProduct(iterable $numbers, int|float $initialValue = null)
use IterTools Math ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 ];
foreach (Math:: runningProduct ( $ numbers ) as $ runningProduct ) {
print ( $ runningProduct );
}
// 1, 2, 6, 24, 120ให้ค่าเริ่มต้นที่เป็นตัวเลือกเพื่อนำไปสู่ผลิตภัณฑ์ที่กำลังดำเนินการ
use IterTools Math ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 ];
$ initialValue = 5 ;
foreach (Math:: runningProduct ( $ numbers , $ initialValue ) as $ runningProduct ) {
print ( $ runningProduct );
}
// 5, 5, 10, 30, 120, 600สะสมยอดรวมที่ทำงานอยู่เหนือรายการตัวเลข
Math::runningTotal(iterable $numbers, int|float $initialValue = null)
use IterTools Math ;
$ prices = [ 1 , 2 , 3 , 4 , 5 ];
foreach (Math:: runningTotal ( $ prices ) as $ runningTotal ) {
print ( $ runningTotal );
}
// 1, 3, 6, 10, 15ให้ค่าเริ่มต้นที่เป็นตัวเลือกเพื่อนำไปสู่ยอดรวมที่กำลังดำเนินอยู่
use IterTools Math ;
$ prices = [ 1 , 2 , 3 , 4 , 5 ];
$ initialValue = 5 ;
foreach (Math:: runningTotal ( $ prices , $ initialValue ) as $ runningTotal ) {
print ( $ runningTotal );
}
// 5, 6, 8, 11, 15, 20 กรององค์ประกอบจากการวนซ้ำเท่านั้นที่จะส่งคืนองค์ประกอบที่แตกต่างกันเท่านั้น
Set::distinct(iterable $data, bool $strict = true)
ค่าเริ่มต้นกับการเปรียบเทียบประเภทที่เข้มงวด กำหนดให้เข้มงวดกับเท็จสำหรับการเปรียบเทียบการบีบบังคับประเภท
use IterTools Set ;
$ chessSet = [ ' rook ' , ' rook ' , ' knight ' , ' knight ' , ' bishop ' , ' bishop ' , ' king ' , ' queen ' , ' pawn ' , ' pawn ' , ... ];
foreach (Set:: distinct ( $ chessSet ) as $ chessPiece ) {
print ( $ chessPiece );
}
// rook, knight, bishop, king, queen, pawn
$ mixedTypes = [ 1 , ' 1 ' , 2 , ' 2 ' , 3 ];
foreach (Set:: distinct ( $ mixedTypes , false ) as $ datum ) {
print ( $ datum );
}
// 1, 2, 3กรององค์ประกอบจากการวนซ้ำเท่านั้นที่จะส่งคืนองค์ประกอบที่แตกต่างกันตามฟังก์ชันเปรียบเทียบที่กำหนดเอง
Set::distinctBy(iterable $data, callable $compareBy)
use IterTools Set ;
$ streetFighterConsoleReleases = [
[ ' id ' => ' 112233 ' , ' name ' => ' Street Fighter 3 3rd Strike ' , ' console ' => ' Dreamcast ' ],
[ ' id ' => ' 223344 ' , ' name ' => ' Street Fighter 3 3rd Strike ' , ' console ' => ' PS4 ' ],
[ ' id ' => ' 334455 ' , ' name ' => ' Street Fighter 3 3rd Strike ' , ' console ' => ' PS5 ' ],
[ ' id ' => ' 445566 ' , ' name ' => ' Street Fighter VI ' , ' console ' => ' PS4 ' ],
[ ' id ' => ' 556677 ' , ' name ' => ' Street Fighter VI ' , ' console ' => ' PS5 ' ],
[ ' id ' => ' 667788 ' , ' name ' => ' Street Fighter VI ' , ' console ' => ' PC ' ],
];
$ compareBy = fn ( $ sfTitle ) => $ sfTitle [ ' name ' ];
$ uniqueTitles = [];
foreach (Set:: distinctBy ( $ streetFighterConsoleReleases , $ compareBy ) as $ sfTitle ) {
$ uniqueTitles [] = $ sfTitle ;
}
// Contains one SF3 3rd Strike entry and one SFVI entry.ทำซ้ำจุดตัดของ iterables
Set::intersection(iterable ...$iterables)
หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎทางแยกหลายชุด
use IterTools Set ;
$ chessPieces = [ ' rook ' , ' knight ' , ' bishop ' , ' queen ' , ' king ' , ' pawn ' ];
$ shogiPieces = [ ' rook ' , ' knight ' , ' bishop ' 'king', ' pawn ' , ' lance ' , ' gold general ' , ' silver general ' ];
foreach (Set:: intersection ( $ chessPieces , $ shogiPieces ) as $ commonPiece ) {
print ( $ commonPiece );
}
// rook, knight, bishop, king, pawnทำซ้ำจุดตัดของ iTerables โดยใช้การบีบบังคับประเภท
Set::intersectionCoercive(iterable ...$iterables)
หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎทางแยกหลายชุด
use IterTools Set ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 ];
$ numerics = [ ' 1 ' , ' 2 ' , 3 ];
foreach (Set:: intersectionCoercive ( $ numbers , $ numerics ) as $ commonNumber ) {
print ( $ commonNumber );
}
// 1, 2, 3วนซ้ำจุดตัดของ iterables M-partial
Set::partialIntersection(int $minIntersectionCount, iterable ...$iterables)
use IterTools Set ;
$ staticallyTyped = [ ' c++ ' , ' java ' , ' c# ' , ' go ' , ' haskell ' ];
$ dynamicallyTyped = [ ' php ' , ' python ' , ' javascript ' , ' typescript ' ];
$ supportsInterfaces = [ ' php ' , ' java ' , ' c# ' , ' typescript ' ];
foreach (Set:: partialIntersection ( 2 , $ staticallyTyped , $ dynamicallyTyped , $ supportsInterfaces ) as $ language ) {
print ( $ language );
}
// c++, java, c#, go, phpทำซ้ำจุดตัดของ m-partial ของ iterables โดยใช้ประเภทการบีบบังคับ
Set::partialIntersectionCoercive(int $minIntersectionCount, iterable ...$iterables)
use IterTools Set ;
$ set1 = [ 1 , 2 , 3 ],
$ set2 = [ ' 2 ' , ' 3 ' , 4 , 5 ],
$ set3 = [ 1 , ' 2 ' ],
foreach (Set:: partialIntersectionCoercive ( 2 , $ set1 , $ set2 , $ set3 ) as $ partiallyCommonNumber ) {
print ( $ partiallyCommonNumber );
}
// 1, 2, 3ทำซ้ำความแตกต่างของสมมาตรของ iterables
Set::symmetricDifference(iterable ...$iterables)
หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎความแตกต่างหลายชุด
use IterTools Set ;
$ a = [ 1 , 2 , 3 , 4 , 7 ];
$ b = [ ' 1 ' , 2 , 3 , 5 , 8 ];
$ c = [ 1 , 2 , 3 , 6 , 9 ];
foreach (Set:: symmetricDifference ( $ a , $ b , $ c ) as $ item ) {
print ( $ item );
}
// 1, 4, 5, 6, 7, 8, 9ทำซ้ำความแตกต่างของสมมาตรของ iterables ด้วยการบีบบังคับประเภท
Set::symmetricDifferenceCoercive(iterable ...$iterables)
หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎความแตกต่างหลายชุด
use IterTools Set ;
$ a = [ 1 , 2 , 3 , 4 , 7 ];
$ b = [ ' 1 ' , 2 , 3 , 5 , 8 ];
$ c = [ 1 , 2 , 3 , 6 , 9 ];
foreach (Set:: symmetricDifferenceCoercive ( $ a , $ b , $ c ) as $ item ) {
print ( $ item );
}
// 4, 5, 6, 7, 8, 9วนซ้ำสหภาพของ iterables
Set::union(iterable ...$iterables)
หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎสหภาพหลายชุด
use IterTools Set ;
$ a = [ 1 , 2 , 3 ];
$ b = [ 3 , 4 ];
$ c = [ 1 , 2 , 3 , 6 , 7 ];
foreach (Set:: union ( $ a , $ b , $ c ) as $ item ) {
print ( $ item );
}
//1, 2, 3, 4, 6, 7วนซ้ำสหภาพของ iterables ด้วยการบีบบังคับประเภท
Set::unionCoercive(iterable ...$iterables)
หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎสหภาพหลายชุด
use IterTools Set ;
$ a = [ ' 1 ' , 2 , 3 ];
$ b = [ 3 , 4 ];
$ c = [ 1 , 2 , 3 , 6 , 7 ];
foreach (Set:: unionCoercive ( $ a , $ b , $ c ) as $ item ) {
print ( $ item );
}
//1, 2, 3, 4, 6, 7 วนซ้ำคอลเลกชันที่จัดเรียงในขณะที่ยังคงความสัมพันธ์ดัชนีคีย์เชื่อมโยง
Sort::sort(iterable $data, callable $comparator = null)
ใช้การเรียงลำดับเริ่มต้นหากไม่มีฟังก์ชั่นเปรียบเทียบตัวเลือก
use IterTools Single ;
$ worldPopulations = [
' China ' => 1_439_323_776 ,
' India ' => 1_380_004_385 ,
' Indonesia ' => 273_523_615 ,
' Pakistan ' => 220_892_340 ,
' USA ' => 331_002_651 ,
];
foreach (Sort:: sort ( $ worldPopulations ) as $ country => $ population ) {
print ( " $ country : $ population " . PHP_EOL );
}
// Pakistan: 220,892,340
// Indonesia: 273,523,615
// USA: 331,002,651
// India: 1,380,004,385
// China: 1,439,323,776ย้ำคอลเลกชันที่จัดเรียง
Sort::sort(iterable $data, callable $comparator = null)
ใช้การเรียงลำดับเริ่มต้นหากไม่มีฟังก์ชั่นเปรียบเทียบตัวเลือก
use IterTools Single ;
$ data = [ 3 , 4 , 5 , 9 , 8 , 7 , 1 , 6 , 2 ];
foreach (Sort:: sort ( $ data ) as $ datum ) {
print ( $ datum );
}
// 1, 2, 3, 4, 5, 6, 7, 8, 9 ย้ำบรรทัดของไฟล์ CSV
File::readCsv(resource $fileHandle, string $separator = ',', string $enclosure = '"', string $escape = '\')
use IterTools File ;
$ fileHandle = fopen ( ' path/to/file.csv ' , ' r ' );
foreach (File:: readCsv ( $ fileHandle ) as $ row ) {
print_r ( $ row );
}
// Each column field is an element of the arrayย้ำบรรทัดของไฟล์
File::readLines(resource $fileHandle)
use IterTools File ;
$ fileHandle = fopen ( ' path/to/file.txt ' , ' r ' );
foreach (File:: readLines ( $ fileHandle ) as $ line ) {
print ( $ line );
}ส่งคืนตัววนซ้ำอิสระ (ซ้ำซ้อน) หลายตัวจากการวนซ้ำครั้งเดียว
Transform::tee(iterable $data, int $count): array
use IterTools Transform ;
$ daysOfWeek = [ ' Mon ' , ' Tues ' , ' Wed ' , ' Thurs ' , ' Fri ' , ' Sat ' , ' Sun ' ];
$ count = 3 ;
[ $ week1 , $ week2 , $ week3 ] = Transform:: tee ( $ data , $ count );
// Each $week contains iterator containing ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']แปลงการทำซ้ำเป็นอาร์เรย์
Transform::toArray(iterable $data): array
use IterTools Transform ;
$ iterator = new ArrayIterator ([ 1 , 2 , 3 , 4 , 5 ]);
$ array = Transform:: toArray ( $ iterator );แปลงการทำซ้ำเป็นอาร์เรย์ที่เชื่อมโยงกัน
Transform::toAssociativeArray(iterable $data, callable $keyFunc = null, callable $valueFunc = null): array
use IterTools Transform ;
$ messages = [ ' message 1 ' , ' message 2 ' , ' message 3 ' ];
$ keyFunc = fn ( $ msg ) => md5 ( $ msg );
$ valueFunc = fn ( $ msg ) => strtoupper ( $ msg );
$ associativeArray = Transform:: toAssociativeArray ( $ messages , $ keyFunc , $ valueFunc );
// [
// '1db65a6a0a818fd39655b95e33ada11d' => 'MESSAGE 1',
// '83b2330607fe8f817ce6d24249dea373' => 'MESSAGE 2',
// '037805d3ad7b10c5b8425427b516b5ce' => 'MESSAGE 3',
// ]แปลงการวนซ้ำเป็นตัววนซ้ำ
Transform::toArray(iterable $data): array
use IterTools Transform ;
$ array = [ 1 , 2 , 3 , 4 , 5 ];
$ iterator = Transform:: toIterator ( $ array );ส่งคืนจริงถ้าองค์ประกอบทั้งหมดตรงกับฟังก์ชั่นเพรดิเคต
Summary::allMatch(iterable $data, callable $predicate): bool
use IterTools Summary ;
$ finalFantasyNumbers = [ 4 , 5 , 6 ];
$ isOnSuperNintendo = fn ( $ ff ) => $ ff >= 4 && $ ff <= 6 ;
$ boolean = Summary:: allMatch ( $ finalFantasyNumbers , $ isOnSuperNintendo );
// true
$ isOnPlaystation = fn ( $ ff ) => $ ff >= 7 && $ ff <= 9 ;
$ boolean = Summary:: allMatch ( $ finalFantasyNumbers , $ isOnPlaystation );
// falseส่งคืนจริงถ้าองค์ประกอบทั้งหมดไม่ซ้ำกัน
Summary::allUnique(iterable $data, bool $strict = true): bool
ค่าเริ่มต้นกับการเปรียบเทียบประเภทที่เข้มงวด กำหนดให้เข้มงวดกับเท็จสำหรับการเปรียบเทียบการบีบบังคับประเภท
use IterTools Summary ;
$ items = [ ' fingerprints ' , ' snowflakes ' , ' eyes ' , ' DNA ' ]
$ boolean = Summary:: allUnique ( $ items );
// trueส่งคืนจริงหากองค์ประกอบใด ๆ ตรงกับฟังก์ชันเพรดิเคต
Summary::anyMatch(iterable $data, callable $predicate): bool
use IterTools Summary ;
$ answers = [ ' fish ' , ' towel ' , 42 , " don't panic " ];
$ isUltimateAnswer = fn ( $ a ) => a == 42 ;
$ boolean = Summary:: anyMatch ( $ answers , $ isUltimateAnswer );
// trueส่งคืนจริงถ้า iterables ทั้งหมดเป็นปริศนาของกันและกัน
Summary::arePermutations(iterable ...$iterables): bool
use IterTools Summary ;
$ iter = [ ' i ' , ' t ' , ' e ' , ' r ' ];
$ rite = [ ' r ' , ' i ' , ' t ' , ' e ' ];
$ reit = [ ' r ' , ' e ' , ' i ' , ' t ' ];
$ tier = [ ' t ' , ' i ' , ' e ' , ' r ' ];
$ tire = [ ' t ' , ' i ' , ' r ' , ' e ' ];
$ trie = [ ' t ' , ' r ' , ' i ' , ' e ' ];
$ boolean = Summary:: arePermutations ( $ iter , $ rite , $ reit , $ tier , $ tire , $ trie );
// trueส่งคืนจริงถ้า iterables ทั้งหมดเป็นแบบแทรกซึมของกันและกันด้วยการบีบบังคับประเภท
Summary::arePermutationsCoercive(iterable ...$iterables): bool
use IterTools Summary ;
$ set1 = [ 1 , 2.0 , ' 3 ' ];
$ set2 = [ 2.0 , ' 1 ' , 3 ];
$ set3 = [ 3 , 2 , 1 ];
$ boolean = Summary:: arePermutationsCoercive ( $ set1 , $ set2 , $ set3 );
// trueส่งคืนจริงถ้ารายการ N นั้นเป็นจริงตามฟังก์ชันเพรดิเคต
Summary::exactlyN(iterable $data, int $n, callable $predicate): bool
use IterTools Summary ;
$ twoTruthsAndALie = [ true , true , false ];
$ n = 2 ;
$ boolean = Summary:: exactlyN ( $ twoTruthsAndALie , $ n );
// true
$ ages = [ 18 , 21 , 24 , 54 ];
$ n = 4 ;
$ predicate = fn ( $ age ) => $ age >= 21 ;
$ boolean = Summary:: exactlyN ( $ ages , $ n , $ predicate );
// falseส่งคืนจริงหากการวนซ้ำว่างเปล่าไม่มีรายการ
Summary::isEmpty(iterable $data): bool
use IterTools Summary ;
$ data = []
$ boolean = Summary:: isEmpty ( $ data );
// trueส่งคืนจริงถ้าองค์ประกอบทั้งหมดของคอลเลกชันที่ได้รับซึ่งเป็นไปตามเพรดิเคตปรากฏขึ้นก่อนองค์ประกอบทั้งหมดที่ไม่ได้
Summary::isPartitioned(iterable $data, callable $predicate = null): bool
use IterTools Summary ;
$ numbers = [ 0 , 2 , 4 , 1 , 3 , 5 ];
$ evensBeforeOdds = fn ( $ item ) => $ item % 2 === 0 ;
$ boolean = Summary:: isPartitioned ( $ numbers , $ evensBeforeOdds );ส่งคืนจริงถ้าองค์ประกอบถูกจัดเรียงมิฉะนั้นเท็จ
Summary::isSorted(iterable $data): bool
use IterTools Summary ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 ];
$ boolean = Summary:: isSorted ( $ numbers );
// true
$ numbers = [ 3 , 2 , 3 , 4 , 5 ];
$ boolean = Summary:: isSorted ( $ numbers );
// falseส่งคืนจริงถ้าองค์ประกอบถูกเรียงลำดับย้อนกลับมิฉะนั้นเท็จ
Summary::isReversed(iterable $data): bool
use IterTools Summary ;
$ numbers = [ 5 , 4 , 3 , 2 , 1 ];
$ boolean = Summary:: isReversed ( $ numbers );
// true
$ numbers = [ 1 , 4 , 3 , 2 , 1 ];
$ boolean = Summary:: isReversed ( $ numbers );
// falseส่งคืนจริงหากไม่มีองค์ประกอบตรงกับฟังก์ชันเพรดิเคต
Summary::noneMatch(iterable $data, callable $predicate): bool
use IterTools Summary ;
$ grades = [ 45 , 50 , 61 , 0 ];
$ isPassingGrade = fn ( $ grade ) => $ grade >= 70 ;
$ boolean = Summary:: noneMatch ( $ grades , $ isPassingGrade );
// trueส่งคืนจริงถ้าคอลเลกชันที่ได้รับทั้งหมดเหมือนกัน
สำหรับรายการ iterables แบบ iterable หรือว่างเปล่าเดียวจะส่งคืนจริง
Summary::same(iterable ...$iterables): bool
use IterTools Summary ;
$ cocaColaIngredients = [ ' carbonated water ' , ' sugar ' , ' caramel color ' , ' phosphoric acid ' ];
$ pepsiIngredients = [ ' carbonated water ' , ' sugar ' , ' caramel color ' , ' phosphoric acid ' ];
$ boolean = Summary:: same ( $ cocaColaIngredients , $ pepsiIngredients );
// true
$ cocaColaIngredients = [ ' carbonated water ' , ' sugar ' , ' caramel color ' , ' phosphoric acid ' ];
$ spriteIngredients = [ ' carbonated water ' , ' sugar ' , ' citric acid ' , ' lemon lime flavorings ' ];
$ boolean = Summary:: same ( $ cocaColaIngredients , $ spriteIngredients );
// falseส่งคืนจริงถ้าคอลเลกชันที่ได้รับทั้งหมดมีความยาวเท่ากัน
สำหรับรายการ iterables แบบ iterable หรือว่างเปล่าเดียวจะส่งคืนจริง
Summary::sameCount(iterable ...$iterables): bool
use IterTools Summary ;
$ prequels = [ ' Phantom Menace ' , ' Attack of the Clones ' , ' Revenge of the Sith ' ];
$ originals = [ ' A New Hope ' , ' Empire Strikes Back ' , ' Return of the Jedi ' ];
$ sequels = [ ' The Force Awakens ' , ' The Last Jedi ' , ' The Rise of Skywalker ' ];
$ boolean = Summary:: sameCount ( $ prequels , $ originals , $ sequels );
// true
$ batmanMovies = [ ' Batman Begins ' , ' The Dark Knight ' , ' The Dark Knight Rises ' ];
$ matrixMovies = [ ' The Matrix ' , ' The Matrix Reloaded ' , ' The Matrix Revolutions ' , ' The Matrix Resurrections ' ];
$ result = Summary:: sameCount ( $ batmanMovies , $ matrixMovies );
// false ลดค่าเฉลี่ย
ส่งคืน null ถ้าคอลเลกชันว่างเปล่า
Reduce::toAverage(iterable $data): float
use IterTools Reduce ;
$ grades = [ 100 , 90 , 95 , 85 , 94 ];
$ finalGrade = Reduce:: toAverage ( $ numbers );
// 92.8ลดความยาวของมัน
Reduce::toCount(iterable $data): int
use IterTools Reduce ;
$ someIterable = ImportantThing:: getCollectionAsIterable ();
$ length = Reduce:: toCount ( $ someIterable );
// 3ลดการติดตั้งเป็นองค์ประกอบแรก
Reduce::toFirst(iterable $data): mixed
พ่น LengthException ถ้าคอลเลกชันว่างเปล่า
use IterTools Reduce ;
$ medals = [ ' gold ' , ' silver ' , ' bronze ' ];
$ first = Reduce:: toFirst ( $ medals );
// goldลดการทำซ้ำเป็นองค์ประกอบแรกและสุดท้าย
Reduce::toFirstAndLast(iterable $data): array{mixed, mixed}
พ่น LengthException ถ้าคอลเลกชันว่างเปล่า
use IterTools Reduce ;
$ weekdays = [ ' Monday ' , ' Tuesday ' , ' Wednesday ' , ' Thursday ' , ' Friday ' ];
$ firstAndLast = Reduce:: toFirstAndLast ( $ weekdays );
// [Monday, Friday]ลดการทำซ้ำเป็นองค์ประกอบสุดท้าย
Reduce::toLast(iterable $data): mixed
พ่น LengthException ถ้าคอลเลกชันว่างเปล่า
use IterTools Reduce ;
$ gnomesThreePhasePlan = [ ' Collect underpants ' , ' ? ' , ' Profit ' ];
$ lastPhase = Reduce:: toLast ( $ gnomesThreePhasePlan );
// Profitลดค่าสูงสุด
Reduce::toMax(iterable $data, callable $compareBy = null): mixed|null
$compareBy ต้องส่งคืนค่าที่เปรียบเทียบได้$compareBy รายการของคอลเลกชันที่กำหนดจะต้องเปรียบเทียบได้ use IterTools Reduce ;
$ numbers = [ 5 , 3 , 1 , 2 , 4 ];
$ result = Reduce:: toMax ( $ numbers );
// 5
$ movieRatings = [
[
' title ' => ' Star Wars: Episode IV - A New Hope ' ,
' rating ' => 4.6
],
[
' title ' => ' Star Wars: Episode V - The Empire Strikes Back ' ,
' rating ' => 4.8
],
[
' title ' => ' Star Wars: Episode VI - Return of the Jedi ' ,
' rating ' => 4.6
],
];
$ compareBy = fn ( $ movie ) => $ movie [ ' rating ' ];
$ highestRatedMovie = Reduce:: toMax ( $ movieRatings , $ compareBy );
// [
// 'title' => 'Star Wars: Episode V - The Empire Strikes Back',
// 'rating' => 4.8
// ];ลดค่าขั้นต่ำ
Reduce::toMin(iterable $data, callable $compareBy = null): mixed|null
$compareBy ต้องส่งคืนค่าที่เปรียบเทียบได้$compareBy รายการของคอลเลกชันที่กำหนดจะต้องเปรียบเทียบได้ use IterTools Reduce ;
$ numbers = [ 5 , 3 , 1 , 2 , 4 ];
$ result = Reduce:: toMin ( $ numbers );
// 1
$ movieRatings = [
[
' title ' => ' The Matrix ' ,
' rating ' => 4.7
],
[
' title ' => ' The Matrix Reloaded ' ,
' rating ' => 4.3
],
[
' title ' => ' The Matrix Revolutions ' ,
' rating ' => 3.9
],
[
' title ' => ' The Matrix Resurrections ' ,
' rating ' => 2.5
],
];
$ compareBy = fn ( $ movie ) => $ movie [ ' rating ' ];
$ lowestRatedMovie = Reduce:: toMin ( $ movieRatings , $ compareBy );
// [
// 'title' => 'The Matrix Resurrections',
// 'rating' => 2.5
// ]ลดลงเป็นอาร์เรย์ของขอบเขตบนและล่าง (สูงสุดและนาที)
Reduce::toMinMax(iterable $numbers, callable $compareBy = null): array
$compareBy ต้องส่งคืนค่าที่เปรียบเทียบได้$compareBy รายการของคอลเลกชันที่กำหนดจะต้องเปรียบเทียบได้[null, null] หากคอลเลกชันที่ได้รับนั้นว่างเปล่า use IterTools Reduce ;
$ numbers = [ 1 , 2 , 7 , - 1 , - 2 , - 3 ];
[ $ min , $ max ] = Reduce:: toMinMax ( $ numbers );
// [-3, 7]
$ reportCard = [
[
' subject ' => ' history ' ,
' grade ' => 90
],
[
' subject ' => ' math ' ,
' grade ' => 98
],
[
' subject ' => ' science ' ,
' grade ' => 92
],
[
' subject ' => ' english ' ,
' grade ' => 85
],
[
' subject ' => ' programming ' ,
' grade ' => 100
],
];
$ compareBy = fn ( $ class ) => $ class [ ' grade ' ];
$ bestAndWorstSubject = Reduce:: toMinMax ( $ reportCard , $ compareBy );
// [
// [
// 'subject' => 'english',
// 'grade' => 85
// ],
// [
// 'subject' => 'programming',
// 'grade' => 100
// ],
// ]ลดมูลค่าที่ตำแหน่งที่ n
Reduce::toNth(iterable $data, int $position): mixed
use IterTools Reduce ;
$ lotrMovies = [ ' The Fellowship of the Ring ' , ' The Two Towers ' , ' The Return of the King ' ];
$ rotk = Reduce:: toNth ( $ lotrMovies , 2 );
// 20ลดลงไปที่ผลิตภัณฑ์ขององค์ประกอบ
ส่งคืน null ถ้าคอลเลกชันว่างเปล่า
Reduce::toProduct(iterable $data): number|null
use IterTools Reduce ;
$ primeFactors = [ 5 , 2 , 2 ];
$ number = Reduce:: toProduct ( $ primeFactors );
// 20ลดคอลเลกชันที่กำหนดให้เป็นค่าสุ่มภายใน
Reduce::toRandomValue(iterable $data): mixed
use IterTools Reduce ;
$ sfWakeupOptions = [ ' mid ' , ' low ' , ' overhead ' , ' throw ' , ' meaty ' ];
$ wakeupOption = Reduce:: toRandomValue ( $ sfWakeupOptions );
// e.g., throwลดคอลเลกชันที่กำหนดให้อยู่ในช่วง (ความแตกต่างระหว่างสูงสุดและนาที)
Reduce::toRange(iterable $numbers): int|float
ส่งคืน 0 หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Reduce ;
$ grades = [ 100 , 90 , 80 , 85 , 95 ];
$ range = Reduce:: toRange ( $ numbers );
// 20ลดลงเป็นสตริงที่เข้าร่วมองค์ประกอบทั้งหมด
Reduce::toString(iterable $data, string $separator = '', string $prefix = '', string $suffix = ''): string
use IterTools Reduce ;
$ words = [ ' IterTools ' , ' PHP ' , ' v1.0 ' ];
$ string = Reduce:: toString ( $ words );
// IterToolsPHPv1.0
$ string = Reduce:: toString ( $ words , ' - ' );
// IterTools-PHP-v1.0
$ string = Reduce:: toString ( $ words , ' - ' , ' Library: ' );
// Library: IterTools-PHP-v1.0
$ string = Reduce:: toString ( $ words , ' - ' , ' Library: ' , ' ! ' );
// Library: IterTools-PHP-v1.0!ลดผลรวมขององค์ประกอบ
Reduce::toSum(iterable $data): number
use IterTools Reduce ;
$ parts = [ 10 , 20 , 30 ];
$ sum = Reduce:: toSum ( $ parts );
// 60ลดองค์ประกอบเป็นค่าเดียวโดยใช้ฟังก์ชั่นตัวลด
Reduce::toValue(iterable $data, callable $reducer, mixed $initialValue): mixed
use IterTools Reduce ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ sum = fn ( $ carry , $ item ) => $ carry + $ item ;
$ result = Reduce:: toValue ( $ input , $ sum , 0 );
// 15 สตรีมให้อินเทอร์เฟซที่คล่องแคล่วในการแปลงอาร์เรย์และ iTerables ผ่านการดำเนินการ
ลำธารประกอบด้วย:
$ result = Stream:: of ([ 1 , 1 , 2 , 2 , 3 , 4 , 5 ])
-> distinct () // [1, 2, 3, 4, 5]
-> map ( fn ( $ x ) => $ x ** 2 ) // [1, 4, 9, 16, 25]
-> filter ( fn ( $ x ) => $ x < 10 ) // [1, 4, 9]
-> toSum (); // 14foreach $ result = Stream:: of ([ 1 , 1 , 2 , 2 , 3 , 4 , 5 ])
-> distinct () // [1, 2, 3, 4, 5]
-> map ( fn ( $ x ) => $ x ** 2 ) // [1, 4, 9, 16, 25]
-> filter ( fn ( $ x ) => $ x < 10 ); // [1, 4, 9]
foreach ( $ result as $ item ) {
// 1, 4, 9
}สร้างสตรีมจากการวนซ้ำ
Stream::of(iterable $iterable): Stream
use IterTools Stream ;
$ iterable = [ 1 , 2 , 3 ];
$ result = Stream:: of ( $ iterable )
-> chainWith ([ 4 , 5 , 6 ], [ 7 , 8 , 9 ])
-> zipEqualWith ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])
-> toValue ( fn ( $ carry , $ item ) => $ carry + array_sum ( $ item ));
// 90 สร้างกระแสของ N สุ่มเหรียญพลิก
Stream::ofCoinFlips(int $repetitions): Stream
use IterTools Stream ;
$ result = Stream:: ofCoinFlips ( 10 )
-> filterTrue ()
-> toCount ();
// 5 (random) สร้างสตรีมของแถวของไฟล์ CSV
Stream::ofCsvFile(resource $fileHandle, string $separator = ',', string $enclosure = '"', string = $escape = '\'): Stream
use IterTools Stream ;
$ fileHandle = fopen ( ' path/to/file.csv ' , ' r ' );
$ result = Stream:: of ( $ fileHandle )
-> toArray ();สร้างกระแสไม่มีอะไร
Stream::ofEmpty(): Stream
use IterTools Stream ;
$ result = Stream:: ofEmpty ()
-> chainWith ([ 1 , 2 , 3 ])
-> toArray ();
// 1, 2, 3 สร้างสตรีมของบรรทัดของไฟล์
Stream::ofFileLines(resource $fileHandle): Stream
use IterTools Stream ;
$ fileHandle = fopen ( ' path/to/file.txt ' , ' r ' );
$ result = Stream:: of ( $ fileHandle )
-> map ( ' strtoupper ' );
-> toArray ();สร้างสตรีมของการเลือกแบบสุ่มจากอาร์เรย์ของค่า
Stream::ofRandomChoice(array $items, int $repetitions): Stream
use IterTools Stream ;
$ languages = [ ' PHP ' , ' Go ' , ' Python ' ];
$ languages = Stream:: ofRandomChoice ( $ languages , 5 )
-> toArray ();
// 'Go', 'PHP', 'Python', 'PHP', 'PHP' (random) สร้างกระแสของตัวเลขสุ่ม (จำนวนเต็ม)
Stream::ofRandomNumbers(int $min, int $max, int $repetitions): Stream
use IterTools Stream ;
$ min = 1 ;
$ max = 3 ;
$ reps = 7 ;
$ result = Stream:: ofRandomNumbers ( $ min , $ max , $ reps )
-> toArray ();
// 1, 2, 2, 1, 3, 2, 1 (random) สร้างกระแสของเปอร์เซ็นต์สุ่มระหว่าง 0 ถึง 1
Stream::ofRandomPercentage(int $repetitions): Stream
use IterTools Stream ;
$ stream = Stream:: ofRandomPercentage ( 3 )
-> toArray ();
// 0.8012566976245, 0.81237281724151, 0.61676896329459 [random] สร้างกระแสของช่วงของตัวเลข
Stream::ofRange(int|float $start, int|float $end, int|float $step = 1): Stream
use IterTools Stream ;
$ numbers = Stream:: ofRange ( 0 , 5 )
-> toArray ();
// 0, 1, 2, 3, 4, 5 สร้างกระแสของมือรองกระดาษหิน
Stream::ofRockPaperScissors(int $repetitions): Stream
use IterTools Stream ;
$ rps = Stream:: ofRockPaperScissors ( 5 )
-> toArray ();
// 'paper', 'rock', 'rock', 'scissors', 'paper' [random]เรียงลำดับสตรีมการบำรุงรักษาคีย์
$stream->asort(callable $comparator = null)
หากไม่ได้ให้ตัวเปรียบเทียบองค์ประกอบของแหล่งที่มาที่วนซ้ำจะต้องเปรียบเทียบได้
use IterTools Stream ;
$ worldPopulations = [
' China ' => 1_439_323_776 ,
' India ' => 1_380_004_385 ,
' Indonesia ' => 273_523_615 ,
' USA ' => 331_002_651 ,
];
$ result = Stream:: of ( $ worldPopulations )
-> filter ( fn ( $ pop ) => $ pop > 300_000_000 )
-> asort ()
-> toAssociativeArray ();
// USA => 331_002_651,
// India => 1_380_004_385,
// China => 1_439_323_776, ส่งคืนสตรีมการผูกมัดแหล่งข้อมูลเพิ่มเติมเข้าด้วยกันเป็นกระแสติดต่อกันเพียงครั้งเดียว
$stream->chainWith(iterable ...$iterables): Stream
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ result = Stream:: of ( $ input )
-> chainWith ([ 4 , 5 , 6 ])
-> chainWith ([ 7 , 8 , 9 ])
-> toArray ();
// 1, 2, 3, 4, 5, 6, 7, 8, 9 บีบอัดเป็นสตรีมใหม่โดยการกรองข้อมูลที่ไม่ได้เลือก
$stream->compress(iterable $selectors): Stream
ตัวเลือกระบุว่าข้อมูลใด ค่าจริงเลือกรายการ ค่าเท็จกรองข้อมูล
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ result = Stream:: of ( $ input )
-> compress ([ 0 , 1 , 1 ])
-> toArray ();
// 2, 3 บีบอัดเป็นสตรีมใหม่โดยการกรองปุ่มที่ไม่ได้เลือก
$stream->compressAssociative(array $keys): Stream
use IterTools Stream ;
$ starWarsEpisodes = [
' I ' => ' The Phantom Menace ' ,
' II ' => ' Attack of the Clones ' ,
' III ' => ' Revenge of the Sith ' ,
' IV ' => ' A New Hope ' ,
' V ' => ' The Empire Strikes Back ' ,
' VI ' => ' Return of the Jedi ' ,
' VII ' => ' The Force Awakens ' ,
' VIII ' => ' The Last Jedi ' ,
' IX ' => ' The Rise of Skywalker ' ,
];
$ sequelTrilogyNumbers = [ ' VII ' , ' VIII ' , ' IX ' ];
$ sequelTrilogy = Stream:: of ( $ starWarsEpisodes )
-> compressAssociative ( $ sequelTrilogyNumbers )
-> toAssociativeArray ();
// 'VII' => 'The Force Awakens',
// 'VIII' => 'The Last Jedi',
// 'IX' => 'The Rise of Skywalker', ส่งคืนสตรีมซึ่งประกอบด้วยชิ้นส่วนขององค์ประกอบจากสตรีม
$stream->chunkwise(int $chunkSize): Stream
ขนาดก้อนต้องมีอย่างน้อย 1
use IterTools Stream ;
$ friends = [ ' Ross ' , ' Rachel ' , ' Chandler ' , ' Monica ' , ' Joey ' ];
$ result = Stream:: of ( $ friends )
-> chunkwise ( 2 )
-> toArray ();
// ['Ross', 'Rachel'], ['Chandler', 'Monica'], ['Joey'] ส่งคืนสตรีมซึ่งประกอบด้วยชิ้นส่วนที่ทับซ้อนกันขององค์ประกอบจากสตรีม
$stream->chunkwiseOverlap(int $chunkSize, int $overlapSize, bool $includeIncompleteTail = true): Stream
use IterTools Stream ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ];
$ result = Stream:: of ( $ friends )
-> chunkwiseOverlap ( 3 , 1 )
-> toArray ()
// [1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9] ส่งคืนสตรีมการกรององค์ประกอบจากสตรีมเท่านั้นที่ส่งคืนองค์ประกอบที่แตกต่างกันเท่านั้น
$stream->distinct(bool $strict = true): Stream
ค่าเริ่มต้นกับการเปรียบเทียบประเภทที่เข้มงวด กำหนดให้เข้มงวดกับเท็จสำหรับการเปรียบเทียบการบีบบังคับประเภท
use IterTools Stream ;
$ input = [ 1 , 2 , 1 , 2 , 3 , 3 , ' 1 ' , ' 1 ' , ' 2 ' , ' 3 ' ];
$ stream = Stream:: of ( $ input )
-> distinct ()
-> toArray ();
// 1, 2, 3, '1', '2', '3'
$ stream = Stream:: of ( $ input )
-> distinct ( false )
-> toArray ();
// 1, 2, 3 ส่งคืนองค์ประกอบการกรองสตรีมออกจากสตรีมเท่านั้นที่ส่งคืนองค์ประกอบที่แตกต่างกันตามฟังก์ชันเปรียบเทียบที่กำหนดเอง
$stream->distinctBy(callable $compareBy): Stream
use IterTools Stream ;
$ streetFighterConsoleReleases = [
[ ' id ' => ' 112233 ' , ' name ' => ' Street Fighter 3 3rd Strike ' , ' console ' => ' Dreamcast ' ],
[ ' id ' => ' 223344 ' , ' name ' => ' Street Fighter 3 3rd Strike ' , ' console ' => ' PS4 ' ],
[ ' id ' => ' 334455 ' , ' name ' => ' Street Fighter 3 3rd Strike ' , ' console ' => ' PS5 ' ],
[ ' id ' => ' 445566 ' , ' name ' => ' Street Fighter VI ' , ' console ' => ' PS4 ' ],
[ ' id ' => ' 556677 ' , ' name ' => ' Street Fighter VI ' , ' console ' => ' PS5 ' ],
[ ' id ' => ' 667799 ' , ' name ' => ' Street Fighter VI ' , ' console ' => ' PC ' ],
];
$ stream = Stream:: of ( $ streetFighterConsoleReleases )
-> distinctBy ( fn ( $ sfTitle ) => $ sfTitle [ ' name ' ])
-> toArray ();
// Contains one SF3 3rd Strike entry and one SFVI entry ลดองค์ประกอบจากสตรีมในขณะที่ฟังก์ชั่นเพรดิเคตเป็นจริง
$stream->dropWhile(callable $predicate): Stream
เมื่อฟังก์ชั่นเพรดิเคตส่งกลับเท็จหนึ่งครั้งองค์ประกอบที่เหลือทั้งหมดจะถูกส่งคืน
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ]
$ result = Stream:: of ( $ input )
-> dropWhile ( fn ( $ value ) => $ value < 3 )
-> toArray ();
// 3, 4, 5 กรององค์ประกอบจากสตรีมเฉพาะการรักษาองค์ประกอบที่มีฟังก์ชั่นเพรดิเคตเป็นจริง
$stream->filter(callable $predicate): Stream
use IterTools Stream ;
$ input = [ 1 , - 1 , 2 , - 2 , 3 , - 3 ];
$ result = Stream:: of ( $ input )
-> filter ( fn ( $ value ) => $ value > 0 )
-> toArray ();
// 1, 2, 3 กรององค์ประกอบจากสตรีมการรักษาองค์ประกอบที่เป็นความจริงเท่านั้น
$stream->filterTrue(): Stream
use IterTools Stream ;
$ input = [ 0 , 1 , 2 , 3 , 0 , 4 ];
$ result = Stream:: of ( $ input )
-> filterTrue ()
-> toArray ();
// 1, 2, 3, 4 กรององค์ประกอบจากสตรีมการรักษาองค์ประกอบที่เป็นเท็จเท่านั้น
$stream->filterFalse(): Stream
use IterTools Stream ;
$ input = [ 0 , 1 , 2 , 3 , 0 , 4 ];
$ result = Stream:: of ( $ input )
-> filterFalse ()
-> toArray ();
// 0, 0 ตัวกรององค์ประกอบจากสตรีมการรักษาองค์ประกอบเฉพาะที่ฟังก์ชั่นเพรดิเคตบนปุ่มเป็นจริง
$stream->filterKeys(callable $filter): Stream
$ olympics = [
2000 => ' Sydney ' ,
2002 => ' Salt Lake City ' ,
2004 => ' Athens ' ,
2006 => ' Turin ' ,
2008 => ' Beijing ' ,
2010 => ' Vancouver ' ,
2012 => ' London ' ,
2014 => ' Sochi ' ,
2016 => ' Rio de Janeiro ' ,
2018 => ' Pyeongchang ' ,
2020 => ' Tokyo ' ,
2022 => ' Beijing ' ,
];
$ winterFilter = fn ( $ year ) => $ year % 4 === 2 ;
$ result = Stream:: of ( $ olympics )
-> filterKeys ( $ winterFilter )
-> toAssociativeArray ();
}
// 2002 => Salt Lake City
// 2006 => Turin
// 2010 => Vancouver
// 2014 => Sochi
// 2018 => Pyeongchang
// 2022 => Beijing แมปฟังก์ชั่นลงบนองค์ประกอบของสตรีมและทำให้ผลลัพธ์แบน
$stream->flatMap(callable $mapper): Stream
$ data = [ 1 , 2 , 3 , 4 , 5 ];
$ mapper fn ( $ item ) => ( $ item % 2 === 0 ) ? [ $ item , $ item ] : $ item ;
$ result = Stream:: of ( $ data )
-> flatMap ( $ mapper )
-> toArray ();
// [1, 2, 2, 3, 4, 4, 5] แบนกระแสหลายมิติ
$stream->flatten(int $dimensions = 1): Stream
$ data = [ 1 , [ 2 , 3 ], [ 4 , 5 ]];
$ result = Stream:: of ( $ data )
-> flatten ( $ mapper )
-> toArray ();
// [1, 2, 3, 4, 5] การกระจายความถี่ขององค์ประกอบสตรีม
$stream->frequencies(bool $strict = true): Stream
use IterTools Stream ;
$ grades = [ ' A ' , ' A ' , ' B ' , ' B ' , ' B ' , ' C ' ];
$ result = Stream:: of ( $ grades )
-> frequencies ()
-> toAssociativeArray ();
// ['A' => 2, 'B' => 3, 'C' => 1] ส่งคืนการจัดกลุ่มสตรีมโดยองค์ประกอบข้อมูลทั่วไป
$stream->groupBy(callable $groupKeyFunction, callable $itemKeyFunction = null): Stream
$groupKeyFunction กำหนดคีย์ในการจัดกลุ่มองค์ประกอบโดย$itemKeyFunction อนุญาตให้ดัชนีที่กำหนดเองภายในสมาชิกแต่ละกลุ่ม use IterTools Stream ;
$ input = [ 1 , - 1 , 2 , - 2 , 3 , - 3 ];
$ groups = Stream:: of ( $ input )
-> groupBy ( fn ( $ item ) => $ item > 0 ? ' positive ' : ' negative ' );
foreach ( $ groups as $ group => $ item ) {
// 'positive' => [1, 2, 3], 'negative' => [-1, -2, -3]
}ส่งคืนสตรีมปั่นจักรยานผ่านองค์ประกอบของสตรีมตามลำดับตลอดไป
$stream->infiniteCycle(): Stream
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ result = Stream:: of ( $ input )
-> infiniteCycle ()
-> print ();
// 1, 2, 3, 1, 2, 3, ... ส่งคืนสตรีมที่ตัดกันสตรีมด้วยอินพุต iTerables
$stream->intersectionWith(iterable ...$iterables): Stream
use IterTools Stream ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ];
$ numerics = [ ' 1 ' , ' 2 ' , 3 , 4 , 5 , 6 , 7 , ' 8 ' , ' 9 ' ];
$ oddNumbers = [ 1 , 3 , 5 , 7 , 9 , 11 ];
$ stream = Stream:: of ( $ numbers )
-> intersectionWith ( $ numerics , $ oddNumbers )
-> toArray ();
// 3, 5, 7 ส่งคืนสตรีมที่ตัดกันสตรีมด้วยอินพุต iterables โดยใช้ประเภทการบีบบังคับ
$stream->intersectionCoerciveWith(iterable ...$iterables): Stream
use IterTools Stream ;
$ languages = [ ' php ' , ' python ' , ' c++ ' , ' java ' , ' c# ' , ' javascript ' , ' typescript ' ];
$ scriptLanguages = [ ' php ' , ' python ' , ' javascript ' , ' typescript ' ];
$ supportsInterfaces = [ ' php ' , ' java ' , ' c# ' , ' typescript ' ];
$ stream = Stream:: of ( $ languages )
-> intersectionCoerciveWith ( $ scriptLanguages , $ supportsInterfaces )
-> toArray ();
// 'php', 'typescript' ส่งคืนสตรีมถึงขีด จำกัด
หยุดแม้ว่าจะมีข้อมูลมากขึ้นหากถึงขีด จำกัด
$stream->limit(int $limit): Stream
Use IterTools Single ;
$ matrixMovies = [ ' The Matrix ' , ' The Matrix Reloaded ' , ' The Matrix Revolutions ' , ' The Matrix Resurrections ' ];
$ limit = 1 ;
$ goodMovies = Stream:: of ( $ matrixMovies )
-> limit ( $ limit )
-> toArray ();
// 'The Matrix' (and nothing else) ส่งคืนสตรีมที่มีผลลัพธ์ของการแมปฟังก์ชั่นไปยังแต่ละองค์ประกอบของสตรีม
$stream->map(callable $function): Stream
use IterTools Stream ;
$ grades = [ 100 , 95 , 98 , 89 , 100 ];
$ result = Stream:: of ( $ grades )
-> map ( fn ( $ grade ) => $ grade === 100 ? ' A ' : ' F ' )
-> toArray ();
// A, F, F, F, A ส่งคืนสตรีมประกอบด้วยองค์ประกอบคู่จากสตรีม
$stream->pairwise(): Stream
ส่งคืนสตรีมที่ว่างเปล่าหากคอลเลกชันที่ได้รับมีน้อยกว่า 2 องค์ประกอบ
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ stream = Stream:: of ( $ input )
-> pairwise ()
-> toArray ();
// [1, 2], [2, 3], [3, 4], [4, 5] ส่งคืนสตรีมบางส่วนที่ตัดกันสตรีมด้วยอินพุต iterables
$stream->partialIntersectionWith(int $minIntersectionCount, iterable ...$iterables): Stream
use IterTools Stream ;
$ numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ];
$ numerics = [ ' 1 ' , ' 2 ' , 3 , 4 , 5 , 6 , 7 , ' 8 ' , ' 9 ' ];
$ oddNumbers = [ 1 , 3 , 5 , 7 , 9 , 11 ];
$ stream = Stream:: of ( $ numbers )
-> partialIntersectionWith ( $ numerics , $ oddNumbers )
-> toArray ();
// 1, 3, 4, 5, 6, 7, 9 ส่งคืนสตรีมบางส่วนที่ตัดกันสตรีมด้วยอินพุต iterables โดยใช้ประเภทการบีบบังคับ
$stream->partialIntersectionCoerciveWith(int $minIntersectionCount, iterable ...$iterables): Stream
use IterTools Stream ;
$ languages = [ ' php ' , ' python ' , ' c++ ' , ' java ' , ' c# ' , ' javascript ' , ' typescript ' ];
$ scriptLanguages = [ ' php ' , ' python ' , ' javascript ' , ' typescript ' ];
$ supportsInterfaces = [ ' php ' , ' java ' , ' c# ' , ' typescript ' ];
$ stream = Stream:: of ( $ languages )
-> partialIntersectionCoerciveWith ( 2 , $ scriptLanguages , $ supportsInterfaces )
-> toArray ();
// 'php', 'python', 'java', 'typescript', 'c#', 'javascript' ส่งคืนสตรีมใหม่ขององค์ประกอบคีย์-ค่าที่ได้รับการจัดอันดับใหม่โดยฟังก์ชั่นตัวทำดัชนีคีย์
$stream->reindex(callable $indexer): Stream
use IterTools Stream ;
$ data = [
[
' title ' => ' Star Wars: Episode IV – A New Hope ' ,
' episode ' => ' IV ' ,
' year ' => 1977 ,
],
[
' title ' => ' Star Wars: Episode V – The Empire Strikes Back ' ,
' episode ' => ' V ' ,
' year ' => 1980 ,
],
[
' title ' => ' Star Wars: Episode VI – Return of the Jedi ' ,
' episode ' => ' VI ' ,
' year ' => 1983 ,
],
];
$ reindexFunc = fn ( array $ swFilm ) => $ swFilm [ ' episode ' ];
$ reindexResult = Stream:: of ( $ data )
-> reindex ( $ reindexFunc )
-> toAssociativeArray ();
// [
// 'IV' => [
// 'title' => 'Star Wars: Episode IV – A New Hope',
// 'episode' => 'IV',
// 'year' => 1977,
// ],
// 'V' => [
// 'title' => 'Star Wars: Episode V – The Empire Strikes Back',
// 'episode' => 'V',
// 'year' => 1980,
// ],
// 'VI' => [
// 'title' => 'Star Wars: Episode VI – Return of the Jedi',
// 'episode' => 'VI',
// 'year' => 1983,
// ],
// ] การกระจายความถี่สัมพัทธ์ขององค์ประกอบสตรีม
$stream->relativeFrequencies(bool $strict = true): Stream
use IterTools Stream ;
$ grades = [ ' A ' , ' A ' , ' B ' , ' B ' , ' B ' , ' C ' ];
$ result = Stream:: of ( $ grades )
-> relativeFrequencies ()
-> toAssociativeArray ();
// A => 0.33, B => 0.5, C => 0.166 ย้อนกลับองค์ประกอบของกระแส
$stream->reverse(): Stream
use IterTools Stream ;
$ words = [ ' are ' , ' you ' , ' as ' , ' bored ' , ' as ' , ' I ' , ' am ' ];
$ reversed = Stream:: of ( $ words )
-> reverse ()
-> toString ( ' ' );
// am I as bored as you are ส่งคืนสตรีมที่สะสมค่าเฉลี่ยที่กำลังทำงานอยู่เหนือสตรีม
$stream->runningAverage(int|float|null $initialValue = null): Stream
use IterTools Stream ;
$ input = [ 1 , 3 , 5 ];
$ result = Stream:: of ( $ input )
-> runningAverage ()
-> toArray ();
// 1, 2, 3 ส่งคืนสตรีมที่สะสมความแตกต่างของการทำงานผ่านสตรีม
$stream->runningDifference(int|float|null $initialValue = null): Stream
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ input )
-> runningDifference ()
-> toArray ();
// -1, -3, -6, -10, -15 ส่งคืนสตรีมที่สะสมสูงสุดที่กำลังทำงานอยู่เหนือสตรีม
$stream->runningMax(int|float|null $initialValue = null): Stream
use IterTools Stream ;
$ input = [ 1 , - 1 , 2 , - 2 , 3 , - 3 ];
$ result = Stream:: of ( $ input )
-> runningMax ()
-> toArray ();
// 1, 1, 2, 2, 3, 3 ส่งคืนสตรีมที่สะสมขั้นต่ำผ่านสตรีม
$stream->runningMin(int|float|null $initialValue = null): Stream
use IterTools Stream ;
$ input = [ 1 , - 1 , 2 , - 2 , 3 , - 3 ];
$ result = Stream:: of ( $ input )
-> runningMin ()
-> toArray ();
// 1, -1, -1, -2, -2, -3 ส่งคืนสตรีมที่สะสมผลิตภัณฑ์ที่ทำงานผ่านสตรีม
$stream->runningProduct(int|float|null $initialValue = null): Stream
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ input )
-> runningProduct ()
-> toArray ();
// 1, 2, 6, 24, 120 ส่งคืนสตรีมที่สะสมรวมการทำงานทั้งหมดผ่านสตรีม
$stream->runningTotal(int|float|null $initialValue = null): Stream
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ input )
-> runningTotal ()
-> toArray ();
// 1, 3, 6, 10, 15 ข้ามองค์ประกอบบางอย่างของสตรีม
$stream->skip(int $count, int $offset = 0): Stream
use IterTools Stream ;
$ movies = [
' The Phantom Menace ' , ' Attack of the Clones ' , ' Revenge of the Sith ' ,
' A New Hope ' , ' The Empire Strikes Back ' , ' Return of the Jedi ' ,
' The Force Awakens ' , ' The Last Jedi ' , ' The Rise of Skywalker '
];
$ onlyTheBest = Stream:: of ( $ movies )
-> skip ( 3 )
-> skip ( 3 , 3 )
-> toArray ();
// 'A New Hope', 'The Empire Strikes Back', 'Return of the Jedi' แยกชิ้นของกระแส
$stream->slice(int $start = 0, int $count = null, int $step = 1)
use IterTools Stream ;
$ olympics = [ 1992 , 1994 , 1996 , 1998 , 2000 , 2002 , 2004 , 2006 , 2008 , 2010 , 2012 , 2014 , 2016 , 2018 , 2020 , 2022 ];
$ summerOlympics = Stream:: of ( $ olympics )
-> slice ( 0 , 8 , 2 )
-> toArray ();
// [1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020] เรียงลำดับกระแส
$stream->sort(callable $comparator = null)
หากไม่ได้ให้ตัวเปรียบเทียบองค์ประกอบของแหล่งที่มาที่วนซ้ำจะต้องเปรียบเทียบได้
use IterTools Stream ;
$ input = [ 3 , 4 , 5 , 9 , 8 , 7 , 1 , 6 , 2 ];
$ result = Stream:: of ( $ input )
-> sort ()
-> toArray ();
// 1, 2, 3, 4, 5, 6, 7, 8, 9 ส่งคืนสตรีมของความแตกต่างแบบสมมาตรของสตรีมและ iterables ที่กำหนด
$stream->symmetricDifferenceWith(iterable ...$iterables): Stream
หมายเหตุ: หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎการแยกหลายชุด
use IterTools Stream ;
$ a = [ 1 , 2 , 3 , 4 , 7 ];
$ b = [ ' 1 ' , 2 , 3 , 5 , 8 ];
$ c = [ 1 , 2 , 3 , 6 , 9 ];
$ stream = Stream:: of ( $ a )
-> symmetricDifferenceWith ( $ b , $ c )
-> toArray ();
// '1', 4, 5, 6, 7, 8, 9 ส่งคืนสตรีมของความแตกต่างแบบสมมาตรของสตรีมและ iterables ที่กำหนดโดยใช้ประเภทการบีบบังคับ
$stream->symmetricDifferenceCoerciveWith(iterable ...$iterables): Stream
หมายเหตุ: หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎการแยกหลายชุด
use IterTools Stream ;
$ a = [ 1 , 2 , 3 , 4 , 7 ];
$ b = [ ' 1 ' , 2 , 3 , 5 , 8 ];
$ c = [ 1 , 2 , 3 , 6 , 9 ];
$ stream = Stream:: of ( $ a )
-> symmetricDifferenceCoerciveWith ( $ b , $ c )
-> toArray ();
// 4, 5, 6, 7, 8, 9 รักษาองค์ประกอบจากสตรีมตราบใดที่ภาคแสดงเป็นจริง
$stream->takeWhile(callable $predicate): Stream
หากไม่มีการจัดเตรียมภาคแสดงค่าบูลีนของข้อมูลจะถูกใช้
use IterTools Stream ;
$ input = [ 1 , - 1 , 2 , - 2 , 3 , - 3 ];
$ result = Stream:: of ( $ input )
-> takeWhile ( fn ( $ value ) => abs ( $ value ) < 3 )
-> toArray ();
// 1, -1, 2, -2 ส่งคืนสตรีมซึ่งประกอบด้วยการรวมกันของสตรีมและอินพุต iterables
$stream->unionWith(iterable ...$iterables): Stream
หมายเหตุ: หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎสหภาพหลายชุด
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ stream = Stream:: of ( $ input )
-> unionWith ([ 3 , 4 , 5 , 6 ])
-> toArray ();
// [1, 2, 3, 4, 5, 6] ส่งคืนสตรีมซึ่งประกอบด้วยการรวมกันของสตรีมและอินพุต iterables โดยใช้ประเภทการบีบบังคับ
$stream->unionCoerciveWith(iterable ...$iterables): Stream
หมายเหตุ: หากอินพุต iterables ผลิตรายการที่ซ้ำกันให้ใช้กฎสหภาพหลายชุด
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ stream = Stream:: of ( $ input )
-> unionCoerciveWith ([ ' 3 ' , 4 , 5 , 6 ])
-> toArray ();
// [1, 2, 3, 4, 5, 6] ส่งคืนสตรีมที่ประกอบด้วยคอลเลกชันที่มีหลายรายการที่มีการสตรีมพร้อมกัน
$stream->zipWith(iterable ...$iterables): Stream
สำหรับความยาวที่ไม่สม่ำเสมอการทำซ้ำจะหยุดลงเมื่อการทำซ้ำที่สั้นที่สุดหมดลง
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ stream = Stream:: of ( $ input )
-> zipWith ([ 4 , 5 , 6 ])
-> zipWith ([ 7 , 8 , 9 ])
-> toArray ();
// [1, 4, 7], [2, 5, 8], [3, 6, 9] ส่งคืนสตรีมที่ประกอบด้วยคอลเลกชันที่วนซ้ำหลายรายการโดยใช้ค่าฟิลเลอร์เริ่มต้นหากความยาวไม่เท่ากัน
$stream->zipFilledWith(mixed $default, iterable ...$iterables): Stream
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ stream = Stream:: of ( $ input )
-> zipFilledWith ( ' ? ' , [ ' A ' , ' B ' ]);
foreach ( $ stream as $ zipped ) {
// [1, A], [2, B], [3, ?]
}ส่งคืนสตรีมซึ่งประกอบด้วยคอลเลกชันที่มีความยาวหลายรายการที่มีความยาวเท่ากันสตรีมพร้อมกัน
$stream->zipEqualWith(iterable ...$iterables): Stream
ใช้งานได้เหมือน Stream::zipWith() วิธีการ แต่โยน lengthException ถ้าความยาวไม่เท่ากันนั่นคืออย่างน้อยหนึ่งตัววนซ้ำสิ้นสุดลงก่อนที่คนอื่น ๆ
use IterTools Stream ;
$ input = [ 1 , 2 , 3 ];
$ stream = Stream:: of ( $ input )
-> zipEqualWith ([ 4 , 5 , 6 ])
-> zipEqualWith ([ 7 , 8 , 9 ]);
foreach ( $ stream as $ zipped ) {
// [1, 4, 7], [2, 5, 8], [3, 6, 9]
}ส่งคืนสตรีมที่ประกอบด้วยคอลเลกชันที่มีหลายรายการที่มีการสตรีมพร้อมกัน
$stream->zipLongestWith(iterable ...$iterables): Stream
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ stream = Stream:: of ( $ input )
-> zipLongestWith ([ 4 , 5 , 6 ])
-> zipLongestWith ([ 7 , 8 , 9 , 10 ]);
foreach ( $ stream as $ zipped ) {
// [1, 4, 7], [2, 5, 8], [3, 6, 9], [4, null, 10], [null, null, 5]
}ส่งคืนจริงถ้าองค์ประกอบทั้งหมดตรงกับฟังก์ชั่นเพรดิเคต
$stream->allMatch(callable $predicate): bool
use IterTools Summary ;
$ finalFantasyNumbers = [ 4 , 5 , 6 ];
$ isOnSuperNintendo = fn ( $ ff ) => $ ff >= 4 && $ ff <= 6 ;
$ boolean = Stream:: of ( $ finalFantasyNumbers )
-> allMatch ( $ isOnSuperNintendo );
// true ส่งคืนจริงถ้าองค์ประกอบทั้งหมดไม่ซ้ำกัน
$stream->allUnique(bool $strict = true): bool
ค่าเริ่มต้นกับการเปรียบเทียบประเภทที่เข้มงวด กำหนดให้เข้มงวดกับเท็จสำหรับการเปรียบเทียบการบีบบังคับประเภท
use IterTools Summary ;
$ items = [ ' fingerprints ' , ' snowflakes ' , ' eyes ' , ' DNA ' ]
$ boolean = Stream:: of ( $ items )
-> allUnique ();
// true ส่งคืนจริงหากองค์ประกอบใด ๆ ตรงกับฟังก์ชันเพรดิเคต
$stream->anyMatch(callable $predicate): bool
use IterTools Summary ;
$ answers = [ ' fish ' , ' towel ' , 42 , " don't panic " ];
$ isUltimateAnswer = fn ( $ a ) => a == 42 ;
$ boolean = Stream:: of ( $ answers )
-> anyMatch ( $ answers , $ isUltimateAnswer );
// true ส่งคืนจริงถ้า iterables ทั้งหมดเป็นแบบต่อเนื่องกับสตรีม
$stream->arePermutationsWith(...$iterables): bool
use IterTools Summary ;
$ rite = [ ' r ' , ' i ' , ' t ' , ' e ' ];
$ reit = [ ' r ' , ' e ' , ' i ' , ' t ' ];
$ tier = [ ' t ' , ' i ' , ' e ' , ' r ' ];
$ tire = [ ' t ' , ' i ' , ' r ' , ' e ' ];
$ trie = [ ' t ' , ' r ' , ' i ' , ' e ' ];
$ boolean = Stream:: of ([ ' i ' , ' t ' , ' e ' , ' r ' ])
-> arePermutationsWith ( $ rite , $ reit , $ tier , $ tire , $ trie );
// true ส่งคืนจริงถ้า iterables ทั้งหมดเป็น permutations ด้วยสตรีมที่มีประเภทการบีบบังคับ
$stream->arePermutationsCoerciveWith(...$iterables): bool
use IterTools Summary ;
$ set2 = [ 2.0 , ' 1 ' , 3 ];
$ set3 = [ 3 , 2 , 1 ];
$ boolean = Stream:: of ([ 1 , 2.0 , ' 3 ' ])
-> arePermutationsCoerciveWith ( $ set2 , $ set3 );
// true ส่งคืนจริงถ้ารายการ N นั้นเป็นจริงตามฟังก์ชันเพรดิเคต
$stream->exactlyN(int $n, callable $predicate = null): bool
use IterTools Summary ;
$ twoTruthsAndALie = [ true , true , false ];
$ n = 2 ;
$ boolean = Stream:: of ( $ twoTruthsAndALie )-> exactlyN ( $ n );
// true ส่งคืนจริงถ้าสตรีมว่างเปล่าไม่มีรายการ
$stream->isEmpty(): bool
use IterTools Summary ;
$ numbers = [ 0 , 1 , 2 , 3 , 4 , 5 ];
$ filterFunc = fn ( $ x ) => $ x > 10 ;
$ boolean = Stream::( $ numbers )
-> filter ( $ filterFunc )
-> isEmpty ();
// true ส่งคืนจริงถ้าองค์ประกอบทั้งหมดของคอลเลกชันที่ได้รับซึ่งเป็นไปตามเพรดิเคตปรากฏขึ้นก่อนองค์ประกอบทั้งหมดที่ไม่ได้
$stream->isPartitioned(callable $predicate = null): bool
use IterTools Summary ;
$ numbers = [ 0 , 2 , 4 , 1 , 3 , 5 ];
$ evensBeforeOdds = fn ( $ item ) => $ item % 2 === 0 ;
$ boolean = Stream::( $ numbers )
-> isPartitioned ( $ evensBeforeOdds );
// true ส่งคืนจริงหากแหล่งที่มาของ iterable ถูกจัดเรียงตามลำดับจากน้อยไปมาก มิฉะนั้นเท็จ
$stream->isSorted(): bool
รายการของแหล่งที่ซ้ำกันจะต้องเปรียบเทียบได้
ส่งคืนจริงหากแหล่งที่มาของ iterable ว่างเปล่าหรือมีเพียงองค์ประกอบเดียว
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ input )
-> isSorted ();
// true
$ input = [ 1 , 2 , 3 , 2 , 1 ];
$ result = Stream:: of ( $ input )
-> isSorted ();
// false ส่งคืนจริงหากแหล่งที่มาของ iterable ถูกจัดเรียงตามลำดับย้อนกลับ มิฉะนั้นเท็จ
$stream->isReversed(): bool
รายการของแหล่งที่ซ้ำกันจะต้องเปรียบเทียบได้
ส่งคืนจริงหากแหล่งที่มาของ iterable ว่างเปล่าหรือมีเพียงองค์ประกอบเดียว
use IterTools Stream ;
$ input = [ 5 , 4 , 3 , 2 , 1 ];
$ result = Stream:: of ( $ input )
-> isReversed ();
// true
$ input = [ 1 , 2 , 3 , 2 , 1 ];
$ result = Stream:: of ( $ input )
-> isReversed ();
// false ส่งคืนจริงหากไม่มีองค์ประกอบตรงกับฟังก์ชันเพรดิเคต
$stream->noneMatch(callable $predicate): bool
use IterTools Summary ;
$ grades = [ 45 , 50 , 61 , 0 ];
$ isPassingGrade = fn ( $ grade ) => $ grade >= 70 ;
$ boolean = Stream:: of ( $ grades )-> noneMatch ( $ isPassingGrade );
// true ส่งคืนจริงถ้าแหล่งที่มาที่ซ้ำและคอลเลกชันทั้งหมดจะเหมือนกัน
$stream->sameWith(iterable ...$iterables): bool
สำหรับรายการ iterables ที่ว่างเปล่าส่งคืนจริง
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ input )
-> sameWith ([ 1 , 2 , 3 , 4 , 5 ]);
// true
$ result = Stream:: of ( $ input )
-> sameWith ([ 5 , 4 , 3 , 2 , 1 ]);
// false ส่งคืนจริงหากแหล่งที่มาที่ซ้ำและคอลเลกชันที่ได้รับทั้งหมดมีความยาวเท่ากัน
$stream->sameCountWith(iterable ...$iterables): bool
สำหรับรายการ iterables ที่ว่างเปล่าส่งคืนจริง
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ input )
-> sameCountWith ([ 5 , 4 , 3 , 2 , 1 ]);
// true
$ result = Stream:: of ( $ input )
-> sameCountWith ([ 1 , 2 , 3 ]);
// false ลดแหล่งที่มาของการวนซ้ำให้กับค่าเฉลี่ยของรายการ
$stream->toAverage(): mixed
ส่งคืนค่า null หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Stream ;
$ input = [ 2 , 4 , 6 , 8 ];
$ result = Stream:: of ( $ iterable )
-> toAverage ();
// 5 ลดแหล่งที่มาของมันเป็นความยาว
$stream->toCount(): mixed
use IterTools Stream ;
$ input = [ 10 , 20 , 30 , 40 , 50 ];
$ result = Stream:: of ( $ iterable )
-> toCount ();
// 5 ลดแหล่งที่มาของมันเป็นองค์ประกอบแรก
$stream->toFirst(): mixed
พ่น LengthException หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Stream ;
$ input = [ 10 , 20 , 30 ];
$ result = Stream:: of ( $ input )
-> toFirst ();
// 10 ลดแหล่งที่มาของมันเป็นองค์ประกอบแรกและองค์ประกอบสุดท้าย
$stream->toFirstAndLast(): array{mixed, mixed}
พ่น LengthException หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Stream ;
$ input = [ 10 , 20 , 30 ];
$ result = Stream:: of ( $ input )
-> toFirstAndLast ();
// [10, 30] ลดแหล่งที่มาของมันเป็นองค์ประกอบสุดท้าย
$stream->toLast(): mixed
พ่น LengthException หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Stream ;
$ input = [ 10 , 20 , 30 ];
$ result = Stream:: of ( $ input )
-> toLast ();
// 30 ลดแหล่งที่มาของการทำซ้ำเป็นค่าสูงสุด
$stream->toMax(callable $compareBy = null): mixed
$compareBy ต้องส่งคืนค่าที่เปรียบเทียบได้$compareBy รายการของคอลเลกชันที่กำหนดจะต้องเปรียบเทียบได้ use IterTools Stream ;
$ input = [ 1 , - 1 , 2 , - 2 , 3 , - 3 ];
$ result = Stream:: of ( $ iterable )
-> toMax ();
// 3 ลดแหล่งที่มาของมันเป็นค่าต่ำสุด
$stream->toMin(callable $compareBy = null): mixed
$compareBy ต้องส่งคืนค่าที่เปรียบเทียบได้$compareBy รายการของคอลเลกชันที่กำหนดจะต้องเปรียบเทียบได้ use IterTools Stream ;
$ input = [ 1 , - 1 , 2 , - 2 , 3 , - 3 ];
$ result = Stream:: of ( $ iterable )
-> toMin ();
// -3 ลดสตรีมให้เป็นอาร์เรย์ของขอบเขตบนและล่าง (สูงสุดและนาที)
$stream->toMinMax(callable $compareBy = null): array
$compareBy ต้องส่งคืนค่าที่เปรียบเทียบได้$compareBy รายการของคอลเลกชันที่กำหนดจะต้องเปรียบเทียบได้[null, null] หากคอลเลกชันที่ได้รับนั้นว่างเปล่า use IterTools Stream ;
$ numbers = [ 1 , 2 , 7 , - 1 , - 2 , - 3 ];
[ $ min , $ max ] = Stream:: of ( $ numbers )
-> toMinMax ();
// [-3, 7] ลดสตรีมให้เป็นค่าที่ตำแหน่งที่ N
$stream->toNth(int $position): mixed
ส่งคืนค่า null หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Stream ;
$ lotrMovies = [ ' The Fellowship of the Ring ' , ' The Two Towers ' , ' The Return of the King ' ];
$ result = Stream:: of ( $ lotrMovies )
-> toNth ( 2 );
// The Return of the King ลดสตรีมไปยังผลิตภัณฑ์ของรายการ
$stream->toProduct(): mixed
ส่งคืนค่า null หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ iterable )
-> toProduct ();
// 120 ลดสตรีมเป็นค่าสุ่มภายใน
$stream->toRandomValue(): mixed
use IterTools Stream ;
$ rpsHands = [ ' rock ' , ' paper ' , ' scissors ' ]
$ range = Stream:: of ( $ numbers )
-> map ( ' strtoupper ' )
-> toRandomValue ();
// e.g., rock ลดสตรีมไปยังช่วง (ความแตกต่างระหว่างสูงสุดและนาที)
$stream->toRange(): int|float
ส่งคืน 0 หากแหล่งที่มาของ iterable ว่างเปล่า
use IterTools Stream ;
$ grades = [ 100 , 90 , 80 , 85 , 95 ];
$ range = Stream:: of ( $ numbers )
-> toRange ();
// 20 ลดลงเป็นสตริงที่เข้าร่วมองค์ประกอบทั้งหมด
$stream->toString(string $separator = '', string $prefix = '', string $suffix = ''): string
use IterTools Stream ;
$ words = [ ' IterTools ' , ' PHP ' , ' v1.0 ' ];
$ string = Stream:: of ( $ words )-> toString ( $ words );
// IterToolsPHPv1.0
$ string = Stream:: of ( $ words )-> toString ( $ words , ' - ' );
// IterTools-PHP-v1.0
$ string = Stream:: of ( $ words )-> toString ( $ words , ' - ' , ' Library: ' );
// Library: IterTools-PHP-v1.0
$ string = Stream:: of ( $ words )-> toString ( $ words , ' - ' , ' Library: ' , ' ! ' );
// Library: IterTools-PHP-v1.0! ลดแหล่งที่มาของรายการที่วนซ้ำเป็นผลรวมของรายการ
$stream->toSum(): mixed
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ iterable )
-> toSum ();
// 15 ลดแหล่งที่มาที่ทำซ้ำเช่นฟังก์ชั่น array_reduce ()
แต่แตกต่างจาก array_reduce() มันใช้งานได้กับทุก iterable
$stream->toValue(callable $reducer, mixed $initialValue): mixed
use IterTools Stream ;
$ input = [ 1 , 2 , 3 , 4 , 5 ];
$ result = Stream:: of ( $ iterable )
-> toValue ( fn ( $ carry , $ item ) => $ carry + $ item );
// 15 ส่งคืนอาร์เรย์ขององค์ประกอบสตรีม
$stream->toArray(): array
use IterTools Stream ;
$ array = Stream:: of ([ 1 , 1 , 2 , 2 , 3 , 4 , 5 ])
-> distinct ()
-> map ( fn ( $ x ) => $ x ** 2 )
-> toArray ();
// [1, 4, 9, 16, 25] ส่งคืนแผนที่ค่าคีย์ขององค์ประกอบสตรีม
$stream->toAssociativeArray(callable $keyFunc, callable $valueFunc): array
use IterTools Stream ;
$ keyFunc
$ array = Stream:: of ([ ' message 1 ' , ' message 2 ' , ' message 3 ' ])
-> map ( ' strtoupper ' )
-> toAssociativeArray (
fn ( $ s ) => md5 ( $ s ),
fn ( $ s ) => $ s
);
// [3b3f2272b3b904d342b2d0df2bf31ed4 => MESSAGE 1, 43638d919cfb8ea31979880f1a2bb146 => MESSAGE 2, ... ] ส่งคืนสตรีมอิสระ (ซ้ำซ้อน) หลายสาย
$stream->tee(int $count): array
use IterTools Transform ;
$ daysOfWeek = [ ' Mon ' , ' Tues ' , ' Wed ' , ' Thurs ' , ' Fri ' , ' Sat ' , ' Sun ' ];
$ count = 3 ;
[ $ week1Stream , $ week2Stream , $ week3Stream ] = Stream:: of ( $ daysOfWeek )
-> tee ( $ count );
// Each $weekStream contains ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'] ดำเนินการผ่านการเรียกสามารถเรียกได้ในแต่ละรายการในสตรีม
$stream->callForEach(callable $function): void
use IterTools Stream ;
$ languages = [ ' PHP ' , ' Python ' , ' Java ' , ' Go ' ];
$ mascots = [ ' elephant ' , ' snake ' , ' bean ' , ' gopher ' ];
$ zipPrinter = fn ( $ zipped ) => print ( "{ $ zipped [ 0 ]} 's mascot: { $ zipped [ 1 ]}" );
Stream:: of ( $ languages )
-> zipWith ( $ mascots )
-> callForEach ( $ zipPrinter );
// PHP's mascot: elephant
// Python's mascot: snake
// ... พิมพ์แต่ละรายการในสตรีม
$stream->print(string $separator = '', string $prefix = '', string $suffix = ''): void
use IterTools Stream ;
$ words = [ ' IterTools ' , ' PHP ' , ' v1.0 ' ];
Stream:: of ( $ words )-> print (); // IterToolsPHPv1.0
Stream:: of ( $ words )-> print ( ' - ' ); // IterTools-PHP-v1.0
Stream:: of ( $ words )-> print ( ' - ' , ' Library: ' ); // Library: IterTools-PHP-v1.0
Stream:: of ( $ words )-> print ( ' - ' , ' Library: ' , ' ! ' ); // Library: IterTools-PHP-v1.0! พิมพ์แต่ละรายการในสตรีมในบรรทัดของตัวเอง
$stream->println(): void
use IterTools Stream ;
$ words = [ ' IterTools ' , ' PHP ' , ' v1.0 ' ];
Stream:: of ( $ words )-> printLn ();
// IterTools
// PHP
// v1.0 เขียนเนื้อหาของสตรีมไปยังไฟล์ CSV
$stream->toCsvFile(resource $fileHandle, array $header = null, string 'separator = ',', string $enclosure = '"', string $escape = '\'): void
use IterTools Stream ;
$ starWarsMovies = [
[ ' Star Wars: Episode IV – A New Hope ' , ' IV ' , 1977 ],
[ ' Star Wars: Episode V – The Empire Strikes Back ' , ' V ' , 1980 ],
[ ' Star Wars: Episode VI – Return of the Jedi ' , ' VI ' , 1983 ],
];
$ header = [ ' title ' , ' episode ' , ' year ' ];
Stream:: of ( $ data )
-> toCsvFile ( $ fh , $ header );
// title,episode,year
// "Star Wars: Episode IV – A New Hope",IV,1977
// "Star Wars: Episode V – The Empire Strikes Back",V,1980
// "Star Wars: Episode VI – Return of the Jedi",VI,1983 เขียนเนื้อหาของสตรีมไปยังไฟล์
$stream->toFile(resource $fileHandle, string $newLineSeparator = PHP_EOL, string $header = null, string $footer = null): void
use IterTools Stream ;
$ data = [ ' item1 ' , ' item2 ' , ' item3 ' ];
$ header = ' <ul> ' ;
$ footer = ' </ul> ' ;
Stream:: of ( $ data )
-> map ( fn ( $ item ) => " <li> $ item </li> " )
-> toFile ( $ fh , PHP_EOL , $ header , $ footer );
// <ul>
// <li>item1</li>
// <li>item2</li>
// <li>item3</li>
// </ul>ดูที่แต่ละองค์ประกอบระหว่างการดำเนินการสตรีมอื่น ๆ เพื่อดำเนินการบางอย่างโดยไม่ต้องแก้ไขสตรีม
$stream->peek(callable $callback): Stream
use IterTools Stream ;
$ logger = new SimpleLog Logger ( ' /tmp/log.txt ' , ' iterTools ' );
Stream:: of ([ ' some ' , ' items ' ])
-> map ( ' strtoupper ' )
-> peek ( fn ( $ x ) => $ logger -> info ( $ x ))
-> foreach ( $ someComplexCallable );ดูที่สตรีมทั้งหมดระหว่างการดำเนินการสตรีมอื่น ๆ เพื่อดำเนินการบางอย่างโดยไม่ต้องแก้ไขสตรีม
$stream->peekStream(callable $callback): Stream
use IterTools Stream ;
$ logger = new SimpleLog Logger ( ' /tmp/log.txt ' , ' iterTools ' );
Stream:: of ([ ' some ' , ' items ' ])
-> map ( ' strtoupper ' )
-> peekStream ( fn ( $ stream ) => $ logger -> info ( $ stream ))
-> foreach ( $ someComplexCallable );ดูที่แต่ละองค์ประกอบระหว่างการดำเนินการสตรีมอื่น ๆ เพื่อพิมพ์แต่ละรายการโดยไม่ต้องแก้ไขสตรีม
$stream->peekPrint(string $separator = '', string $prefix = '', string $suffix = ''): void
use IterTools Stream ;
Stream:: of ([ ' some ' , ' items ' ])
-> map ( ' strtoupper ' )
-> peekPrint ()
-> foreach ( $ someComplexCallable ); ดูที่แต่ละองค์ประกอบระหว่างการดำเนินการสตรีมอื่น ๆ เพื่อ print_r แต่ละรายการโดยไม่ต้องแก้ไขสตรีม
$stream->peekPrintR(callable $callback): void
use IterTools Stream ;
Stream:: of ([ ' some ' , ' items ' ])
-> map ( ' strtoupper ' )
-> peekPrintR ()
-> foreach ( $ someComplexCallable ); print_r แต่ละรายการในสตรีม
$stream->printR(): void
use IterTools Stream ;
$ items = [ $ string , $ array , $ object ];
Stream:: of ( $ words )-> printR ();
// print_r output var_dump แต่ละรายการในสตรีม
$stream->varDump(): void
use IterTools Stream ;
$ items = [ $ string , $ array , $ object ];
Stream:: of ( $ words )-> varDump ();
// var_dump output Itertools สามารถรวมกันเพื่อสร้างองค์ประกอบใหม่ที่ทำซ้ำได้
use IterTools Multi ;
use IterTools Single ;
$ letters = ' ABCDEFGHI ' ;
$ numbers = ' 123456789 ' ;
foreach (Multi:: zip (Single:: string ( $ letters ), Single:: string ( $ numbers )) as [ $ letter , $ number ]) {
$ battleshipMove = new BattleshipMove( $ letter , $ number )
}
// A1, B2, C3 use IterTools Multi ;
use IterTools Single ;
$ letters = ' abc ' ;
$ numbers = ' 123 ' ;
foreach (Multi:: chain (Single:: string ( $ letters ), Single:: string ( $ numbers )) as $ character ) {
print ( $ character );
}
// a, b, c, 1, 2, 3 เมื่อมีตัวเลือกค่าเริ่มต้นจะทำการเปรียบเทียบประเภทที่เข้มงวด:
เมื่อประเภทการบีบบังคับ (ประเภทที่ไม่ใช่ Strict) พร้อมใช้งานและเปิดใช้งานผ่าน Flag เสริม:
Itertools PHP เป็นไปตามมาตรฐานต่อไปนี้:
Itertools PHP ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT
ฟังก์ชั่น ITERTOOLS ไม่ได้ จำกัด อยู่ที่ PHP และ Python Other languages have similar libraries. Familiar functionality is available when working in other languages.