Hash join: Difference between revisions
Content added Content deleted
(→{{header|AppleScript}}: Updated primitives) |
|||
Line 122: | Line 122: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
Native AppleScript records lack introspection, but from Yosemite onwards we can read and write them a little more flexibly through the Foundation classes. |
Native AppleScript records lack introspection, but from Yosemite onwards we can read and write them a little more flexibly through the Foundation classes. |
||
Line 128: | Line 127: | ||
<lang AppleScript>use framework "Foundation" -- Yosemite onwards, for record-handling functions |
<lang AppleScript>use framework "Foundation" -- Yosemite onwards, for record-handling functions |
||
-- HASH JOIN ----------------------------------------------------------------- |
|||
-- hashJoin :: [Record] -> [Record] -> String -> [Record] |
-- hashJoin :: [Record] -> [Record] -> String -> [Record] |
||
Line 134: | Line 135: | ||
script instanceOfjB |
script instanceOfjB |
||
on |
on |λ|(a, x) |
||
set strID to keyValue(x, jB) |
set strID to keyValue(x, jB) |
||
Line 143: | Line 144: | ||
updatedRecord(a, strID, [x]) |
updatedRecord(a, strID, [x]) |
||
end if |
end if |
||
end |
end |λ| |
||
end script |
end script |
||
Line 149: | Line 150: | ||
script joins |
script joins |
||
on |
on |λ|(a, x) |
||
set matches to keyValue(M, keyValue(x, jA)) |
set matches to keyValue(M, keyValue(x, jA)) |
||
if matches is not missing value then |
if matches is not missing value then |
||
script concat |
script concat |
||
on |
on |λ|(row) |
||
x & row |
x & row |
||
end |
end |λ| |
||
end script |
end script |
||
Line 162: | Line 163: | ||
a |
a |
||
end if |
end if |
||
end |
end |λ| |
||
end script |
end script |
||
Line 168: | Line 169: | ||
end hashJoin |
end hashJoin |
||
-- TEST ---------------------------------------------------------------------- |
|||
-- TEST |
|||
on run |
on run |
||
set lstA to [¬ |
set lstA to [¬ |
||
Line 189: | Line 190: | ||
-- RECORD FUNCTIONS ---------------------------------------------------------- |
|||
-- RECORD PRIMITIVES |
|||
-- keyValue :: String -> Record -> Maybe a |
-- keyValue :: String -> Record -> Maybe a |
||
on keyValue(rec, strKey) |
on keyValue(rec, strKey) |
||
set ca to current application |
set ca to current application |
||
set v to (ca's NSDictionary's dictionaryWithDictionary:rec)'s |
set v to (ca's NSDictionary's dictionaryWithDictionary:rec)'s ¬ |
||
objectForKey:strKey |
|||
if v is not missing value then |
if v is not missing value then |
||
item 1 of ((ca's NSArray's arrayWithObject:v) as list) |
item 1 of ((ca's NSArray's arrayWithObject:v) as list) |
||
Line 211: | Line 213: | ||
-- GENERIC FUNCTIONS --------------------------------------------------------- |
|||
-- GENERIC PRIMITIVES |
|||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
||
Line 219: | Line 221: | ||
set lng to length of xs |
set lng to length of xs |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set v to |
set v to |λ|(v, item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return v |
return v |
||
Line 231: | Line 233: | ||
set lst to {} |
set lst to {} |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set end of lst to |
set end of lst to |λ|(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
end tell |
end tell |
||
end map |
end map |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- Lift 2nd class handler function into 1st class script wrapper |
-- Lift 2nd class handler function into 1st class script wrapper |
||
Line 252: | Line 246: | ||
else |
else |
||
script |
script |
||
property |
property |λ| : f |
||
end script |
end script |
||
end if |
end if |
||
end mReturn |
end mReturn |
||
</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{Out}} |
{{Out}} |
||
<pre>{{age:27, |name|:"Jonah", |character|:"Jonah", nemesis:"Whales"}, |
<pre>{{age:27, |name|:"Jonah", |character|:"Jonah", nemesis:"Whales"}, |
||
Line 266: | Line 266: | ||
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Ghosts"}, |
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Ghosts"}, |
||
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Zombies"}}</pre> |
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Zombies"}}</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |