Forward difference: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives. Refactored test.
(→‎JS ES6: Updated primitives, refactored main.)
(→‎{{header|AppleScript}}: Updated primitives. Refactored test.)
Line 234:
{{Trans|JavaScript}}
 
<lang AppleScript>-- FORWARDforwardDifference DIFFERENCE:: Num a => [a] --------------------------------------------------------> [a]
on forwardDifference(xs)
 
zipWith(my subtract, xs, rest of xs)
-- forwardDifference :: Int -> [Int] -> [Int]
on forwardDifference(n, xs)
set lng to length of xs
-- atLength :: [Int] -> Bool
script atLength
property fullLength : lng
property ndx : n
on |λ|(ds)
(atLength's fullLength) - (length of ds) ≥ atLength's ndx
end |λ|
end script
-- fd :: [Int] -> [Int]
script fd
on |λ|(xs)
script minus
on |λ|(a, b)
a - b
end |λ|
end script
zipWith(minus, tail(xs), xs)
end |λ|
end script
|until|(atLength, fd, xs)
end forwardDifference
 
 
-- nthForwardDifference :: Num a => Int -> [a] -> [a]
-- TEST ----------------------------------------------------------------------
on nthForwardDifference(xs, i)
|index|(iterate(forwardDifference, xs), 1 + i)
end nthForwardDifference
 
 
-------------------------- TEST ---------------------------
on run
script show
set xs to {90, 47, 58, 29, 22, 32, 55, 5, 55, 73}
on |λ|(xs, i)
((i - 1) as string) & " -> " & showList(xs)
script test
on |λ|(n)
intercalate(" -> [", ¬
{{n}} & intercalate(", ", forwardDifference(n, xs))) & "]"
end |λ|
end script
intercalateunlines(linefeed, map(test, enumFromTo(1show, 9)))¬
take(10, ¬
iterate(forwardDifference, ¬
{90, 47, 58, 29, 22, 32, 55, 5, 55, 73}))))
end run
 
 
-- GENERIC FUNCTIONS ------------------------------------- GENERIC FUNCTIONS --------------------
 
-- enumFromToJust :: Inta -> IntMaybe -> [Int]a
on enumFromToJust(m, nx)
-- Constructor for an inhabited Maybe (option type) value.
if m > n then
-- Wrapper containing the result of a computation.
set d to -1
{type:"Maybe", Nothing:false, Just:x}
end Just
 
-- Nothing :: Maybe a
on Nothing()
-- Constructor for an empty Maybe (option type) value.
-- Empty wrapper returned where a computation is not possible.
{type:"Maybe", Nothing:true}
end Nothing
 
 
-- index (!!) :: [a] -> Int -> Maybe a
-- index (!!) :: Gen [a] -> Int -> Maybe a
-- index (!!) :: String -> Int -> Maybe Char
on |index|(xs, i)
if script is class of xs then
repeat with j from 1 to i
set v to |λ|() of xs
end repeat
if missing value is not v then
Just(v)
else
Nothing()
end if
else
setif dlength toof 1xs < i then
Nothing()
else
Just(item i of xs)
end if
end if
end |index|
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
 
 
-- intercalate :: Text -> [Text] -> Text
on-- intercalate(strText, lstText):: String -> [String] -> String
on intercalate(delim, xs)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined{dlm, tomy lstTexttext asitem textdelimiters} to ¬
{my text item delimiters, delim}
set str to xs as text
set my text item delimiters to dlm
str
return strJoined
end intercalate
 
 
-- iterate :: (a -> a) -> a -> Gen [a]
on iterate(f, x)
script
property v : missing value
property g : mReturn(f)'s |λ|
on |λ|()
if missing value is v then
set v to x
else
set v to g(v)
end if
return v
end |λ|
end script
end iterate
 
 
-- length :: [a] -> Int
on |length|(xs)
set c to class of xs
if list is c or string is c then
length of xs
else
(2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
end if
end |length|
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
Line 317 ⟶ 352:
end tell
end map
 
 
-- min :: Ord a => a -> a -> a
Line 327 ⟶ 363:
end min
 
 
-- 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 339 ⟶ 376:
end mReturn
 
 
-- tail :: [a] -> [a]
-- showList :: [a] -> String
on tail(xs)
on showList(xs)
if length of xs > 1 then
"[" & intercalate(", ", itemsmap(my 2str, thruxs)) -1& of xs"]"
end showList
 
 
-- str :: a -> String
on str(x)
x as string
end str
 
 
-- subtract :: Num -> Num -> Num
on subtract(x, y)
y - x
end subtract
 
 
-- take :: Int -> [a] -> [a]
-- take :: Int -> String -> String
on take(n, xs)
set c to class of xs
if list is c then
if 0 < n then
items 1 thru min(n, length of xs) of xs
else
{}
end if
else if string is c then
if 0 < n then
text 1 thru min(n, length of xs) of xs
else
""
end if
else if script is c then
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
else
{}missing value
end if
end tailtake
 
 
-- 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
 
-- 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|
 
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to min(|length of |(xs), |length of |(ys))
if 1 > lng then return {}
set xs_ to take(lng, xs) -- Allow for non-finite
set ys_ to take(lng, ys) -- generators like cycle etc
set lst to {}
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xsxs_, item i of ysys_)
end repeat
return lst
Line 373 ⟶ 455:
end zipWith</lang>
{{Out}}
<pre>1 0 -> [-4390, 1147, 58, -29, -722, 1032, 2355, -505, 5055, 1873]
2 1 -> [54-43, 11, -4029, 22-7, 1710, 1323, -7350, 10050, -3218]
3 2 -> [54, [-9440, 6222, -517, -413, -8673, 173100, -13232]
4 3 -> [156-94, 62, -675, 1-4, -8286, 259173, -305132]
5 4 -> [156, -22367, 681, -8382, 341259, -564305]
6 5 -> [291-223, 68, -15183, 424341, -905564]
7 6 -> [291, -442151, 575424, -1329905]
8 7 -> [1017-442, 575, -19041329]
9 8 -> [1017, [-29211904]</pre>
9 -> [-2921]</pre>
 
=={{header|AutoHotkey}}==
9,655

edits