feat: partition と partition_map を追加する - #87
Open
Javakky wants to merge 2 commits into
Open
Conversation
Javakky
force-pushed
the
javakky/partition
branch
from
July 27, 2026 07:16
2985ab8 to
a984b77
Compare
This was referenced Jul 27, 2026
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 分割する
partitionと、分割と変換を同時に行うpartition_mapを追加します。変更点
src/partition.php—array{0: true側, 1: false側}。両側とも元の順序を維持src/partition_map.php— callback がarray{0: bool, 1: 変換後の値}を返す。1 パスで振り分けと変換tests/PartitionTest.php/tests/PartitionMapTest.phpcomposer.jsonのautoload.filesに 2 件追加動作確認
partition([1, 2, 3, 4], fn ($v) => $v % 2 === 0)→[[2, 4], [1, 3]]partition_map([20, 17], fn ($v) => $v >= 20 ? [true, $v] : [false, "minor($v)"])→[[20], ['minor(17)']]vendor/bin/php-cs-fixer fix --dry-run --diff/vendor/bin/phpstan analyse -c phpstan.neon/vendor/bin/phpunit testsOK (119 tests, 125 assertions)/ PHPStan level 10[OK] No errors/ CS FixerFixed 0 of 33 files補足(任意)
Eitherを作らずタプルで表現していますScala の
partitionMapはEitherを返しますが、README の設計思想「新しいクラスを定義しない」に従いarray{0: bool, 1: 値}にしました。partitionと同じく 添字 0 が true 側で規約を揃えています。@templateを 1 本にした理由 (検証結果つき)当初は
@template L/@template Rを切り、判別可能 union (array{0: true, 1: L}|array{0: false, 1: R}) から左右の型を分けようとしましたが、PHPStan 2.x はこの分離推論をしません。\PHPStan\dumpType()で確認した結果:Closure(int): (array{false, non-falsy-string}|array{true, int}))list<int|non-falsy-string>LとRの両方に union 全体が推論される潰れているのは関数本体ではなくテンプレート推論そのものなので、本体に
/** @var L */と言い切っても利用者側には分離が届きません。動かない型注釈は読み手に嘘をつくので、@template E1 本に畳んで phpdoc に「左右で異なる型を返しても PHPStan 上は union として推論される」と明記しました。同時に
partition_mapの実装をpartitionと同じ形 ($result = [[], []]+$sideで振り分け) に揃えられたので、姉妹関数の非対称も解消しています。この検証の経緯は
tests/PartitionMapTest.phpのヘルパーの docblock に残してあります (次に型パラメータを 2 本に分けたくなった人が同じ検証を繰り返さずに済むように)。その他
@phpstan-paramの条件型は付けていません (「assoc 入力 +MODE_LIST」が型エラーになるため。slice.php/map.php/intersect.phpと同じ形)Closes #59
Closes #23
🤖 Generated with Claude Code