Jump to content

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 mf totell mReturn(f)
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 iftell
end filter
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
set mf totell mReturn(f)
set v to startValue
set vlng to startValuelength of xs
repeat with i from 1 to lng
set lng to length of xs
set v to mf's lambda(v, item i of xs, i, xs)
repeat with i from 1 to lng
end repeat
set v to mf's lambda(v, item i of xs, i, xs)
end repeat return v
returnend vtell
end foldl
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set mf totell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
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 {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if mf's lambda(v, i, xs) then
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 return f
script f
else
property lambda : f
end script
property lambda : f
end mReturn
end script
</lang>
end if
end mReturn</lang>
 
{{Out}}
<lang AppleScript>{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}</lang>
 
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.