let length = 500.0<m> let time = 20.0<s> let velocity = length / time // 25.0<m/s> let square = length * length // 250000.0<m^2> let length' = sqrt square // 500.0<m> let mass = 60.0<kg> let accel = length / time / time // 1.25<m/s^2> let force: float<N> = mass * accel // 75.0<N> let error = length + time // コンパイルエラー // ⾃作単位を宣⾔することも可能 [<Measure>] type YEN 最強のスクリプト⾔語F# @adacola 8
他のプログラム⾔語のバイナリを静的に読み込んで型定義付きラッパーを⽣成す るチートみたいなライブラリもある open System.IO open FSharpPlus open FSharp.Data type Recognition = JsonProvider<"""{"status":"success","candidates":[{"label":"hoge","score":0.8}]}"""> let tryRecognize imageFile = let image = imageFile |> File.ReadAllBytes let response = Http.RequestString(url, httpMethod = "POST", body = BinaryUpload image) let recognition = response |> Recognition.Parse if recognition.Status = "success" then recognition.Candidates |> map (fun candidate -> candidate.Label) |> Some else None 最強のスクリプト⾔語F# @adacola 10
2 // タプル type Person = { // レコード Name: string Age: int } // 直和型、匿名レコード type Family = | Person of Person | Pet of {| Name: string; Master: Person |} let family = Pet {| Name = "マロ"; Master = { Name = "相⽥マナ"; Age = 14 } |} match family with | Person { Name = name } -> printfn $"⼈間で名前は{name}です" | Pet pet -> printfn $"ペットで名前は{pet.Name}、飼い主の名前は{pet.Master.Name}です" 最強のスクリプト⾔語F# @adacola 11
-> 0 | 1 -> 1 | n -> fibonacci (n - 1) + fibonacci (n - 2) アクティブパターン Scalaのunapplyに相当 let (|Positive|_|) x = if x > 0 then Some x else None let (|Negative|_|) x = if x < 0 then x |> abs |> Some else None match -42 with | Positive result -> printfn $"positive abs:{result}" | Negative result -> printfn $"negative abs:{result}" | _ -> printfn "zero" 最強のスクリプト⾔語F# @adacola 12