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,
Or, emphasising code reuse (and speed of writing and refactoring):
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 filePath : "~/Desktop/data.txt"
property fp : "~/Desktop/data.txt"




-------------------------- TEST ---------------------------
on run
on run
unlines({("Magnitudes above " & magnitude as string) & ¬
bindLR(readFileLR(fp), ¬
" in " & filePath & ":", ""} & ¬
report(fp, magnitude))
filter(aboveThreshold(magnitude), paragraphs of readFile(filePath)))
end run
end run


-------------------- EARTHQUAKE REPORT --------------------

-- report :: FilePath -> Float -> String -> String
on report(fp, threshold)
script
on |λ|(s)
unlines({("Magnitudes above " & magnitude as string) & ¬
" in " & fp & ":", ""} & ¬
concatMap(aboveThreshold(threshold), paragraphs of s))
end |λ|
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 if
end |λ|
end |λ|
end script
end script
Line 160: Line 182:
-------------------- REUSABLE GENERICS --------------------
-------------------- REUSABLE GENERICS --------------------


-- filter :: (a -> Bool) -> [a] -> [a]
-- Left :: a -> Either a b
on filter(p, xs)
on |Left|(x)
{type:"Either", |Left|:x, |Right|:missing value}
tell mReturn(p)
end |Left|
set lst to {}

set lng to length of xs

-- 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 {}
tell mReturn(f)
repeat with i from 1 to lng
repeat with i from 1 to lng
set v to item i of xs
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
return lst
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



-- readFile :: FilePath -> IO String
-- readFileLR :: FilePath -> Either String IO String
on readFile(strPath)
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 missing value is e then
if s is missing value then
s as string
|Left|((localizedDescription of e) as string)
else
else
(localizedDescription of e) as string
|Right|(s as string)
end if
end if
end readFile
end readFileLR



-- unlines :: [String] -> String
-- unlines :: [String] -> String