Hash join: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives
(→‎{{header|AppleScript}}: Updated primitives)
Line 122:
=={{header|AppleScript}}==
{{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.
Line 128 ⟶ 127:
 
<lang AppleScript>use framework "Foundation" -- Yosemite onwards, for record-handling functions
 
-- HASH JOIN -----------------------------------------------------------------
 
-- hashJoin :: [Record] -> [Record] -> String -> [Record]
Line 134 ⟶ 135:
script instanceOfjB
on lambda|λ|(a, x)
set strID to keyValue(x, jB)
Line 143 ⟶ 144:
updatedRecord(a, strID, [x])
end if
end lambda|λ|
end script
Line 149 ⟶ 150:
script joins
on lambda|λ|(a, x)
set matches to keyValue(M, keyValue(x, jA))
if matches is not missing value then
script concat
on lambda|λ|(row)
x & row
end lambda|λ|
end script
Line 162 ⟶ 163:
a
end if
end lambda|λ|
end script
Line 168 ⟶ 169:
end hashJoin
 
-- TEST ----------------------------------------------------------------------
-- TEST
on run
set lstA to [¬
Line 189 ⟶ 190:
 
 
-- RECORD FUNCTIONS ----------------------------------------------------------
-- RECORD PRIMITIVES
 
-- keyValue :: String -> Record -> Maybe a
on keyValue(rec, strKey)
set ca to current application
set v to (ca's NSDictionary's dictionaryWithDictionary:rec)'s objectForKey:strKey¬
objectForKey:strKey
if v is not missing value then
item 1 of ((ca's NSArray's arrayWithObject:v) as list)
Line 211 ⟶ 213:
 
 
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- GENERIC PRIMITIVES
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 219 ⟶ 221:
set lng to length of xs
repeat with i from 1 to lng
set v to lambda|λ|(v, item i of xs, i, xs)
end repeat
return v
Line 231 ⟶ 233:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- splitOn :: Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set lstParts to text items of strMain
set my text item delimiters to dlm
return lstParts
end splitOn
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 252 ⟶ 246:
else
script
property lambda|λ| : f
end script
end if
end mReturn
</lang>
 
-- splitOn :: Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set lstParts to text items of strMain
set my text item delimiters to dlm
return lstParts
end splitOn</lang>
{{Out}}
<pre>{{age:27, |name|:"Jonah", |character|:"Jonah", nemesis:"Whales"},
Line 266:
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Ghosts"},
{age:28, |name|:"Alan", |character|:"Alan", nemesis:"Zombies"}}</pre>
 
=={{header|AWK}}==
<lang AWK>
9,655

edits