Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
jsakamoto / gist:1985918
Created March 6, 2012 12:16
gist にデビュー。
Console.WriteLine("Hello, World.");
@jsakamoto
jsakamoto / gist:2011416
Created March 10, 2012 13:25
GDataDB を使って、ASP.NET MVC3 アプリから Google Docs スプレッドシートへ書き込みを行う
// GDataDB を NuGet Package Manager を使ってプロジェクトに追加しておくこと。
[HttpPost]
public ActionResult Index(Attendee attendee)
{
// ここでスプレッドシートへ書き込み
var gdbclient = new GDataDB.DatabaseClient("(user name)", "(password)");
var db = gdbclient.GetDatabase("Attendees");
var tbl = db.GetTable<Attendee>("Sheet1");
tbl.Add(attendee);
@jsakamoto
jsakamoto / gist:2204346
Created March 26, 2012 10:28
count number of bits that type is integer. (by F#)
let rec numofbits bitwidth x =
match bitwidth with
|0 -> 0
|_ -> (x &&& 1) + numofbits (bitwidth - 1) (x / 2)
@jsakamoto
jsakamoto / summary.cs
Created April 3, 2012 14:19
sammary of CLRH Extra session-1 sample code.
// http://atnd0.apphb.com/api/attendees
public class Attendee
{
public int AttendeeID { get; set; }
[Required, MaxLength(20)]
public string Name { get; set; }
[Required, MaxLength(20)]
@jsakamoto
jsakamoto / MyRemoteAttribute.cs
Created April 14, 2012 01:30
ASP.NET MVC Data Annotation Valiteion - Remote Validation with Server Side Validation!
// NOTICE:
// This is "concept" sample code.
// Not enough resouce disposing, etc.
public class MyRemoteAttribute : RemoteAttribute
{
public RemotableCustomAttribute(string action, string controller) : base(action, controller)
{
}
@jsakamoto
jsakamoto / gist:3009448
Created June 28, 2012 06:12
rename all files by F#
open System;;
open System.IO;;
let dir = @"c:\workdir";;
Directory.GetFiles(dir)
|> Seq.map (fun x-> (x, x.Replace("original", "replaced")))
|> Seq.iter (fun (x,y) -> File.Move(x, y));;
@jsakamoto
jsakamoto / gist:3009493
Created June 28, 2012 06:24
re-connect UDL to local server by F#
open System;;
open System.IO;;
open System.Text;;
open System.Text.RegularExpressions;;
let dir = @"c:\workdir";;
Directory.GetFiles(dir, "*.udl")
|> Seq.map (fun x-> (x, File.ReadAllText(x)))
|> Seq.map (fun(x,y)->(x, Regex.Replace(y,"Data Source=[^;]+",@"Data Source=.\SQLEXPRESS")))
|> Seq.iter (fun (x,y) -> File.WriteAllText(x,y,Encoding.Unicode));;
@jsakamoto
jsakamoto / Bot.cs
Created August 14, 2012 12:52
C# で Twitterizer を使って Twitter につぶやく
using System.Configuration;
using Newtonsoft.Json;
using Twitterizer;
public class Bot
{
public static void Tweet()
{
// Need appSettings section in .config:
// <add key="key" value="{ConsumerKey:'...', ConsumerSecret:'...', AccessToken:'...', AccessTokenSecret:'...'}" />
@jsakamoto
jsakamoto / sample1.fs
Created August 27, 2012 12:18
これはコンパイルが通る(.NETのFileInfoクラスはnullになりうる)
open System.IO
open System.Linq
let dir = new DirectoryInfo("c:\\")
let fileOfFoo = dir.GetFiles().FirstOrDefault(fun f -> f.Name = "foo")
printfn "%s" (if fileOfFoo = null then "null!" else "exists!")
@jsakamoto
jsakamoto / sample2.fs
Created August 27, 2012 12:24
しかしこれはビルドエラー(F#のレコード型(下記 MyFile)はnullになるはずがない)。でもprintfnの行を外すとビルドは通るので実行してデバッガで見るとnullになってる。
open System.IO
open System.Linq
type MyFile = {Name:string; Size:int64}
let toMyFile (f:FileInfo) = {Name=f.Name; Size=f.Length}
let dir = new DirectoryInfo("c:\\")
let fileOfFoo =
dir.GetFiles()
|> Array.map toMyFile