8 ms·
So wrap it in a function default(Dict, Key, Default) -> case dict:is_key(Key, Dict) of true -> dict:fetch(Key, Dict); _ -> Default
by sofuture 16y ago
So wrap it in a function
default(Dict, Key, Default) ->
case dict:is_key(Key, Dict) of
true -> dict:fetch(Key, Dict);
_ -> Default
end.
- jrockway 16y agoDunno about Erlang, but in Haskell, you can actually call this function ||: import Prelude hiding ((||)) import qualified Data.Map as M import Data.Maybe (||) = flip fromMaybe main = print $ result || "OH NOES!" where result = M.lookup "baz" map map = M.fromList [("foo", "bar"), ("bar", "baz")] ghci> main "OH NOES!" With some typeclass magic, you can even make this work for arbitrary types. But don't waste your time with this, since it's already built in as `mplus`: ghci> Nothing `mplus` Just 42 Just 42 ghci> Left "OH NOES" `mplus` Right "it worked" Right "it worked"
- davidw 16y agoBelieve it or not, I actually had figured that out. The point was about the language in general, its verbosity, and the ability to string things together.