{"action":"created","issue":{"url":"https://api.github.com/repos/ko1/rubyhackchallenge/issues/13","repository_url":"https://api.github.com/repos/ko1/rubyhackchallenge","labels_url":"https://api.github.com/repos/ko1/rubyhackchallenge/issues/13/labels{/name}","comments_url":"https://api.github.com/repos/ko1/rubyhackchallenge/issues/13/comments","events_url":"https://api.github.com/repos/ko1/rubyhackchallenge/issues/13/events","html_url":"https://github.com/ko1/rubyhackchallenge/issues/13","id":254181748,"number":13,"title":"Procに関数合成を実装する","user":{"login":"yuroyoro","id":18634,"avatar_url":"https://avatars0.githubusercontent.com/u/18634?v=4","gravatar_id":"","url":"https://api.github.com/users/yuroyoro","html_url":"https://github.com/yuroyoro","followers_url":"https://api.github.com/users/yuroyoro/followers","following_url":"https://api.github.com/users/yuroyoro/following{/other_user}","gists_url":"https://api.github.com/users/yuroyoro/gists{/gist_id}","starred_url":"https://api.github.com/users/yuroyoro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yuroyoro/subscriptions","organizations_url":"https://api.github.com/users/yuroyoro/orgs","repos_url":"https://api.github.com/users/yuroyoro/repos","events_url":"https://api.github.com/users/yuroyoro/events{/privacy}","received_events_url":"https://api.github.com/users/yuroyoro/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-08-31T01:32:26Z","updated_at":"2017-08-31T01:39:14Z","closed_at":null,"author_association":"NONE","body":"## 概要\r\n\r\n`Proc#compose` でProc同士を合成できるようにする。\r\n`f.compose g` で、 `Proc{|x| f.call(g.call(x)) }` となるProcを生成する\r\n\r\n```\r\nf = Proc.new{|x| x + 1 }\r\ng = Proc.new{|x| x * x }\r\n\r\nf.compose(g).call(2) #=> 5\r\n```\r\n\r\n雑な実装イメージはこんな感じ\r\n\r\n```\r\nclass Proc\r\n  def compose(f)\r\n    Proc{|x| self.call(f.to_proc.call(x)) }\r\n  end\r\nend\r\n```\r\n\r\n## 逆方向の関数合成のサポート\r\n\r\nさらに、 `Proc#reverse_compose` で 逆の関数合成もできるようにする。 `f.reverse_compose(g).call(x) == g.compose(f).call(x)` となる。\r\n\r\n## `Proc#>>` と `Proc#<<` にaliasする\r\n\r\n`alias_method :<<, :compose` と `alias_method :>>, :reverse_compose` にaliasしておくことで利便性が向上する\r\n\r\n\r\n## `Symbol` への拡張\r\n\r\n`Symbol` にも `compose` および `reverse_compose` を実装する。 実装は`self.to_proc.compose` するだけ\r\n\r\n## モチベーション\r\n\r\n以下のようなコードは、関数合成があればすっきりと記述できる\r\n\r\n```\r\narr = [:foo, :bar, :baz]\r\n\r\n# arrをto_sしてupcaseする\r\n\r\n# blockを書く場合\r\narr.map{|v| v.to_s.upcase } \r\n# mapを2回書く場合\r\narr.map(&:to_s).map(&:upcase) \r\n\r\n# composeを使うとこのように書ける\r\narr.map(&:to_s.to_proc.compose(&:upcase))\r\n\r\n# さらに、 >> を使うと呼び出しの流れを可視化できる\r\narr.map(&:to_s.to_proc >> :upcase.to_proc)\r\n\r\n# Symbolに >> が実装されていればさらにすっきりする\r\narr.map(&:to_s >> :upcase)\r\n```"},"comment":{"url":"https://api.github.com/repos/ko1/rubyhackchallenge/issues/comments/326164900","html_url":"https://github.com/ko1/rubyhackchallenge/issues/13#issuecomment-326164900","issue_url":"https://api.github.com/repos/ko1/rubyhackchallenge/issues/13","id":326164900,"user":{"login":"shunsakai","id":8718096,"avatar_url":"https://avatars1.githubusercontent.com/u/8718096?v=4","gravatar_id":"","url":"https://api.github.com/users/shunsakai","html_url":"https://github.com/shunsakai","followers_url":"https://api.github.com/users/shunsakai/followers","following_url":"https://api.github.com/users/shunsakai/following{/other_user}","gists_url":"https://api.github.com/users/shunsakai/gists{/gist_id}","starred_url":"https://api.github.com/users/shunsakai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shunsakai/subscriptions","organizations_url":"https://api.github.com/users/shunsakai/orgs","repos_url":"https://api.github.com/users/shunsakai/repos","events_url":"https://api.github.com/users/shunsakai/events{/privacy}","received_events_url":"https://api.github.com/users/shunsakai/received_events","type":"User","site_admin":false},"created_at":"2017-08-31T01:39:14Z","updated_at":"2017-08-31T01:39:14Z","author_association":"NONE","body":"http://yuroyoro.hatenablog.com/entry/2012/08/10/232443"}}
{"actor":{"display_login":"shunsakai"}}