Created
June 2, 2011 09:50
-
-
Save nagat01/1004190 to your computer and use it in GitHub Desktop.
My training.Translating C# code to F#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
My training.Translating C# code to F#. | |
http://www.fincher.org/tips/Languages/csharp.shtml | |
*) | |
open System | |
open System.IO | |
open System.Net | |
// Console.WriteLine test | |
let k = 35 | |
Console.WriteLine("k={0,-4}.",k) // k=35 . | |
Console.WriteLine("k={0,4}.",k) // k= 35. | |
Console.WriteLine("k={0:X}.",k) // k=23. | |
Console.WriteLine("k={0:X4}.",k) // k=0023. | |
// TODO: How to write extended method? | |
// Console.WriteLine(123.ToString("#,#0")) | |
// This code offers key input after every expression evaluated. | |
Console.ReadKey() |> ignore | |
// F. example of virtual functions | |
// Plane is a class contains abstruct methods | |
type Plane() as this = | |
[<DefaultValue>] | |
val mutable X:int | |
do this.X<-10 | |
abstract topSpeed : unit->double | |
default this.topSpeed() = 300.0 | |
type Jet() = | |
inherit Plane() | |
override x.topSpeed()=900.0 | |
// TODO: couldn't assign base class instance. | |
// Is covariance not satisfied? | |
// let plane:Plane = new Jet() | |
let jet=new Jet() | |
Console.WriteLine("planes top speed:{0}",jet.topSpeed()) | |
// Class with Property,explicit class,initializer | |
type Nirvana = class | |
val mutable mySpeed : double | |
// I don't know why but constructor requires curly brace | |
new()={ mySpeed=300.00 } | |
member x.TopSpeed | |
with get() = x.mySpeed | |
and set v = x.mySpeed <- v | |
end | |
let nirvana = Nirvana() | |
nirvana.TopSpeed <- 100. | |
printfn"%f" <| nirvana.TopSpeed | |
// I. Write the current time | |
printfn"%s"<|DateTime.Now.ToString() | |
// Couldn't write | |
let mutable streamWriter = null | |
try | |
streamWriter <- new StreamWriter(@"C:\sandbox\sample2.txt") | |
streamWriter.WriteLine "This is first line" | |
streamWriter.WriteLine "This is second line" | |
finally | |
if streamWriter<>null then streamWriter.Close() | |
// download specified urls | |
let download(urls:string list) = | |
for url in urls do | |
let req = WebRequest.Create url | |
let res = req.GetResponse() | |
let stream = res.GetResponseStream() | |
let reader = new StreamReader(stream,Encoding.ASCII) | |
printfn"%s"<| reader.ReadToEnd() | |
download<| ["http://d.hatena.ne.jp/mclh46"] | |
// read html of my twitter | |
using(new WebClient()) | |
(fun client-> printfn"%A"<|client.DownloadString("http://twitter.com/#!/nagat01")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment