Last active
June 1, 2019 00:26
-
-
Save Y-Koji/da6ee4c0f2720c0d9cc08bd921ac4ee6 to your computer and use it in GitHub Desktop.
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
open FSharp.Data | |
[<EntryPoint>] | |
let main argv = | |
let req = | |
Http.AsyncRequest( | |
"http://url/", | |
httpMethod = "POST", | |
headers = [ "Key", "Value"; ], | |
body = FormValues [ "Key", "Value"; ], | |
cookies = [ "Key", "Value" ], | |
timeout = 5000) |> Async.RunSynchronously | |
match req.Body with | |
| Binary x -> printfn "response is binary. on size of %d bytes." x.Length | |
| Text x -> printfn "response is text. on length of %d." x.Length | |
0 |
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
type FizzBuzzMessage = int * AsyncReplyChannel<int * string> | |
let fizzbuzzActor = new MailboxProcessor<FizzBuzzMessage>(fun inbox -> | |
let fizzbuzz = function | |
| x when x % 15 = 0 -> "FizzBuzz" | |
| x when x % 3 = 0 -> "Fizz" | |
| x when x % 5 = 0 -> "Buzz" | |
| x -> x.ToString() | |
async { | |
while true do | |
let! x, replyChannel = inbox.Receive() | |
let converted = x |> fizzbuzz | |
replyChannel.Reply (x, converted) | |
}) | |
fizzbuzzActor.Start() | |
[<EntryPoint>] | |
let main argv = | |
let result = fizzbuzzActor.PostAndAsyncReply(fun replyChannel -> 3, replyChannel) |> Async.RunSynchronously | |
0 |
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
type IPerson = | |
abstract Name: string with get, set | |
abstract Age: int with get, set | |
and Person(name, age) = | |
let mutable _name = "" | |
let mutable _age = 20 | |
do | |
_name <- name | |
_age <- age | |
interface IPerson with | |
member __.Name with get() = _name and set v = _name <- v | |
member __.Age with get() = _age and set v = _age <- v | |
[<EntryPoint>] | |
let main argv = | |
let person1: IPerson = Person("タカシ", 20) :> IPerson | |
let person2: IPerson = Person("タナカ", 23) :> IPerson | |
person1.Greet() | |
person2.Greet() | |
0 |
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
type PersonInfo = { | |
mutable Name: string; (* 可変 *) | |
Age: int; (* 固定 *) | |
} | |
[<EntryPoint>] | |
let main argv = | |
let info = { Name = "Takashi"; Age = 20 } | |
info.Name <- "Takahashi" | |
let info2 = { info with Age = 22 } (* Ageを22で追加定義 *) | |
0 |
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
type OddOrEven = | |
| Odd of int | |
| Even of int | |
let test x = if x % 2 = 0 then Even x else Odd x | |
[<EntryPoint>] | |
let main argv = | |
match test(3) with | |
| Odd x -> printfn "number %d is odd." x | |
| Even x -> printfn "number %d is even." x | |
0 |
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
[<AbstractClass>] | |
type AbstractPerson() = | |
abstract Name: string with get | |
abstract Age: int with get | |
member this.Greet() = | |
printfn "Hello, %s" this.Name | |
type AbstractPersonImplement(name, age) = | |
inherit AbstractPerson() | |
override this.Name with get() = name | |
override this.Age with get() = age | |
[<EntryPoint>] | |
let main argv = | |
let person: AbstractPerson = AbstractPersonImplement("Takashi", 20) :> AbstractPerson | |
person.Greet() | |
0 |
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
type String with | |
member this.ToNumber() = int(this) | |
[<EntryPoint>] | |
let main argv = | |
let num = "30".ToNumber() | |
0 |
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
let ``Hello, world Function!``() = | |
printfn "Hello, World!" | |
[<EntryPoint>] | |
let main argv = | |
``Hello, world Function!``() | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment