Skip to content

Instantly share code, notes, and snippets.

@liammclennan
liammclennan / gist:4639397
Last active February 11, 2024 22:32
DIY modules
(function (root) {
// declare a module and its dependencies
root.define = function define(name, dependencies, moduleFactory) {
root.amdModules = root.amdModules || {};
if (!_.isArray(dependencies) && typeof dependencies !== 'function')
throw new Error('dependencies must be an array. moduleFactory must be a function.');
root.amdModules[name] = {
'moduleFactory': moduleFactory || dependencies,
@liammclennan
liammclennan / useEffect.js
Created October 15, 2022 03:28
React `useEffect`
function App() {
let divRef = useRef(null);
useEffect(() => {
var text = document.createTextNode(" Rendered ");
divRef.current.appendChild(text);
return () => {
var text = document.createTextNode(" Unrendered ");
divRef.current.appendChild(text);
};
@liammclennan
liammclennan / README.md
Last active April 17, 2020 18:29
RedisMQ

RedisMQ

Many people use Redis for a simple message broker. RedisMQ is a trivial layer on Redis and StackExchange.Redis that probably does not work. It provides two simple messaging patterns:

Publish / subscribe

A publisher publishes messages. 0, 1 or more consumers subscribe to the messages.

@liammclennan
liammclennan / blog_backbone_app_framework.md
Created July 12, 2012 01:39
A Backbone Application Framework

Backbone is a library of tools that simplify the design and implementation of client-side web applications. It is explicitly not a framwework. Backbone does not provide guidance about how to assemble an application. This post will be an initial attempt at filling that gap. It assumes an intermediate level of Backbone.js knowledge. If you have never used Backbone.js try Backbone.js Basics or the Backbone.js homepage.

The Application Root

I like to provide a root object/namespace for my application. Let's call it app.

var app = {};

Register Components on the Application Object

@liammclennan
liammclennan / a.md
Created May 30, 2012 02:33
Object-object Mapping - Why I don't use Automapper

Object-object Mapping - Why I don't use Automapper

At the boundaries of an application it is often nice to map objects from one form to another. The most common example is mapping domain entities to view models, or mapping to dtos for network transfer.

Automapper is a library for removing the duplication from object-object mapping code by mapping by convention. When the conventions don't map the way you want you can explicitly map properties.

After using AutoMapper on a couple of projects I have stopped using it. The problem is that the convention based mappings introduce annoying bugs when property names change or the object graph changes. When a change breaks the convention it results in a runtime exception. Very annoying.

What I Do Instead

@liammclennan
liammclennan / gist:542cb1264305f525c70b
Created May 27, 2015 10:37
Partitioning events with Akka.net (linqpad - reference akka.net and Rx-main)
void Main()
{
var system = ActorSystem.Create("MySystem");
var greeters = system.ActorOf(Props.Create(() => new DeviceSplitter()));
var r = new System.Random();
var measures = Enumerable.Repeat(new [] {"a","b","c","d"}, 250000)
.SelectMany(c => c)
.Select (id => new Measure(id,r.Next()));
foreach (var m in measures) greeters.Tell(m);
Console.ReadLine();
@liammclennan
liammclennan / blog_backbone_style.md
Created June 7, 2012 06:33
Backbone.js Style / Patterns

Naming Rules

Use PascalCase for constructors, namespaces and modules:

var m = new Backbone.Model(); 

Prefix private properties with _

This is a convention to compensate for JavaScript's lack of private properties on objects. Being able to identify private methods is important because it tells us that we don't need to test those methods and that they will not be coupled to anything outside of the object.

@liammclennan
liammclennan / skeleton.js
Created August 24, 2017 00:50
Node.js console application skeleton
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
var fs = require('fs')
, filename = process.argv[2];
let input = fs.readFileSync(filename, 'utf8');
@liammclennan
liammclennan / gist:4957758
Created February 15, 2013 00:44
mapMany (SelectMany) in JavaScript
function mapMany (arr, mapper) {
return arr.reduce(function (prev, curr, i) {
return prev.concat(mapper(curr));
},[]);
}
// usage
var res1 = mapMany(['abcd', '1234'], function (item) {
return item.split('');
});
@liammclennan
liammclennan / leftrotation.fs
Created March 16, 2017 11:15
Hackerrank: Arrays: Left Rotation
let rotations = System.Console.ReadLine().Split().[1] |> int
let vals = System.Console.ReadLine().Split()
let rotate arr n =
let cut = n % Array.length arr
Array.append arr.[cut..] arr.[..cut-1]
String.concat " " (rotate vals rotations) |> printfn "%s"