首页 \ 问答 \ 在Rebol 2中,对象上的位置PICK是什么,以及等效的Rebol 3是什么?(What does positional PICK on an object do in Rebol 2, and what's the equivalent Rebol 3?)

在Rebol 2中,对象上的位置PICK是什么,以及等效的Rebol 3是什么?(What does positional PICK on an object do in Rebol 2, and what's the equivalent Rebol 3?)

在Rebol 2中:

>> foo: make object! [a: 10 b: 20]

>> foo/a
== 10

>> foo/b
== 20

>> first foo
== [self a b]

>> second foo
== [make object! [
        a: 10
        b: 20
    ] 10 20]

>> third foo
== [a: 10 b: 20]

>> fourth foo
** Script Error: fourth expected series argument of type:
   series date port tuple event
** Near: fourth foo

所以你可以选择它,就好像它是一个值为1,2,3的块。但是在Rebol 3中进行位置选择是正确的:

>> first foo
** Script error: cannot use pick on object! value
** Where: first
** Near: first foo

我收集到现在已经弃用(比如选择一个函数来获取它的参数列表)。 但是,我正在尝试翻译一些代码,如下所示:

bar: construct/with (third foo) mumble

(a)该代码的重点是什么?

(b)我如何将它翻译成Rebol 3?


In Rebol 2:

>> foo: make object! [a: 10 b: 20]

>> foo/a
== 10

>> foo/b
== 20

>> first foo
== [self a b]

>> second foo
== [make object! [
        a: 10
        b: 20
    ] 10 20]

>> third foo
== [a: 10 b: 20]

>> fourth foo
** Script Error: fourth expected series argument of type:
   series date port tuple event
** Near: fourth foo

So you can pick out of it as if it were a block for values 1, 2, 3. But doing positional selection is right out in Rebol 3:

>> first foo
** Script error: cannot use pick on object! value
** Where: first
** Near: first foo

I gather that this is deprecated now (like picking out of a function to get its parameter list). However, I'm trying to translate some code that says something like:

bar: construct/with (third foo) mumble

(a) What is the point of that code?

(b) How would I translate it to Rebol 3?


原文:https://stackoverflow.com/questions/14330738
更新时间:2023-06-29 19:06

最满意答案

调用(list (A 3))返回列表中的函数:

user> (list (A 3) 0)
(#<user$A$fn__2934 user$A$fn__2934@2f09b4cb> 0)

eval期望在列表中获得一个符号,它正在获得它自己的功能。 如果您引用(A 3)的电话,那么您将获得您寻求的结果

user> (println (eval (list '(A 3) 0)))
3
nil

在调用eval之前正在评估此代码的一部分,然后eval正在评估其余代码。 在引用的表格中使用的seval可能是一个术语或者有选择地不加引号( ~ )更常见。

user> (eval '((A 3) 0))
3

要么

user> (def my-number 3)
#'user/my-number
user> (eval `((A ~my-number) 0))
3

编辑:关于为什么它与零args一起工作的问题并且使用一个arg失败:

似乎两种形式都可以工作,如果你不将它们存储在变量中(即使用defn定义它们),而是手动内联它们:

user> (def A (fn [b] (fn [a] (+ a b))))
#'user/A
user> (eval (list (A 3) 0))
; Evaluation aborted.
user> (eval (list (fn [b] (fn [a] (+ a b)) 3) 0))
3

Calling (list (A 3)) returns a function in a list:

user> (list (A 3) 0)
(#<user$A$fn__2934 user$A$fn__2934@2f09b4cb> 0)

eval expects to get a symbol in a list and it was getting the function it's self. if you quote the call to (A 3) then you will get the result you seek

user> (println (eval (list '(A 3) 0)))
3
nil

Part of this code is being evaluated before the call to eval and then eval is evaluating the rest. It is more common to se eval used on quoted forms with perhaps a term or to selectively unquoted (~).

user> (eval '((A 3) 0))
3

or

user> (def my-number 3)
#'user/my-number
user> (eval `((A ~my-number) 0))
3

edit: on the question of why it works with zero args and fails with one arg:

it seems that both forms work if you don't store them in vars (ie define them with defn) and instead manually inline them:

user> (def A (fn [b] (fn [a] (+ a b))))
#'user/A
user> (eval (list (A 3) 0))
; Evaluation aborted.
user> (eval (list (fn [b] (fn [a] (+ a b)) 3) 0))
3

相关问答

更多
  • 其他答案已经解释了这个机制,但我认为哲学问题在于lisp和python以“代码”的不同方式。 在python中,表示代码的唯一方法是作为一个字符串,所以尝试评估一个非字符串当然会失败。 Lisp拥有更丰富的代码数据结构:列表,数字,符号等等。 所以表达式(+ 1 2)是一个包含符号和两个数字的列表。 评估一个列表时,你必须首先评估它的每个元素。 所以,在正常运行lisp代码的过程中需要评估一个数字是非常自然的。 为此,数字被定义为“评估自己”,这意味着评估后的数字与之前相同:只是一个数字。 eval函数将相 ...
  • 您是正确的,您需要编译meta-clojure.stm.RetryEx命名空间以生成类文件,然后才能包含它。 如果您正在编写一个供Java库使用的类,那么这有时很有用。而core.clj只是一个用于开发的测试工具,尽管它不是简单或典型的方法。 使用require而不是include从Clojure项目引用clojure命名空间更常见。 您可以通过查看项目的target/classes目录来检查实际编译的内容。 在编译之前我看到: arthur@a:~/meta-clojure$ ls target/clas ...
  • 调用(list (A 3))返回列表中的函数: user> (list (A 3) 0) (# 0) eval期望在列表中获得一个符号,它正在获得它自己的功能。 如果您引用(A 3)的电话,那么您将获得您寻求的结果 user> (println (eval (list '(A 3) 0))) 3 nil 在调用eval之前正在评估此代码的一部分,然后eval正在评估其余代码。 在引用的表格中使用的seval可能是一个术语 ...
  • 我已经切换到Sun的JDK6而不是GIJ。 我收到了“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure”。 我找到了这个帖子 ,这对我帮助很大。 我添加了选项“-Djava.net.preferIPv4Stack = true”,它现在可以正常工作了。 谢谢大家! I've switched to Sun's JDK6 instead of GIJ. I got "com.mysql.jdb ...
  • 在macvim中似乎存在与python相关的问题(例如,请参阅此线程)。 一些macvim快照可以工作,有些则不行。 一个用户试图编译一个快照,这个快照是python支持与2.6而不是2.7链接的segfaulting,它解决了崩溃问题。 所以它必须是python 2.7绑定的问题,如果可以,尝试将其更改为2.6。 您可以通过以下命令从vim中验证python版本: :python import sys :python print sys.version_info 但是,我不确定你有同样的问题,因为你没有 ...
  • 我假设这是因为列表的第一个arg应该是函数的名称,而这里是另一个列表 关闭 - 当评估代码时,将评估列表表单中的第一个项目,并将结果作为函数调用(第一个项目可以是函数的名称,或者是另一个返回函数的函数调用)。 在这种情况下, println函数总是返回nil ; 尝试调用nil作为函数会产生您看到的NPE。 在这种情况下,编译器/运行时不应该给出更好的错误吗? 不一定 - 编译器不能提前知道评估内部函数调用的结果是否是用于外部函数调用的有效函数。 以下工作正常: ((partial + 1) 2) 因为p ...
  • 那么,你也可以eval let表达式,看看这是否是你需要的: (eval '(let [x 3 y 3] (* x y))) 编辑: 根据评论,这将适用于你的情况: (def a (list (list * 'x 'y))) (eval (concat '(let [x 3 y 3]) a)) 更好的是,使用quasiquoting: (def a (list * 'x 'y)) (eval `(let ~'[x 3 y 3] ~a)) Well, you could also eval the le ...
  • 事情是with-collection宏扩展为-> ,所以你的函数应该把coll作为第一个参数。 你也可以使用`find函数来遮蔽monger find函数。 可能你应该做这样的事情: (defn my-find [sort] (with-collection my-db my-coll (find {}) sort) 并称之为: (my-find #(sort % (array-map :score -1 :name 1)) the thing is the with-collect ...
  • 该错误表明编译器正在处理类似的事情 (defn function-name comp (do-stuff ...)) 也许 (defn [arg1] comp (do-stuff ...)) 代替 (defn function-name [comp] (do-stuff ...)) load-file接受一个字符串而不是一个vector (load-file "path/to/file.clj)两个系统上的代码是不一样的? 路径解释/ vs \导致其他东西被加载吗? That error indica ...
  • 你的out-of-bounds? 检查错了。 你想要> =而不是>。 索引从0到n-1。 Your out-of-bounds? check is wrong. You want >= instead of >. Indexes go from 0 to n-1.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。