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

查看:22
本文介绍了在 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的内容而不仅仅是x(感谢@vadian),因为 contentsdereferences(即评估)一个 referenced 项目.(暂时不要担心,稍后再了解它,因为如果您知道如何使用它,它可能是一个非常有用的概念.

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,而 AppleScriptObjC 最初会因桥接到 Objective-C 所产生的额外开销而受到影响.当处理每个列表的数百或数千个项目时,成本最终会得到补偿,AppleScriptObjC 毫无疑问会在速度测试中用 AppleScript 擦掉地板.但是对于处理少于一百个项目的列表的许多日常情况,乌龟出乎意料且令人满意地击败了兔子,但您需要知道如何构建脚本才能做到这一点.

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 的项目与最后一个位置的项目进行比较.获胜者(最大的值)可以坐在最后一个位置.然后 list 通过获取 rest of... 被缩短(或 reduced),它每次删除第一项,直到我们只剩下一个项目列表.该项目将是原始列表中的幸存者,因此这是我们检索和返回的最大值.

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.

此技术是reducefold 一个列表一点一点地,通常用于降低列表的复杂性(但它不是必须的),这样你就可以从一个由多个对象组成的对象开始可能以复杂方式构建的项目,但经过反复折叠后,您最常留下的是一个更简单的对象,例如一个单一的一元值,例如数字.

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).

明天还有更多……

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

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