Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. // 鳥とバランス棒のシノニムを定義
  2. typealias Birds = Int
  3. typealias Pole = (left: Birds, right: Birds)
  4.  
  5. // 左右に鳥が飛んできたことを表現
  6. func landLeft(n: Birds) -> Pole -> Pole {
  7. return {p in (p.left + n, p.right)};
  8. }
  9.  
  10. func landRight(n: Birds) -> Pole -> Pole {
  11. return {p in (p.left, p.right + n)};
  12. }
  13.  
  14. // 動作チェック
  15. landLeft(2)((0, 0))
  16. landRight(1)((1, 2))
  17.  
  18. landLeft(2)( landRight(1)( landLeft(1)((0, 0))))
  19.  
  20. // 複数回鳥が飛んできた場合見づらいので演算子を定義
  21. infix operator => {
  22. associativity left
  23. }
  24. func =><T, U>(t: T, f : T -> U) -> U {
  25. return f(t)
  26. }
  27.  
  28. (0, 0) => landLeft(1) => landRight(1) => landLeft(2)
  29.  
  30. // 今の実装だと失敗を表現できない
  31. landLeft(10)((0, 3))
  32.  
  33. // 途中で失敗しているがそのことがわからない
  34. // 大量のif文を書くしかなくなる
  35. (0, 0) => landLeft(1) => landRight(4) => landLeft(-1) => landRight(-2)
  36.  
  37. /////////////////////
  38. // 失敗を表現する型を定義
  39. enum Maybe<T>: Printable {
  40. case Success(T)
  41. case Failure
  42.  
  43. // 実行時の見やすさのために定義
  44. var description: String {
  45. switch self {
  46. case let .Success(v): return "Success(\(v))"
  47. case .Failure: return "Failure"
  48. }
  49. }
  50. }
  51.  
  52.  
  53. func mLandLeft(n: Birds) -> Pole -> Maybe<Pole> {
  54. return {p in
  55. switch (p.left + n, p.right) {
  56. case let (l, r) where abs(l - r) < 4:
  57. return .Success((l, r))
  58. case _:
  59. return .Failure
  60. }
  61. }
  62. }
  63.  
  64. func mLandRight(n: Birds) -> Pole -> Maybe<Pole> {
  65. return {p in
  66. switch (p.left, p.right + n) {
  67. case let (l, r) where abs(l - r) < 4:
  68. return .Success((l, r))
  69. case _:
  70. return .Failure
  71. }
  72. }
  73. }
  74.  
  75. func p(v : Printable) {
  76. println(v.description)
  77. }
  78.  
  79. mLandLeft(1)((1, 2)) => p
  80. mLandLeft(10)((0, 3)) => p
  81.  
  82. println("")
  83.  
  84. infix operator >=> {
  85. associativity left
  86. }
  87.  
  88. func >=><T, U>(t: Maybe<T>, f : T -> Maybe<U>) -> Maybe<U> {
  89. switch t {
  90. case let .Success(v): return f(v)
  91. case .Failure: return .Failure
  92. }
  93. }
  94.  
  95. .Failure >=> mLandLeft(2) => p;
  96. .Success((0, 0)) >=> mLandLeft(1) >=> mLandRight(4) >=> mLandLeft(-1) >=> mLandRight(-2) => p;
  97.  
  98. ////////////////////////
  99.  
  100.  
  101. func oLandLeft(n: Birds) -> Pole -> Pole? {
  102. return {p in
  103. switch (p.left + n, p.right) {
  104. case let (l, r) where abs(l - r) < 4:
  105. return (l, r)
  106. case _:
  107. return nil
  108. }
  109. }
  110. }
  111.  
  112. func oLandRight(n: Birds) -> Pole -> Pole? {
  113. return {p in
  114. switch (p.left, p.right + n) {
  115. case let (l, r) where abs(l - r) < 4:
  116. return (l, r)
  117. case _:
  118. return nil
  119. }
  120. }
  121. }
  122.  
  123.  
  124. infix operator >>= {
  125. associativity left
  126. }
  127.  
  128. func >>=<T, U>(t: T?, f : T -> U?) -> U? {
  129. switch t {
  130. case let .Some(v): return f(v)
  131. case .None: return .None
  132. }
  133. }
  134.  
  135.  
  136. nil >>= oLandLeft(2)
  137. (0, 0) >>= oLandLeft(1) >>= oLandRight(4)
  138. (0, 0) >>= oLandLeft(1) >>= oLandRight(4) >>= oLandLeft(-1) >>= oLandRight(-2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement