Skip to content

Instantly share code, notes, and snippets.

View neoGeneva's full-sized avatar

Philip Cox neoGeneva

  • Wellington, New Zealand
View GitHub Profile
@neoGeneva
neoGeneva / RedBlackTree.fsx
Created June 30, 2011 11:13
Red-Black Tree in F#
// Inspired by: http://jng.imagine27.com/articles/2011-06-28-141124_purely_functional_types_red_black_trees_in_qi.html
type TreeNode<'a> = { Key: int; Val: 'a }
type Color = Red | Black
type Tree<'a> = { Color: Color; LTree: Tree<'a> option; TreeNode: TreeNode<'a>; RTree: Tree<'a> option; }
let makeTreeBlack tree =
match tree with
@neoGeneva
neoGeneva / gist:1200123
Created September 7, 2011 09:11
Async CTP and Entity Framework
// This code is provided "as-is" without warranty of any kind, either expressed or implied.
public static class AsyncExtensions
{
public static async Task<IEnumerable<T>> AsAsync<T>(this IQueryable<T> source)
where T : EntityObject
{
var query = (ObjectQuery<T>)source;
var cmd = new SqlCommand();
@neoGeneva
neoGeneva / gist:1216184
Created September 14, 2011 09:27
C# like properties in JavaScript
var createProperty = function(getSet) {
var _value;
var autoImplement = {
get: function() { return _value; },
set: function(value) { _value = value; return _value; }
};
getSet = getSet || autoImplement;
var get = getSet.get || function() { throw new Error('No get method defined for this property.'); },
@neoGeneva
neoGeneva / gist:1234779
Created September 22, 2011 13:34
Detect Encoding in C#
public static Encoding GetFileEncoding(string path)
{
if (path == null)
throw new ArgumentNullException("path");
var encodings = Encoding.GetEncodings()
.Select(e => e.GetEncoding())
.Select(e => new { Encoding = e, Preamble = e.GetPreamble() })
.Where(e => e.Preamble.Any())
.ToArray();
@neoGeneva
neoGeneva / gist:1309070
Created October 24, 2011 13:51
Simple Event Creator in JavaScript
var createEvent = function() {
var _handlers = [];
return function() {
if (arguments.length === 1 && typeof arguments[0] === 'function') {
_handlers.push(arguments[0]);
return;
}
for (var i = 0; i < _handlers.length; ++i) {
@neoGeneva
neoGeneva / gist:1878868
Created February 21, 2012 20:55
An OrderBy extension method for IQueryable that takes string
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortExpression)
{
if (source == null)
throw new ArgumentNullException("source", "source is null.");
if (string.IsNullOrEmpty(sortExpression))
throw new ArgumentException("sortExpression is null or empty.", "sortExpression");
var parts = sortExpression.Split(' ');
@neoGeneva
neoGeneva / gist:2722775
Created May 18, 2012 02:23
ASP.NET MVC3 EditorFor that looks for inheritance
public static MvcHtmlString EditorForInherited<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
if (htmlHelper.ViewContext.ViewData.Model == null)
return htmlHelper.EditorFor(expression);
var modelType = htmlHelper.ViewContext.ViewData.Model.GetType();
if (modelType == typeof(TModel))
return htmlHelper.EditorFor(expression);
@neoGeneva
neoGeneva / gist:3007125
Created June 27, 2012 21:57
Full-Text Search From EF4
public static class ObjectQueryExtensions
{
public static ObjectQuery<T> FullTextSearch<T, TProperty>(this ObjectQuery<T> query, Expression<Func<T, TProperty>> propertyExpression, string searchText)
{
var path = string.Join(".", propertyExpression.ToString().Split('.').Skip(1));
return query.Where(string.Format("CONTAINS(it.{0}, @search_text)", path), new ObjectParameter("search_text", searchText));
}
}
@neoGeneva
neoGeneva / gist:3660470
Created September 6, 2012 21:23
Proxy using ApiController
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Http = System.Net.WebRequestMethods.Http;
namespace PhilsVersion
{
public class ProxyController : ApiController
{
@neoGeneva
neoGeneva / gruntfile.js
Created July 24, 2015 00:19
SCSS compilation on save for VS2015 (for scss files under /Content/)
/// <binding ProjectOpened='watch' />
module.exports = function (grunt) {
grunt.initConfig({
bower: {
install: {
options: {
targetDir: "lib",
layout: "byComponent",
cleanTargetDir: false
}