RFC: Porting map() and grep() to AppleScript
in Meditations
1 direct reply — Read more / Contribute
|
by mjg
on Nov 22, 2009 at 18:51
|
No real Perl code here, but I thought it would be neat to try to port the map() and grep() functions to AppleScript and see if that much-maligned language could use a little higher-order programming love.
I've also posted a thread on MacScripter, but here's the code in question. Thoughts?
on map over theList given script:theScript
set resultList to {}
repeat with theItem in theList
set resultList to resultList & theScript's lambda(theItem)
end repeat
return resultList
end map
on grep over theList given script:theScript
set resultList to {}
repeat with theItem in theList
tell theScript
if lambda(theItem) is true then
set resultList to resultList & theItem
end if
end tell
end repeat
return resultList
end grep
-- end library
script mapScript
property HowMany : 0
on lambda(someone)
set HowMany to HowMany + 1
return someone & " Gardner " & HowMany
end lambda
end script
map over {"Mark", "Erin", "David"} given script:mapScript
script grepScript
on lambda(someone)
considering case
if contents of someone is equal to "David" then
return true
end if
return false
end considering
end lambda
end script
grep over {"Mark", "Erin", "David"} given script:grepScript
|
My bologna.pl
in Perl Poetry
2 direct replies — Read more / Contribute
|
by mjg
on Jun 06, 2007 at 15:42
|
#!/usr/bin/perl
my %bologna = (
firstname => 'OSCAR',
secondname => 'MAYER',
);
chomp foreach @days;
read STDIN, $me, 3;
print "Because ", values %bologna, " has a way with BOLOGNA\n"
if $me eq 'why';
|
Output results from a DBI select query in one line
in Cool Uses for Perl
1 direct reply — Read more / Contribute
|
by mjg
on Apr 11, 2006 at 11:47
|
I'm finding myself using this idiom a lot for a current project. Adjust to taste for your own applications.
# given a DBI handle $dbh, a database table "foo" with a column "bar"
print join "\n", map join("\t", @$_), @{$dbh->selectall_arrayref( q{
select bar from foo
} )};
|
TMTOWTDI, WMI, and map abuse
in Cool Uses for Perl
No replies — Read more | Post response
|
by mjg
on Mar 30, 2006 at 11:31
|
I'm working on a behemoth of a script to inventory Windows Oracle servers, using Win32::OLE and Microsoft's WMI to pick up various interesting facts. This is probably of interest to only a few people (if that's you, I feel your pain), but I ended up writing two snippets that do the same thing, and I thought it would make a good example of traditional procedural code vs. utter and total abuse of the map function.
# %oracle_instance's keys are names of Oracle instances on the server
# $WMI_Services is a Win32::OLE object corresponding to a server's
# "root/cimv2" WMI namespace
# And the "in" function is an import from Win32::OLE
# Here's the traditional code
print "These need to be active Oracle admins with DBA privileges:\n";
# get ORA_DBA and ORA_<instance>_DBA groups
my $group_query = "select * from Win32_Group where Name = 'ORA_DBA'";
foreach my $instance ( keys %oracle_instance ) {
$group_query .= " or Name = 'ORA_${instance}_DBA'";
}
my $ora_groups = $WMI_Services->ExecQuery($group_query);
# get users in the above groups
foreach my $group ( in $ora_groups ) {
my $domain_ora_dba = $WMI_Services->ExecQuery(<<WQL);
select PartComponent
from Win32_GroupUser
where GroupComponent =
"Win32_Group.Domain='$group->{Domain}',Name='$group->{Name}'"
WQL
# print the users' names
foreach my $dba_groupuser ( in $domain_ora_dba ) {
print "$1\n"
if $dba_groupuser->{PartComponent} =~ /,Name="(\w+)"$/;
}
}
print "\n";
# And now in the spirit of TMTOWTDI, let's hear it for map!
print "These need to be active Oracle admins with DBA privileges:\n",
join( "\n",
# list the names of users
map $_->{PartComponent} =~ /,Name="(\w+)"$/,
# that are members of
in $WMI_Services->ExecQuery(
'select PartComponent from Win32_GroupUser where ' .
join ' or ', map "GroupComponent =
\"Win32_Group.Domain='$_->{Domain}',Name='$_->{Name}'\"",
# the Windows groups named
in $WMI_Services->ExecQuery(
# ORA_DBA
"select * from Win32_Group where Name = 'ORA_DBA' or " .
join ' or ',
# or ORA_<instance>_DBA
map "Name = 'ORA_${_}_DBA'", keys %oracle_instance
)
)
),
"\n\n";
|
Physical memory on remote machine
in Code Catacombs
No replies — Read more | Post response
|
by mjg
on Mar 27, 2006 at 18:19
|
A short example of WMI and Win32::OLE to get info from another server. Note that the hostname needs to be laundered or Win32::OLE will cry.
|