Skip to content

Instantly share code, notes, and snippets.

@philiplaureano
philiplaureano / gist:f481e6b0ec68173d04d9
Last active August 29, 2015 14:04
An example of how to use LinFu's new ability to append extension methods to an existing dynamic type and treat it as if it were an instance method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using LinFu.Finders;
using LinFu.Finders.Interfaces;
using LinFu.Reflection;
using CLRDynamicObject = System.Dynamic.DynamicObject;
@philiplaureano
philiplaureano / gist:51413336b6c3c6d604e3
Last active September 15, 2016 11:51
Currying and chaining funcs and actions together in C#
public static class FunctionalBindingExtensions
{
public static Action<T2> Bind<T1, T2>(this Action<T1, T2> action, Func<T1> getValueFunc)
{
return action.Bind(getValueFunc());
}
public static Action<T1> Bind<T1, T2>(this Action<T1, T2> action, Func<T2> getValueFunc)
{
@philiplaureano
philiplaureano / gist:5689661
Created June 1, 2013 08:17
Here's the simplest possible BDD framework I could conceive, using Nemerle. It uses FizzBuzz as a demonstration.
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.Collections.Generic;
using System.Console;
using System.Linq;
public class FizzBuzzer
@philiplaureano
philiplaureano / gist:5423458
Created April 19, 2013 21:46
An example of how I used CFF explorer to generate the byte array for sampling and roundtripping an MS-DOS header from a .NET assembly.
public ShouldMatchGivenHeaderBytesWhenWrittenToTargetStream() : void
{
def bytes = array[ // dos header start
0x4d : byte, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
using Nemerle;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
@philiplaureano
philiplaureano / gist:5423009
Created April 19, 2013 20:28
This extension method is what allows Tao to find the first byte mismatch within two given streams, regardless of their size.
public static ShouldMatch(this stream : Stream, other : Stream) : void
{
def hash = stream.GetHash();
def otherHash = other.GetHash();
mutable smallerStream = other;
when(other.Length > stream.Length)
{
smallerStream = stream;
}
@philiplaureano
philiplaureano / gist:5422630
Created April 19, 2013 19:31
This sample shows how Tao uses hashing to verify all of its streams and byte arrays. It makes CLR metadata and bytecode verification possible since we can use it to incrementally verify small chunks of data against a stream of bytes from an assembly.
using Nemerle;
using Nemerle.Assertions;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
@philiplaureano
philiplaureano / gist:5279046
Created March 31, 2013 00:58
An example of how you can use Design by Contract macros and Non-nullable type macros in Nemerle to write more reliable code
public FlushContentsTo([NotNull] outputStream : Stream) : uint
requires outputStream.CanWrite
{
def startPosition = 0;
_heap.Seek(startPosition);
outputStream.Seek(startPosition);
def bytes = _heap.ToArray();
def writer = BinaryWriter(outputStream);
writer.Write(bytes);
var testHarness = new TestHarness();
// The processors decide which methods should be replaced and how they should behave once the method is called
var consoleTestProcessor = new ConsoleTestProcessor();
testHarness.Processors.Add(consoleTestProcessor);
var fileSystemTestProcessor = new FileSystemTestProcessor();
fileSystemTestProcessor.AddMockFile(@"c:\foo.txt", "bar");
testHarness.Processors.Add(fileSystemTestProcessor);
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="sdkDir" value="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\bin"/>
<add key="sdkDirUnderVista" value="C:\Program Files (x86)\Microsoft.NET\SDK\v2.0\Bin"/>
</appSettings>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>