XList Reference

Synopsis

What ?

Iterator, Queue, Stack として使えるリストのラッパーオブジェクトを提供する AppleScript のモジュールです。

AppleScript のリストの欠けている機能の多くを補完します。

また、AppleScript のリストはリファレンスを経由して要素を参照しないと速度が大きく低下するという性質があります。XList は内部で、常にリファレンスを経由してリストにアクセスする為、常に良好な動作速度が得られます。

Sample

use XList : script "XList"

on run
(*== Iterator ==*)
set an_iterator to XList's make_with({"a", "b", "c"})

repeat while an_iterator's has_next()
set an_item to next() of an_iterator
log an_item
end repeat

(*== Queue ==*)
set a_queue to make XList
a_queue's unshift("a")
a_queue's unshift("b")
log a_queue's shift() -- result : "b"
log a_queue's shift() -- result : "a"

(*== Stack ==*)
set a_stack to make XList
a_queue's push("a")
a_queue's push("b")
log a_queue's pop() -- result : "b"
log a_queue's pop() -- result : "a"

(*== Accessing list elements ==*)
set a_list to XList's make_with({"a", "b", "c"})
log a_list's item_counts() -- 3
log a_list's item_at(2) -- "b"
log a_list's has_item("b") --true
log a_list's has_item("d") --false
log a_list's index_of("b") -- 2
log a_list's index_of("d") -- 0
log a_list's delete_at(2) -- "b"
log (set_item of a_list for "e" at 2) -- "e"
log a_list's list_ref() -- {"a", "e"}

(*== Conversion to Text ==*)
log a_list's as_text_with(", ") -- "a, e"

(*== Looping with block script ==*)
set before_c to missing value
script block1
on do(x)
if (x is "c") then
return false
else
set before_c to x
return true
end if
end do
end script
an_iterator's each(block1)
log before_c -- result : b

script block2
on do(x, sender)
if x is "b" then
tell sender
set_item_at("d", current_index()) -- change an item of a list
end tell
end if
return true
end do
end script
an_iterator's enumerate(block2)
log an_iterator's all_items() -- result : {"a", "d", "c"}

(*== Generating new list using “map” ==*)
script block3
on do(x)
return x & "a"
end do
end script
log an_iterator's map(block3)
(*[XListInstance]
1 aa
2 da
3 ca*)
end run

XList Reference