feat: chunk_by を追加#75
Open
Javakky wants to merge 1 commit into
Open
Conversation
連続する要素を callback の戻り値が等しい範囲ごとにまとめた配列の配列を返す。 PHP 標準の array_chunk が指定サイズで等分するのに対し、 本関数は隣接する要素間で callback の結果が変わる位置で chunk を切る。 例: chunk_by([1, 1, 2, 3, 3, 1], fn (int \$v): int => \$v) => [[1, 1], [2], [3, 3], [1]] Scala の List#groupAdjacent / Haskell の Data.List.groupBy に相当する操作で、 「状態が切り替わる位置でランを区切る」 系の処理が array_chunk_by 一発で書ける。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
連続する要素を callback の戻り値が等しい範囲ごとにまとめた配列の配列を返す
chunk_byを追加します。PHP 標準の
array_chunkが指定サイズで等分するのに対し、 本関数は隣接する要素間で callback の結果が変わる位置で chunk を切ります。Scala の
List#groupAdjacent/ Haskell のData.List.groupByに相当する操作で、 「状態が切り替わる位置でランを区切る」 系の処理が一発で書けるようになります。Why
社内の dic.pixiv.net で TipTap ノード木の整形処理 (連続する illust 埋め込みを 1 つの block にまとめる等) で必要になり、 Phollection に欠けていたので追加。 既存の
group_by(キーごとに非隣接でもまとめる) とは別の用途。変更点
src/chunk_by.phpを追加tests/ChunkByTest.phpに 7 ケース動作確認
Note
ローカル環境の制約により vendor を install できなかったため、 PHPUnit / PHPStan の確認は CI 側にお願いします。 ロジック自体は社内利用の同等実装でテストが通っていることを確認済みです。
🤖 Generated with Claude Code