XList Reference

XList provides a wrapper object for AppleScript's list data to enable functions of Iterator, Queue, Stack. Many missing feature of AppleScript's list are covered.

Also XList will contribute pefermance of the script. The AppleScript have a characteristics that fast accessing list items must be thorugh a reference of a list. XList can hide such complications, and give simple codes with best performance.

Example

use XList : script "XList"

(* 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_unicode_with(", ") -- "a, e"

(* Accessing all items with a closure *)
script scriptA
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(scriptA)
log an_iterator's all_items() -- result : {"a", "d", "c"}


script scriptB
on do(x)
return x & "a"
end do
end script

log an_iterator's map_as_list(scriptB) -- result : {"aa", "da", "ca"}

Constructor Method

make

Meke an instance of empty XList.

make_with

Meke an instance of XList with given an AppleScript's list object.

make_with_list

A synonym of make_with

make_with_text

Meke an instance of XList with from a list splitting a text with a delimiter.

Methods for Iterator

next

return an item in the list next to the item obtained by previous next()

has_next

check whether next() can return a next item or not

current_item

The item obtained by previous next() is returned.

current_index

An index number of an item obtained by previous next() is returned.

decrement_index

Decrements the index of the item obtained by next(). i.e. same item can be obtained once more.

increment_index

Increments the index of the item obtained by next(). i.e. skip an item.

reset

Make next() return items form first.

Stack and Quene

push

Append an item at the end of the list.

pop

Obtain last item in the stored list, and remove the item.

unshift

Append an item at the beginning of the list.

shift

Obtain first item in the list and remove it.

Accessing List Items

count_items

Return number of elements

delete_at

Delete an item specified with an index number.

item_at

Obtain an item specified with an index number. When a list of indexes is passed as an argument, multiple items will be obtained.

items_in_range

Obtain items between two indexes.

set_item

set an item at a specified index.

set_item_at

set an item at a specified index.

exchange_items

Exchange items specified with indexes

has_item

Check whether the object "an_item" is included in the XList instance or not.

index_of

Obtain an index number of the object "an_item" in the XList instance.

all_items

return a copy of stored list.

list_ref

return the stored list.

add_from_list

Add each item contained passed list at end of the reciver's XList instance.

Conversion to Text

as_xtext_with

Join every elements with given a delimiters as XText

as_unicode_with

Join every elements with given a delimiters as Unicode text

as_text_with

A synonym of as_unicode_with. Join every elements with given a delimiters as Unicode text.

as_string_with

Join every elements with given a delimiters as string

Loop with Script Object

each

Call do handler of given script object with passing a reference to each item in the XList as an argument.

enumerate

Call do handler of given script object with each item in the XList as an argument.

map

Call do handler of given script object with passing each element as an argument. A XList consisting of the results of do handler is returned.

map_as_list

Call do handler of given script object with passing each element as an argument.An AppleScript's list of the results of do handler is returned.

Make a copy of the instance

shallow_copy

Make a new instance which share internal list with the original.The internal counter for the iterator will be copied.

deep_copy

Make a new instance which have copied internal list from the original.

iterator

Make a shallow copy with resetting the internal iterator counter.