This file contains 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
static int Double(int i) => i * 2; |
This file contains 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 User | |
{ | |
public readonly Guid Id; | |
public readonly string Name; | |
public readonly int Age; | |
public User(Guid id, string name, int age) | |
{ | |
Id = id; | |
Name = name; |
This file contains 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
Option<int> aValue = 2; | |
aValue.Map(x => x + 3); // Some(5) | |
Option<int> none = None; | |
none.Map(x => x + 3); // None | |
//Left -> Some, Right -> None | |
aValue.Match(x => x + 3, () => 0); // 5 | |
none.Match(x => x + 3, () => 0); // 0 |
This file contains 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
new int[] { 2, 4, 6 }.Map(x => x + 3); // 5,7,9 | |
new List<int> { 2, 4, 6 }.Map(x => x + 3); // 5,7,9 | |
//Prefer use List (Immutable list) | |
List(2, 4, 6).Map(x => x + 3); // 5,7,9 |
This file contains 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
static Func<int, int> Add2 = x => x + 2; | |
static Func<int, int> Add3 = x => x + 3; | |
static int Add5(int x) => Add2.Compose(Add3)(x); |
This file contains 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
static Option<double> Half(double x) | |
=> x % 2 == 0 ? x / 2 : Option<double>.None; | |
[Fact] | |
public void bind_monad() | |
{ | |
Option<double>.Some(3).Bind(x => Half(x));// None | |
Option<double>.Some(4).Bind(x => Half(x));// Some(2) | |
} |
This file contains 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
static Func<string, string> GenerateGuidForUser = user => user + ":" + Guid.NewGuid(); | |
static Func<string, string> GenerateGuidForUserMemoized = memo(GenerateGuidForUser); | |
[Fact] | |
public void memoization_example() | |
{ | |
GenerateGuidForUserMemoized("spongebob");// spongebob:e431b439-3397-4016-8d2e-e4629e51bf62 | |
GenerateGuidForUserMemoized("buzz");// buzz:50c4ee49-7d74-472c-acc8-fd0f593fccfe | |
GenerateGuidForUserMemoized("spongebob");// spongebob:e431b439-3397-4016-8d2e-e4629e51bf62 | |
} |
This file contains 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 static Either<Exception, string> GetHtml(string url) | |
{ | |
var httpClient = new HttpClient(new HttpClientHandler()); | |
try | |
{ | |
var httpResponseMessage = httpClient.GetAsync(url).Result; | |
return httpResponseMessage.Content.ReadAsStringAsync().Result; | |
} | |
catch (Exception ex) { return ex; } | |
} |
This file contains 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
static Func<int, int, int> Multiply = (a, b) => a * b; | |
static Func<int, int> TwoTimes = par(Multiply, 2); | |
[Fact] | |
public void partial_app_example() | |
{ | |
Multiply(3, 4); // 12 | |
TwoTimes(9); // 18 | |
} |
This file contains 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
[Fact] | |
public void fold_vs_reduce() | |
{ | |
//fold takes an explicit initial value for the accumulator | |
//Can choose the result type | |
var foldResult = List(1, 2, 3, 4, 5) | |
.Map(x => x * 10) | |
.Fold(0m, (x, s) => s + x); // 150m | |
//reduce uses the first element of the input list as the initial accumulator value |
OlderNewer