text
stringlengths
0
15.7k
source
stringlengths
6
112
return listObject's _result_
lns3.scpt
_error("find in list", eText, eNumber, eFrom, eTo)
lns3.scpt
end find in list
lns3.scpt
to map list theList using mapObject
lns3.scpt
set mapObject to _support's asScriptParameter(mapObject, "using")
lns3.scpt
repeat with i from 1 to length of resultListObject's _list_ -- use counting loop rather than `repeat with aRef in theList` as the item's index is also used when constructing error messages
lns3.scpt
set item i of resultListObject's _list_ to mapObject's mapItem(item i of resultListObject's _list_)
lns3.scpt
on error eText number eNumber to eTo
lns3.scpt
set ePartial to items 1 thru (i - 1) of resultListObject's _list_
lns3.scpt
set ePartial to {}
lns3.scpt
error "Couldn’t map item " & i & ": " & eText number eNumber ¬
lns3.scpt
from (a reference to item i of _support's asListParameter(theList, "")) to eTo partial result ePartial
lns3.scpt
_errorWithPartialResult("map list", eText, eNumber, eFrom, eTo, ePartial)
lns3.scpt
end map list
lns3.scpt
to filter list theList using filterObject
lns3.scpt
set lastIndex to 0
lns3.scpt
repeat with i from 1 to length of resultListObject's _list_
lns3.scpt
set theItem to item i of resultListObject's _list_
lns3.scpt
if filterObject's filterItem(theItem) then
lns3.scpt
set lastIndex to lastIndex + 1
lns3.scpt
set item lastIndex of resultListObject's _list_ to theItem
lns3.scpt
if lastIndex > 1 then
lns3.scpt
set ePartial to items 1 thru (lastIndex - 1) of resultListObject's _list_
lns3.scpt
error "Couldn’t filter item " & i & ": " & eText number eNumber ¬
lns3.scpt
if lastIndex = 0 then return {}
lns3.scpt
return resultListObject's _list_'s items 1 thru lastIndex
lns3.scpt
_errorWithPartialResult("filter list", eText, eNumber, eFrom, eTo, ePartial)
lns3.scpt
end filter list
lns3.scpt
to reduce list theList using reduceObject
lns3.scpt
set theResult to missing value
lns3.scpt
if length of listObject's _list_ = 0 then _support's throwInvalidParameter(theList, "", list, "List can’t be empty.")
lns3.scpt
set reduceObject to _support's asScriptParameter(reduceObject, "using")
lns3.scpt
set theResult to item 1 of listObject's _list_
lns3.scpt
repeat with i from 2 to length of listObject's _list_
lns3.scpt
set theResult to reduceObject's reduceItem(theResult, item i of listObject's _list_)
lns3.scpt
error "Couldn’t reduce item " & i & ": " & eText number eNumber ¬
lns3.scpt
from (a reference to item i of _support's asListParameter(theList, "")) to eTo
lns3.scpt
_errorWithPartialResult("reduce list", eText, eNumber, eFrom, eTo, theResult)
lns3.scpt
end reduce list
lns3.scpt
property _quicksortThreshold : 8 -- shorter ranges are sorted using insertion sort; longer ranges using quicksort
lns3.scpt
to _sort(resultListObject, startIndex, endIndex, sortComparator, useQuickSort) -- performs in-place quicksort/insertionsort
lns3.scpt
if useQuickSort then -- sort mostly uses quicksort, but falls through to insertionsort when sorting small number of items (<8), or when sorting a mostly-sorted list, or when quicksort recursion exceeds AS's stack depth
lns3.scpt
if endIndex - startIndex < 1 then return
lns3.scpt
set {leftIndex, rightIndex} to {startIndex, endIndex}
lns3.scpt
set pivotValue to some item of items startIndex thru endIndex of resultListObject's _keys_
lns3.scpt
repeat while leftIndex ≤ rightIndex
lns3.scpt
repeat while sortComparator's compareKeys(get item leftIndex of resultListObject's _keys_, pivotValue) as number < 0 -- while cmp returns -1; note that if compareKeys() returns a non-numeric value/no result, this will throw -1700/-2763 error
lns3.scpt
set leftIndex to leftIndex + 1
lns3.scpt
error "Couldn’t compare object keys: " & eText number eNum from {item leftIndex of resultListObject's _keys_, pivotValue} to eTo
lns3.scpt
repeat while sortComparator's compareKeys(item rightIndex of resultListObject's _keys_, pivotValue) as number > 0 -- while cmp returns 1;
lns3.scpt
set rightIndex to rightIndex - 1
lns3.scpt
error "Couldn’t compare object keys: " & eText number eNum from {item rightIndex of resultListObject's _keys_, pivotValue} to eTo
lns3.scpt
if (not leftIndex > rightIndex) then
lns3.scpt
set {item leftIndex of resultListObject's _keys_, item rightIndex of resultListObject's _keys_} to {get item rightIndex of resultListObject's _keys_, get item leftIndex of resultListObject's _keys_}
lns3.scpt
set {item leftIndex of resultListObject's _list_, item rightIndex of resultListObject's _list_} to {get item rightIndex of resultListObject's _list_, get item leftIndex of resultListObject's _list_}
lns3.scpt
set {leftIndex, rightIndex} to {leftIndex + 1, rightIndex - 1}
lns3.scpt
_sort(resultListObject, startIndex, rightIndex, sortComparator, rightIndex - startIndex > _quicksortThreshold)
lns3.scpt
_sort(resultListObject, leftIndex, endIndex, sortComparator, endIndex - leftIndex > _quicksortThreshold)
lns3.scpt
on error number -2706 -- stack overflow, so fall-through to use non-recursive insertion sort (this should rarely happen in practice)
lns3.scpt
repeat with i from startIndex to endIndex
lns3.scpt
repeat with j from i to startIndex by -1
lns3.scpt
set {leftKey, rightKey} to {get item (j - 1) of resultListObject's _keys_, get item j of resultListObject's _keys_}
lns3.scpt
if sortComparator's compareKeys(leftKey, rightKey) ≤ 0 then exit repeat -- stop when leftKey≤rightKey
lns3.scpt
set {item (j - 1) of resultListObject's _keys_, item j of resultListObject's _keys_} to {rightKey, leftKey}
lns3.scpt
set {item (j - 1) of resultListObject's _list_, item j of resultListObject's _list_} to {get item j of resultListObject's _list_, get item (j - 1) of resultListObject's _list_}
lns3.scpt
end _sort
lns3.scpt
to sort list theList using sortComparator : (missing value)
lns3.scpt
property _keys_ : items of theList -- (replacing items in an existing list of the correct length is a little faster than appending items to a new empty list)
lns3.scpt
property _list_ : items of theList
lns3.scpt
if resultListObject's _list_'s length < 2 then return resultListObject's _list_
lns3.scpt
if sortComparator is missing value then
lns3.scpt
set sortComparator to default comparator
lns3.scpt
set sortComparator to _support's asScriptParameter(sortComparator, "using")
lns3.scpt
set disorderedCount to 0 -- while generating keys also check if list is already almost/fully sorted and if it is use insertionsort/return as-is
lns3.scpt
set previousKey to sortComparator's makeKey(get item 1 of resultListObject's _keys_)
lns3.scpt
on error eText number eNum to eTo
lns3.scpt
error "Couldn’t makeKey for item 1: " & eText number eNum from (a reference to item 1 of (get resultListObject's _list_)) to eTo
lns3.scpt
set item 1 of resultListObject's _keys_ to previousKey
lns3.scpt
repeat with i from 2 to resultListObject's _keys_'s length
lns3.scpt
set currentKey to sortComparator's makeKey(item i of resultListObject's _keys_)
lns3.scpt
error "Couldn’t makeKey for item " & i & ": " & eText number eNum from (a reference to item i of (get resultListObject's _list_)) to eTo
lns3.scpt
set item i of resultListObject's _keys_ to currentKey
lns3.scpt
if sortComparator's compareKeys(previousKey, currentKey) > 0 then set disorderedCount to disorderedCount + 1
lns3.scpt
set previousKey to currentKey
lns3.scpt
error eText number eNum from eFrom to eTo
lns3.scpt
if disorderedCount > 0 then -- some sorting required
lns3.scpt
set useQuickSort to (resultListObject's _list_'s length > _quicksortThreshold) and (disorderedCount / (resultListObject's _list_'s length)) > 0.01 -- if list is small or already 99% in-order then insertion sort is faster than quicksort
lns3.scpt
_sort(resultListObject, 1, resultListObject's _list_'s length, sortComparator, useQuickSort)
lns3.scpt
_error("sort list", eText, eNumber, eFrom, eTo)
lns3.scpt
end sort list
lns3.scpt
to default comparator
lns3.scpt
script DefaultComparator
lns3.scpt
property _supportedTypes : {number, text, date}
lns3.scpt
property _type : missing value
lns3.scpt
on makeKey(anObject)
lns3.scpt
if _type is missing value then
lns3.scpt
repeat with aRef in my _supportedTypes
lns3.scpt
if (count {anObject} each (get aRef's contents)) > 0 then
lns3.scpt
set _type to aRef's contents
lns3.scpt
return anObject
lns3.scpt