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 -------------------
--
on fizzEtc(rules)
-- A non-finite sequence of fizzEtc symbols,
--
script
on |λ|(
script ruleMatch
if integer is class of a then
k
else
a & k
end if
else
a
end if
end |λ|
end script
end |λ|
end script
end
--------------------------- TEST -------------------------
on run
unlines(take(20, ¬
fizzEtc({{3, "Fizz"}, {5, "Buzz"}, {7, "Baxx"}})))
end run
------------------------- GENERIC ------------------------
-- enumFrom :: Enum a => a -> [a]
on enumFrom(x)
if missing value is not v then
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
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 276 ⟶ 290:
end foldl
-- mReturn ::
on mReturn(f)
if script is class of f then
f
else
Line 286 ⟶ 301:
end script
end if
end mReturn
-- 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
|