Skip to content

Instantly share code, notes, and snippets.

View leppie's full-sized avatar
🤡

Llewellyn Pritchard leppie

🤡
  • Cape Town, South Africa
View GitHub Profile
@leppie
leppie / gist:2868328
Created June 4, 2012 13:20
CallWithEscapeContinuation Usage
using System;
using System.Linq;
class Program
{
sealed class EscapeContinuation<T> : Exception
{
public T Value { get; private set; }
public void Raise(T value)
@leppie
leppie / gist:3187332
Created July 27, 2012 10:29
Binary period
(define (>> n i) (bitwise-arithmetic-shift n (- i)))
(define << bitwise-arithmetic-shift-left)
(define & bitwise-and)
(define (split n p)
(let* ((bc (bitwise-length n))
(mid (div bc 2)))
(define (get-bit-seq s c)
(let ((rs (- bc s c))
(m (- (<< 1 c) 1)))
@leppie
leppie / GetLastChangesetNumber.cs
Created September 8, 2012 06:52
GetLastChangesetNumber
using System;
using System.Text.RegularExpressions;
class Program
{
static int Main(string[] args)
{
string line = null;
while ((line = Console.ReadLine()) != null)
{
@leppie
leppie / gist:5081390
Created March 4, 2013 10:34
Roslyn stuff
var ast = SyntaxTree.ParseText(@"
namespace Foo {
#define MyIf = if
#define MyElse = else
public class some
{
public void someMethod()
{
MyFor(int i = 0; i < 10; i++)
@leppie
leppie / gist:5099596
Created March 6, 2013 14:20
IronScheme eval in C#
using IronScheme;
using System;
class Program
{
static void Main(string[] args)
{
var kk = "'hello".Eval();
Console.WriteLine(kk);
@leppie
leppie / gist:5099693
Created March 6, 2013 14:35
Map with variable number of lists in C#
static IEnumerable<T> Map<T>(this Func<IEnumerable<T>, T> f, params IEnumerable<T>[] arr)
{
var enums = Array.ConvertAll(arr, x => x.GetEnumerator());
try
{
while (enums.All(x => x.MoveNext()))
{
yield return f(enums.Select(x => x.Current));
}
}
var re = new Regex(string.Format(@"
(({0} # switch
(?<name>[_A-Za-z][_\w]*) # name (any legal C# name)
({1} # sep + optional space
(((""(?<value>((\\"")|[^""])*)"")| # match a double quoted value (escape "" with \)
('(?<value>((\\')|[^'])*)'))| # match a single quoted value (escape ' with \)
(\{{(?<arrayval>[^\}}]*)\}})| # list value (escaped for string.Format)
(?<value>\S+)) # any single value
)?)| # sep option + list
(((""(?<value>((\\"")|[^""])*)"")| # match a double quoted value (escape "" with \)
@leppie
leppie / gist:5293332
Created April 2, 2013 15:53
Scheme Unicode identifiers
private static bool IsLetterOrUnicodeSchemeIdentifier(char num)
{
if (num < '\x0080')
{
return char.IsLetter(num);
}
switch (char.GetUnicodeCategory(num))
{
case UnicodeCategory.UppercaseLetter:
case UnicodeCategory.LowercaseLetter:
@leppie
leppie / gist:5299147
Last active December 15, 2015 17:49
Explaining thread safety
class Foo
{
static string bar;
internal static readonly Random RANDOM = new Random();
internal const int SLEEP1 = 10;
internal const int SLEEP2 = 25;
public void Baz(string value)
{
bar = "#1: " + value;
@leppie
leppie / gist:5362154
Created April 11, 2013 09:57
List<T> in terms of Cons<Car,Cdr>
class Cons<Car, Cdr>
{
public Car Car { get; set; }
public Cdr Cdr { get; set; }
}
class List<T> : Cons<T, List<T>>
{
}