Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
88
Task/Dot-product/AppleScript/dot-product-1.applescript
Normal file
88
Task/Dot-product/AppleScript/dot-product-1.applescript
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
----------------------- DOT PRODUCT -----------------------
|
||||
|
||||
-- dotProduct :: [Number] -> [Number] -> Number
|
||||
on dotProduct(xs, ys)
|
||||
if length of xs = length of ys then
|
||||
sum(zipWith(my mul, xs, ys))
|
||||
else
|
||||
missing value -- arrays of differing dimension
|
||||
end if
|
||||
end dotProduct
|
||||
|
||||
|
||||
-------------------------- TEST ---------------------------
|
||||
on run
|
||||
|
||||
dotProduct([1, 3, -5], [4, -2, -1])
|
||||
|
||||
--> 3
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS --------------------
|
||||
|
||||
-- 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
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- min :: Ord a => a -> a -> a
|
||||
on min(x, y)
|
||||
if y < x then
|
||||
y
|
||||
else
|
||||
x
|
||||
end if
|
||||
end min
|
||||
|
||||
|
||||
-- mul :: Num -> Num -> Num
|
||||
on mul(a, b)
|
||||
a * b
|
||||
end mul
|
||||
|
||||
|
||||
-- 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
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- sum :: [Number] -> Number
|
||||
on sum(xs)
|
||||
script add
|
||||
on |λ|(a, b)
|
||||
a + b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(add, 0, xs)
|
||||
end sum
|
||||
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set lng to min(length of xs, length of ys)
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end zipWith
|
||||
1
Task/Dot-product/AppleScript/dot-product-2.applescript
Normal file
1
Task/Dot-product/AppleScript/dot-product-2.applescript
Normal file
|
|
@ -0,0 +1 @@
|
|||
3
|
||||
Loading…
Add table
Add a link
Reference in a new issue