Topic variable: Difference between revisions

→‎{{header|AppleScript}}: Added illustration of 'result' bound to value of expression most recently evaluated in the current scope
(Added Kotlin)
(→‎{{header|AppleScript}}: Added illustration of 'result' bound to value of expression most recently evaluated in the current scope)
Line 5:
 
For instance you can (but you don't have to) show how the topic variable can be used by assigning the number <math>3</math> to it and then computing its square and square root.
 
=={{header|AppleScript}}==
 
AppleScript binds the name '''result''' to the value of the expression most recently evaluated in the current scope.
 
<lang applescript>on run
1 + 2
ap({square, squareRoot}, {result})
--> {9, 1.732050807569}
end run
 
 
-- square :: Num a => a -> a
on square(x)
x * x
end square
 
-- squareRoot :: Num a, Float b => a -> b
on squareRoot(x)
x ^ 0.5
end squareRoot
 
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
 
-- A list of functions applied to a list of arguments
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
on ap(fs, xs)
set intFs to length of fs
set intXs to length of xs
set lst to {}
repeat with i from 1 to intFs
tell mReturn(item i of fs)
repeat with j from 1 to intXs
set end of lst to |λ|(contents of (item j of xs))
end repeat
end tell
end repeat
return lst
end ap
 
-- 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 |λ| : f
end script
end if
end mReturn</lang>
{{Out}}
<pre>{9, 1.732050807569}</pre>
 
 
=={{header|Axe}}==
9,655

edits