Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@sweet-monads/maybe
Advanced tools
[Maybe Monad](https://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe), The Maybe monad represents computations which might "go wrong" by not returning a value.
Maybe Monad, The Maybe monad represents computations which might "go wrong" by not returning a value.
sweet-monads — easy-to-use monads implementation with static types definition and separated packages.
npm install @sweet-monads/maybe
import { Maybe } from "@sweet-monads/maybe";
type User = { email: string, password: string };
function getUser(id: number): Maybe<User> {
return Maybe.just({ email: "test@gmail.com", password: "test" });
}
// Maybe<string>
const user = getUser(1).map(({ email }) => email);
Maybe.merge
function merge<V1>(values: [Maybe<V1>]): Maybe<[V1]>;
function merge<V1, V2>(values: [Maybe<V1>, Maybe<V2>]): Maybe<[V1, V2]>;
function merge<V1, V2, V3>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>]): Maybe<[V1, V2, V3]>;
// ... until 10 elements
values: Array<Maybe<T>>
- Array of Maybe values which will be merged into Maybe of ArrayMaybe<Array<T>>
Maybe of Array which will contain Just<Array<T>>
if all of array elements was Just<T>
else None
.Example:
const v1 = Maybe.just(2); // Maybe<number>.Just
const v2 = Maybe.just("test"); // Maybe<string>.Just
const v3 = Maybe.none<boolean>(); // Maybe<boolean>.None
Maybe.merge([v1, v2]) // Maybe<[number, string]>.Just
Maybe.merge([v1, v2, v3]) // Maybe<[number, string, boolean]>.None
Maybe.none
function none<T>(): Maybe<T>;
Maybe
with None
state
Example:const v1 = Maybe.none(); // Maybe<unknown>.None
const v2 = Maybe.none<number>(); // Maybe<number>.None
Maybe.just
function just<T>(value: T): Maybe<T>;
Maybe
with Just
state which contain value with T
type.
Example:const v1 = Maybe.just(2); // Maybe<number>.Just
const v2 = Maybe.just<2>(2); // Maybe<2>.Just
Maybe#isNone
function isNone(): boolean;
true
if state of Maybe
is None
otherwise false
Example:const v1 = Maybe.just(2);
const v2 = Maybe.none();
v1.isNone() // false
v2.isNone() // true
Maybe#isJust
function isJust(): boolean;
true
if state of Maybe
is Just
otherwise false
Example:const v1 = Maybe.just(2);
const v2 = Maybe.none();
v1.isJust() // true
v2.isJust() // false
Maybe#map
function map<Val, NewVal>(fn: (val: Val) => NewVal): Maybe<NewVal>;
fn
function value wrapped by Maybe
if Maybe
is Just
otherwise None
Example:const v1 = Maybe.just(2);
const v2 = Maybe.none<number>();
const newVal1 = v1.map(a => a.toString()); // Maybe<string>.Just with value "2"
const newVal2 = v2.map(a => a.toString()); // Maybe<string>.None without value
Maybe#chain
function chain<Val, NewVal>(fn: (val: Val) => Maybe<NewVal>): Maybe<NewVal>;
fn
function value wrapped by Maybe
if Maybe
is Just
and returned by fn
value is Just
too otherwise None
Example:const v1 = Maybe.just(2);
const v2 = Maybe.none<number>();
const newVal1 = v1.chain(a => Maybe.just(a.toString())); // Maybe<string>.Just with value "2"
const newVal2 = v1.chain(a => Maybe.none()); // Maybe<string>.None without value
const newVal3 = v2.chain(a => Maybe.just(a.toString())); // Maybe<string>.None without value
const newVal4 = v2.chain(a => Maybe.none()); // Maybe<string>.None without value
// Value from Maybe instance
const { value } = Maybe.just(2); // number | undefined
MIT (c) Artem Kobzar see LICENSE file.
FAQs
[Maybe Monad](https://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe), The Maybe monad represents computations which might "go wrong" by not returning a value.
The npm package @sweet-monads/maybe receives a total of 3,418 weekly downloads. As such, @sweet-monads/maybe popularity was classified as popular.
We found that @sweet-monads/maybe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.