functional programming

Given a list of records:

set theListOfRecords to {{name:“foo”}, {name:“bar”}}

I can extract the same property from each record in the list:

set theNames to {}
repeat with theItem in theListOfRecords
set theNames to theNames & the name of theItem
end repeat
theNames

which seems like an awful lot of code for such a simple operation. It would be great if there were a one line equivalent of this, perhaps something like:

get the name of every item of theListOfRecords

but this doesn’t work. I would like to be able to perform the same operation on every item in a list and end up with a list of the results. Is there a concise way to do this sort of functional programming in AppleScript?

ragfield:

Dealing with records isn’t as elegant as we would like. You can’t just say “name of every item of [some record]”. Using conditionals, handlers, exit repeat, etc. while walking the record is the only way to really process it.

Jim Neumann
BLUEFROG

If you have a list of lists, as in the first example, this is feasible:

tell {{pocketComputer:"Kraft"}, {dataDate:"Werk"}} to item 1 & item 2
result's items

If you have one record, rather than a list of lists, the items could be obtained like this:

{foo:"pocketComputer", bar:"dataDate"}'s items

Kittens saved: 2 :smiley:

The downside of that approach being that you lose the association with the record name.

That’s not functional programming, which is a very different beast. But anyway, no; that sort of query only works on application object models, not on AppleScript lists and records. There’s no really concise way of doing this. Various ways your basic iteration approach could be finessed; alternative tools or techniques that may work better for certain situations; sometimes though you just gotta do a bit of legwork. Whatever; write the code, shove it in a subroutine, and ignore the details thereafter.