Skip to content

Instantly share code, notes, and snippets.

@erdomke
erdomke / XElementStream.cs
Created December 16, 2016 16:08
Correct streaming of XElements
// The code posted at https://blogs.msdn.microsoft.com/xmlteam/2007/03/24/streaming-with-linq-to-xml-part-2/
// will skip every other element with compressed XML. This occurs because the reader after ReadFrom
// is already positioned on the next node and then the reader.Read() will move it to the node afterwards.
// The logic in this snippet fixes the problem.
internal static IEnumerable<XElement> StreamElements(Stream stream, XName matchName)
{
using (var reader = XmlReader.Create(stream))
{
reader.MoveToContent();
var continueNoRead = false;
@erdomke
erdomke / LinkedListOps.cs
Created March 15, 2017 18:04
Custom linked list implementation which avoids extra data classes
public interface ILink<T> where T : ILink<T>
{
string Term { get; }
T Next { get; set; }
}
static class LinkedListOps
{
public static void Add<T>(ref T lastLink, T newLink) where T : class, ILink<T>
{
@erdomke
erdomke / SigFigFormatProvider.cs
Last active March 16, 2017 00:22
Formats a number to a specific number of significant digits
using System;
using System.Globalization;
public static class FormatExtensions
{
public static string ToPrecision(this int value, int digits, string format = "")
{
return SigFigFormatProvider.Instance.Format("s" + digits.ToString() + format, value, null);
}
public static string ToPrecision(this double value, int digits, string format = "")
@erdomke
erdomke / NancyRequestLifeCycle.svg
Created June 6, 2017 02:21
NancyRequestLifeCycle
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@erdomke
erdomke / aras_labs_combining_items_best_practice.cs
Last active December 1, 2017 03:27 — forked from EliJDonahue/aras_labs_combining_items_best_practice.cs
Demonstrates the Aras Best Practice of building a single AML statement instead of combining items with appendItem()
string myAml;
using (var writer = new System.IO.StringWriter())
using (var xml = System.Xml.XmlWriter.Create(writer, new System.Xml.XmlWriterSettings()
{
OmitXmlDeclaration = true
}))
{
xml.WriteStartElement("AML");
for (var i = 0; i < 10; i++)
@erdomke
erdomke / MergeSort.cs
Created January 16, 2018 17:21
Merge two sorted lists
public static class Utils
{
[Flags]
public enum MergeStatus
{
LeftOnly = 1,
RightOnly = 2,
Both = 3,
}
@erdomke
erdomke / Aggregator.cs
Created March 1, 2017 22:29
Special dictionary for aggregating values
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Special dictionary for aggregating values
/// </summary>
public class Aggregator<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
{
private Dictionary<TKey, TValue> _values = new Dictionary<TKey, TValue>();
@erdomke
erdomke / QueryString.cs
Last active October 12, 2020 01:25
Class for parsing a query string from a URL into a dictionary-like structure for easy modification using a fluent API.
namespace QS
{
public class QueryString : IDictionary<string, QueryValue>
{
private string _root;
private Dictionary<string, QueryValue> _dict;
public ICollection<string> Keys { get { return _dict.Keys; } }
public ICollection<QueryValue> Values { get { return _dict.Values; } }
public int Count { get { return _dict.Count; } }
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace Namespace
{
internal class AsyncProcess
@erdomke
erdomke / grapesjs.d.ts
Last active February 15, 2023 01:00
Grapes.js TypeScript Definitions
declare namespace Backbone {
class Model<T> {
constructor(attr?:T, opt?:any)
attributes : T
collection: Collection<this>
cid: string
get<K extends keyof T>(prop:K) : T[K]
set<K extends keyof T>(prop:K , val:T[K]) : void
defaults() : T
on(eventName: string, callback: (...args: any[]) => void)