在AppleScript中查找最大值 [英] Finding the Max in AppleScript

查看:66
本文介绍了在AppleScript中查找最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我只需要帮助才能找到此代码的最大值.

Basically I just need help with finding the max of this code.

set numberList to {3, 4, 5, 6, 7, 8, 9, 10}
set max to numberList
repeat with x in numberList
    if x > max then set max to x
end repeat
display dialog max

我得到的错误是:

"Can’t make {3, 4, 5, 6, 7, 8, 9, 10} into type number." number -1700 from {3, 4, 5, 6, 7, 8, 9, 10} to number

推荐答案

我很高兴 @vadian 选择了AppleScriptObjC方法,因为它使我可以尝试使用普通的AppleScript就像您尝试过(非常接近实现)一样.让我告诉你你有多近.这是您的脚本,在这里我只是切换出一个标识符,因为我喜欢用L表示通用的材料清单,并将在最初的天真之后的各种改进中始终如一地做到这一点. >算法.
另外,让我们让列表比大小逐渐增加的数字序列更有趣.

I'm glad @vadian went for the AppleScriptObjC method, because it allows me to geek out on doing it the vanilla AppleScript way, just like you tried (and came very close to achieving). Let me show you how close you were. Here's your script, where I'm just switching out an identifier because I like denoting a generic list of stuff by L, and will do this consistently in the various improvements that will follow on from this initial naive algorithm.
Also, let's make the list a bit more interesting than a sequence of numbers with increasing size.

我已经用紧跟脚注的数字标记了问题行:

I've labelled problem lines with a number that refers to footnotes immediately following:

set L to {6, 3, 7, 9, 4, 10, 2, 10, 8, 2}
set max to L --(1)

repeat with x in L
    if x > max then set max to x
end repeat

display dialog max --(2)

¹ max设置为初始列表,L是我们稍后将要讨论的一个想法,但是在这里将无法使用,因为不可能将数字与数字列表进行比较,然后告诉我这两个数字中哪个具有更大的数值?因此,您希望max的初始值是一个数字,但它本身不能大于列表中包含的最大值,这就是为什么认为0是一个常见错误的原因.合适的初始值会吸引您(考虑负数列表):我无法创建保证此算法返回错误结果的列表(任何长度)的唯一一组值是该算法对其进行操作的任何列表的任何子集.换句话说,从列表L中选择任何值.
¹ Setting max to the initial list, L is an idea we'll look at later, but won't work here because it's not possible to compare a number to a list of numbers and tell me which of the two has a greater numeric value ? Therefore, you want your initial value of max to be a number, but it can't be a number that is, itself, greater than the maximum value contained by your list, which is why the common mistake of thinking 0 is a suitable initial value will catch you out (consider a list of negative numbers): the only set of values that I could not create a list (of any length) that guarantees this algorithm to return the incorrect result is any subset of whatever list the algorithm operates upon. In other words, choose any value from the list L.

² 远离新手的倾向,在对话框中可视化结果.您会很感激的.而是使用脚本编辑器窗口底部的结果(或更有用的是答复)窗格.如果您使用的是 Script Debugger ,这是他们的事件面板和结果面板试图进行如此努力改进的一个疏忽,但有时越简单越好.
² Get away from the novice's inclination to visualise results in dialog boxes. You'll be thankful you did. Instead, use the Results (or, even more useful, the Replies) pane at the bottom of your Script Editor window. If you're using Script Debugger, it's the one oversight that their events panel and their results panel tried so hard to improve on, but sometimes simpler is better.

现在是工作版本:

And now the working version:

set L to {6, 3, 7, 9, 4, 10, 2, 10, 8, 2}
set max to some item in L

repeat with x in L
    if x > max then set max to x's contents
end repeat

return max

我在青少年时代度过了很多时间,没有朋友学习通过将事物组合到列表中来完成事情的乐趣.在数学中,列表称为集合,并且有一个研究领域称为集合论".该结果证明可以专门用于定义每个已经存在或将要存在的数学原理.那就是惊人的列表.

I spent a lot of time as a teenager with no friends learning the joys of what can be done by grouping things together in lists. In mathematics, lists are called sets, and there's a field of study called Set Theory, which it turns out can be used exclusively to define every mathematical principle that exists or ever will exist. That's how amazing lists are.

在列表中找到最大值是一个是人们自己了解或发现的,非常重要的事情之一.这是一种在集合上操作的映射类型,它挑选出最大的元素,这是定义一个数字系统的基础,该数字系统现在可以将数字排序.我首先用Pascal编写了这样的过程,此后我已经用其他十多种语言编写了程序,但是AppleScript可以比其他语言更加有趣,因为您可以用它来做可能不是本来可以做的事情.并非意味着能够做到,但是却以某种方式做到了. AppleScript列表也是一个奇怪的结构,因为它们是递归的,并且是无限的自我参照,这是一个有趣的特性.

