@@ -164,6 +164,78 @@ $firstOdd; // Maybe::just(9)
164164$sequence->find(static fn() => false); // Maybe::nothing()
165165```
166166
167+ ### ` ->lookup() `
168+
169+ This is similar to ` ->find() ` except it allows to return a computed value while searching for it. This is useful when parsing content.
170+
171+ === "` ->maybe() ` "
172+ ```php
173+ final class Email
174+ {
175+ /**
176+ * @return Maybe<self >
177+ * /
178+ public static function of(string $email): Maybe
179+ {
180+ }
181+ }
182+
183+ $email = Sequence::of('foo', 'valid-email@example.com', 'invalid')
184+ ->lookup()
185+ ->first()
186+ ->maybe(Email::of(...));
187+ $email == Maybe::just(new Email('valid-email@example.com')); // true
188+ ```
189+
190+ === "` ->attempt() ` "
191+ ```php
192+ final class Email
193+ {
194+ /**
195+ * @return Attempt<self >
196+ * /
197+ public static function of(string $email): Attempt
198+ {
199+ }
200+ }
201+
202+ $email = Sequence::of('foo', 'valid-email@example.com', 'invalid')
203+ ->lookup()
204+ ->first()
205+ ->attempt(
206+ new \RuntimeException('No valid email found'),
207+ Email::of(...),
208+ );
209+ $email == Attempt::result(new Email('valid-email@example.com')); // true
210+ ```
211+
212+ === "` ->either() ` "
213+ ```php
214+ final class Email
215+ {
216+ /**
217+ * @return Either<string, self>
218+ * /
219+ public static function of(string $email): Either
220+ {
221+ }
222+ }
223+
224+ $email = Sequence::of('foo', 'valid-email@example.com', 'invalid')
225+ ->lookup()
226+ ->first()
227+ ->attempt(
228+ 'No valid email found', #(1)
229+ Email::of(...),
230+ );
231+ $email == Either::right(new Email('valid-email@example.com')); // true
232+ ```
233+
234+ 1. Must be of the same type as the left side returned by `Email::of()`
235+
236+ !!! note ""
237+ If you need to access the last value that matches the predicate you can use ` #!php ->lookup()->last() ` instead of ` #!php ->lookup()->first() ` .
238+
167239### ` ->matches() ` :material-memory-arrow-down:
168240
169241Check if all the elements of the sequence matches the given predicate.
0 commit comments