Suppose I want to do repeated map lookups.
In C#, I can use return
for a "flat" control-flow:
Thing v = null;
if (a.TryGetValue(key, out v))
{
return v;
}
if (b.TryGetValue(key, out v))
{
return v;
}
if (c.TryGetValue(key, out v))
{
return v;
}
return defaultValue;
It's a bit ugly, but quite readable.
In F#, which I am less familiar with, I would use match
expressions:
match a.TryGetValue(key) with
| (true, v) -> v
| _ ->
match b.TryGetValue(key) with
| (true, v) -> v
| _ ->
match c.TryGetValue(key) with
| (true, v) -> v
| _ -> defaultValue
This feels wrong - the code gets more and more nested with each map.
Does F# provide a way to "flatten" this code?