RosettaCodeData/Task/Loop-over-multiple-arrays-simultaneously/AppleScript/loop-over-multiple-arrays-simultaneously-1.applescript

92 lines
2 KiB
AppleScript
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
-- ZIP LISTS WITH FUNCTION ---------------------------------------------------
2016-12-05 22:15:40 +01:00
-- zipListsWith :: ([a] -> b) -> [[a]] -> [[b]]
on zipListsWith(f, xss)
2017-09-23 10:01:46 +02:00
set n to length of xss
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
script
on |λ|(_, i)
script
on |λ|(xs)
2016-12-05 22:15:40 +01:00
item i of xs
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
2017-09-23 10:01:46 +02:00
if i n then
apply(f, (map(result, xss)))
2016-12-05 22:15:40 +01:00
else
{}
end if
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
2017-09-23 10:01:46 +02:00
if n > 0 then
map(result, item 1 of xss)
2016-12-05 22:15:40 +01:00
else
[]
end if
end zipListsWith
2017-09-23 10:01:46 +02:00
-- TEST ( zip lists with concat ) -------------------------------------------
2016-12-05 22:15:40 +01:00
on run
intercalate(linefeed, ¬
2017-09-23 10:01:46 +02:00
zipListsWith(concat, ¬
2016-12-05 22:15:40 +01:00
[["a", "b", "c"], ["A", "B", "C"], [1, 2, 3]]))
end run
2017-09-23 10:01:46 +02:00
-- GENERIC FUNCTIONS ---------------------------------------------------------
2016-12-05 22:15:40 +01:00
-- apply (a -> b) -> a -> b
on apply(f, a)
2017-09-23 10:01:46 +02:00
mReturn(f)'s |λ|(a)
2016-12-05 22:15:40 +01:00
end apply
2017-09-23 10:01:46 +02:00
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
if length of xs > 0 and class of (item 1 of xs) is string then
set acc to ""
else
set acc to {}
end if
repeat with i from 1 to length of xs
set acc to acc & item i of xs
end repeat
acc
end concat
2016-12-05 22:15:40 +01:00
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
2017-09-23 10:01:46 +02:00
set end of lst to |λ|(item i of xs, i, xs)
2016-12-05 22:15:40 +01:00
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
2017-09-23 10:01:46 +02:00
property |λ| : f
2016-12-05 22:15:40 +01:00
end script
end if
end mReturn