General FizzBuzz: Difference between revisions

→‎{{header|AppleScript}}: Tidied and partially restructured.
(→‎{{header|AppleScript}}: Tidied and partially restructured.)
Line 203:
=={{header|AppleScript}}==
 
<lang AppleScript>--------------------- GENERAL FIZZBUZZ -------------------
{{Trans|JavaScript}}
<lang AppleScript>-- GENERAL FIZZBUZZ ----------------------------------------------------------
 
-- fizzfizzEtc :: [[(Int, String])] -> Int -> String[Symbol]
on fizzEtc(rules)
on fizz(lstRules, intMax)
-- A non-finite sequence of fizzEtc symbols,
-- fizzLineas ::defined Stringby ->the Intgiven ->list Stringof rules.
script fizzlinego
on |λ|(strSeries, n)
script ruleMatch
-- Multiple rule matches ->on |λ|(a, single or concatenated wordsmk)
-- wordIfRuleMatch :: String -> (Int set {m, String)k} ->to Stringmk
script wordIfRuleMatch
on |λ|(str, rulePair)
set {factor, noiseWord} to rulePair
cond(n mod factor >if 0, str,= str(n &mod noiseWordm) then
if integer is class of a then
k
else
a & k
end if
else
a
end if
end |λ|
end script
set strPhrase to foldl(wordIfRuleMatchruleMatch, ""n, lstRulesrules)
strSeries & cond(strPhrase ≠ "", strPhrase, n as string) & linefeed
end |λ|
end script
foldlfmapGen(fizzlinego, "", enumFromToenumFrom(1, intMax))
end fizzfizzEtc
 
 
-- TEST ----------------------------------------------------------------------
--------------------------- TEST -------------------------
on run
unlines(take(20, ¬
fizz([[3, "Fizz"], [5, "Buzz"], [7, "Baxx"]], 20)
fizzEtc({{3, "Fizz"}, {5, "Buzz"}, {7, "Baxx"}})))
end run
 
-- GENERIC FUNCTIONS ---------------------------------------------------------
 
------------------------- GENERIC ------------------------
-- cond :: Bool -> a -> a -> a
 
on cond(bool, x, y)
-- enumFrom :: Enum a => a -> [a]
if bool then
on enumFrom(x)
x
elsescript
yproperty v : missing value
end if on |λ|()
if missing value is not v then
end cond
set v to 1 + v
else
set v to x
end if
return v
end |λ|
end script
end enumFrom
 
 
-- fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
on fmapGen(f, gen)
script
property g : mReturn(f)
on |λ|()
set v to gen's |λ|()
if v is missing value then
v
else
g's |λ|(v)
end if
end |λ|
end script
end fmapGen
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 276 ⟶ 290:
end foldl
 
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: HandlerFirst-class m => (a -> b) -> m (a -> Scriptb)
on mReturn(f)
if-- 2nd class ofhandler ffunction islifted into 1st class script thenwrapper.
if script is class of f then
f
else
Line 286 ⟶ 301:
end script
end if
end mReturn</lang>
 
 
-- take :: Int -> [a] -> [a]
-- take :: Int -> String -> String
on take(n, xs)
set ys to {}
repeat with i from 1 to n
set v to |λ|() of xs
if missing value is v then
return ys
else
set end of ys to v
end if
end repeat
return ys
end take
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines</lang>
{{Out}}
<pre>1
9,659

edits