Skip to content

Instantly share code, notes, and snippets.

open System
open System.IO
let printMeanScore(row: string) =
let columns = row.Split('\t')
let name = columns.[0]
let id = columns.[1]
let scores = columns |> Array.skip 2 |> Array.map float
let avg = scores |> Array.average
let min = scores |> Array.min
@mrpmorris
mrpmorris / Program.cs
Created June 4, 2022 18:13
SpinLock extension without a ref
using ConsoleApp11;
var sl = new SpinLock();
for (int i = 0; i < 3; i++)
{
_ = Task.Run(() => DoSomething(ref sl));
}
Console.WriteLine("Ready");
Console.ReadLine();
@mrpmorris
mrpmorris / Client.Program.cs
Created June 2, 2022 10:19
Slow TcpClient DNS resolution
using System.Net.Sockets;
int iterations = 10;
long totalElapsed = 0;
for (int i = 0; i < iterations; i++)
{
var tcpClient = new TcpClient();
var sw = System.Diagnostics.Stopwatch.StartNew();
await tcpClient.ConnectAsync("localhost", 6510);
long elapsed = sw.ElapsedMilliseconds;
@mrpmorris
mrpmorris / app.css
Created May 20, 2022 09:12
The CSS I put in the top of every new web app
:root {
box-sizing: border-box;
}
*, ::before, ::after {
box-sizing: inherit;
}
html {
scroll-behavior: smooth;
}
@mrpmorris
mrpmorris / IntegrationTestsServer.cs
Created May 12, 2022 15:20
ASP.NET integration testing
namespace Whatever
public static class IntegrationTestsServer
{
public static string? IdentityConfirmationCode { get; private set; }
public static string? SignInOneTimeCode { get; private set; }
public static IOptions<GameServerOptions> GameServerOptions { get; set; }
private static readonly HttpClient HttpClient;
private static readonly IConfiguration Configuration;
@mrpmorris
mrpmorris / EnumDescriptions.cs
Created May 6, 2022 08:16
Cached lookups for enum [Description] attributes
using System.Collections.Concurrent;
namespace Whatever;
public static class EnumDescriptions
{
private readonly static ConcurrentDictionary<Type, EnumDescriptionsCache> Cache = new();
public static IEnumerable<KeyValuePair<Enum, string>> GetValuesAndDescriptions(Type enumType) =>
GetOrAdd(enumType).GetAll();
@mrpmorris
mrpmorris / ApiEffects.cs
Created May 3, 2022 14:41
Fluxor auto-dispatch API calls
using System.Collections.Concurrent;
using System.Reflection;
using CarePlace.Client.Services;
using Fluxor;
using MediatR;
namespace XXXXXXX.Client.ViewState;
public class ApiEffects
{
using Gambit.GameServer.Contracts;
using Gambit.GameServerIntegrationTests.Drivers;
namespace Gambit.GameServerIntegrationTests.Features.Users;
[Binding]
public class SignInStepDefinitions
{
private readonly UserDriver UserDriver;
// BenchmarkDotNet benchmarks to test the speed of .net framework immutable classes
// See this image for results => https://user-images.githubusercontent.com/3111981/165134368-5875560d-47ef-4627-b51f-854e0cf24d36.png
using BenchmarkDotNet.Attributes;
using System.Collections.Immutable;
namespace ConsoleApp20;
[MemoryDiagnoser]
public class BM
@mrpmorris
mrpmorris / Benchmarks.cs
Created April 23, 2022 21:45
An immutable array class that uses multi-levels of arrays to speed up copying
// Benchmarks
using BenchmarkDotNet.Attributes;
using LanguageExt;
using System.Collections.Immutable;
namespace MyImmutableArray;
[MemoryDiagnoser]
public class Benchmarks
{