Bruce Dou
Bruce Dou

Reputation: 4761

Better code for the case of Erlang

Given:

C = case A of
  undefined ->
    "";
  Value ->
    Value
end

How would I express this as a single line?

Upvotes: 1

Views: 281

Answers (1)

Dustin
Dustin

Reputation: 90980

Besides the obvious (putting all of that code on one line), you could make a helper function like this:

with_default(undefined, D) -> D;
with_default(X, _) -> X.

with_default(X) -> with_default(X, "").

Upvotes: 3

Related Questions