Skip to content

Instantly share code, notes, and snippets.

@hiratara
Last active September 29, 2016 14:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiratara/22384fab8cbf5ca7670b2dc348b916c7 to your computer and use it in GitHub Desktop.
Save hiratara/22384fab8cbf5ca7670b2dc348b916c7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env stack
-- stack --resolver lts-7.0 --install-ghc runghc --package turtle
{-# OPTIONS_GHC -fwarn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
main = do
name <- hostname
echo name
#!/usr/bin/env stack
-- stack runghc
{-# OPTIONS_GHC -fwarn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
main = do
args <- arguments
let fileTxt = head args
file = fromText fileTxt
dt <- datefile file
let dateTxt = repr dt
echo dateTxt
#!/usr/bin/env stack
-- stack runghc
{-# OPTIONS_GHC -fwarn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
echoModified fname = do
let file = fromText fname
dt <- datefile file
printf (s%"\t"%utc%"\n") fname dt
main = do
args <- arguments
mapM echoModified args
#!/usr/bin/env stack
-- stack --resolver lts-7.0 --install-ghc runghc --package turtle
{-# LANGUAGE OverloadedStrings #-}
import Turtle
nestedIO = do
putStr "Hello, "
return (putStrLn "I/O!")
main = do
printIO <- nestedIO
printIO
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle
{-# OPTIONS_GHC -fwarn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
main = do
args <- arguments
let path = head args
empty & inproc "find" ([path] <> ["-name", "*.hs"])
& inshell "xargs grep '^import '"
& shell "wc -l"
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle
{-# OPTIONS_GHC -fwarn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import qualified Control.Foldl as Fold
main = do
args <- arguments
let path' = head args
path = fromText path'
find (suffix ".hs") path
& grepImport
& (`fold` Fold.length)
& view
grepImport upstream = do
path <- upstream
input path & grep (prefix "import")
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle
{-# OPTIONS_GHC -fwarn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import qualified Control.Foldl as Fold
parser = optional $ optText "dir" 'd' "the root of search directories"
main = do
mPath' <- options "Count imported modules" parser
let path' = case mPath' of Nothing -> "."
Just p -> p
path = fromText path'
find (suffix ".hs") path
& grepImport
& (`fold` Fold.length)
& view
grepImport upstream = do
path <- upstream
input path & grep (prefix "import ")
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle
{-# OPTIONS_GHC -fwarn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import qualified Control.Foldl as Fold
parser = (,) <$> optional (optText "dir" 'd' "the root of search directories")
<*> switch "show" 's' "Show the name of all imported modules"
main = do
(mPath', isShow) <- options "Count imported modules" parser
let path' = case mPath' of Nothing -> "."
Just p -> p
path = fromText path'
sink = if isShow then dump else wc
find (suffix ".hs") path
& grepImport
& sink
grepImport upstream = do
path <- upstream
input path & grep (prefix "import ")
wc src = src & (`fold` Fold.length) & view
dump src = src
& sed (prefix (importDef *> moduleName))
& stdout
where
importDef = "import" <> spaces1 <> ("qualified" <> spaces1 <|> "")
moduleName = plus (notChar ' ')
[Haskell Day のチュートリアル](http://qiita.com/hiratara/items/169b5cb83b0adbfda764) で出題した問題の回答例をここで公開予定です。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment