Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
jsakamoto / gist:1985918
Created Mar 6, 2012
gist にデビュー。
View gist:1985918
Console.WriteLine("Hello, World.");
@jsakamoto
jsakamoto / gist:2011416
Created Mar 10, 2012
GDataDB を使って、ASP.NET MVC3 アプリから Google Docs スプレッドシートへ書き込みを行う
View gist:2011416
// 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 Mar 26, 2012
count number of bits that type is integer. (by F#)
View gist:2204346
let rec numofbits bitwidth x =
match bitwidth with
|0 -> 0
|_ -> (x &&& 1) + numofbits (bitwidth - 1) (x / 2)
@jsakamoto
jsakamoto / summary.cs
Created Apr 3, 2012
sammary of CLRH Extra session-1 sample code.
View summary.cs
// 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 Apr 14, 2012
ASP.NET MVC Data Annotation Valiteion - Remote Validation with Server Side Validation!
View MyRemoteAttribute.cs
// 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 Jun 28, 2012
rename all files by F#
View gist:3009448
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 Jun 28, 2012
re-connect UDL to local server by F#
View gist:3009493
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 Aug 14, 2012
C# で Twitterizer を使って Twitter につぶやく
View Bot.cs
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 Aug 27, 2012
これはコンパイルが通る(.NETのFileInfoクラスはnullになりうる)
View sample1.fs
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 Aug 27, 2012
しかしこれはビルドエラー(F#のレコード型(下記 MyFile)はnullになるはずがない)。でもprintfnの行を外すとビルドは通るので実行してデバッガで見るとnullになってる。
View sample2.fs
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