Skip to content

Instantly share code, notes, and snippets.

View andylshort's full-sized avatar

Andrew Lamzed-Short andylshort

View GitHub Profile
@andylshort
andylshort / RPN.java
Created March 28, 2018 19:06
Simple Reverse Polish Notation Implementation
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Stack;
public class ReversePolish {
private ReversePolish() { }
@andylshort
andylshort / Delegates.cs
Last active March 24, 2018 11:12
Useful methods for use with delegates
public static class DelegateUtility
{
public static T Cast<T>(Delegate source) where T : class
{
return Cast(source, typeof(T)) as T;
}
public static Delegate Cast(Delegate source, Type type)
{
if (source == null) return null;
@andylshort
andylshort / Application.Helper.cs
Created March 24, 2018 11:05
Extension methods and useful features to add to WinForms projects
using System;
using System.Diagnostics;
namespace System.Windows.Forms.Helpers
{
public class ApplicationHelper
{
/// <summary>
/// Check if current process already running. If running, set focus to existing process and
/// returns <see langword="true"/> otherwise returns <see langword="false"/>.
@andylshort
andylshort / DoubleBuffering.cs
Created March 24, 2018 11:03
Force double buffering on any WinForms control
public static void DoubleBuffering(this Control control, bool enable)
{
var method = typeof(Control).GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(control, new object[] { ControlStyles.OptimizedDoubleBuffer, enable });
}
@andylshort
andylshort / Maths.cs
Created March 24, 2018 11:01
Maths functions and experimentation (Fibonacci, Mersenne Primes etc)
using System;
using System.Collections.Generic;
using System.Linq;
namespace Maths
{
internal class Maths
{
public static IEnumerable<long> Fibonacci(int n)
{
@andylshort
andylshort / GetCurrentMethodName.cs
Created March 24, 2018 10:59
Gets the name of the currently executing method
// Requires:
// using System.Diagnostics;
// using System.Runtime.CompilerServices;
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCurrentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
@andylshort
andylshort / FSM.cs
Created March 24, 2018 10:58
Basic Finite State Machine Implementation
using System;
using System.Linq;
public class FSM<T> where T : struct, IConvertible, IComparable
{
public Transition<T, T>[] Transitions { get; private set; }
public T CurrentState { get; private set; }
@andylshort
andylshort / Math.js
Created March 23, 2018 23:12
JavaScript Math lib
// Converts from degrees to radians.
Math.radians = function(degrees) {
return degrees * Math.PI / 180;
};
// Converts from radians to degrees.
Math.degrees = function(radians) {
return radians * 180 / Math.PI;
};
@andylshort
andylshort / RGBtoHSV.js
Created March 23, 2018 23:10
RGB to HSV
var rgbToHsv = function(r, g, b) {
var rp = r / 255;
var gp = g / 255;
var bp = b / 255;
var cmax = Math.max(rp, gp, bp);
var cmin = Math.min(rp, gp, bp);
var delta = cmax - cmin;
@andylshort
andylshort / chiptune.sh
Created March 23, 2018 22:30
Example chiptune bash script I found online
echo "g(i,x,t,o){return((3&x&(i*((3&i>>16?\"BY}6YB6%\":\"Qj}6jQ6%\")[t%8]+51)>>o))<<4);};main(i,n,s){for(i=0;;i++)putchar(g(i,1,n=i>>14,12)+g(i,s=i>>17,n^i>>13,10)+g(i,s/3,n+((i>>11)%3),10)+g(i,s/5,8+n-((i>>10)%3),9));}"|gcc -xc -&&./a.out|aplay