This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Foo | |
{ | |
private readonly IHttpClientFactory _httpClientFactory; | |
public Foo(IHttpClientFactory httpClientFactory) | |
=> _httpClientFactory = httpClientFactory; | |
public async Task<Bar> GetBarAsync() | |
{ | |
var httpClient = _httpClientFactory.GetClient(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Foo | |
{ | |
private readonly HttpClient _httpClient; | |
public Foo(HttpClient httpClient) | |
=> _httpClient = httpClient; | |
public async Task<Bar> GetBarAsync() | |
{ | |
await _httpClient.PostAsync(/* Some message */); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class (Functor t, Foldable t) => Traversable t where | |
sequenceA :: Applicative f => t (f a) -> f (t a) | |
-- t is IEnumerable | |
-- f is Task | |
-- a is T |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<IEnumerable<User>> FetchUsersAsync(IEnumerable<int> userIds) | |
{ | |
return await Task.WhenAll(userIds.Select(id => Database.LookupUserAsync(id))); | |
} | |
// Or, performing an eta-reduction | |
public async Task<IEnumerable<User>> FetchUsersAsync(IEnumerable<int> userIds) | |
{ | |
return await Task.WhenAll(userIds.Select(Database.LookupUserAsync)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<IEnumerable<User>> FetchUsersAsync(IEnumerable<int> userIds) | |
{ | |
var result = new List<User>(); | |
foreach (var userId in userIds) | |
{ | |
result.Add(await Database.LookupUserAsync(userId)); | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Task<IEnumerable<User>> FetchUsersAsync(IEnumerable<int> userIds) | |
{ | |
var u = userIds.Select(id => Database.LookupUserAsync(id)); | |
// Houston, we have a problem. | |
// | |
// u is of type IEnumerable<Task<User>>. | |
// We want Task<IEnumerable<User>> so that we can await | |
// on it. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Synchronous | |
public string ReadMyFile() | |
{ | |
using (var reader = File.OpenText("myfile.txt")) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
// Asynchronous |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Calling a function f with three parameters: x, y, and z | |
// f: ('a -> 'b -> 'c -> 'd) | |
// x: 'a | |
// y: 'b | |
// z: 'c | |
// result: 'd | |
let result = f x y z | |
// 'Enhancing' f to support parameters inside the Result wrapper | |
// f: ('a -> 'b -> 'c -> 'd) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (<!>) f = function | |
| Ok x -> Ok (f x) | |
| Error e -> Error e | |
let (<*>) fR xR = | |
match fR, xR with | |
| Ok f, Ok x -> Ok (f x) | |
| Error e1, Error e2 -> Error (e1 @ e2) | |
| Error e, _ | _, Error e -> Error e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let makeConfig (url: string) (name: string) (age: int) = | |
let nameResult = validateName name | |
let urlResult = validateUrl url | |
let ageResult = validateAge age | |
match nameResult, urlResult, ageResult with | |
| Ok name, Ok url, Ok age -> Ok { Name = name; Url = url; Age = age } | |
| Error e1, Error e2, Error e3 -> Error (e1 @ e2 @ e3) | |
| Error e1, Error e2, _ -> Error (e1 @ e2) | |
| Error e1, _, Error e2 -> Error (e1 @ e2) |