4 ms·
I've used Erlang for a while, although sporadically. Honestly, I think I can do more with less code in Ruby, though. Erlang's great for the things it's good a
by davidw 16y ago
I've used Erlang for a while, although sporadically. Honestly, I think I can do more with less code in Ruby, though. Erlang's great for the things it's good at, but brevity and clarity are really things I think are critical.
Here's a small example I bumped into the other day, providing a default value if a hash value isn't present:
foo[:bar] || "beebop"
vs
case (catch dict:fetch(bar, Foo)) of {'EXIT', _} ->
"beebop";
Val -> Val
end
(or something close, there might be a typo)
- sofuture 16y agoSo 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.
- deleted 16y ago[deleted]
- joshhart 16y agoThis is built into the Scala API - foo.getOrElse(bar, "beebop") Stuff like this should be built into the standard language.
- koenigdavidmj 16y agoPython has it too: * foo.get(bar) will return None if the element is not there * foo.get(bar, "beebop") does what your Scala example does * foo.setdefault works just like foo.get. However, if foo did not have key bar when it was called, it will fill the key in with the default value before returning. * collections.defaultdict is also worth a look.