feat: zip と zip_with を追加する - #83
Open
Javakky wants to merge 2 commits into
Open
Conversation
左右の配列を反復順序で組み合わせ、ペアの配列を返す zip 関数を追加する。右のキーは一切使用せず反復順序のみでペアにする。 Mode で戻り値のキーの扱いを切り替える。MODE_ASSOC は左のキーを維持し、MODE_LIST は左右のキーを無視して list を返す。MODE_AUTO は左の形状で判定する。 Closes #68
zip と同じ組み合わせ規則で左右の配列からペアを作り、各ペアに callback を適用した結果を返す zip_with 関数を追加する。中間のペア配列を作らない独立実装。 Mode で戻り値のキーの扱いを切り替える。MODE_ASSOC は左のキーを維持し、MODE_LIST は左右のキーを無視して list を返す。MODE_AUTO は左の形状で判定する。 Closes #69
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.
概要(What / Why)
2 つの配列を反復順序で組み合わせる
zipと、ペアに関数を適用するzip_withを追加します。変更点
src/zip.php—list<array{0: L, 1: R}>または左のキーを維持した assoc を返す。長さはmin(count($left), count($right))src/zip_with.php— 同じ組み合わせ規則で callback を適用tests/ZipTest.php/tests/ZipWithTest.phpcomposer.jsonのautoload.filesに 2 件追加動作確認
zip([1, 2, 3], ['a', 'b'])→[[1, 'a'], [2, 'b']]zip(['x' => 1, 'y' => 2], [10, 20])→['x' => [1, 10], 'y' => [2, 20]](左が assoc なので AUTO で キー維持)Mode::MODE_LIST→[[1, 10], [2, 20]]vendor/bin/php-cs-fixer fix --dry-run --diff/vendor/bin/phpstan analyse -c phpstan.neon/vendor/bin/phpunit testsOK (137 tests, 144 assertions)/ PHPStan level 10[OK] No errors/ CS FixerFixed 0 of 35 files補足(任意)
Modeの扱い当初は「キーを一切使わない」仕様で
Modeを省いていましたが、新規関数にはModeを持たせる方針に合わせて追加しました。MODE_LIST: 左右のキーを無視して 0 始まり連番の listMODE_ASSOC: 左のキーを維持。右のキーは使わない (右は反復順のみ)MODE_AUTO: 左 の形状で判定 (Mode::check_mode($mode, $left))右のキーは mode によらず一切使いません。左右で役割が非対称なので phpdoc に明記しています。
この変更で
MODE_AUTOの既定が「左の形状で判定」になったため、assoc 同士を渡していた既存テストは「MODE_LISTを明示すればキーが無視される」形に書き換えました (削除ではなく改名 + 明示指定)。zip_withは独立実装ですzipに委譲せずforループで直接 callback に渡しています。zipと同じループ構造にして姉妹関数間の対称性を取り、中間のペア配列の生成も無くしています。その他
array_values()の結果は$leftValues/$rightValuesという別変数に受けています (引数への再代入だと「入力を破壊しない」ことの確認に一手間かかるため)@phpstan-paramの条件型は付けていません (「assoc 入力 +MODE_LIST」が型エラーになるため。slice.php/map.php/intersect.phpと同じ形)Closes #68
Closes #69
🤖 Generated with Claude Code