Finding the maximum value in a list is one of the first, and incredibly important things one either learns about or discovers for themselves. It's a type of mapping that operates on a set, picking out the largest element, which is fundamental to defining a number system that now has a means of putting numbers in order. I first wrote such a procedure in Pascal, which I've since written in a dozen other languages, but AppleScript is so much more fun to do this with than in some other languages, because you can get it to do things that it probably wasn't meant to be able to do but somehow does. AppleScript lists are also an odd construct, as they are recursively, and infinitely, self-referential, which turns out is an interestingly property.

我不会探究引用值是什么,但这就是为什么我的脚本引用x's contents而不是x的原因(感谢 @vadian ),因为contents引用的(即评估)引用的项. (暂时不必担心,但是以后再学习它,因为如果您知道如何使用它,它将是一个非常有用的概念.)

I won't explore what referenced values are, but it's why my script refers to x's contents rather than just x (thanks @vadian), as contents is what dereferences (i.e. evaluates) a referenced item. (Don't worry about it for now, but learn about it later on, as it can be a very useful concept to make use of if you know how).

这还很有趣,除非处理非常大的列表,否则普通的AppleScript通常会迅速地胜过AppleScriptObjC,这最初会因桥接到Objective-C而产生额外的开销.当处理每个列表中成百上千个项目时,成本最终会降低,AppleScriptObjC毫无疑问将在速度测试中使用AppleScript消除成本.但是对于许多日常情况下处理少于100件物品的清单来说,乌龟出乎意料地令人满意地击败了野兔,但是您需要知道如何构造脚本才能做到这一点.

It's also amusing that, unless dealing with lists that are really big, vanilla AppleScript typically out-performs AppleScriptObjC speedwise, which initially suffers with the extra overhead incurred from bridging to Objective-C. The cost eventually gets recuperated when dealing with hundreds or thousands of items per list, which AppleScriptObjC will, without question, wipe the floor with AppleScript in a speed test. But for many everyday situations handling lists with under a hundred items, the tortoise beats the hare quite unexpectedly and satisfyingly, but you need to know how to structure your script to get it to do this.

所以...接下来的其他事情都是为了教育乐趣...

So... The rest of what will follow is for educational fun...

我将首先考虑将初始值max设置为初始列表的想法,然后一遍又一遍地对该列表执行一个过程,最终使我们获得最大值.但是我不会费心定义一个单独的max变量,因为我们可以在原始列表L上执行该过程.看看您的初始技术的这一细微变化:

I'll start by taking your initial idea of setting max to the initial list, and perform a process on this list over and over that ends up getting us the maximum value. But I'm not going to bother to define a separate max variable, as we can just perform the process on the original list, L. Take a look at this slight variation on your initial technique:

set L to {6, 3, 7, 9, 4, 10, 2, 10, 8, 2}

repeat while L's length > 1
    if L's first item > L's last item then set ¬
        L's last item to L's first item
    set L to the rest of L
end repeat

return L's first item

AppleScript中任何list都有两个属性,上述脚本可以利用这些属性:length告诉使用列表中包含多少项;并且rest只是列表中的第一项.

There are two properties of any list in AppleScript that the above script makes use of: length tells use how many items the list contains; and rest is all but the first item in the list.

在此方法中,我们将位置1的项目与最后位置的项目进行比较.获胜者(最大的赢家)将坐到最后一个位置.然后,通过获取rest of...list缩短(或减少 ),每次删除第一项直到我们剩下一个单项列表.该项目将是原始列表中的幸存者,因此这是我们检索并返回的最大值.

In this method, we compare the item at position 1 to the item at the last position. The winner (the largest value) gets to sit in the last position. Then the list is shortened (or reduced) by getting the rest of... it, which removes the first item is each time until we're left with just a one-item list. That item will be survivor from the original list and so that's our maximum value that we retrieve and return.

此技术是reduce 一点一点的列表,通常用来降低列表的复杂性(但不是必须这样做),因此您可以从一个由许多可能以复杂方式构造的项目组成的对象开始,但是经过反复折叠后,剩下的最简单的对象是一个简单的对象,例如单个一元值(例如数字).

This technique is a basic implementation of a process to reduce or fold a list bit by bit, often acting to reduce the complexity of a list (but it doesn't have to), so that you start with an object composed of many items that might be structured in a complex way, but after repeated folding, what you're most often left with is a simpler object, like a single, unary value such as a number.

它可能不会出现,但这是一项非常强大的技术,可以构成我们想对列表进行的许多其他操作的基础,例如计算列表中包含的项目数,或通过使用相同的规则适用于每个规则,例如将所有项目加倍(这是一种称为map的过程),或删除所有奇数值的项目但保留所有偶数值的项目(这是filter).

It might not appear so, but this is a really powerful technique that can form the basis of lots of other things we like to do with lists, such as counting the number of items it contains, or changing the items by using the same rules applied to each, e.g. doubling all items (this is a type of process called a map), or removing all odd-valued items but keeping all even-valued ones (this is a filter).

明天还有更多后续活动...

More to follow tomorrow...

这篇关于在AppleScript中查找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