是否有类似于上下文管理器的东西,因此无需运行近输入端口?
#!/usr/bin/env racket
#lang racket/base
(require net/url racket/port)
(require (planet neil/html-parsing:3:0))
(define p (get-pure-port (string->url "https://www.rosettacode.org/wiki/Web_scraping")))
(define my-html (port->string p))
(close-input-port p)
是否有另一种重写上述代码的方式,因此无需明确 指示(关闭输入 - 端口P)
? 我牢记一些类似的呼叫输入文件
涉及关闭的东西 端口读取完成的文件后(类似于)。
#!/usr/bin/env racket
#lang racket/base
(require net/url racket/port)
(require (planet neil/html-parsing:3:0))
(define p (get-pure-port (string->url "https://www.rosettacode.org/wiki/Web_scraping")))
(define my-html (port->string p))
(close-input-port p)
Is there another way of rewriting the above code so that there is no need to explicitly
instruct (close-input-port p)
?
I have in mind something similar call-with-input-file
that deals with closing
the port once the job of reading the file done (akin to context managers in Python).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种特殊情况下,您可以使用
call/input-url
:它使用给定的连接功能从URL打开端口,使用该端口调用处理程序功能,然后在返回处理程序返回之前关闭端口值。
开放端口的许多方案和球拍特定的功能类别都具有等效物,例如
call-input-input-file
就像我在回答您的另一个问题的回答时所使用的那样。In this particular case, you can use
call/input-url
:which opens a port from a URL using the given connection function, calls a handler function with that port, and closes the port before returning the handler's return values.
Many Scheme and Racket-specific function categories that open ports have equivalents, like the
call-with-input-file
like I used in an answer to another of your questions.