Skip to content

Instantly share code, notes, and snippets.

View SoulFireMage's full-sized avatar

Richard Griffiths SoulFireMage

View GitHub Profile
@SoulFireMage
SoulFireMage / listChunking
Last active December 21, 2015 11:08
Break a list(of T) into arbitrary fixed chunks - with or without Yield.
'Usage example:
'Dim setsOfList = OriginalList.Chunk(3)
'For Each innerList In setsOfList
'Do stuff
'Next
<Extension()>
Public Iterator Function ChunkData(Of TSource)(source As IEnumerable(Of TSource), chunkSize As Integer) As IEnumerable(Of IEnumerable(Of TSource))
Dim _chunkSize = IIf(source.Count < chunkSize, source.Count, chunkSize)
Dim i = 0
@SoulFireMage
SoulFireMage / DynamicRuleEngineBuilder
Last active December 21, 2015 11:39
A generic constrained class that dynamically generates a rule engine based on reflection and specified properties-an expression tree is built for each property, compiled and added to a list of rules - this is generate but once for the lifetime of the instance. This is intended for use with a database migration testing engine.
Imports System.Reflection
Imports System.Linq.Expressions
''' <summary>
'''This generic class encapsulates the main algorithms for building a rule engine and a list of matched source/merge '''"rows"To prove whether a row in source and target are equivalent.
''' A property in the classes we may be using represent columns in a table or query.
'''Say we have an instance thus:
'''SourcePerson = New Person with {.Name = "ommitted db query",
''' .DOB =" ommitted db query"}
'''MergedPerson = New Person with {.Name = "ommitted db query",
''' .DOB =" ommitted db query"}
@SoulFireMage
SoulFireMage / 2D Array with Comprehensions
Created December 23, 2013 14:21
Learning series - very basics
let squaresNcubes = Array2D.create 10 10 ([|1 .. 10|] |> Array.map (fun x -> x * x), [|1 .. 10|] |> Array.map (fun x -> x * x * x))
@SoulFireMage
SoulFireMage / Graphics stuff
Last active January 13, 2016 14:13
Learning Snippets
//Totally unnecessary implementation of course but attempting to implement one's own graphics also means
//getting to play with the idioms and datastructures of a language IMHO
//I might implement Logo in here someday :).
let square (x,y) length = [ (x,y,x+length,y); //top
(x,y+length,x+length,y+length); //bottom
(x,y,x ,y + length ); //left
(x + length,y,x + length, y + length )] //right
@SoulFireMage
SoulFireMage / Character frequency
Last active January 1, 2016 13:09
Count chars in all the CSV files in a folder hierarchy without leaking memory. This gist has some meaning to me at time of writing!
let counter(str:string,ch:char) = str.ToCharArray()
|> Array.sumBy(fun s ->
let x = 0
match s with
| c when ch = s-> x + 1
| _ -> x)
let chrs = ['e';'a';'o';'s';'t']
let sample = "tesqatetweafwefwlvuioneuiounrv;onr;rvoian;c;oi;oijewrrfaa"
let character = chrs |> List.map(fun x-> counter (sample ,x))
@SoulFireMage
SoulFireMage / VB WPF Interface function
Created December 27, 2013 17:01
OData with Netflix from 2012
//This belongs to the F# dll above.
Function buildFilmStackPanel(Title As netflix.ServiceTypes.Title)
Dim stack As New StackPanel
Dim Filmtitle As New Label With {.Content = Title.Name, .ClipToBounds = True}
Dim Synopsis As New TextBlock With {.Text = Title.Synopsis, .TextWrapping = TextWrapping.Wrap, .FontSize = 10, .ClipToBounds = True, .MaxWidth = 400}
Dim imageStr As String = Title.BoxArt.MediumUrl
Dim image As New Image() With {.Source = New BitmapImage(New Uri(imageStr)),
.Width = 256,
@SoulFireMage
SoulFireMage / Accessing SQL
Created December 27, 2013 17:14
First shot at using a Type Provider, last year :)
open System
open System.Data
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq
type dbschema = SqlDataConnection<"Data Source=Katrina\SQLExpress;Initial Catalog=RCPD;Integrated Security=SSPI;">
let db = dbschema.GetDataContext()
db.DataContext.Log |> System.Console.Out
@SoulFireMage
SoulFireMage / VwlCount
Created December 29, 2013 07:50
VwlCount
let (|Vowel|) chr = match chr with
|'a'|'e'|'i'|'o'|'u' -> 1
| _ -> 0
let count = "afwefaf;oijv;oiwerj;gfj;wjofjojejfjov ns" |> Seq.map(fun (Vowel x) ->x ) |> Seq.sum
count
@SoulFireMage
SoulFireMage / Parallel Test
Created October 17, 2014 08:21
Foreach and Dictionary initialising
//I had a need to test a few combinations of AsParallel and OrderBy whilst learning C#
//Here I wanted to try initialising a dictionary inline - not keen on code repetition.
//Outcome was for this dataset, AsParallel didn't help but worsened performance.
//Note, I tried more combinations than this.
private void timedResult(IEnumerable<Data> data)
{
var NoOrderNoParallel = data.Where(g => g.CODE == "BXF04A").Select(g => g);
var OrderedOnly = data.OrderBy(c => c.CODE).Where(g => g.CODE == "BXF04A").Select(g => g);
var ParallelOnly = data.AsParallel().Where(g => g.CODE == "BXF04A").Select(g => g);
@SoulFireMage
SoulFireMage / Count integers
Created October 29, 2014 09:16
Coding Interview Exercise
using System;
namespace PlayingAround
{
class Program
{
/// <summary>
/// 15 minute answer to Coding Interview Puzzle 1. Previous solution used a dictionary and extension method.
/// This is more old school and faster.
/// The question is given a million integers between 1 and 130, count how many of each value there are.