feat: take と drop を追加する - #84
Open
Javakky wants to merge 2 commits into
Open
Conversation
Javakky
force-pushed
the
javakky/take-drop
branch
from
July 27, 2026 07:16
c8c2297 to
c126e94
Compare
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)
先頭 n 件を取り出す
takeと、先頭 n 件を捨てるdropを追加します。親 issue #56 の breakdown で挙がっていた基本操作で、
sliceのオフセット計算を毎回書かずに済みます。変更点
src/take.php— 先頭から最大 n 件。n <= 0は空、n >= countは全件src/drop.php— 先頭 n 件を除外。n <= 0は全件、n >= countは空tests/TakeTest.php/tests/DropTest.phpcomposer.jsonのautoload.filesに 2 件追加動作確認
take(['a' => 1, 'b' => 2, 'c' => 3], 2, Mode::MODE_LIST)→[1, 2]drop([1, 2, 3], 1, Mode::MODE_ASSOC)→[1 => 2, 2 => 3]vendor/bin/php-cs-fixer fix --dry-run --diff/vendor/bin/phpstan analyse -c phpstan.neon/vendor/bin/phpunit testsOK (117 tests, 122 assertions)/ PHPStan level 10[OK] No errors/ CS FixerFixed 0 of 33 files補足(任意)
array_sliceの$preserve_keysに丸投げしていませんarray_slice()は$preserve_keys = falseでも文字列キーを保持します (再付番されるのは数値キーだけ)。そのためarray_slice(..., true)で取り出してからMODE_LISTのときだけarray_values()する実装にしています。既存の
src/slice.phpはこの対処がなく、slice(['a' => 1, 'b' => 2], 0, 2, Mode::MODE_LIST)が['a' => 1, 'b' => 2]を返して宣言しているlist<V>に反します (tests/SliceTest.phpに assoc 入力 +MODE_LISTのケースが無いため露見していません)。既存関数の変更はこの PR のスコープ外なので触っていませんが、別 issue に切り出す価値があります。その他
@phpstan-paramの条件型は付けていません。付けると「assoc 入力 +MODE_LIST」が型エラーになるためで、src/slice.php/src/map.php/src/intersect.phpと同じ形ですMODE_ASSOCで整数キーが 0 始まりに詰め直されないこともテストで固定していますCloses #64
Closes #65
🤖 Generated with Claude Code