Skip to content

Instantly share code, notes, and snippets.

View posaunehm's full-sized avatar

Hiroshi Maekawa (a.k.a. Posaune) posaunehm

View GitHub Profile
@posaunehm
posaunehm / ControlBindingHelper.cs
Created November 30, 2011 04:22
Extension method that helps updating binding target.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows;
namespace Infrastructure.Extensions
{
@posaunehm
posaunehm / AssemblyExtension
Created December 27, 2011 02:56
AssemblyExtension: A GetTypes method which have exception-ignoring option.
public static Type[] GetTypes(this Assembly assembly, bool ignoreReflectionTypeloadException)
{
if(ignoreReflectionTypeloadException)
{
Type[] types;
try
{
types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
@posaunehm
posaunehm / gist:1660918
Created January 23, 2012 05:39
Vector Helper Extention Method
static readonly Vector ANGLE_BASE_VECTOR = new Vector(1, 0);
/// <summary>
/// ベクトルの角度をDegreeで返します
/// </summary>
/// <param name="vector">ベクトル</param>
/// <returns>角度(Degree値)</returns>
public static double GetAngle(this Vector vector)
{
return Vector.AngleBetween(ANGLE_BASE_VECTOR, vector);
@posaunehm
posaunehm / gist:1681072
Created January 26, 2012 04:52
BucketSort
public static IEnumerable<int> BucketSort(IEnumerable<int> input)
{
int[] bucket = new int[input.Max() + 1];
foreach(var ele in input)
{
bucket[ele]++;
}
return bucket.SelectMany((i, ele) => Enumerable.Range(0, i).Select(_ => ele));
}
@posaunehm
posaunehm / gist:2281427
Created April 2, 2012 07:36
A method enumurating all controls
static IEnumerable<Control> EnumurateAllControl(Control root)
{
yield return root;
foreach (var ctrl in root.Controls.Cast<Control>().SelectMany(c => EnumurateAllControl(c)))
{
yield return ctrl;
}
}
@posaunehm
posaunehm / gist:2282396
Created April 2, 2012 10:22
A method which returns all elements included in passed element
static IEnumerable<System.Windows.DependencyObject> EnumurateAllElement(System.Windows.DependencyObject root)
{
yield return root;
foreach (var element in GetChilren(root).SelectMany(ele => EnumurateAllElement(ele)))
{
yield return element;
}
}
static IEnumerable<System.Windows.DependencyObject> GetChilren(System.Windows.DependencyObject root)
@posaunehm
posaunehm / gist:2359381
Created April 11, 2012 13:45
Enumerate number without any local varuable
Console.Write(
string.Concat(
Enumerable.Range(1,100).SelectMany(ele => ele.ToString() + System.Environment.NewLine))
);
@posaunehm
posaunehm / gist:2359539
Created April 11, 2012 14:12
Calculate summation for 1 - 100 using only one valurable
int x = 100;
while((x & 255) != 0)
{
x += ((x & 255) << 8);
x--;
}
Console.WriteLine(x >> 8);
@posaunehm
posaunehm / gist:2943026
Created June 17, 2012 00:46
An quick sort implementation by F#
let rec qSort (x : List<'a>) =
if x.Length <= 1 then x
else
let parted = x.Tail |> List.partition (fun i -> i < x.Head)
List.append (fst parted |> qSort) (x.Head::(snd parted |> qSort))
@posaunehm
posaunehm / こんなかんじで使うイメージ
Created July 17, 2012 09:32
スレッドセーフにメソッドを実行する拡張メソッドスニペット
/// <summary>
/// Controlに対してスレッドセーフにActionを実行する
/// </summary>
/// <param name="control">対象コントロール</param>
/// <param name="action">Controlに対してスレッドセーフに実行したいAction</param>
public static void InvokeThreadSafely(this Control control, Action action)
{
if (control.InvokeRequired)
{
var asyncResult = control.BeginInvoke(action);