100 doors: Difference between revisions
m
AppleScript →Functional composition: updated primitives
(added FreeBASIC) |
m (AppleScript →Functional composition: updated primitives) |
||
Line 833:
-- GENERIC FUNCTIONS
-- filter :: (a -> Bool) -> [a] -> [a]▼
on filter(f, xs)▼
set lst to {}▼
set lng to length of xs▼
repeat with i from 1 to lng▼
set v to item i of xs▼
if lambda(v, i, xs) then set end of lst to v▼
end repeat▼
return lst▼
end filter▼
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
set v to startValue
set
repeat with i from 1 to lng▼
▲ set lng to length of xs
▲ repeat with i from 1 to lng
end repeat
▲ set v to mf's lambda(v, item i of xs, i, xs)
end foldl
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
end tell
end map
Line 893 ⟶ 907:
return out & dbl
end replicate
▲-- filter :: (a -> Bool) -> [a] -> [a]
▲on filter(f, xs)
▲ set mf to mReturn(f)
▲ set lst to {}
▲ repeat with i from 1 to lng
▲ set v to item i of xs
▲ set end of lst to v
▲ end if
▲ end repeat
▲ return lst
▲end filter
-- range :: Int -> Int -> [Int]
Line 923 ⟶ 922:
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
▲ else
property lambda : f▼
▲ property lambda : f
end mReturn▼
end script
end if
▲end mReturn</lang>
{{Out}}
<lang AppleScript>{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}</lang>
|