Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Created December 7, 2013 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiantakle/7843962 to your computer and use it in GitHub Desktop.
Save christiantakle/7843962 to your computer and use it in GitHub Desktop.
// Ideas
//-- note (() -> IO), random function
//-- Maybe denote it with a lambda instead
//+ given :: (a -> Bool) -> (() -> IO) -> Maybe IO
//+ ifthen :: (a -> Bool) -> (() -> IO) -> Maybe IO
function ifthen(predicate, action) {
if (predicate()) {
action();
}
}
//-- Alias ------------------------------------------------------------
//+ given :: (a -> Bool) -> (() -> IO) -> Maybe IO
var given = ifthen;
//+ when :: (a -> Bool) -> (() -> IO) -> Maybe IO
var when = ifthen;
//+ ifelse :: (a -> Bool) -> (() -> IO) -> (() -> IO) -> Maybe IO
function ifelse(predicate, if_action, else_action) {
if (predicate()) {
if_action();
} else {
else_action();
}
}
//-- Examples ---------------------------------------------------------
ifthen(isLoggedIn, welcome);
given(isLoggedIn, welcome);
when(loggedIn, welcome);
//-- Make it polymorphic? With optional else path
given(isLoggedIn, welcome, signup);
ifelse(isLoggedIn, welcome, signup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment