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.
分享到:
相关推荐
"idiom翻译软件"是一款专为用户提供了高效翻译服务的应用程序。它被设计用来帮助用户理解和传达不同语言之间的意义,确保跨文化交流的准确性和流畅性。这款软件在经过实际测试后,被认为是非常实用且功能完备的。 ...
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
### Idiom WorldServer Desktop Workbench 8.1.0.208 使用指南:关键知识点解析 #### 一、概述与快速入门 **Idiom WorldServer Desktop Workbench**是一款专为语言服务提供商(LSPs)和企业翻译部门设计的翻译记忆...
四字成语mysql数据30810条
资源来自pypi官网。 资源全名:china_idiom-0.0.2-py3-none-any.whl
**Pimpl Idiom(指针实现技巧)** 在C++编程中,`Pimpl Idiom`,也称为`Pointer to Implementation`或`Impl Pointer`,是一种设计模式,它用于实现类接口与其实现的分离,以提高代码的编译时间和模块化。这种技术的...
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 文件...
thread safe, how to implement the Pimpl Idiom using std::unique_ptr, why you should avoid default capture modes in lambda expressions, or the differences between std::atomic and volatile? The answers ...
中文成语的情感分类是基于中文成语知识库的一个研究主题,这个研究的核心是通过构建中文成语知识库,并运用该知识库进行自动化的情感分类实验。成语作为一种语言表达方式,不仅具有连续性和隐喻性,而且通常还含有...
【标题】"猜成语小程序数据库表ims_yf_chengyu_idiom"涉及到的是一个与成语相关的微信小程序的数据存储结构。这个数据库表很可能是用来管理小程序内部的成语信息,包括成语的ID、成语内容、含义、出处等关键数据,以...
"Idiom-看图猜成语程序"是一款基于Python语言开发的应用,旨在提供一种娱乐与学习相结合的方式,让用户通过观察图片来猜测对应的成语。这个程序可能是为教育或休闲目的设计的,利用Python的图像处理和人机交互功能,...
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, ...