Kernighans large earthquake problem: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Quick refactor to handle a missing file and any blank lines.) |
|||
Line 130: | Line 130: | ||
kernighansEarthquakes(6)</lang> |
kernighansEarthquakes(6)</lang> |
||
===Functional=== |
|||
Checking that the file exists, and discarding any blank lines, |
|||
while emphasising code reuse, and speed of writing and refactoring: |
|||
<lang applescript>use AppleScript version "2.4" |
<lang applescript>use AppleScript version "2.4" |
||
Line 138: | Line 140: | ||
property magnitude : 6.0 |
property magnitude : 6.0 |
||
property |
property fp : "~/Desktop/data.txt" |
||
-------------------------- TEST --------------------------- |
|||
on run |
on run |
||
⚫ | |||
bindLR(readFileLR(fp), ¬ |
|||
⚫ | |||
report(fp, magnitude)) |
|||
⚫ | |||
end run |
end run |
||
-------------------- EARTHQUAKE REPORT -------------------- |
|||
⚫ | |||
on report(fp, threshold) |
|||
script |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end script |
|||
end report |
|||
Line 152: | Line 170: | ||
script |
script |
||
on |λ|(s) |
on |λ|(s) |
||
threshold < (item -1 of words of s) as number |
if "" ≠ s and threshold < (item -1 of words of s) as number then |
||
{s} |
|||
else |
|||
{} |
|||
⚫ | |||
end |λ| |
end |λ| |
||
end script |
end script |
||
Line 160: | Line 182: | ||
-------------------- REUSABLE GENERICS -------------------- |
-------------------- REUSABLE GENERICS -------------------- |
||
-- |
-- Left :: a -> Either a b |
||
on |
on |Left|(x) |
||
{type:"Either", |Left|:x, |Right|:missing value} |
|||
⚫ | |||
end |Left| |
|||
⚫ | |||
⚫ | |||
-- Right :: b -> Either a b |
|||
on |Right|(x) |
|||
{type:"Either", |Left|:missing value, |Right|:x} |
|||
end |Right| |
|||
-- bindLR (>>=) :: Either a -> (a -> Either b) -> Either b |
|||
on bindLR(m, mf) |
|||
if missing value is not |Left| of m then |
|||
m |
|||
else |
|||
mReturn(mf)'s |λ|(|Right| of m) |
|||
end if |
|||
end bindLR |
|||
-- concatMap :: (a -> [b]) -> [a] -> [b] |
|||
on concatMap(f, xs) |
|||
set lng to length of xs |
|||
set acc to {} |
|||
⚫ | |||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set |
set acc to acc & (|λ|(item i of xs, i, xs)) |
||
if |λ|(v, i, xs) then set end of lst to v |
|||
end repeat |
end repeat |
||
⚫ | |||
end tell |
end tell |
||
return acc |
|||
end filter |
|||
end concatMap |
|||
-- mReturn :: First-class m => (a -> b) -> m (a -> b) |
-- mReturn :: First-class m => (a -> b) -> m (a -> b) |
||
Line 185: | Line 229: | ||
end mReturn |
end mReturn |
||
⚫ | |||
-- readFileLR :: FilePath -> Either String IO String |
|||
on |
on readFileLR(strPath) |
||
set ca to current application |
set ca to current application |
||
set e to reference |
set e to reference |
||
Line 194: | Line 239: | ||
stringByStandardizingPath) ¬ |
stringByStandardizingPath) ¬ |
||
encoding:(ca's NSUTF8StringEncoding) |error|:(e)) |
encoding:(ca's NSUTF8StringEncoding) |error|:(e)) |
||
if |
if s is missing value then |
||
|Left|((localizedDescription of e) as string) |
|||
else |
else |
||
( |
|Right|(s as string) |
||
end if |
end if |
||
end |
end readFileLR |
||
-- unlines :: [String] -> String |
-- unlines :: [String] -> String |