Multiplication tables: Difference between revisions

→‎{{header|AppleScript}}: Subheaders: 1. Iteration 2. Functional composition – (and some updated primitives)
(Added FreeBASIC)
(→‎{{header|AppleScript}}: Subheaders: 1. Iteration 2. Functional composition – (and some updated primitives))
Line 291:
 
=={{header|AppleScript}}==
===Iteration===
<lang AppleScript >set n to 12 -- Size of table.
repeat with x from 0 to n
Line 329 ⟶ 330:
"</pre>
 
===Functional composition===
 
As an alternative to iteration, we could also write the top level more declaratively, drawing on a library of generic functions.
 
{{trans|JavaScript}} (ES5 functional version)
<lang AppleScript>-- MULTIPLICATION TABLE FOR INTEGERS M TO N
 
 
<lang AppleScript>
-- MULTIPLICATION TABLE
 
-- table :: Int -> Int -> [[String]]
Line 403 ⟶ 401:
end concatMap
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
on tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
end scriptrepeat
return strJoinedv
end tell
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
 
-- Lift 2nd class handler function into 1st class script wrapper
-- foldl :: (a -> b -> a) -> a -> [b] -> a
-- mReturn :: Handler -> Script
on foldl(f, startValue, xs)
set mf toon mReturn(f)
if class of f is script then return f
set v to startValue f
else
set lng to length of xs
repeat with i from 1 to lngscript
set v to mf's property lambda(v, item i of xs, i,: xs)f
end repeatscript
returnend vif
end foldlmReturn
 
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
 
-- range :: Int -> Int -> [Int]
on range(m, n)
setif dn to< 1m then
if n < m then set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
Line 446 ⟶ 451:
end range
 
-- intercalate :: Text -> [Text] -> Text
-- Lift 2nd class handler function into 1st class script wrapper
on intercalate(strText, lstText)
-- mReturn :: Handler -> Script
set {dlm, my text item delimiters} to {my text item delimiters, strText}
on mReturn(f)
set strJoined to lstText as text
if class of f is script then return f
set my text item delimiters to dlm
script
return strJoined
property lambda : f
end intercalate</lang>
end script
end mReturn
</lang>
 
{{Out}}
9,659

edits