{"id":16318536,"url":"https://github.com/vic/macpato","last_synced_at":"2025-10-25T18:30:22.499Z","repository":{"id":57519083,"uuid":"70944973","full_name":"vic/macpato","owner":"vic","description":"Simple pattern matching on Elixir quoted expressions.","archived":false,"fork":false,"pushed_at":"2017-01-11T16:47:53.000Z","size":16,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T10:04:54.771Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://hex.pm/packages/macpato","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vic.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":"2016-10-14T20:27:57.000Z","updated_at":"2020-08-25T15:09:03.000Z","dependencies_parsed_at":"2022-09-26T18:00:52.074Z","dependency_job_id":null,"html_url":"https://github.com/vic/macpato","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Fmacpato","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Fmacpato/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Fmacpato/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Fmacpato/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vic","download_url":"https://codeload.github.com/vic/macpato/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238191666,"owners_count":19431464,"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-10-10T22:23:50.462Z","updated_at":"2025-10-25T18:30:17.191Z","avatar_url":"https://github.com/vic.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Macpato\n\n\u003ca href=\"https://travis-ci.org/vic/macpato\"\u003e\u003cimg src=\"https://travis-ci.org/vic/macpato.svg\"\u003e\u003c/a\u003e\n\nMacpato is a macro for very simple pattern matching on quoted elixir expressions.\n\n## Installation\n\n[Available in Hex](https://hex.pm/packages/macpato), the package can be installed as:\n\n  1. Add `macpato` to your list of dependencies in `mix.exs`:\n\n    ```elixir\n    def deps do\n      [{:macpato, \"~\u003e 0.1.2\"}]\n    end\n    ```\n\n## Usage\n\nA typical pattern match on a quoted expression looks like this:\n\n```elixir\n{:foo, _ctx, _args} = quote(do: foo)\n```\n\nHowever for complex expression, the pattern to match the AST can get way longer.\nmacpato helps by giving you a way to create these patterns easily.\n\n```elixir\niex\u003e import Macpato\n...\u003e expr = quote do\n...\u003e   fn a, b, c -\u003e a + b + c end\n...\u003e end\n...\u003e case expr do\n...\u003e   macpato(fn _, b, _ -\u003e _ end) -\u003e :b_is_second_arg\n...\u003e   _ -\u003e :dunno\n...\u003e end\n:b_is_second_arg\n```\n\nThe `_` function is special inside macpato, and when given an argument\nit will just place whatever you give it into the pattern.\n\nThis way you can for example, assign part of the match into a variable \nin this case to get the name of the third argument bellow:\n\n```elixir\niex\u003e import Macpato\n...\u003e expr = quote do\n...\u003e   fn a, b, c -\u003e a + b + c end\n...\u003e end\n...\u003e case expr do\n...\u003e   macpato(fn _, _, _({name, _, _}) -\u003e _ end) -\u003e name\n...\u003e   _ -\u003e :dunno\n...\u003e end\n:c\n```\n\nOr use a pinned value, for example to check we are adding the number 22.\n\n```elixir\niex\u003e import Macpato\n...\u003e expr = quote do\n...\u003e   fn a -\u003e a + 22 end\n...\u003e end\n...\u003e x = 22\n...\u003e case expr do\n...\u003e   macpato(fn _ -\u003e _ + _(^x) end) -\u003e :good\n...\u003e   _ -\u003e :dunno\n...\u003e end\n:good\n```\n\nYou can capture arrays from the AST with `_(@)` for example:\n\n```elixir\niex\u003e import Macpato\n...\u003e expr = quote do\n...\u003e   fn a, b, c -\u003e x end\n...\u003e end\n...\u003e case expr do\n...\u003e   macpato(fn _(@args) -\u003e _ end) -\u003e length(args)\n...\u003e end\n3\n```\n\nNote that `macpato` can be used on any place where you can have a pattern in Elixir,\nlike macro definition arguments, cases, with expressions, etc.\n\n\nSee the [tests](https://github.com/vic/macpato/blob/master/test/macpato_test.exs) for more examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvic%2Fmacpato","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvic%2Fmacpato","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvic%2Fmacpato/lists"}