Skip to content

Instantly share code, notes, and snippets.

@buggymcbugfix
Forked from tfausak/total.hs
Last active October 18, 2017 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buggymcbugfix/0cab91093559c45848c07bfa52063af9 to your computer and use it in GitHub Desktop.
Save buggymcbugfix/0cab91093559c45848c07bfa52063af9 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
{-# LANGUAGE ScopedTypeVariables #-}
-- 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 = print . sum . mapMaybe (readMaybe :: String -> Maybe Double) . words =<< getContents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment