Higher-order functions: Difference between revisions

m
→‎{{header|AppleScript}}: updated primitives, (and normalisation of indents in an earlier contribution, though it doesn't seem to run ...)
(→‎{{header|AutoHotkey}}: make it more straightforward)
m (→‎{{header|AppleScript}}: updated primitives, (and normalisation of indents in an earlier contribution, though it doesn't seem to run ...))
Line 186:
-- with another handler (call).
on sing about topic by singer
call of singer for "Of " & topic & " I sing"
end sing
 
-- Define a handler in a script object,
-- then pass the script object.
script cellos
on call for what
say what using "Cellos"
end call
end script
sing about "functional programming" by cellos
 
-- Pass a different handler. This one is a closure
-- that uses a variable (voice) from its context.
on hire for voice
script
on call for what
say what using voice
end call
end script
end hire
sing about "closures" by (hire for "Pipe Organ")</lang>
Line 215:
<lang applescript>on run
-- PASSING FUNCTIONS AS ARGUMENTS TO
-- MAP, FOLD/REDUCE, AND FILTER, ACROSS A LIST
set lstRange to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Line 222:
--> {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
reducefoldl(summed, 0, map(squared, lstRange))
--> 385
Line 250:
-- 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 repeattell
end map
 
Line 262 ⟶ 263:
-- each list element (from left-to-right) to reduce it
-- to a single return value
 
-- In some languages, like JavaScript, this is called reduce()
 
-- Arguments: function, initial value of accumulator, list
-- reducefoldl :: (a -> b -> a) -> a -> [b] -> a
on reducefoldl(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 reducefoldl
 
 
-- Sublist of those elements for which the predicate
-- function returns true
-- filter :: (a -> Bool) -> [a] -> [a]
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
set mf totell mReturn(f)
set lst to {}
set lstlng to {}length of xs
repeat with i from 1 to lng
set lng to length of xs
repeat with i from 1 set v to lngitem i of xs
set v to item if lambda(v, i, xs) then set end of xslst to v
ifend mf's lambda(v, i, xs) thenrepeat
set end ofreturn lst to v
end iftell
end repeat
return lst
end filter
 
Line 295 ⟶ 299:
-- 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 script
end if
end mReturn
 
Line 321 ⟶ 328:
on isEven(x)
x mod 2 = 0
end isEven</lang>
 
{{Out}}
</lang>
<lang applescript>{{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, {true, false, true, false, true, false, true, false, true, false, true}}</lang>
 
=={{header|ATS}}==
9,655

edits