feat: add IDictionary.TryGet to return Maybe<T>

This commit is contained in:
Kyle Ratti 2023-08-02 20:26:10 -04:00
parent 466f90cca5
commit 1b15162ba5
No known key found for this signature in database
GPG Key ID: 067C5D2D7C62E0F8

View File

@ -22,6 +22,9 @@ public static class MaybeExtensions
return Maybe.Empty<T>(); return Maybe.Empty<T>();
} }
public static Maybe<TValue> TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) =>
dict.TryGetValue(key, out var value) ? Maybe.Just(value) : Maybe.Empty<TValue>();
public static Maybe<TValue> TryGet<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key) => public static Maybe<TValue> TryGet<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key) =>
dict.TryGetValue(key, out var value) ? Maybe.Just(value) : Maybe.Empty<TValue>(); dict.TryGetValue(key, out var value) ? Maybe.Just(value) : Maybe.Empty<TValue>();