Skip to content

Instantly share code, notes, and snippets.

@DavidArno
DavidArno / gist:ff9f08392988dab2ca1e5ba1d87d684d
Created August 2, 2018 11:56
Switch expression example
public class C
{
RedisValue ToRedisValue(object obj)
{
return obj switch
{
null => Null,
RedisValue v => v,
string v => (RedisValue)v,
int v => v,
@DavidArno
DavidArno / FizzBuzz2.cs
Created October 17, 2017 12:23
FizzBuzz using SuccincT
IEnumerable<string> GenerateFizzBuzzList()
{
    var fizzes = Cycle("", "", "Fizz");
    var buzzes = Cycle("", "", "", "", "Buzz");
    var words = fizzes.Zip(buzzes, (f, b) => f + b);
    var numbers = Range(1, 100);
    return numbers.Zip(words, (n, w) => w == "" ? n.ToString() : w);
}
@DavidArno
DavidArno / FizzBuzz.cs
Created October 17, 2017 11:03
Fizzbuzz in C# using tuples, pattern matching and local functions
using System;
namespace FizzBuzz
{
static class Program
{
static void Main()
{
for (var i = 1; i <= 100; i++)
{
@DavidArno
DavidArno / gist:a09aa73764231c74077f0c4ffac5cc2d
Last active July 14, 2017 13:25
Using C# 7 features, throw expressions & x is T y
public Bar DoSomething(Foo uncheckedFoo) =>
uncheckedFoo is Foo foo
? DoSomething2(foo)
: throw new ArgumentNullException(nameof(uncheckedFoo));
private Bar DoSomething2(Foo foo) => // foo is definitely set here
@DavidArno
DavidArno / gist:ec6654e48d82143dbd8dd0f89b67bfb5
Created March 17, 2017 11:14
Siwtch on type of T example for Jonathan Channon
public void F<T>()
{
switch (typeof(T))
{
case Type t when t == typeof(Foo): break;
case Type t when t == typeof(Bar): break;
}
}
@DavidArno
DavidArno / example.py
Created September 28, 2016 20:49
Example of python with else if
if condition_1:
statement_1()
else if condition_2:
statement_2()
else if condition_3:
statement_3()
else:
statement_9()
@DavidArno
DavidArno / gist:77894aa795d175e76590
Created September 22, 2015 11:16
Reply to blog.ploeh.dk-2015-09-21-public-types-hidden-in-plain-sight
<div class="comment">
<div class="comment-author">David Arno</div>
<div class="comment-content">
In your summary, you state "The main disadvantage of using the internal access modifier is that you can't easily unit test such a class".
Your entire post builds a complex, messy workaround to this through marking active methods as obsolete and having to wrap tests in "ignore warning"
pragmas. Yet the whole article misses the elephant in the room: internal classes are implementation details; they should not be unit tested directly.
Implementation details change, leading to brittle tests that need rewriting as those details change. Unit tests should test public APIs. If internal code
cannot be reached via a public API, then delete it as it has no purpose.
</div>
<div class="comment-date">2015-08-22 12:15 UTC</div>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace GithubWikiDoc
{
@DavidArno
DavidArno / SimplifiedCat
Created June 1, 2015 12:45
Simplified cat
public class Cat
{
public string Name { get; set; }
public string Color { get; set; }
public Cat()
{
Cat("Unamed", "gray");
}
DoSomethingCodeA();
try
{
var exceptionsToBeIgnored = false;
foreach (var x in SomeExpression)
{
exceptionsToBeIgnored = true;
DoSomethingCodeB();
exceptionsToBeIgnored = false;
}