Skip to content

Option#filter and #flatten - #32

Merged
nz merged 1 commit into
mainfrom
feat/option-combinators
Aug 7, 2026
Merged

nz merged 1 commit into
mainfrom
feat/option-combinators

Conversation

@nz

@nz nz commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

Fixes #23.

Adds filter (Some survives only if the predicate holds) and flatten (one level of nesting removed). Both sit in the intersection of Rust and Ruby idiom — Enumerable gives them Ruby precedent, and their semantics match Option::filter / Option::flatten. flatten raises TypeMismatchError when the inner value isn't an Option, a case Rust rejects at compile time, handled in the gem's pedantic style.

Declined, with rationale:

  • unzip — Rust-idiomatic but has no Ruby analogue to anchor it (nothing in Enumerable unzips), so it falls outside the both-idioms sweet spot this gem targets. It also wasn't on the issue's priority list.
  • unwrap_or_default — Ruby has no per-type default convention (Integer has no .default); unwrap_or(0) at the call site is clearer than any registry we could invent. The commented-out sketch in the source is removed.
  • The mutating four (insert, get_or_insert, take, replace) — the issue itself suggests declining; an Option here is a return value, not a mutable slot. The stale TODO comment list is replaced with a one-line statement of that decision.

xor from the issue's table already landed in #13. Specified as doctests.

@nz
nz enabled auto-merge August 7, 2026 18:18
@nz
nz force-pushed the feat/option-combinators branch from 188d0dc to a4b3412 Compare August 7, 2026 18:27
@nz nz changed the title Option#filter, #flatten, #unzip Option#filter and #flatten Aug 7, 2026
lutzcc1
lutzcc1 previously approved these changes Aug 7, 2026
Fill the non-mutating gaps against Rust's Option so application code
has a combinator instead of unwrap!: filter keeps a Some only when
the predicate holds, flatten removes one level of nesting. Both sit
in the intersection of Rust and Ruby idiom via Enumerable precedent;
flatten raises TypeMismatchError on inner values Rust's types would
have rejected at compile time.

unzip is declined: Ruby has no unzip idiom to anchor it. The mutating
combinators (insert, get_or_insert, take, replace) are likewise
omitted: an Option here is a value, not a slot. unwrap_or_default is
declined; Ruby has no per-type default convention to lean on.

Fixes #23.
@nz
nz force-pushed the feat/option-combinators branch from a4b3412 to 3dc3dc9 Compare August 7, 2026 19:38
@nz
nz merged commit f2379a1 into main Aug 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option: missing Rust combinators (filter, flatten, unwrap_or_default, xor, unzip, …)

2 participants