arliss_obsidian/fp/Classes/Alternative/Plus.md
Orion Kindel d74fc46755
update
2024-09-24 14:53:59 -05:00

1.0 KiB

Extends Alt with an empty value:

class Alt f <= Plus f where
  empty :: forall a. f a

[!info] Conceptually, this lives next to Applicative in my mind; in Maybe for example pure and empty are aliases for Just and Nothing.

In Array, empty is empty array []. In Maybe, empty is Nothing.

The most common place you'll see empty is when using MaybeT as an early return:

logDebug :: String -> Effect Unit
logDebug msg = void $ runMaybeT do
  env <- MaybeT $ Process.lookupEnv "NODE_ENV"
  when (env /= "DEVELOPMENT") empty
  lift $ Console.log msg

this is equivalent to:

logDebug :: String -> Effect Unit
logDebug msg = do
  menv <- Process.lookupEnv "NODE_ENV"
  case menv of
    Just env ->
      if env == "DEVELOPMENT" then
        Console.log msg
      else
        pure unit
    Nothing -> pure unit

[!info] Note that guard is a shorthand available for when (..) empty