Skip to content

Instantly share code, notes, and snippets.

View hidori's full-sized avatar
🏠
Working from home

Hiroaki SHIBUKI hidori

🏠
Working from home
  • Tokyo, Japan
  • 05:24 (UTC +09:00)
View GitHub Profile
@hidori
hidori / Median.cs
Last active August 29, 2015 14:00
Median
double Median<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
{
if (!source.Any()) throw new InvalidOperationException();
var sorted = source.OrderBy(selector);
var count = (int)(sorted.Count());
int index = count / 2;
@hidori
hidori / RubyUnless.cs
Last active August 29, 2015 14:00
Ruby の unlessぽいヤツ
void Main()
{
var x = 1;
Unless(x == 0, () => Console.WriteLine("A"));
Unless(x == 0, () => Console.WriteLine("A"), () => Console.WriteLine("B"));
Console.WriteLine(Unless(x == 0, () => "A"));
Console.WriteLine(Unless(x == 0, () => "A", () => "B"));
}
using System;
using System.Linq;
using System.Threading;
namespace ConsoleApplication2
{
/// <summary>
/// static の悪夢
/// </summary>
class Program
static void Main()
{
var values = new[] { -12321, -123, -121, -12, -1, 0, 1, 12, 121, 123, 12321 };
var query = values.Select(value => new { Value = value, IsPalindrome = IsPalindrome(value) });
foreach(var item in query)
{
Console.WriteLine("{0}: {1}", item.Value, item.IsPalindrome);
}
@hidori
hidori / Scala.cs
Created June 23, 2014 05:07
Scala っぽいやつ
public static class Scala
{
public static Func<T, V> Compose<T, U, V>(this Func<U, V> first, Func<T, U> second)
{
if (first == null) throw new ArgumentNullException("first");
if (second == null) throw new ArgumentNullException("second");
return t => first(second(t));
}
using System;
using System.Threading;
namespace Mjollnir
{
public class AsyncResult : IAsyncResult, IDisposable
{
public AsyncResult(AsyncCallback callback, object state)
{
// callback can be null.
static class DictionaryExtensions
{
public static T GetValueOrDefault<T>(this IDictionary<string, object> source, string name)
{
if (source == null) throw new ArgumentNullException("source");
if (name == null) throw new ArgumentNullException("name");
var value = default(object);
return (source.TryGetValue(name, out value) && (value is T))
@hidori
hidori / GetDisplayName.cs
Created March 27, 2015 01:09
Get a text which specified with DisplayAttribute
static string GetDisplayName<TModel>(Expression<Func<TModel, object>> expression)
{
var type = typeof(TModel);
var propertyList = default(IEnumerable<string>);
switch (expression.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expression.Body as UnaryExpression;
@hidori
hidori / StringFactory.cs
Created May 10, 2012 04:23
Blding string simply
using System;
using System.Text;
namespace Mjollnir
{
static class StringFactory
{
public static string Create(Action<StringBuilder> action)
{
if (action == null) throw new ArgumentNullException("action");
@hidori
hidori / Syncronized.cs
Last active October 4, 2015 14:08
Waiting asyncronous works completion simpley
static class Syncronized
{
public static void Invoke(Action<ManualResetEvent> action)
{
if (action == null) throw new ArgumentException("action");
using (var signal = new ManualResetEvent(false))
{
action(signal);
signal.WaitOne();