fork download
  1. open System
  2. open System.IO
  3. open System.Net
  4. open System.Runtime.Serialization
  5. open System.Text
  6. open Microsoft.FSharp.Core
  7.  
  8. type Article = {
  9. name: string
  10. }
  11.  
  12. type Error = {
  13. code: string
  14. message: string
  15. }
  16.  
  17. let create (schema: Article) : Result<Article, Error> =
  18. let request = WebRequest.Create("http://e...content-available-to-author-only...e.com") :?> HttpWebRequest
  19. request.Method <- "GET"
  20. try
  21. use response = request.GetResponse() :?> HttpWebResponse
  22. use reader = new StreamReader(response.GetResponseStream())
  23. use memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(reader.ReadToEnd()))
  24. Ok ((new DataContractJsonSerializer(typeof<Article>)).ReadObject(memoryStream) :?> Article)
  25. with
  26. | :? WebException as e ->
  27. use reader = new StreamReader(e.Response.GetResponseStream())
  28. use memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(reader.ReadToEnd()))
  29. Error ((new DataContractJsonSerializer(typeof<Error>)).ReadObject(memoryStream) :?> Error)
  30.  
  31. let update (schema: Article) : Result<Article, Error> =
  32. let request = WebRequest.Create("http://e...content-available-to-author-only...e.com") :?> HttpWebRequest
  33. request.Method <- "PUT"
  34. try
  35. use response = request.GetResponse() :?> HttpWebResponse
  36. use reader = new StreamReader(response.GetResponseStream())
  37. use memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(reader.ReadToEnd()))
  38. Ok ((new DataContractJsonSerializer(typeof<Article>)).ReadObject(memoryStream) :?> Article)
  39. with
  40. | :? WebException as e ->
  41. use reader = new StreamReader(e.Response.GetResponseStream())
  42. use memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(reader.ReadToEnd()))
  43. Error ((new DataContractJsonSerializer(typeof<Error>)).ReadObject(memoryStream) :?> Error)
  44.  
  45. let delete (schema: Article) : Result<Article, Error> =
  46. let request = WebRequest.Create("http://e...content-available-to-author-only...e.com") :?> HttpWebRequest
  47. request.Method <- "PUT"
  48. try
  49. use response = request.GetResponse() :?> HttpWebResponse
  50. use reader = new StreamReader(response.GetResponseStream())
  51. use memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(reader.ReadToEnd()))
  52. Ok ((new DataContractJsonSerializer(typeof<Article>)).ReadObject(memoryStream) :?> Article)
  53. with
  54. | :? WebException as e ->
  55. use reader = new StreamReader(e.Response.GetResponseStream())
  56. use memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(reader.ReadToEnd()))
  57. Error ((new DataContractJsonSerializer(typeof<Error>)).ReadObject(memoryStream) :?> Error)
  58.  
  59.  
  60. let chain = create >> Result.bind update >> Result.bind delete
  61. match chain({name = "test"}) with
  62. | Ok message -> Debug.WriteLine(message)
  63. | Error error -> Debug.WriteLine(error)
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/yckFZO/prog.fs(17,32): error FS0039: The type 'Result' is not defined

/home/yckFZO/prog.fs(24,9): error FS0039: The value or constructor 'Ok' is not defined

/home/yckFZO/prog.fs(29,9): error FS0039: The value or constructor 'Error' is not defined

/home/yckFZO/prog.fs(31,32): error FS0039: The type 'Result' is not defined

/home/yckFZO/prog.fs(38,9): error FS0039: The value or constructor 'Ok' is not defined

/home/yckFZO/prog.fs(43,9): error FS0039: The value or constructor 'Error' is not defined

/home/yckFZO/prog.fs(45,32): error FS0039: The type 'Result' is not defined

/home/yckFZO/prog.fs(52,9): error FS0039: The value or constructor 'Ok' is not defined

/home/yckFZO/prog.fs(57,9): error FS0039: The value or constructor 'Error' is not defined

/home/yckFZO/prog.fs(60,23): error FS0039: The namespace or module 'Result' is not defined

/home/yckFZO/prog.fs(60,45): error FS0039: The namespace or module 'Result' is not defined

/home/yckFZO/prog.fs(62,7): error FS0039: The pattern discriminator 'Ok' is not defined
stdout
Standard output is empty