Re: POSIX and lists question
Re: POSIX and lists question
- Subject: Re: POSIX and lists question
- From: has <email@hidden>
- Date: Fri, 12 Aug 2005 22:26:40 +0100
John R. wrote:
>One point I am confused about: Are Mark's "map" and "apply" examples the right way to do this in AS, or (as HTH [sic] seems to imply with his AS complaint) are Mark's routines going to yield unexpected results sometimes?
The concept is fine, being lifted directly from functional programming, but the original implementation was flawed for the reasons given. Here's a corrected version:
on map(L, evalObj)
(* Returns a new list containing the results of applying a function, evalObj's eval, to the items of a given list, L.
L : list -- a list of values to map to a new list
evalObj : script -- a script object containing a handler named 'eval' that takes a single parameter and returns a result
Result : list -- a new list of mapped values
*)
set newList to {}
repeat with itemRef in L
set end of newList to evalObj's eval(itemRef's contents)
end repeat
return newList
end map
script POSIXize
on eval(pathString)
return POSIX file pathString
end eval
end script
set thePaths to map(theFiles, POSIXize)
>It seems that Mark's handlers would be very useful models for similar AS handlers: for sorting a list, or for selecting item(s) from a list using custom criteria. Another benefit is to clean up my code by pushing annoying repeat blocks into (a few simple-to-maintain) handlers.
AppleMods' List library <http://applemods.sourceforge.net/mods/Data/List.php> already provides well-optimised mapList, filterList and reduceList commands for functional-style programming, along with a powerSort command for custom sorting lists.
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden