has's list wrappers [Re: Passing *possible* variables to a handler]
has's list wrappers [Re: Passing *possible* variables to a handler]
- Subject: has's list wrappers [Re: Passing *possible* variables to a handler]
- From: Nigel Garvey <email@hidden>
- Date: Mon, 15 Jul 2002 03:02:34 +0100
has wrote on Sat, 13 Jul 2002 12:00:14 +0100:
>
Script objects are happy to accept pretty much any object as their parent.
>
Your timing is pretty good, Arthur: I was playing about with this recently,
>
looking for ways to provide a wrapper for lists that'd allow O(1) access
>
times. Essentially the same trick as Serge and others were discussing on
>
the macscrpt list a couple months back, except in OO form.
>
>
======================================================================
>
>
on wrapList(theList)
>
if theList's class is not list then error "Not a list."
>
script
>
property parent : theList
>
>
on getItem(itemIndex)
>
return parent's item itemIndex
>
end getItem
>
>
on getItems(index1, index2)
>
return parent's items index1 thru index2
>
end getItems
>
>
on setItem(itemIndex, newValue)
>
set parent's item itemIndex to newValue
>
end setItem
>
>
--[any other methods here]
>
end script
>
end newList
>
>
>
--TEST
>
set wrappedList to wrapList({})
>
>
--populate the list with N items
>
repeat 1000 times
>
set wrappedList's end to 0
>
end repeat
As Arthur has already enthused, that's cool! :-) Non item-specific list
references - such as 'end of', 'contains', etc. - are actually a little
slower with references and Serge Objects than they are with direct list
variables. However, the difference isn't much and the above three lines
weren't the main thrust of your idea.
[Speed tests snipped]
>
Unfortunately, AS doesn't allow users to overload operators so there's no
>
way to do this properly by overriding 'text n thru m', '&', etc, which
>
would make the whole thing transparent and a simple shoe-in for existing
>
code. The new object doesn't respond to the concatenation operator at all,
>
which is a pain: you'd need to provide your own 'concat()' method and
>
direct folk to use that instead. And you only get the performance benefits
>
using the custom methods I've added, obviously, so you'd still need to
>
change your code in the places where the speed is needed.
If I've understood your wishes correctly, you could get your handler to
return a reference to the script property (which doesn't have to be a
parent) rather than the script itself - and junk all the "methods".
on wrapList(theList)
if theList's class is not list then error "Not a list."
script
property p : theList
end script
return a reference to the result's p
end wrapList
This actually works inside a handler. Access to specific items of the
wrappedList is then possible via normal AppleScript syntax and is even
faster than with your "methods". You'd have to use the 'contents'
operator to signify or compare the list itself, but that wouldn't
normally be a great inconvenience.
set wrappedList to wrapList({"a", "b", "c", "d", "e"})
set wrappedList's first item to 9
wrappedList's contents
--> {9, "b", "c", "d", "e"}
{"Hello"} & wrappedList
--> {"Hello", 9, "b", "c", "d", "e"}
wrappedList as string
--> {"Hello", 9, "b", "c", "d", "e"}
I'll double-check all this on my other computer in the morning....
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.