Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Last active September 14, 2017 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeMKH/cabd17faa9290d6f15592c5df8eded9f to your computer and use it in GitHub Desktop.
Save MikeMKH/cabd17faa9290d6f15592c5df8eded9f to your computer and use it in GitHub Desktop.
Code examples for the talk A Divine Data Comedy
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.3.0-beta2-build3683" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.ValueTuple" Version="4.3.1" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using static System.ValueTuple;
using Xunit;
namespace dotnet
{
public class ExampleTests
{
[Fact]
public void ImperativeStyle()
{
var s = "Midway in our life's journey, I went astray";
var count = 0;
foreach(var word in s.Split(' '))
{
var stripped =
Regex.Replace(word, "('s)|\\W+", "");
if (stripped.Length > 2)
count++;
}
Assert.Equal(6, count);
}
[Fact]
public void NestedStyle()
{
var s = "Midway in our life's journey, I went astray";
var count = Enumerable.Count(
Enumerable.Where(
Enumerable.Select(
s.Split(' '),
word => Regex.Replace(
word, "('s)|\\W+", "")),
stripped => stripped.Length > 2));
Assert.Equal(6, count);
}
[Fact]
public void DeclarativeStyle()
{
var count = "Midway in our life's journey, I went astray"
.Split(' ')
.Select(word =>
Regex.Replace(word, "('s)|\\W+", ""))
.Where(word => word.Length > 2)
.Count();
Assert.Equal(6, count);
}
[Fact]
public void DataFlowManipulationsExample()
{
var count = new[] {
"Midway in our life's journey, I went astray"
}
.SelectMany(s => s.Split(' '))
.Select(word =>
Regex.Replace(word, "('s)|\\W+", ""))
.Where(word => word.Length > 2)
.Aggregate(0, (number, _) => number + 1);
Assert.Equal(6, count);
}
[Fact]
public void MapExample()
{
var numbers = new [] {3, 9, 10}
.Select(x => x * 10)
.Select(x => x + 3);
Assert.Equal(new [] {33, 93, 103}, numbers);
}
[Fact]
public void BindExample1()
{
var list = new [] {
"Midway in our life's journey,",
"I went astray"
}
.SelectMany(s => s.Split(' '))
.Select(w => Regex.Replace(w, "('s)|\\W+", ""));
Assert.Equal(
new [] { "Midway", "in", "our", "life", "journey", "I", "went", "astray" },
list);
}
[Fact]
public void BindExample2()
{
var list =
from sentences in
new [] {
"Midway in our life's journey,",
"I went astray"
}
from words in sentences.Split(' ')
select Regex.Replace(words, "('s)|\\W+", "");
Assert.Equal(
new [] { "Midway", "in", "our", "life", "journey", "I", "went", "astray" },
list);
}
[Fact]
public void FilterExample()
{
var numbers = new [] {3, 9, 10, 33, 100}
.Where(n => n % 2 != 0);
Assert.Equal(new [] {3, 9, 33}, numbers);
}
[Fact]
public void FoldExample()
{
var sum = new [] {3, 9, 10, 33, 100}
.Aggregate(0, (m, n) => m + n);
Assert.Equal(
new [] {3, 9, 10, 33, 100}.Sum(), sum);
}
[Fact]
public void MutateExample()
{
var visited = new List<int>();
new [] {3, 9, 10, 33, 100}
.Where(n => n % 3 == 0)
.ToList()
.ForEach(n => visited.Add(n));
Assert.Equal(new [] {3, 9, 33}, visited);
}
[Fact]
public void GroupByExample()
{
var groups = new [] {3, 9, 10, 33, 100}
.GroupBy(n => n % 2 == 0)
.ToDictionary(g => g.Key, g => g.ToList());
Assert.Equal(
new Dictionary<bool, List<int>>()
{
{true, new List<int>() {10, 100}},
{false, new List<int>() {3, 9, 33}}
},
groups);
}
[Fact]
public void OrderByExample()
{
var unordered = new [] {33, 9, 100, 3, 10}
.OrderBy(n => n);
var ordered = new [] {33, 9, 100, 3, 10};
Array.Sort(ordered);
Assert.Equal(ordered, unordered);
}
string G (int x) => x.ToString();
bool F (string y) => int.TryParse(y, out int _);
class Identity
{
public static T [] Of<T>(T x) => new [] { x };
}
[Fact]
public void F_Following_G_Identity()
=> Assert.True(
Identity.Of(33)
.Select(G)
.Select(F)
.First()
);
[Fact]
public void IdentityExample()
{
var count = Identity.Of(
"Midway in our life's journey, I went astray")
.SelectMany(s => s.Split(' '))
.Select(word =>
Regex.Replace(word, "('s)|\\W+", ""))
.Where(word => word.Length > 2)
.Aggregate(0, (number, _) => number + 1);
Assert.Equal(6, count);
}
[Fact]
public void StringExample()
{
var count = "Midway in our life's journey, I went astray"
.Split(' ')
.Select(word => Regex.Replace(word, "('s)|\\W+", ""))
.Where(word => word.Length > 2)
.Count();
Assert.Equal(6, count);
}
}
}
@MikeMKH
Copy link
Author

MikeMKH commented Sep 14, 2017

Here is the presentation I gave that goes with this gist: https://www.slideshare.net/secret/ySVlRZU0HF3E8L

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment