仕事で使うF#

15,333 views

Published on

Published in: Technology
2 Comments
19 Likes
Statistics
Notes
No Downloads
Views
Total views
15,333
On SlideShare
0
From Embeds
0
Number of Embeds
10,205
Actions
Shares
0
Downloads
30
Comments
2
Likes
19
Embeds 0
No embeds

No notes for slide

仕事で使うF#

  1. 1. F# bleis-tiftAugust 28, 2011
  2. 2. F# F#NParsec FParsec
  3. 3. F#
  4. 4. F#OCaml.NET Framework
  5. 5. F#
  6. 6. F#cons
  7. 7. 1 3[1; 2; 3][1..3]1::2::3::[]1::2::[3][ 1 2 3 ]
  8. 8. cons
  9. 9. cons
  10. 10. cons
  11. 11. cons
  12. 12. cons
  13. 13. cons
  14. 14. let xs = [2..10]let ys = 1::xs
  15. 15. int string int * string
  16. 16. 1let x, y = 10, 20printfn "%A" (x, y) // => (10, 20) 2let (x, y) as tpl = 1, 2printfn "%A" tpl // => (1, 2)printfn "%A" (x, y) // => (1, 2)printfn "(%d, %d)" x y // => (1, 2)
  17. 17. let [x; y] as lst = [1; 2]printfn "%A" lst // => [1; 2]printfn "%A" [x; y] // => [1; 2]printfn "[%d; %d]" x y // => [1; 2]warning FS0025: ’[_;_;_]’
  18. 18. 1
  19. 19. 1
  20. 20. 1
  21. 21. 2(2, "hoge") // int * string//[2; "hoge"][box 1; box "hoge"] // obj list
  22. 22. 2
  23. 23. // ,let p = " ", 21type Person = { Name: string Age: int}let p = { Name = " "; Age = 21 } 4
  24. 24. F#type Person(name: string, age: int) = member this.Name = name member this.Age = agelet p = Person(" ", 21)type Person = { Name: string; Age: int }let p = { Name = " "; Age = 21 }
  25. 25. type Person(name: string, age: int) = member x.Name = name member x.Age = agelet f p = p.Nameerror FS0072:
  26. 26. type Person = { Name: string; Age: int }let f p = p.Nametype Person = {Name: string; Age: int;}val f : Person -> string
  27. 27. ( )
  28. 28. (enum)
  29. 29. HtmlElem
  30. 30. C#public abstract class HtmlElem {}public class Heading : HtmlElem { public int Level { get; private set; } public string Text { get; private set; } public Heading(int level, string txt) { Level = level; Text = txt; } }public class Text : HtmlElem { public string Text { get; private set; } public Text(string txt) { Text = txt; } }public class HorizontalLine : HtmlElem {}
  31. 31. F#type HtmlElem =| Text of string| Heading of int * string| HorizontalLine
  32. 32. EqualsGetHashCodeMake static analyzers happy== !=ToString
  33. 33. sprintf ”%A”
  34. 34. F#F# C#
  35. 35. HtmlElem HTMLtype HtmlElem =| Heading of int * string| Text of string| HorizontalLinelet toHtmlStr elem = let h lv s = sprintf "<h%d>%s</h%d>" lv s lv match elem with | Heading(level, txt) -> h level txt | Text txt -> "<p>" + txt + "</p>" | HorizontalLine -> "<hr/>" match
  36. 36. let add = function| 0, y -> y| x, 0 -> x| x, y -> x + y function
  37. 37. match functionmatchlet add tpl = match tpl with | x, y -> x + yfunctionlet add = function| x, y -> x + y
  38. 38. type t = { Tag: int; Value: int }let value { Value = v } = v
  39. 39. matchmatchlet add tpl = match tpl with | x, y -> x + yfunctionlet add = function| x, y -> x + ylet add (x, y) = x + y
  40. 40. Persontype Name = { FirstName: string LastName: string}type Person = { Name: Name; Age: int }let f x = let { Name = { FirstName = fn } } = x fn
  41. 41. F#
  42. 42. Maybetype MaybeBuilder() = member this.Bind(x, f) = x |> Option.bind f member this.Return(x) = Some xlet maybe = MaybeBuilder()let plus db = maybe { let! x = db |> Map.tryFind "x" let! y = db |> Map.tryFind "y" return x + y}
  43. 43. Map-Reduce ( )fold reducefold
  44. 44. fold reducefold reduce
  45. 45. F#fold reduce> List.fold;;val it : ((’a -> ’b -> ’a) -> ’a -> ’b list -> ’a) = <fun:clo@1>> List.reduce;;val it : ((’a -> ’a -> ’a) -> ’a list -> ’a) = <fun:clo@2-1>foldreduce list list
  46. 46. fold
  47. 47. fold
  48. 48. fold
  49. 49. fold
  50. 50. foldfoldsumforallmapfilter
  51. 51. foldfoldsumforallmapfilter
  52. 52. fold sumsumlet sum xs = List.fold (+) 0 xs
  53. 53. fold forallforalllet forall p xs = xs |> List.fold (fun acc x -> acc && (p x)) trueexists
  54. 54. fold mapmaplet map f xs = ([], xs) ||> List.fold (fun acc x -> acc @ [f x] )
  55. 55. fold filterfilterlet filter p xs = ([], xs) ||> List.fold begin acc x -> if p x then acc @ [x] else acc end
  56. 56. foldfold fold( unfold) fold
  57. 57. F#
  58. 58. F#F# .NET
  59. 59. F# F#.NET Framework
  60. 60. .NET Framework 2.0 .NET Framework F#
  61. 61. .NET Framework F# .NET Framework2.0 Runtime .NET Framework2.0 LINQ F # List.NET Framework 2.0 F#
  62. 62. .NET Framework 4.0 F# TDDBC Tokyo 1.6 C# ( ) KeyValueTime.cs 42 SystemClock.cs 30 TddbcKeyValueStore.cs 67 F# ( ) KeyValueStore.fs 48C# 139 F# 48
  63. 63. .NET Framework 4.0 F# TDDBC Tokyo 1.6 C# ( ) KeyValueTimeTest.cs 141 SystemClockTest.cs 34 TddbcKeyValueStoreTest.cs 346 ( ) F# ( ) KeyValueStore.fs 170C# 521 F# 170
  64. 64. .NET Framework.NET Framework F# 2.0 C# F#
  65. 65. F#.NET F# option
  66. 66. F# C#VB
  67. 67. .NETC# VB
  68. 68. F# list F# dll array seq
  69. 69. staticNewHoge Tag IsHoge F#
  70. 70. module Hogelet plus10 = ((+)10) Hoge.plus10 FSharp.Core.FSharpFunc FSharp.Core.dll
  71. 71. NParsec FParsec
  72. 72. yacc lex yacc lex
  73. 73. BNF
  74. 74. parsecJParsecNParsecFParsecscala.util.parsingBoost.Spirit
  75. 75. NParsecparsec .NETC# C# JavaScanner
  76. 76. FParsecparsec .NETF#
  77. 77. NParsec FParsec
  78. 78. NParsecvar lazyexpr = new Parser<double>[1];var plazyexpr = Parsers.Lazy<double>(() => lazyexpr[0]);var pterm = plazyexpr.Between(popen, pclose) | pnum;var pexpr = ...lazyexpr[0] = pexpr;
  79. 79. FParseclet pexpr, exprref = createParserForwardedToRef()let pterm = pexpr |> between popen pclose <|> pnumdo exprref := ...
  80. 80. F# 10 C# 54 C# Visitor
  81. 81. C#F#
  82. 82. NParsec Bindreturn Parsers.Return(...)FParsec
  83. 83. NParsec
  84. 84. F# FParsec F#

×