Multiple distinct objects: Difference between revisions

Content added Content deleted
m (Remove from object-oriented category.)
(→‎{{header|AppleScript}}: Slight reorganisation, updated primitives)
Line 110: Line 110:
=={{header|AppleScript}}==
=={{header|AppleScript}}==


<lang AppleScript>
<lang AppleScript>-- nObjects Constructor -> Int -> [Object]
-- nObjects Int -> Constructor -> [Object]
on nObjects(f, n)
map(f, range(1, n))
on nObjects(n, foo)
map(foo, range(1, n))
end nObjects
end nObjects




-- TEST
-- TEST

on run
on run
nObjects(6, someConstructor)
-- someConstructor :: a -> Int -> b
script someConstructor
on lambda(_, i)
{index:i}
end lambda
end script
nObjects(someConstructor, 6)
--> {{index:1}, {index:2}, {index:3}, {index:4}, {index:5}, {index:6}}
end run
end run


-- someConstructor :: a -> Int -> b
on someConstructor(_, i)
{index:i}
end someConstructor




-- GENERIC FUNCTIONS -----------------------------------------------------------



-- GENERIC LIBRARY FUNCTIONS


-- map :: (a -> b) -> [a] -> [b]
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
on map(f, xs)
script mf
tell mReturn(f)
property lambda : f
set lng to length of xs
end script
set lst to {}
repeat with i from 1 to lng
set lng to length of xs
set end of lst to lambda(item i of xs, i, xs)
set lst to {}
end repeat
repeat with i from 1 to lng
return lst
end tell
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map
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
property lambda : f
end script
end if
end mReturn


-- range :: Int -> Int -> [Int]
-- range :: Int -> Int -> [Int]
Line 160: Line 171:
end repeat
end repeat
return lst
return lst
end range
end range</lang>

</lang>


{{Out}}
{{Out}}