monad-maybe
Installation
SKILL.md
Maybe Monad
Installation: If not already installed, add the package with pnpm add @efesto-cloud/maybe.
Use this skill to keep Maybe<T> usage simple and predictable.
Core Rules
- Wrap optional value with
Maybe.maybe(value). - Build explicit presence with
Maybe.some(value). - Build explicit absence with
Maybe.none(). - Check state with
isSome()/isNone(). - Provide fallback with
else(() => defaultValue). - Convert to
Resultwhen needed withtoResult()— returnsResult<T, NoneError>. - Pattern match with
fold(onNone, onSome)when both branches need to return the same type. - Execute side effects with
run(fn => {...})— Some runs the function, None does nothing. - Serialize to plain object with
toObject()— returnsISome<T> | INone. - Deserialize with
Maybe.fromObject(obj)— reconstructs from{ some: boolean, data: T | null }. - Combine two Maybes with
Maybe.combine(m1, m2)— returnsMaybe<[T1, T2]>.
Common Mistakes To Avoid
- Do not treat
MaybelikeResult. - Do not use
isFailureonMaybe. - Do not access
.datawithout checkingisSome()first.
Procedure
- Identify nullable or optional source values.
- Convert early to
Maybe(Maybe.maybe(value)). - Use one handling style consistently:
- Branching:
if (m.isNone()) ... else m.data - Functional:
map,flatMap,filter,fold - Fallback:
m.else(() => defaultValue)
- Branching:
- If caller expects
Result<T, E>, convert once withtoResult()and continue in Result flow.
Quick Patterns
const maybeEmail = Maybe.maybe(input.email);
if (maybeEmail.isNone()) return Result.err(new Error("Missing email"));
return Result.ok(maybeEmail.data.trim());
const displayName = Maybe.maybe(user.nickname)
.filter((v) => v.length > 0)
.else(() => user.nome)
.data;
Related skills
More from efesto-cloud/lib
usecase
>
2observer
Use when writing or reviewing Observable code from the @efesto-cloud/observable package.
2entity
Create or modify domain entities using the @efesto-cloud/entity package. Use this skill whenever the user asks to add a new entity, update an existing entity, add properties or methods to an entity, or work on the entity/dto layer. Trigger when the user says things like "create a Foo entity", "add a field to Bar", "I need a new domain object", or "add entity X". Also trigger for DTO creation or modification.
2persistence
>
2type-enum-dict
|
2value-object
|
2