Mathematica 中的符号替换问题

发布于 2024-11-24 19:59:40 字数 317 浏览 1 评论 0原文

我想定义一个符号并在函数中使用它。例如,将 IDnumbers 定义为数字列表:

ParallelMap[{#1, Name[#1], Age[#1]} &, IDnumbers]

userlist={#1, Name[#1], Age[#1]} 变为:

ParallelMap[userlist &, IDnumbers]

它与列表本身一起工作得很好代码,但不带有符号。字符串列表与分配给字符串列表的符号也会发生同样的情况。这是为什么呢?

I want to define a symbol and use it within a function. For example, with IDnumbers defined as a list of numbers:

ParallelMap[{#1, Name[#1], Age[#1]} &, IDnumbers]

With userlist={#1, Name[#1], Age[#1]} becomes:

ParallelMap[userlist &, IDnumbers]

It works just fine with the list itself in the code, but not with the symbol. The same thing happens with a list of strings vs. a symbol assigned to a list of strings. Why is this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏蝶舞 2024-12-01 19:59:40

由于 f[#]&Function[f[#]] 的简写,因此您应该始终使用尾随 & 来完成匿名函数得到一个工作函数。
在您的示例中:

userlist={#1, Name[#1], Age[#1]}&
ParallelMap[userlist, IDnumbers]

更彻底的解释:

通过仅使用诸如f[#]之类的东西,您会得到(在FullForm[]中),

In[15]            := f[#] // FullForm
Out[15]//FullForm  = f[Slot[1]]

而这会被转换通过尾随 & 运算符将其转换为函数:

In[16]            := f[#]& // FullForm
Out[16]//FullForm  = Function[f[Slot[1]]]

如果分两步执行此操作,& 不会计算中间变量 expr

In[25]:= expr = f[#]//FullForm
In[26]:= expr &
Out[25]//FullForm = f[Slot[1]]
Out[26]           = expr &

您可以在包装之前强制对 expr 求值在 Function[] 中使用 Evaluate[]

In[27]:= expr=f[#]//FullForm
In[28]:= Evaluate[expr]&
Out[27]//FullForm = f[Slot[1]]
Out[28]           = f[Slot[1]]&

另一种方法是自己提供 Function[] 包装器:

userlist={#1, Name[#1], Age[#1]}
ParallelMap[Function[userlist], IDnumbers]

就我个人而言,我会考虑这样做糟糕的编码风格。只需习惯于始终使用尾随 & 来结束匿名函数,就像您为相应的左括号 ( 提供右括号 ) 一样。

编辑
好的,在您动态生成的匿名函数的情况下,我可以明白为什么您不能直接提供 & 。只需将表达式与 Slot[] 封装在 Function[] 中即可。

Since f[#]& is shorthand for Function[f[#]] you should always complete your anonymous function with a trailing & to get a working function.
In your example:

userlist={#1, Name[#1], Age[#1]}&
ParallelMap[userlist, IDnumbers]

More thorough explanation:

By just using something like f[#] you get (in FullForm[])

In[15]            := f[#] // FullForm
Out[15]//FullForm  = f[Slot[1]]

whereas this gets transformed to a Function by the trailing & operator:

In[16]            := f[#]& // FullForm
Out[16]//FullForm  = Function[f[Slot[1]]]

If you do this in two steps, & doesn't evaluate the intermediate variable expr:

In[25]:= expr = f[#]//FullForm
In[26]:= expr &
Out[25]//FullForm = f[Slot[1]]
Out[26]           = expr &

You can force the evaluation of expr before it gets wrapped in the Function[] by using Evaluate[]:

In[27]:= expr=f[#]//FullForm
In[28]:= Evaluate[expr]&
Out[27]//FullForm = f[Slot[1]]
Out[28]           = f[Slot[1]]&

Another way is to supply the Function[] wrapper yourself:

userlist={#1, Name[#1], Age[#1]}
ParallelMap[Function[userlist], IDnumbers]

Personally, i would consider this bad coding style. Just get used to always finishing an anonymous function with a trailing & like you would supply a closing paranthesis ) to a corresponding opening one (.

Edit
Ok, in your case of a dynamically generated anonymous function i can see why you couldn't supply the & directly. Just wrap the expression with the Slot[]s in a Function[] instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文