Kernighans large earthquake problem: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
Hout (talk | contribs)
→‎{{header|Python}}: An in-memory variant
Line 341: Line 341:
if float(ln.strip().split()[2]) > 6:
if float(ln.strip().split()[2]) > 6:
print(ln.strip())'</lang>
print(ln.strip())'</lang>


Or, if scale permits a file slurp and a parse retained for further processing, we can combine the parse and filter with a concatMap abstraction:

<lang python>from os.path import expanduser
from functools import (reduce)
import re


# main :: IO ()
def main():
xs = concatMap(
lambda x: (
lambda cs=cols(x): (
lambda n=float(cs[2]):
[cs] if 6 < n else []
)()
)()
)(
lines(readFile('~/data.txt'))
)

print (xs)


# GENERIC ABSTRACTIONS ----------------------------

# cols :: String -> String
def cols(s):
return re.split('\s+', s)


# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
return lambda xs: (
reduce(lambda a, b: a + b, map(f, xs), [])
)


# lines :: String -> [String]
def lines(s):
return s.splitlines()


# readFile :: FilePath -> IO String
def readFile(fp):
return open(expanduser(fp)).read()


# MAIN ---
main()</lang>
{{Out}}
<pre>
[['8/27/1883', 'Krakatoa', '8.8'], ['5/18/1980', 'MountStHelens', '7.6']]</pre>


=={{header|REXX}}==
=={{header|REXX}}==