- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 Python 脚本移植到 Racket 作为学习经验,并且我有这个功能:
(define (check-status)
(define git [find-executable-path "git"])
(define-values (ckot out in err)
(subprocess #f #f #f git "checkout" "-q" "master"))
(define-values (local lout lin lerr)
(subprocess #f #f #f git "rev-parse" "@"))
(define-values (remote rout rin rerr)
(subprocess #f #f #f git "rev-parse" "@{u}"))
(define-values (merge-base mbout mbin mberr)
(subprocess #f #f #f git "merge-base" "@" "@{u}"))
(display-lines (port->lines mbout))
(define ports '(ckot out in err local lout lin lerr remote rout rin rerr merge-base mbout mbin mberr))
(map (lambda (x)
(cond ((input-port? x) (close-input-port x))
((output-port? x) (close-output-port x)))) ports))
(define (check-status)
(define commands '(
'("checkout" "-q" "master")
'("rev-parse" "@")
'("rev-parse" "@{u}")
'("merge-base" "@" "@{u}"))
(map currently-immaginary-git-command-fn commands))
最佳答案
首先,对您想要提出更清洁的解决方案有好处!你是对的,有一种更优雅的方式来做你尝试过的事情。
首先,使用 subprocess
在您的特定用例中几乎可以肯定是矫枉过正。 racket/system
模块提供了一个更简单的接口(interface),应该足以满足您的需求。具体来说,我会使用 system*
函数,它使用提供的参数执行单个进程,然后将其输出打印到标准输出。
使用 system*
,可以创建一个非常通用的帮助函数,该函数可以为特定的可执行文件执行命令并将其输出作为字符串返回。
(define (execute-command proc-name)
(define proc (find-executable-path proc-name))
(λ (args)
(with-output-to-string
(thunk (apply system* proc args)))))
((execute-command "git") '("checkout" "-q" "master"))
execute-command
的实现, 我们使用
with-output-to-string
重定向
system*
的所有输出调用一个字符串(而不仅仅是将其打印到标准输出)。这实际上只是使用
parameterize
的缩写。设置
current-output-port
参数,但它更简单。
check-status
非常简单地。
(define (check-status)
(define commands
'(("checkout" "-q" "master")
("rev-parse" "@")
("rev-parse" "@{u}")
("merge-base" "@" "@{u}")))
(map (execute-command "git") commands))
(execute-command "git")
的原因return a new function 变得很明显:我们可以使用它来创建一个函数,然后映射到
commands
list 生成一个新的字符串列表。
commands
的定义列表仅使用单个
'
一开始。您提供的定义不起作用,事实上,
ports
您在原始实现中定义的列表不是您所期望的。这是因为
'(...)
与
(list ...)
不完全相同——它们是不同的,所以在使用它们时要小心。
关于scheme - 我将如何使这个 Racket 代码干燥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30625909/
我正在使用 PLT 方案 (DrScheme)。我想加载从 here 获得的文件.要加载文件,我进入 PLT Scheme 并在交互窗口(或底部窗口)中输入(加载“simply.scm”),然后按 E
根据我的阅读,Scheme 中的符号不区分大小写 - 即 (eq? 'Hello 'hello) 评估为 #t (因为两者都由符号 'hello 表示,并且 scheme 具有两个具有相同名称的符
我正在尝试在 Scheme 中完成一个有限状态机。问题是,我不确定如何告诉它应该测试哪些字符。如果我想测试字符串“abc112”,我该怎么做? 代码如下: #lang racket (defin
我正在尝试做我的家庭作业,并通过一些示例代码进行破解,我看到了这一行: [(:+ (:or VAR)) (token-VAR (string->symbol lexeme))] 这是来自计算器中
我在 MIT/GNU Scheme 中运行了这个: (define x (+ 2 3)) 然后解释器打印: ;Value: x 但是根据我的教科书,define 表达式返回的值应该是未定义的。为什么解
考虑以下定义数字树的 BNF。请注意,树可以是叶子、具有一个子树的节点 1 或节点 2有两个子树。 tree ::= (’leaf number) | (’node-1 tree) | (’node-
(show-data 'YHOO :config 'my-config) 我看到了一些 Scheme 代码(在 Guile 中),如上面的行,并且对 colon 语法 :config 感到困惑。 这有
我目前正在尝试理解方案中流的概念。例如,我应该编写一个函数 fibonacci,它返回斐波那契数作为流表示形式。 函数的期望输出/用法如下所示: > (define a (finbonacci)) >
我想创建一个可以确定方案中任意函数定义的函数。如果我们将这样的函数称为“定义”,它会这样工作: (define (triple x) (* 3 x)) (definition triple) woul
在 Common Lisp 中,当我想根据 Common Lisp 实现使用不同的代码片段时,我可以使用 *features* 和提供的 #+ 和 #- 符号来检查给定功能的可用性并相应地进行。例如(
我正在学习 Scheme,具有 C/C++ 背景。我非常习惯于将相关的值组合到结构中,而且我发现 Scheme 的记录在这方面工作得很好。 如果发现自己经常这样做以避免函数体中的视觉噪音: (defi
在 Scheme R7RS 中,有 load 和 include 两种形式。 包含描述为: Semantics: Both include and include-ci take one or mor
我无法理解 Scheme 中收集器函数的使用。我正在使用“The Little Schemer”一书(Daniel P. Friedman 和 Matthias Felleisen 着)。一个带有一些
我知道您可以使用 (read) 来获取用户输入的表达式,但是 (read) 只会获取第一个表达式,然后对任何内容进行评估。我想知道是否有任何方法可以读取整行用户输入,也许将所述行转换为列表? (let
我正在重新熟悉 Scheme,我遇到了一个问题,这可能反射(reflect)了我的根本误解。 假设我在 Scheme 中执行以下操作(在这种情况下使用 Guile,但在 Chicken 中也是如此):
这是我目前正在自学的链接 Scheme,http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-1.html 据作者介绍, 然后我尝试了
假设我有这样的事情: (define pair (cons 1 (lambda (x) (* x x)) 如果我想返回这对的前对象,我会这样做: (car pair) 它返回 1。但是当对象是一个过程
我编写了以下示例,以尝试在 Chibi Scheme 0.5.3 中使用 R7RS 库: (define-library (example hello) (export hello-world
CODE SNIPPET 1 和 CODE SNIPPET 2 有什么区别? ;CODE SNIPPET 1 (define i 0) (do ()
长度为 n 的 k 元项链是一个长度为 n 的有序列表,其项目是从长度为 k 的字母表中抽取的,它是所有共享轮换排序的列表中按字典顺序排列的第一个列表。 例子: (1 2 3) 和 (1 3 2) 是
我是一名优秀的程序员,十分优秀!