Skip to content

Instantly share code, notes, and snippets.

@dlidstrom
dlidstrom / NTimes.cs
Created April 1, 2015 14:57
Output number of words occurring exactly n times
public class Program
{
static void Main()
{
var words = File.ReadAllText(@"C:\Programming\words.txt")
.Split(' ', '\r', '\n')
.Select(x => x.Trim())
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
var candidates = new Dictionary<string, int>();
@dlidstrom
dlidstrom / IndexTests.cs
Created March 30, 2015 10:44
Indexer with tests
public class Indexer
{
private readonly Dictionary<string, List<string>> terms =
new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, List<string>> docsTerms =
new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
public void Index(string docId, string text)
{
List<string> docTerms;
@dlidstrom
dlidstrom / boot.js
Last active August 29, 2015 14:16
run-jasmine.js for Jasmine 2.2. Run using phantomjs.exe run-jasmine.js SpecRunner.html
/**
Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. This file should be loaded after `jasmine.js` and `jasmine_html.js`, but before any project source files or spec files are loaded. Thus this file can also be used to customize Jasmine for a project.
If a project is using Jasmine via the standalone distribution, this file can be customized directly. If a project is using Jasmine via the [Ruby gem][jasmine-gem], this file can be copied into the support directory via `jasmine copy_boot_js`. Other environments (e.g., Python) will have different mechanisms.
The location of `boot.js` can be specified and/or overridden in `jasmine.yml`.
[jasmine-gem]: http://github.com/pivotal/jasmine-gem
*/
@dlidstrom
dlidstrom / ddd-guidelines.md
Last active August 29, 2015 14:13
Domain Driven Design Guidelines
  • Aggregate roots should not refer to any other aggregate roots, even in the constructor. Only the id of related aggregate roots is allowed.
  • Constructors can only accept ids and value types.
  • No setter properties are allowed. Use only methods for mutating state.
  • All business rules should be coded in the aggregate roots. Querying is the only type of "business rules" that are allowed outside of aggregate roots.
  • Use domain events to signal events in the domain. This is how related aggregate roots can get notified of what happens in other aggregate roots.
  • Log all non-standard exits from aggregate root methods. For example, return statements in the middle must have a logging statement that explains the decision to exit.
@dlidstrom
dlidstrom / ExtractIssues.ps1
Created November 7, 2014 15:24
Can be used to get a record of which files were associated with which Jira issues.
#foreach line in the input, until empty line
# [DateTime]::Parse('2014-05-28 12:43:43 +0200', [System.Globalization.CultureInfo]::InvariantCulture)
# try some (known) variations
$regex = New-Object System.Text.RegularExpressions.Regex('(?i)(HMO-|HMO -|HMO |HMO - )(\d+)')
$issueNumbers = @{}
$input | % { $regex.Matches($_) } | % {
$issueNumber = "HMO-$($_.Groups[2])"
if (-not $issueNumbers.ContainsKey($issueNumber))
(function () {
'use strict';
angular.module('PortalModule')
.factory('Toaster', ToasterProvider);
function ToasterProvider(toaster) {
return {
success: function (title, body) {
toaster.pop('success', title, body);
(function () {
'use strict';
angular.module('PortalModule')
.factory('httpRequestInterceptor', HttpRequestInterceptor);
function HttpRequestInterceptor($q, toaster) {
return {
responseError: function (response) {
if (response.status === 500) {
(function () {
//'use strict'; allow default mode here, to be able to capture more detail
angular.module('PortalModule')
.config(ExceptionHandlerDecorator);
function ExceptionHandlerDecorator($provide) {
$provide.decorator('$exceptionHandler', ['$delegate', '$injector', function ($delegate, $injector) {
var apiUrl = '/api/logerror';
return function (exception, cause) {
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="StyleCop" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="tools\MSBuild Extension Pack\bin\MSBuild.ExtensionPack.tasks"/>
<UsingTask AssemblyFile="$(MSBuildProjectDirectory)\tools\StyleCop-4.7\StyleCop.dll" TaskName="StyleCopTask"/>
<Target Name="StyleCop">
<PropertyGroup>
<BuildOutputDirectory>$(MSBuildProjectDirectory)\StyleCop</BuildOutputDirectory>
<StyleCopOutputFile>$(BuildOutputDirectory)\StyleCopViolations.xml</StyleCopOutputFile>
<StyleCopTestOutputFile>$(BuildOutputDirectory)\StyleCopTestViolations.xml</StyleCopTestOutputFile>
param([switch]$verbose, [switch]$v)
$TeamCity = $env:TEAMCITY_PROJECT_NAME
# read list of files to ignore
$ignoredFiles = @{}
$filesWithoutIssues = @{}
Get-Content .\StyleCop.IgnoredFiles.txt | % {
if (Test-Path($_))
{
$item = Get-ChildItem $_