Skip to content

Instantly share code, notes, and snippets.

View takeshik's full-sized avatar

Takeshi KIRIYA takeshik

View GitHub Profile
// this is Form
var textBoxes = this.Controls
.OfType<TextBox>()
.Where(t => t.Name.StartsWith("textBox"))
.OrderBy(t => Int32.Parse(t.Name.Substring("textBox".Length)))
.ToList();
foreach (var _ in textBoxes.Select((t, i) => new { t, i, }))
{
_.t.Text = "This is TextBox #" + _.i + "!";
}
// From Achiral.Make.Dictionary (modified)
/// <see>http://blogs.wankuma.com/mayuki/archive/2007/12/06/111901.aspx</see>
public static Dictionary<String, Object> Dictionary(params Expression<Func<Object, Object>>[] exprs)
{
return exprs.ToDictionary(expr => expr.Parameters[0].Name, expr => expr.Compile().Invoke(null));
}
var o = (IDictionary<String,Object>) new ExpandoObject();
Dictionary(a => 123, b => "foo").ForEach(_ => o.Add(_.Key, _.Value));
void Main()
{
Account acc = Account.Create("test", "foo");
CreateCreationData(acc.Id, "Foo", 123,
a => a.Advertise(DateTime.Now, AdvertisementFlags.Created),
a => a.Act("Child1", "abc",
a2 => a2.Act("Grandchild1", "hoge", DateTime.UtcNow),
a2 => a2.Act("Grandchild2", "fugo", DateTime.UtcNow.AddHours(123),
a3 => a3.Act("Grand2child", "zzz")
)
@takeshik
takeshik / Pairwise.cs
Created April 4, 2011 04:30
from Achiral (Ms-PL)
public static IEnumerable<TResult> Pairwise<T, TResult>(this IEnumerable<T> source, Func<T, T, TResult> selector)
{
using (var enumerator = source.GetEnumerator())
{
if (!enumerator.MoveNext())
{
yield break;
}
var prev = enumerator.Current;
while (enumerator.MoveNext())
protected virtual Tuple<Activity, Boolean> CreateObject(Account account, IEnumerable<ActivityId> ancestorIds, String name, Object value)
{
ActivityId id = ActivityId.Create(account.Id, ancestorIds, name, value);
StorageObject obj;
Boolean created = false;
if ((obj = this.Load(id)) == null && !this.AddingObjects.TryGetValue(id, out obj))
{
obj = Activity.Create(account.Id, ancestorIds, name, value);
this.AddingObjects.Add(obj.ObjectId, obj);
obj.Context = this;
@takeshik
takeshik / object-to-iobservable`1[object].cs
Created March 30, 2011 16:49
Convert Object to IObservable<Object>
static void Main()
{
Object obj = Observable.Range(0, 100);
IObservable<Object> source = obj.GetType().GetInterface("System.IObservable`1") != null
? (IObservable<Object>) typeof(UserQuery)
.GetMethod("AsObject", BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(obj.GetType().GetInterface("System.IObservable`1").GetGenericArguments())
.Invoke(null, new Object[] { obj, })
: Observable.Return(obj);
}
@takeshik
takeshik / http-chunked.cs
Created March 30, 2011 00:48
C# WebServer 2.0 (http://webserver.codeplex.com) chunked data sending code
public ProcessingResult Process(RequestContext context)
{
Console.WriteLine(context.Request.Uri.PathAndQuery);
if (context.Request.Uri.PathAndQuery != "/")
{
return ProcessingResult.Continue;
}
ResponseWriter generator = new ResponseWriter();
context.Response.ContentType.Value = "application/json";
context.Response.Status = HttpStatusCode.OK;
#!/usr/bin/perl -w
length q eq ge and print chr ord uc q eval le and print chr ord uc q sin s and print chr oct ord q qx eq and print chr oct hex length q q redo index seekdir next chop ne getnetbyaddr abs setpwent getprotobyname continue getpwuid pack join or shmctl m syswrite seekdir do study while getsockopt q and print chr hex ord uc q eq le and print chr oct hex ord q tie lt and print chr oct hex length q q reset msgget splice socket dbmopen rmdir endhostent send syscall wantarray index getpwuid splice exists waitpid getpgrp shmwrite each localtime join caller q and print chr hex ord uc q eq le and print chr length q q gethostbyname setgrent split setnetent utime getc sethostent endnetent reset telldir getprotobyname msgctl shmctl flock foreach chmod wait undef vec local open sort exec s q and print chr oct hex length q q reset msgget splice socket dbmopen rmdir endhostent send syscall wantarray index getpwuid splice exists waitpid getpgrp shmwrite each localtime join caller q and print chr hex ord uc q
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace LinqToTwitter
{
public class MetaTweetAuthorizer
Dim inReplyToPosts = From tab In _statuses.Tabs.Values
Order By tab IsNot curTabClass
From post In DirectCast(IIf(tab.IsInnerStorageTabType, tab.Posts, _statuses.Posts), Dictionary(Of Long, PostClass)).Values
Where post.StatusId = inReplyToId
Let index = tab.IndexOf(post.StatusId)
Where index <> -1
Select New With {.Tab = tab, .Index = index}