Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HaskellでGitHub APIを叩いてみた

HaskellでGitHub APIを叩いてみた

Tomohiko Himura

August 02, 2015
Tweet

More Decks by Tomohiko Himura

Other Decks in Programming

Transcript

  1. GitHubのリポジトリ傾向 • Ruby 67 • Objective-C 14 • Shell 14

    • JavaScript 9 • Haskell 6 • Emacs Lisp 5 • C# 2 • HTML 2 • TypeScript 2 • C 1 • C++ 1 • CSS 1 • CoffeeScript 1 • Java 1 • PHP 1 • Python 1 • Scala 1 • Swift 1
  2. GitHubのリポジトリ傾向 • Ruby 67 • Objective-C 14 • Shell 14

    • JavaScript 9 • Haskell 6 • Emacs Lisp 5 • C# 2 • HTML 2 • TypeScript 2 • C 1 • C++ 1 • CSS 1 • CoffeeScript 1 • Java 1 • PHP 1 • Python 1 • Scala 1 • Swift 1 Haskellで求めてみました
  3. hogeコマンドを作りたい場合 executable hoge hs-source-dirs: app main-is: Main.hs ghc-options: -threaded -rtsopts

    -with-rtsopts=-N build-depends: base , hoge default-language: Haskell2010 修正 修正
  4. $ stack ghci > Github.Repos.userRepos "eiel" Github.Repos.All Right [Repo {

    repoSshUrl = Just “[email protected]:cldwalker/debugger- ruby_core_source.git", repoDescription = Just "Retrieve ruby core source files”, repoHtmlUrl = “https://github.com/cldwalker/debugger- ruby_core_source", repoSvnUrl = Just “https://github.com/cldwalker/ debugger-ruby_core_source", … repoLanguage = Just “C" … }. Repo { … ]
  5. 実装方針 • Either Error [Repo] ͕ͱΕΔ • Right [ Repo

    { repoLanguage = Just “C”… }, Repo { repoLanguage = Just “Ruby”…} … ] • 言語の一覧にする • [“C”, “Ruby”,… ] • 言語とその言語の数を数える • [(“Ruby”, 4), (“C”, 2)]
  6. Either Error [Repo] -> [(String, Int)] • ふたつに分解 • Either

    Error [Repo] -> [String] • [String] -> [(String, Int)]
  7. Either Error [Repo] -> [String] either (const []) id >>>

    map repoLanguage >>> filter isJust >>> map fromJust
  8. map fromJust • Just をはずす [Just “C”, Just “Ruby”, Just

    “Ruby”…] が [“C”, “Ruby” , “Ruby”…] になる
  9. 言語の数を数えて、並び換え sort >>> group >>> map keyAndSize >>> rankSort where

    keyAndSize = head &&& length rankSort = sortBy (flip $ comparing snd)
  10. head &&& length • 左に headの結果を 右にlengthの結果を [“C”,”C”] が (head

    [“C”,”C”], length [“C”,”C”]) ʹͳΓ (“C”,2) ʹͳΔ
  11. rankSort rankSort = sortBy (flip $ comparing snd) • 右の結果で並びかえ

    [(“C”, 2),(“Ruby”, 4)] が [(“Ruby”, 4),(“C”, 2)] になる
  12. sort >>> group >>> map keyAndSize >>> rankSort • >>>

    は シェルのパイプみたいなもの • 関数と関数をつなげて大きな関数に • あとは関数に値を [“C”,“Ruby”,”Ruby”,”C”ɼ ”Ruby”,”Ruby”] [[“C”,”C”], [“Ruby”,”Ruby”,”Ruby”,”Ruby”]] [“C,”C”ɼ”Ruby”,”Ruby”, ”Ruby”,”Ruby”] [(“C”,2),(“Ruby”,4)] [(“Ruby”,4),(“C”,2)]