feat: find_option を追加する - #82
Open
Javakky wants to merge 1 commit into
Open
Conversation
Javakky
force-pushed
the
javakky/find-option
branch
2 times, most recently
from
July 27, 2026 07:31
9bb2423 to
940b6d8
Compare
条件に一致する最初の要素の値を返す関数を追加する。先頭一致を短絡評価で 探索し、未検出・空配列は null を返す。 Scala の find (値だけを返す Option) や Kotlin の firstOrNull はいずれも 値だけを返し、このリポジトリ内の head_option / last_option も値だけを 返すため、[キー, 値] のタプルと Mode 引数を持つ設計を撤回する。Mode は 戻り値のキーの扱いを切り替えるものだが、値だけを返す関数にはキーの扱い 自体が存在せず、Mode を持たせる余地がない。キーが必要な場合は将来別関数 (find_entry 等) に切り出す。 Closes #60
Javakky
force-pushed
the
javakky/find-option
branch
from
July 27, 2026 11:15
940b6d8 to
c7be6be
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)
条件に一致する最初の要素の値を返す
find_optionを追加します。head_option/last_optionと同じ「見つからなければ null」の系列です。Important
issue #60 は
array{0: key, 1: value}のタプルを返す仕様でしたが、値だけを返す形に変更しました。理由は下記「補足」を参照してください。反対でしたら戻します。変更点
src/find_option.php— 先頭一致で即 return。未検出と空配列はnulltests/FindOptionTest.php— 8 観点composer.jsonのautoload.filesに 1 件追加動作確認
find_option(['a' => 1, 'b' => 2], fn ($v) => $v === 2)→2vendor/bin/php-cs-fixer fix --dry-run --diff/vendor/bin/phpstan analyse -c phpstan.neon/vendor/bin/phpunit testsOK (114 tests, 123 assertions)/ PHPStan level 10[OK] No errors/ CS FixerFound 0 of 33 files補足(任意)
issue の仕様 (タプルを返す) を撤回しました
標準ライブラリはどれも値だけを返します。
IterableOps.find(p: (A) => Boolean)Option[A]— 値だけIterable<T>.firstOrNull(predicate)T?— 値だけ加えてこのリポジトリ内でも非対称でした。
head_option/last_optionは値だけを返しているので、find_optionだけがタプルを返す設計になっていました。Mode引数も削除しました。キーを返さないので mode で変わるものがありません (当初の実装ではMODE_LISTで第 1 要素を反復順の index にしていました)。「null 値と未検出を区別できない」制約について
値だけを返すので、一致した要素が
nullの場合と未検出を区別できません。これはhead_option/last_optionが既に持っている制約と同じで、phpdoc に同じ書式で明記し、テストでも「区別できないこと」を固定しています。キーが必要なケース
将来
find_entryのような別関数に切り出せます。今は作りません — 必要になってから追加する方が、使われない API を増やさずに済みます。Kotlin でも「Map のエントリを探す」はentries.firstOrNull { … }とエントリのコレクションを経由する形になっています。副次的な効果
#76 (カリー化) で戻り値の「器」ごとにクラスを分ける設計を検討していますが、タプルを返す
find_optionは 1 関数専用の器が必要でした。値だけ返す形にするとany/all/none/reduceと同じ「配列でない戻り」の器で賄えるので、クラスが 1 つ減ります。その他
$callbackの第 2 引数に元のキーが渡ること (list なら int、assoc なら string) をテストしていますCloses #60
🤖 Generated with Claude Code