Skip to content

Commit 4318a1d

Browse files
committed
feat(option): new Option::apply() method
1 parent 4affb62 commit 4318a1d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/Psl/Option/Option.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ public function contains(mixed $value): bool
199199
return false;
200200
}
201201

202+
/**
203+
* Applies a function to a contained value and returns the original `Option<T>`.
204+
*
205+
* @param (Closure(T): void) $closure
206+
*
207+
* @return Option<T>
208+
*/
209+
public function apply(Closure $closure): Option
210+
{
211+
if ($this->option !== null) {
212+
$closure($this->option[0]);
213+
}
214+
215+
return $this;
216+
}
217+
202218
/**
203219
* Maps an `Option<T>` to `Option<Tu>` by applying a function to a contained value.
204220
*

0 commit comments

Comments
 (0)