Skip to content

Instantly share code, notes, and snippets.

View alexander-williamson's full-sized avatar

Alexander Williamson alexander-williamson

View GitHub Profile
// guiding principles
// 1. to think in command / query separation - there are commands and there are queries
// strict CQRS recommends that commands do not return results apart from creation which returns an ID
// I can agree with that at a repo events level but not sure about command handlers however that does work here
// Useful links
// https://github.com/gregoryyoung/m-r/blob/master/SimpleCQRS/CommandHandlers.cs
interface ICommandHandler<Command, ResultType> {
// you don't really need this interface but I am enforcing command handler shapes with it
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<div class="editor">
<h2>Input</h2>
<textarea id="editor" name="input-textarea" onchange="update(this)" placeholder="Try ^6hello world">
</textarea>
<ul class="examples">
<li><button type="button" onClick="window.rainbow()">Rainbow</button>
</ul>
</div>
@alexander-williamson
alexander-williamson / javascript.js
Last active September 10, 2019 22:25
Javascript Bowling Kata
function bowlingScore(frames) {
const rolls = frames.split("").filter(function(value, index) { return value != " " });
var frames = getFramesFromRolls(rolls);
var score = calculateScoreFromFrames(rolls, frames);
return score;
}
function getFramesFromRolls(rolls) {
if (rolls === undefined || rolls.length == 0) { return []; }
@alexander-williamson
alexander-williamson / mario.js
Created August 27, 2019 14:50
Binary Search Kata in Javascript
function binSearch(arr, toSearch) {
var lowerBound = 0, upperBound = arr.length -1;
while(true) {
var index = Math.floor((upperBound - lowerBound) / 2 + lowerBound);
var current = arr[index];
if(current === toSearch) return index;
if(current > toSearch) {
using System;
using System.Linq;
using NUnit.Framework;
namespace Alexw.Katas.BinarySearch
{
public class BinarySearchTests
{
private static readonly DataItem[] Items =
{
@alexander-williamson
alexander-williamson / gist:4f0726f3d31f38c16c284f164b46c5f9
Created November 27, 2017 21:12 — forked from efbenson/gist:4259486
Automapper for MongoDB ObjectIds
Mapper.CreateMap<List<ObjectId>, List<string>>().ConvertUsing(o => o.Select(os => os.ToString()).ToList());
Mapper.CreateMap<List<string>, List<ObjectId>>().ConvertUsing(o => o.Select(os => ObjectId.Parse(os)).ToList());
Mapper.CreateMap<ObjectId, string>().ConvertUsing(o => o.ToString());
Mapper.CreateMap<string, ObjectId>().ConvertUsing(s => ObjectId.Parse(s));
@alexander-williamson
alexander-williamson / KibanaClient.cs
Created June 14, 2017 16:00
How to send files to logz.io kibana and have properties accepted as fields
using System;
using System.Net;
using RestSharp;
namespace Alexw.ParkingAvailable
{
public class KibanaClient
{
private readonly IRestClient _client;
private readonly string _token;
@alexander-williamson
alexander-williamson / TcpPorts.cs
Created May 28, 2017 19:02
Get free TCP Ports
using System.Net;
using System.Net.Sockets;
// taken from https://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net
public static class TcpPorts
{
public static int GetFreeTcpPort()
{
var l = new TcpListener(IPAddress.Loopback, 0);
/// <summary>
/// Loop until a condition is met and your function returns true
/// </summary>
/// <param name="action">The delegate to run. Return true to exit the loop.</param>
/// <param name="delay">Return a timespan for the delay between loops</param>
/// <param name="whentoStop">Return true when the loop should stop when catching exceptions or looping</param>
public static void DoUntil(Func<bool> action, Func<int, TimeSpan, TimeSpan> delay, Func<int, TimeSpan, bool> whentoStop)
{
var startedUtc = DateTime.UtcNow;
var amountOfRetriesAllowed = 0;
@alexander-williamson
alexander-williamson / Owin.Authentication.Examples.cs
Created December 30, 2016 12:54
Examples for simple owin authentication
# Your use statement can do all the work for you
app.Use(async (context, next) =>
{
if (!IsAuthenticated(context))
{
context.Response.StatusCode = 401;
var bytes = System.Text.Encoding.UTF8.GetBytes("You are not authorised!");
context.Response.Body.Write(bytes, 0, bytes.Length);
await Task.FromResult(context);