June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,9 +1,9 @@
-- maximumBy :: (a -> a -> Ordering) -> [a] -> a
on maximumBy(f, xs)
-- maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a
on maximumByMay(f, xs)
set cmp to mReturn(f)
script max
on |λ|(a, b)
if a is missing value or cmp's |λ|(a, b) < 0 then
if cmp's |λ|(a, b) < 0 then
b
else
a
@ -11,8 +11,8 @@ on maximumBy(f, xs)
end |λ|
end script
foldl(max, missing value, xs)
end maximumBy
foldl1May(max, xs)
end maximumByMay
-- TEST -----------------------------------------------------------------------
on run
@ -34,16 +34,33 @@ on run
end |λ|
end script
return {¬
maximumBy(comparing(|length|), lstWords), ¬
maximumBy(comparing(population), lstCities)}
return catMaybes({¬
maximumByMay(comparing(|length|), lstWords), ¬
maximumByMay(comparing(|length|), {}), ¬
maximumByMay(comparing(population), lstCities)})
--> {"epsilon", {name:"Shanghai", population:24.15}}
end run
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- catMaybes :: [Maybe a] -> [a]
on catMaybes(mbs)
script emptyOrListed
on |λ|(m)
if nothing of m then
{}
else
{just of m}
end if
end |λ|
end script
concatMap(emptyOrListed, mbs)
end catMaybes
-- comparing :: (a -> b) -> (a -> a -> Ordering)
on comparing(f)
set mf to mReturn(f)
@ -64,23 +81,62 @@ on comparing(f)
end script
end comparing
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
set acc to {}
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)
repeat with x in xs
set acc to acc & |λ|(contents of x)
end repeat
return v
end tell
end foldl
return acc
end concatMap
-- foldl1May :: (a -> a -> a) -> [a] -> Maybe a
on foldl1May(f, xs)
set lng to length of xs
if lng > 0 then
if lng > 1 then
tell mReturn(f)
set v to item 1 of xs
set lng to length of xs
repeat with i from 2 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return just(v)
end tell
else
just(item 1 of xs)
end if
else
nothing("Empty list")
end if
end foldl1May
-- just :: a -> Just a
on just(x)
{nothing:false, just:x}
end just
-- length :: [a] -> Int
on |length|(xs)
length of xs
end |length|
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
-- nothing :: () -> Nothing
on nothing(msg)
{nothing:true, msg:msg}
end nothing
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)