Skip to content

Instantly share code, notes, and snippets.

View ValdemarOrn's full-sized avatar

Valdemar Erlingsson ValdemarOrn

View GitHub Profile
struct ZeroDelay2Lp
{
double z1_state;
float g;
// directory from page 48
// https://www.native-instruments.com/fileadmin/ni_media/downloads/pdf/VAFilterDesign_1.1.1.pdf
inline float Process(float x)
{
// perform one sample tick of the lowpass filter
@ValdemarOrn
ValdemarOrn / _monteCarlo.md
Last active December 20, 2015 09:59
Estimating Pi with Monte Carlo Simulation

Estimating Pi with Monte Carlo Simulation

Notes:

  • I decided to use Random() for the random number generator, but I seed it with a RNGCryptoServiceProvider. That should be "random enough" for what we're trying to achieve (the first 5-7 digits of pi).
  • Program utilizes Parallel.For to run simulations in parallel. It will literally thrash your CPU to 100%.
  • Make sure to pick a radius that's a power of 2, otherwise the modulo won't distribute your random values evenly.
@ValdemarOrn
ValdemarOrn / _monteCarlo.cs
Created July 30, 2013 11:34
Estimating Pi with Monte Carlo Simulation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace MonteCarloPi
{
class Program

Just how read-only is "readonly" in C#?

Did you know you can modify read-only fields via reflection?

Simple Monad example for chaining code asynchronously in C#

This is a simple implementation of a function chainer in C#, using AutoResetEvent and TaskFactory to control execution flow.

The code is meant for demonstration and educational purposes. I wrote it as an experiment while studying monads. The Task Parallel Library combined with the new async/await functionality can probably give you a better way to write async code.

Evaluating expression chains of nullable objects in C#

Evaluating deeply nested properties can be tedious if you want your code to be safe from the dreaded NullReferenceException. Although sometimes considered a bad habit, occasionally you just have to dive into an object hierarchy to fish out that one value you're interested in.

In these cases, you must check every object in the hierarchy, usually falling back to a default value in case the value you're looking for isn't there. Even so, you often end up with null references in places you never thought to anticipate.

Using these simple extension methods can make it a lot easier (and prettier) to access deeply nested values. It also lets you set an explicit default value in case your expression fails.

A word of caution

/// <summary>
/// Cache/Memoizer class that stores output values based on input.
/// Can also expire elements and has a fixed lower and upper limit to the number of cached elements
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
public class Memoize<TInput, TOutput>
{
private Dictionary<TInput, Tuple<TOutput, DateTime>> Values;
private int Min;
@ValdemarOrn
ValdemarOrn / NonNullable.md
Last active December 16, 2015 10:29
Required<T> - Non-nullable reference objects in C#

Non-nullable reference objects in C#

This is an example of a non-nullable wrapper for C#.

You can think of it like the complete opposite of Nullable<T>:

  • Nullable<T> gives you the ability to pass around or store either a value or null.
  • Required<T> forces the value to be non-null.

Trying to assign null to a Required<T> variable, or passing null into a method that defines the input as Required<T> will cause an exception. This is good because the sooner we catch errors, the better. Code that fails early is much easier to debug and maintain than code that fails five calls deeper.

@ValdemarOrn
ValdemarOrn / SimpleDFT.md
Last active November 1, 2021 20:28
A very simple Discrete Fourier Transform algorithm (not suitable for real-time processing)

Real-Value Discrete Fourier Transformation in C#

This is a very basic version of a Discrete Fourier Transformation.

Not suitable for real-time analysis

@ValdemarOrn
ValdemarOrn / AnonList.cs
Created October 10, 2011 00:47
Create a list of an Anonymous Type
class AnonList
{
/// <summary>
/// Creates a list of type T using a prototype object. Prototype can be any object, including an anonymous type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="prototype">The prototype object, of which a List of the same type is returned</param>
/// <returns>A new empty List of the same type as prototype</returns>
public static List<T> CreateAnonList<T>(T prototype)