{"id":20842803,"url":"https://github.com/mudge/comp","last_synced_at":"2025-05-08T22:41:52.461Z","repository":{"id":59152297,"uuid":"39970533","full_name":"mudge/comp","owner":"mudge","description":"A Ruby library to add function composition to Procs and Methods.","archived":false,"fork":false,"pushed_at":"2015-07-30T21:10:00.000Z","size":140,"stargazers_count":24,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-08T18:53:37.795Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://bugs.ruby-lang.org/issues/6284","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mudge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-30T20:18:05.000Z","updated_at":"2019-08-18T16:49:01.000Z","dependencies_parsed_at":"2022-09-13T11:01:09.188Z","dependency_job_id":null,"html_url":"https://github.com/mudge/comp","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fcomp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fcomp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fcomp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fcomp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mudge","download_url":"https://codeload.github.com/mudge/comp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253160727,"owners_count":21863624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T01:25:32.573Z","updated_at":"2025-05-08T22:41:52.444Z","avatar_url":"https://github.com/mudge.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# comp [![Build Status](https://travis-ci.org/mudge/comp.svg?branch=master)](https://travis-ci.org/mudge/comp)\n\nA Ruby library to add [function\ncomposition](https://en.wikipedia.org/wiki/Function_composition) to\n[`Proc`s](http://ruby-doc.org/core/Proc.html) (including `lambda`s) and\n[`Method`s](http://ruby-doc.org/core/Method.html).\n\n**Current version:** 1.0.0  \n**Supported Ruby versions:** 1.9.2, 1.9.3, 2.0, 2.1, 2.2\n\n## Installation\n\n```\ngem install comp -v '~\u003e 1.0'\n```\n\nOr, in your `Gemfile`:\n\n```ruby\ngem 'comp', '~\u003e 1.0'\n```\n\n## Usage\n\n```ruby\nrequire 'comp'\n\n# Basic usage\nadd = proc { |x, y| x + y }\ndouble = proc { |x| x * 2 }\ndouble_after_add = double * add\n\ndouble_after_add.call(1, 2)\n#=\u003e 6\n\n# Composing Procs and other callables\nclass Parser\n  def call(json)\n    JSON.load(json)\n  end\nend\n\nextract_name = proc { |attributes| attributes.fetch('name') }\n(extract_name * Parser.new).call('{\"name\": \"Alice\"}')\n#=\u003e \"Alice\"\n```\n\nRequiring `comp` will require two files:\n\n* `comp/proc`: add [`Proc#*`](#proc) if it hasn't already been defined;\n* `comp/method`: add [`Method#*`](#method) if it hasn't already been defined.\n\n## API Documentation\n\n### `Proc#*`\n\n```ruby\nincrement_and_add = proc { |x| x * 2 } * proc { |x| x + 1 }\nincrement_and_add.call(1)\n#=\u003e 4\n```\n\nComposes a `Proc` (`f`) with any object that responds to `call` (`g`) returning\na new `Proc` which will call `g` with any given arguments (including blocks)\nand then call `f` with the result.\n\nIf `f` was created with `lambda`, the resulting `Proc` will also be a\n`lambda`.\n\n### `Method#*`\n\n```ruby\nclass Number\n  def add(x, y)\n    x + y\n  end\n\n  def double(x)\n    x * 2\n  end\nend\n\n(Number.new.method(:double) * Number.new.method(:add)).call(2, 3)\n#=\u003e 10\n```\n\nComposes a `Method` (`f`) with any object that responds to `call` (`g`)\nreturning a new `Proc` which will call `g` with any given arguments (including\nblocks) and then call `f` with the result.\n\n## Why isn't this in Ruby itself?\n\nGood question: I'd like to [change](https://bugs.ruby-lang.org/issues/6284) [that](https://github.com/ruby/ruby/pull/935).\n\n## Acknowledgements\n\n* [Piotr Solnica's Transproc](http://solnic.github.io/transproc/) for\n  initially inspiring me to look into function composition in Ruby;\n* [Tom Stuart](http://codon.com/) for suggesting that I extract this into a\n  gem.\n\n## License\n\nCopyright © 2015 Paul Mucur.\n\nDistributed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Fcomp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmudge%2Fcomp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Fcomp/lists"}