Skip to content

Instantly share code, notes, and snippets.

@liammclennan
liammclennan / gist:9455524
Created March 9, 2014 22:10
React mixin that warns if a propTypes key is missing
define('validation', [], function() {
return {
allPropsSpecifiedMixing: {
componentDidMount: function() {
var unspecifiedPropKeys = _.difference(
_.keys(this.props),
_.keys(this.type.propTypes || {}).concat('ref'));
if (unspecifiedPropKeys.length) {
console.warn('Component ' +
@liammclennan
liammclennan / gist:8949046
Last active January 23, 2017 05:18
Simple, re-useable state machine (c#)
// this script works in linqpad (https://www.linqpad.net/)
void Main()
{
var day = new DaysRevisions();
day.State.Dump();
day.State.To(LifecycleState.Draft, "me");
day.State.Dump();
day.State.To(LifecycleState.Historical, "me");
}
@liammclennan
liammclennan / gist:8020631
Created December 18, 2013 11:09
Hexagonal Architecture. No ORM.
module domain =
type post = { id: Guid; title: string; content: string; author_id: Guid }
type author = { id: Guid; name: string; }
type crudAction = Create | Update | Delete
type change = { action: crudAction; entity: obj; id: Guid }
let addPost (p:post) : seq<change> =
// do stuff
seq { yield {action = Create; entity = box p; id = p.id} }
@liammclennan
liammclennan / gist:7958108
Last active December 31, 2015 08:08
F# Event Sourcing
/// types
type addPersonEvent = { name: string; age: int }
type birthdayEvent = { name: string; }
type event =
| AddPerson of addPersonEvent
| Birthday of birthdayEvent
type person = { name: string; age: int; joined: DateTime }
type aggregateStream = seq<event>
/// event stream for person aggregate
@liammclennan
liammclennan / gist:7446911
Created November 13, 2013 10:30
React component content
/** @jsx React.DOM */
var Hello = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
React.renderComponent(<Hello name="World"><p>foo</p></Hello>, document.body);
@liammclennan
liammclennan / gist:7352790
Last active December 27, 2015 16:09
string calculator
let lToS (l:char list) = System.String.Concat(Array.ofList(l))
let sumString =
let parse (l:string) =
let guessDelimeter (l:string) =
match Seq.toList l with
| '/'::'/'::c::'\n'::_ -> c
| _ -> Seq.find (fun i -> i = ',' || i = '\n') l
let valuePart = (fun (s:string) ->
match Seq.toList s with
@liammclennan
liammclennan / gist:6301387
Created August 21, 2013 23:13
Curl with integrated authentication (NTLM)
$ curl --ntlm http://localhost:50695/Scripts/src.js -u : -o src.js
@liammclennan
liammclennan / gist:6277299
Created August 20, 2013 05:10
Merge dictionaries (ie _.extend)
public static class DictionaryExtensions
{
public static Dictionary<TKey, TValue> Merge<TKey,TValue>(this Dictionary<TKey, TValue> first,
Dictionary<TKey, TValue> second)
{
return new[] {first, second}
.SelectMany(d => d) // flatten dictionaries to IEnumerable<KeyValuePair<TKey,TValue>>
.ToLookup(kvp => kvp.Key, kvp => kvp.Value) // group by key
.ToDictionary(g => g.Key, g => g.Last()); // last value wins
}
_ = require 'underscore'
class SurveyResponse
constructor: (@site, @results) ->
default_result =
'csharp': 0
'fsharp': 0
'haskell': 0
'ruby': 0
@liammclennan
liammclennan / gist:5639688
Last active December 17, 2015 16:38
Strongly typed view functions
public Markup Home(string name) {
return P("Welcome " + name)
}
public Markup Layout(Markup content) {
return Html(
Body (
Div(content).attr(new {@class= "content"})
)
}