In Don Syme's
excellent book drafts of his upcoming book
Expert F#, the following code is used to demonstrate the quickness and easiness of F# interactive development.
> let req = WebRequest.Create("http://www.microsoft.com");;
val req : WebRequest
> let resp = req.GetResponse();;
val resp : WebResponse
> let stream = resp.GetResponseStream();;
val stream : Stream
> let reader = new StreamReader(stream) ;;
val reader : StreamReader
> let html = reader.ReadToEnd();;
val html : string = "<html>...</html>"
However, writing codes in such way would be too imperative. By using the idiomatic
using function, we can write it in a more concise and safer form:
#light
let http (url: string) =
let req = WebRequest.Create(url)
using (req.GetResponse())
(fun res -> using (res.GetResponseStream())
(fun stream -> let reader = new StreamReader(stream)
reader.ReadToEnd()))
The arguments of
using are:
val it : 'a -> ('a -> 'b) -> 'b when 'a :> IDisposable = <fun:clo@0>
Clearly, the first argument requires a resource that implements the IDisposable interface (
:> is a special operator in F# for up-casting); then
using will feed the resource to a function to process further and finally will guarantee to release the resource when everything is done.
分享到:
相关推荐
You can significantly minimize the number of header files you need to include in your own header files by using forward declarations. For example, if your header file uses the File class in ways that ...
This paper conducts a comprehensive investigation based on idiom in four-year series of textbooks of undergraduate and gets the distribution of different grades idioms. We also analyze idioms ...
useEnglish 资源E nglish Phrasal Verb & Idiom
四字成语mysql数据30810条
Emotional Classification of Chinese Idioms Based on Chinese Idiom Knowledge Base
1.10 Using List Comprehensions Instead of map and filter 1.11 Unzipping Simple List-Like Objects 1.12 Flattening a Nested Sequence 1.13 Looping in Parallel over Index and Sequence Items 1.14 ...
Processing All of a List's Items in Random Order Recipe 5.7. Keeping a Sequence Ordered as Items Are Added Recipe 5.8. Getting the First Few Smallest Items of a Sequence Recipe 5.9. Looking for...
好的翻译软件,能用于有效的翻译,是一个非常不错的软件。本人经过测试,完全好用。
iom` (`id`, `guid`, `idiom`, `full_pinyin`, `pinyin`, `az`, `description`, `source`, `idiom_traditional`, `spinyin`, `idiom_phonetic`, `idiom_correct_pronunciation`, `idiom_distinguish`, `idiom_...
成语Idiom 是 AngularJS 的新翻译系统,它允许您在当前范围内阅读以提供基于上下文的翻译 - 没有更多的东西,他/她,您实际上知道您在写什么。 #用法向您的项目添加一个文件夹,其中包含每种语言的一个 json 文件...
四个基本技巧:如果可能尽量使用接口来编程,使用属性代替原始数据,在Producer/Consumer 的Idiom中使用Delegate,注意初始化顺序
Idiom WorldServer Desktop Workbench 8.1.0.208 使用指南
and numeric types * Creating, comparing, indexing, slicing, and formatting strings * Understanding Go,s highly efficient built-in collection types: slices and maps * Using Go as a procedural ...
Python惯例“惯例”这个词指的是“习惯的做法,常规的办法,一贯的做法”,与这个词对应的英文单词叫“idiom”。好的代码:print(name) # jac
This makes it possible to design the business logic using plain .NET (CLR) objects and object-oriented idiom. * Natural programming model - NHibernate supports natural OO idiom; inheritance, ...
This makes it possible to design the business logic using plain .NET (CLR) objects and object-oriented idiom. * Natural programming model - NHibernate supports natural OO idiom; inheritance, ...
This makes it possible to design the business logic using plain .NET (CLR) objects and object-oriented idiom. * Natural programming model - NHibernate supports natural OO idiom; inheritance, ...
Python惯例“惯例”这个词指的是“习惯的做法,常规的办法,一贯的做法”,与这个词对应的英文单词叫“idiom”。好的代码:print(name) # jac
NHibernate-2.0.1 + Movies + Sample Source Part1 NHibernate is a port of Hibernate Core for... This makes it possible to design the business logic using plain .NET (CLR) objects and object-oriented idiom.
猜成语小程序数据库表ims_yf_chengyu_idiom