RosettaCodeData/Task/Happy-numbers/AppleScript/happy-numbers-2.applescript

124 lines
2.9 KiB
AppleScript
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
-- HAPPY NUMBERS --------------------------------------------------------------
2016-12-05 22:15:40 +01:00
-- isHappy :: Int -> Bool
on isHappy(n)
-- endsInOne :: [Int] -> Int -> Bool
script endsInOne
-- sumOfSquaredDigits :: Int -> Int
script sumOfSquaredDigits
-- digitSquared :: Int -> Int -> Int
script digitSquared
2017-09-23 10:01:46 +02:00
on |λ|(a, x)
2016-12-05 22:15:40 +01:00
(a + (x as integer) ^ 2) as integer
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
2017-09-23 10:01:46 +02:00
on |λ|(n)
2016-12-05 22:15:40 +01:00
foldl(digitSquared, 0, splitOn("", n as string))
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
-- [Int] -> Int -> Bool
2017-09-23 10:01:46 +02:00
on |λ|(s, n)
2016-12-05 22:15:40 +01:00
if n = 1 then
true
else
if s contains n then
false
else
2017-09-23 10:01:46 +02:00
|λ|(s & n, |λ|(n) of sumOfSquaredDigits)
2016-12-05 22:15:40 +01:00
end if
end if
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
2017-09-23 10:01:46 +02:00
endsInOne's |λ|({}, n)
2016-12-05 22:15:40 +01:00
end isHappy
2017-09-23 10:01:46 +02:00
-- TEST -----------------------------------------------------------------------
2016-12-05 22:15:40 +01:00
on run
-- seriesLength :: {n:Int, xs:[Int]} -> Bool
script seriesLength
property target : 8
2017-09-23 10:01:46 +02:00
on |λ|(rec)
2016-12-05 22:15:40 +01:00
length of xs of rec = target of seriesLength
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
-- succTest :: {n:Int, xs:[Int]} -> {n:Int, xs:[Int]}
script succTest
2017-09-23 10:01:46 +02:00
on |λ|(rec)
tell rec to set {xs, n} to {its xs, its n}
2016-12-05 22:15:40 +01:00
script testResult
2017-09-23 10:01:46 +02:00
on |λ|(x)
2016-12-05 22:15:40 +01:00
if isHappy(x) then
xs & x
else
xs
end if
2017-09-23 10:01:46 +02:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
2017-09-23 10:01:46 +02:00
{n:n + 1, xs:testResult's |λ|(n)}
end |λ|
2016-12-05 22:15:40 +01:00
end script
xs of |until|(seriesLength, succTest, {n:1, xs:{}})
--> {1, 7, 10, 13, 19, 23, 28, 31}
end run
2017-09-23 10:01:46 +02:00
-- GENERIC FUNCTIONS ----------------------------------------------------------
2016-12-05 22:15:40 +01:00
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
2017-09-23 10:01:46 +02:00
set v to |λ|(v, item i of xs, i, xs)
2016-12-05 22:15:40 +01:00
end repeat
return v
end tell
end foldl
-- 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
2017-09-23 10:01:46 +02:00
property |λ| : f
2016-12-05 22:15:40 +01:00
end script
end if
end mReturn
2017-09-23 10:01:46 +02:00
-- splitOn :: Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set xs to text items of strMain
set my text item delimiters to dlm
return xs
end splitOn
-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
set mp to mReturn(p)
set v to x
tell mReturn(f)
repeat until mp's |λ|(v)
set v to |λ|(v)
end repeat
end tell
return v
end |until|