Skip to content

Instantly share code, notes, and snippets.

View bennage's full-sized avatar
📺
please stay tuned

Christopher Bennage bennage

📺
please stay tuned
View GitHub Profile
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
public class When_overriding_GetHashCode
{
[Fact]
public void Equals_should_be_overridden()
{
public IEnumerable<IResult> DoSomethingBaseOnType(SomeBaseClass item)
{
var function = _typeToFunction[item.GetType()];
foreach (var result in function(item))
{
yield return result;
}
}
// what's a better way?
let fullName (first,last) =
match (first, last) with
"",_ | null,_ -> first
| _,null | _,"" -> last
| _ -> sprintf "%s.%s" first last
open System
open System.ComponentModel
open System.Web.Mvc
open Microsoft.FSharp.Reflection
// inspired by http://www.atrevido.net/blog/CommentView,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx
type MyModelBinder() =
let rec makeDefaultRecord ty =
@bennage
bennage / reduce.cs
Created September 12, 2010 04:54
reduce function, imperative version
// imperative version - uses a loop
static int Reduce(Func<int, int, int> reducer, IEnumerable<int> values)
{
int accum = 0;
foreach (var i in values)
{
accum = reducer(accum, i);
}
return accum;
}
@bennage
bennage / reduce.cs
Created September 12, 2010 05:00
reduce function, functional version
// functional version - recursion instead of a loop
static int ReduceF(Func<int, int, int> reducer, IEnumerable<int> values, int seed)
{
return values.Any()
? ReduceF(reducer, values.Skip(1), reducer(seed, values.First()))
: seed;
}
@bennage
bennage / pattern-matching.fs
Created November 19, 2010 20:08
a simple example of pattern matching in F#
type Shape =
| Circle of float
| Rectangle of double*double
| EquilateralTriangle of double
let pi = 3.141592654
let area shape =
match shape with
| Circle r -> r*r*pi
@bennage
bennage / AsyncDocumentStoreServerTests.cs
Created January 15, 2011 22:19
improved syntax for Raven's Silverlight unit tests
namespace Raven.Tests.Silverlight
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Client.Document;
using Client.Extensions;
using Database.Data;
using Database.Indexing;
using Document;
@bennage
bennage / Can_insert_async_and_get_sync.cs
Created January 17, 2011 15:43
example of a Raven client test on the .NET side
[Fact]
public void Can_insert_async_and_get_sync()
{
using (var server = GetNewServer(port, path))
{
var documentStore = new DocumentStore { Url = "http://localhost:" + port };
documentStore.Initialize();
var entity = new Company { Name = "Async Company" };
using (var session = documentStore.OpenAsyncSession())
@bennage
bennage / jslint-helper-for-wsh.js
Created April 26, 2011 07:20
a script for executing jslint on a command line in Windows. Execute with: cscript.exe jslint-helper-for-wsh.js file-to-test.js
/*jslint evil: true, white: true, onevar: true, undef: true, nomen: true, regexp: true, plusplus: true, bitwise: true, newcap: true, maxerr: 10, indent: 4 */
/*global JSLINT, WScript, ActiveXObject, Enumerator*/
(function () {
var jslint_path = 'jslint.js',
jslint_source = '',
utf8 = '',
fso = new ActiveXObject('Scripting.FileSystemObject'),
files = [],
args = WScript.Arguments;