Skip to content

Instantly share code, notes, and snippets.

----------------------------- MODULE Liverpool -----------------------------
EXTENDS Naturals, FiniteSets, Integers
VARIABLES discard, stock, players, player_score, round, player_hand, top_discard, player_goal_state
Bool == { TRUE, FALSE }
Rank == 1..14
Suite == { "H", "S", "C", "D" }
using System;
using System.Linq;
using System.Threading;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public class C
{
public abstract class Maybe<T>
{
public abstract T Value { get; }
public virtual string Failure => null;
public Maybe<V> SelectMany<U, V>(Func<T, Maybe<U>> f, Func<T, U, V> g)
{
var that = f(this.Value);
@noblethrasher
noblethrasher / calendar.sql
Created August 23, 2017 22:29
A 100-year calendar table
create table Calendar
(
id int primary key identity,
date datetime unique not null,
day_of_week as datepart(dw, date),
month as datepart(month, date),
day_name as datename(dw, date),
year as datepart(year, date),
day_of_month as datepart(day, date),
week as datepart(week, date),
begin tran
create table Calendar
(
id int primary key identity,
date datetime not null
)
declare @dt datetime
set @dt = '1/1/2017'
@noblethrasher
noblethrasher / 45576292.sql
Last active August 9, 2017 23:35
Solution to quest 45576292 on Stack Overflow
set xact_abort on
begin tran
create table Calendar
(
id int primary key identity,
[date] datetime,
[day] as datepart(day, [date]) persisted,
[month] as datepart(month, [date]) persisted,
@noblethrasher
noblethrasher / fb.cs
Last active August 3, 2017 19:23
Pure OO FizzBuzz
using System;
using System.Collections.Generic;
namespace FizzBuzz
{
class Program
{
static void Main(string[] args)
{
FizzBuzz(0);
static class RegexUtil
{
static readonly memo = new Dictionary<string, Regex>();
public static bool IsMatch(this string s, RegexOptions? options = null)
{
if(!memo.TryGetValue(s, out RegEx rgx))
memo.Add(s, rgx = new RegEx(s, options ?? RegExOptions.None));
return rgx.IsMatch(s);
@noblethrasher
noblethrasher / Traits.cs
Created October 13, 2015 21:12
An example of using type class style, ad-hoc polymorphism
class Program
{
static void Main(string[] args)
{
var buffy = new Person("Buffy Summers", DateTime.Parse("1/19/1981"));
var xander = new Person("Xander Harris", DateTime.Parse("5/2/1981"));
var willow = new Person("Willow Rosenberg", DateTime.Parse("12/3/1980"));
var oldest = OrderModule<Person>.Max(buffy, xander, willow);
}
@noblethrasher
noblethrasher / MaybeMonad.cs
Created October 13, 2015 20:18
An example of implementing a monad in C#/LINQ
namespace Prelude
{
public static class Utils
{
public struct RetrievedValue<T>
{
readonly T value;
readonly string failed;
internal RetrievedValue(T value, string failed)