Skip to content

Instantly share code, notes, and snippets.

@tfausak
Last active April 2, 2024 20:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tfausak/fcd11dfcec616622033dfb64eb2378e1 to your computer and use it in GitHub Desktop.
Save tfausak/fcd11dfcec616622033dfb64eb2378e1 to your computer and use it in GitHub Desktop.
Sums numbers on the command line.
#! /usr/bin/env stack
-- stack --resolver lts-8.15 --install-ghc script --package base
-- Usage:
-- $ echo -e "1.2 3.4\n5.6 not-a-number" | ./total.hs
-- 10.2
import Data.Maybe (mapMaybe)
import Text.Read (readMaybe)
main :: IO ()
main =
print
. (\x -> x :: Double)
. sum
. mapMaybe readMaybe
. words
=<< getContents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment