Skip to content

Instantly share code, notes, and snippets.

View DavidBrower's full-sized avatar

David Brower DavidBrower

  • Civica MDM
  • Glasgow, Scotland
View GitHub Profile
@DavidBrower
DavidBrower / EventStoreConnectionExtensions.cs
Created March 21, 2016 17:10 — forked from jen20/EventStoreConnectionExtensions.cs
Quick and dirty way to hook up RX to an Event Store subscription. Emphasis on "quick" and "dirty".
static class EventStoreConnectionExtensions
{
public static Task<EventStoreRxSubscription> SubscribeToAll(this EventStoreConnection connection, bool resolveLinkTos)
{
return Task<EventStoreRxSubscription>.Factory.StartNew(() => {
var subject = new Subject<ResolvedEvent>();
var subscriptionTask = connection.SubscribeToAll(resolveLinkTos, subject.OnNext, () => subject.OnError(new SubscriptionDroppedException()));
subscriptionTask.Wait();
@DavidBrower
DavidBrower / New.cs
Last active May 9, 2016 11:00
Generic Class Instantiator
/// <summary>
/// Instantiates TClass using a compiled lambda expression and caches it. This assumes a parameterless constructor.
/// </summary>
/// <typeparam name="TClass">The type of the class to be instantiated.</typeparam>
internal static class New<TClass> where TClass : class
{
public static readonly Func<TClass> Instance = Expression.Lambda<Func<TClass>>(Expression.New(typeof(TClass))).Compile();
/// <summary>
/// Allows a class to be instantiated using a parameterised constructor
@DavidBrower
DavidBrower / Euler5.cs
Last active March 29, 2016 04:04
Smallest Common Multiple
open System
let isDivisibleBy seq n =
seq
|> Seq.forall (fun x -> n % x = 0)
Seq.unfold (fun x -> Some(x, x + 20)) 20
|> Seq.find (isDivisibleBy [|1 .. 20|])
@DavidBrower
DavidBrower / Euler6.cs
Last active March 29, 2016 20:20
Sum of Squares vs Square of Sum
open System
let sumOfSquares =
[|1 .. 100|]
|> Array.map (fun x -> x * x)
|> Array.sum
let squareOfSum =
[|1 .. 100|]
|> Array.sum
@DavidBrower
DavidBrower / Euler7.fs
Created March 29, 2016 20:54
10,001st Prime
open System
let isPrime (n : int64) =
let upperBound = n |> float |> sqrt |> int64
seq {2L .. upperBound}
|> Seq.forall (fun x -> n % x <> 0L)
Seq.unfold (fun x -> Some(x, x + 1L)) 1L
|> Seq.filter isPrime
|> Seq.nth 10001
@DavidBrower
DavidBrower / Euler.hs
Created April 19, 2016 11:58
First Euler in Haskell
sum [x | x <- [1..999], mod x 3 == 0 || mod x 5 == 0]
@DavidBrower
DavidBrower / TypeSwitch.cs
Created May 16, 2016 10:01
TypeSwitch that returns a string
using System;
public static class TypeSwitch
{
public class CaseInfo
{
public bool IsDefault { get; set; }
public Type Target { get; set; }
public Func<object, string> Action { get; set; }
}
@DavidBrower
DavidBrower / .bash_profile
Last active July 20, 2018 13:03
Bash Profile
yellow() { tput setaf 3; cat; tput sgr0; }
green() { tput setaf 2; cat; tput sgr0; }
cyan() { tput setaf 6; cat; tput sgr0; }
alias ll='ls -lhA'
alias g='git'
alias c='clear'
alias d='docker'
alias reload='source ~/.bash_profile'
alias v='vim'
source ~/git-completion.bash
@DavidBrower
DavidBrower / SQLServerSpace.sql
Created October 19, 2016 09:08
Display Used and Available Space on SQL Server Database
select
name
, filename
, convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB
, convert(decimal(12,2),round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpaceUsedMB
, convert(decimal(12,2),round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpaceMB
from dbo.sysfiles a
@DavidBrower
DavidBrower / RowCount.sql
Last active October 24, 2016 15:13
Gets the row count from a SQL Server with around two logical reads
DECLARE @TableName sysname
SET @TableName = 'table_name'
SELECT OBJECT_NAME(object_id) as [Table Name], format(SUM(row_count), '###,###') AS [Row Count]
FROM sys.dm_db_partition_stats
WHERE object_id = OBJECT_ID(@TableName)
AND index_id < 2
GROUP BY OBJECT_NAME(object_id);