有没有一种有效的方法可以在一个代码中找到最大值,最小值,奇数和偶数?

时间:2019-11-24 19:51:15

标签: applescript

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

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

set numberList to {3, 4, 5, 6, 7, 8, 9, 10}
repeat with i in numberList
    if i mod 2 = 0 then
        display dialog "These numbers are even " & i
    else
        display dialog "These numbers are odd " & i
    end if
end repeat

是否有使用AppleScript确定列表的奇数,偶数,最大值和最小值的更有效方法?

1 个答案:

答案 0 :(得分:0)

AppleScriptObjC可以做到无循环

use AppleScript version "2.5"
use framework "Foundation"

set numberList to {3, 4, 5, 6, 7, 8, 9, 10}
set nsList to current application's NSArray's arrayWithArray:numberList
set max to (nsList's valueForKeyPath:"@max.intValue") as integer
set min to (nsList's valueForKeyPath:"@min.intValue") as integer
set even to (nsList's filteredArrayUsingPredicate:(current application's NSPredicate's predicateWithFormat:"modulus:by:(intValue,2) == 0")) as list
set odd to (nsList's filteredArrayUsingPredicate:(current application's NSPredicate's predicateWithFormat:"modulus:by:(intValue,2) == 1")) as list