Skip to content

Instantly share code, notes, and snippets.

@dtchepak
dtchepak / echo.rb
Last active September 12, 2022 07:24
Simple Ruby HTTP server to echo whatever GET or POST requests come through. Largely based on https://www.igvita.com/2007/02/13/building-dynamic-webrick-servers-in-ruby/.
# Reference: https://www.igvita.com/2007/02/13/building-dynamic-webrick-servers-in-ruby/
require 'webrick'
class Echo < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
puts request
response.status = 200
end
def do_POST(request, response)
puts request
@dtchepak
dtchepak / sample.cs
Created August 14, 2012 11:59
SAMPLE: Sub properties using reflection
// SAMPLE ONLY -- DON'T USE FOR REAL STUFF UNLESS YOU'VE REVIEWED AND TESTED :)
// Needs work to make robust: check property type's class has default ctor and so on.
// Can also filter out interfaces as these will be auto-subbed.
public static T SubFor<T>() where T : class
{
var gettableVirtualProps = typeof(T).GetProperties()
.Where(x => x.CanRead && x.GetGetMethod().IsVirtual)
.Select(x => x);
var sub = Substitute.For<T>();
@dtchepak
dtchepak / Node.cs
Created July 8, 2012 13:10
Monadic parser combinators in C#, attempting to parse some basic text markup
using System;
using System.Collections.Generic;
using System.Linq;
namespace MarkupParser
{
public abstract class Node
{
public static Parser<String> TextParser()
{
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
namespace NSubWorkshop
@dtchepak
dtchepak / Monoid.kt
Last active October 31, 2017 23:40
Kotlin monoid attempt
/**
* A type [T] with an associative binary operation.
*
* Must satisfy the associative property:
* `combine(combine(a, b), c) == combine(a, combine(b, c))`
*/
interface Semigroup<T> {
fun combine(a: T, b: T): T
@dtchepak
dtchepak / a.fs
Created September 25, 2015 05:08
FsCheck: Replaying a particular seed
// Based on example from http://fsharpforfunandprofit.com/posts/property-based-testing/
[<Test>]
let repro() =
let seed = (771943725,296061824) // seed reported by failed FsCheck output
let config = {
Config.QuickThrowOnFailure with
Replay = Random.StdGen seed |> Some
}
Check.One (config, prop)
@dtchepak
dtchepak / Functors.cs
Created April 30, 2012 13:12
Attempt at C# Functors
public static class Functors
{
public static IEnumerable<B> fmap<A,B>(this IEnumerable<A> functor, Func<A, B> f)
{
return functor.Select(f);
}
public static B? fmap<A, B>(this A? functor, Func<A, B> f) where A : struct where B : struct
{
return functor == null ? null : (B?) f(functor.Value);
public class DiagnosticsSubstitutionContext : ISubstitutionContext
{
[ThreadStatic]
private static IList<IArgumentSpecification> LastDequeuedList;
private static ConcurrentDictionary<IArgumentSpecification, ArgumentInfo> ArgumentInfos { get; } = new ConcurrentDictionary<IArgumentSpecification, ArgumentInfo>();
private class ArgumentInfo
{
public string CreationStack { get; }
public int DequeueCounter { get; set; }
//Extensions.cs
public static IObservable<T> ObservableFunc<T>(this Func<T> f) {
return Observable.Create<T>(obs => {
try {
obs.OnNext(f());
obs.OnCompleted();
} catch (Exception e) {
obs.OnError(e);
}
return () => {};
@dtchepak
dtchepak / rx.cs
Last active January 4, 2016 07:19
Rather than explicitly unsubscribing observers, is building completion into all observables a good idea? (For observables that don't naturally complete; streams of mouse moves, driver events etc)
var done = new Subject<Unit>();
var status = Observable
.Interval(TimeSpan.FromMilliseconds(450))
.Select(_ => driver.Status()) // driver keep-alive
.TakeUntil(done);
// On dispose, window close, etc...
done.CompletedWith(Unit.Value);