Implementations of common structures using ramda for utilities. ~~No inheritance. Just plain objects~~ Now with inheritance!
npm install @juan-utils/ramda-structuresjavascript
// "~>" used to define the object's type
// This means map must be called on an array object
// and that the object is an array of a's
// map :: [a] ~> (a -> b) -> [b]
[1,2,3].map(() => 42)
// "=>" type constraints are always before the implicit object
// match :: Cases b => Maybe a ~> b -> c
// This means that b" is of type "Cases" and
// "match" is a method of a "Maybe" object
`
Maybe
`
Maybe = Just | None
`
Maybe should be used when communicating the posibility of a falsy value is expected. The default behavior is that a falsy value maps to None and any other value maps to Just.
| constructor | description |
| -------------------- | ------------------------------------------- |
| Just :: a -> Just a | Just constructor |
| None :: () -> None | None constructor |
| from :: a -> Maybe a | returns None on falsy value. Just otherwise |
| fromFalsy :: a -> Maybe a | returns None on falsy value. Just otherwise |
| fromArray :: [a] -> Maybe [a] | returns None on empty array. Just otherwise |
| fromNullish :: a -> Maybe a | returns None on null or undefined. Just otherwise |
| fromEmpty :: a -> Maybe a | returns None if the value provided is the empty value for the type. Just otherwise. Uses ramda's isEmpty function |
| fromPredicate :: (a -> Boolean) -> a -> Maybe a | returns Just if the predicate returns a truthy value. None otherwise. The second argument is used to evaluate the predicate |
| fromTry :: Try a => a -> Maybe a | returns Just on a Success. None on a Failure |
| fromResult :: Result a => a -> Maybe a | returns Just on a Ok. None on a Err |
| isEmpty :: Maybe a -> Boolean | returns true on None. False otherwise |
| match :: (Cases a, Maybe b) => a -> b -> c | returns the result of evaluating the cases with the provided value |
| equals :: a -> b -> Boolean | returns whether two values are equal. Reference to ramda's equals function |
$3
these are the methods that Just and None have defined.
| method | description |
| ------ | ----------- |
| match :: Cases a => Maybe b ~> a -> c | returns the result of evaluating the cases with the called object |
| get :: Maybe a ~> () -> a | returns the inner value. For None, the inner value is undefined |
| map :: Maybe a ~> (a -> b) -> Maybe b | returns Just of the mapped value if called on a Just. None otherwise |
| effect :: Maybe a ~> (a -> any) -> Maybe a | calls the provided function with the inner value and returns the same object if it is a Just. Does nothing otherwise |
| chain :: Maybe a ~> (a -> b) -> b | Call the received function with the inner value |
| equals :: Maybe a ~> b -> Boolean | Tests equality. Two Just are equal if their inner values are equal. Two Nones are always equal |
| onNone :: Maybe a ~> ( (() -> b) \| c) -> a \| b \| c | This function returns the inner value when the object is a Just. Otherwise either calls the provided function and returns it's result or in the case a value was provided, returns said value |
| isJust :: Maybe a ~> () -> Boolean | returns true if it is a Just. False otherwise |
| isNone :: Maybe a ~> () -> Boolean | returns true if it is a None. False otherwise |
| empty :: Maybe a ~> () -> None | return the empty value for the type. Always None |
Result
`
Result = Ok | Err
``