{"id":13508564,"url":"https://github.com/meyercm/shorter_maps","last_synced_at":"2026-02-23T05:01:46.305Z","repository":{"id":57547828,"uuid":"60717246","full_name":"meyercm/shorter_maps","owner":"meyercm","description":"Elixir ~M sigil for map shorthand. `~M{id, name} ~\u003e %{id: id, name: name}`","archived":false,"fork":false,"pushed_at":"2024-06-28T20:39:32.000Z","size":51,"stargazers_count":235,"open_issues_count":8,"forks_count":11,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-10-21T19:02:53.628Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Elixir","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/meyercm.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-08T17:34:54.000Z","updated_at":"2025-10-21T06:17:37.000Z","dependencies_parsed_at":"2024-01-31T07:12:41.641Z","dependency_job_id":"9244adeb-785d-467e-8b7c-5fdb01c7d204","html_url":"https://github.com/meyercm/shorter_maps","commit_stats":{"total_commits":59,"total_committers":6,"mean_commits":9.833333333333334,"dds":0.4067796610169492,"last_synced_commit":"04b1f4b51a4d7d1cca0707e4ec86ab0cb8a454fd"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/meyercm/shorter_maps","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meyercm%2Fshorter_maps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meyercm%2Fshorter_maps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meyercm%2Fshorter_maps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meyercm%2Fshorter_maps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/meyercm","download_url":"https://codeload.github.com/meyercm/shorter_maps/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meyercm%2Fshorter_maps/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29738083,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-23T04:51:08.365Z","status":"ssl_error","status_checked_at":"2026-02-23T04:49:15.865Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-08-01T02:00:54.849Z","updated_at":"2026-02-23T05:01:46.288Z","avatar_url":"https://github.com/meyercm.png","language":"Elixir","readme":"## ShorterMaps\n\n`~M` sigil for map shorthand. `~M{a} ~\u003e %{a: a}`\n\n[![Build Status](https://travis-ci.org/meyercm/shorter_maps.svg?branch=master)](https://travis-ci.org/meyercm/shorter_maps)\n\n### Getting started\n\n1) Add `{:shorter_maps, \"~\u003e 2.0\"},` to your mix deps\n2) Add `import ShorterMaps` to the top of your module\n3) DRY up your maps and structs with `~M` and `~m`. Instead of `%{name: name}`\n   use `~M{name}`, and for `%{\"name\" =\u003e name}` use `~m{name}`. When the key and\n   the variable don't match, don't fret: `~M{name, id: current_id}` expands\n   to `%{name: name, id: current_id}`.\n\n### Motivation\n\nCode like `%{id: id, name: name, address: address}` occurs with high frequency\nin many programming languages.  In Elixir, additional uses occur as we pattern\nmatch to destructure existing maps.\n\nES6 provided javascript with a shorthand to create maps with keys inferred by\nvariable names, and allowed destructuring those maps into variables named for\nthe keys.  `ShorterMaps` provides that functionality to Elixir.\n\n### Syntax:\n\n`~M` and `~m` can be used to replace maps __anywhere__ in your code. The\n`ShorterMaps` sigil syntax operates just like a vanilla elixir map, with two\nmain differences:\n\n  1) When a variable name stands alone, it is replaced with a key-value pair,\n  where the key is the variable name as a string (~m) or an atom (~M). The value\n  will be the variable. For example, `~M{name, id: get_free_id()}` expands to\n  `%{name: name, id: get_free_id()}`.\n\n  2) Struct names are enclosed in the sigil, rather than outside, e.g.:\n  `~M{%StructName key, key2}` === `%StructName{key: key, key2: key2}`. The\n  struct name must be followed by a space, and then comma-separated keys.\n  Structs can be updated just like maps: `~M{%StructName old_struct|key_to_update}`\n\n### Examples\n\n```elixir\niex\u003e import ShorterMaps\n...\u003e name = \"Chris\"\n...\u003e id = 6\n...\u003e ~M{name, id}\n%{name: \"Chris\", id: 6}\n\n# It's ok to mix in other expressions:\n...\u003e ~M{name, id: id + 200}\n%{name: \"Chris\", id: 206}\n\n# or even nest the sigil (note the change in delimiters to paren):\n...\u003e ~M{name, id, extra_copy: ~M(name, id)}\n%{name: \"Chris\", id: 6, extra_copy: %{name: \"Chris\", id: 6}}\n\n# We can use String keys:\n...\u003e ~m{name, id}\n%{\"name\" =\u003e \"Chris\", \"id\" =\u003e 6}\n\n# And we can update existing maps:\n...\u003e map_1 = %{name: \"Bob\", id: 9}\n...\u003e ~M{map_1|name}\n%{name: \"Chris\", id: 9}\n\n# Struct syntax is a little funky:\n...\u003e defmodule MyStruct do\n...\u003e   defstruct [id: nil, name: :default]\n...\u003e end\n...\u003e ~M{%MyStruct id}\n%MyStruct{id: 6, name: :default}\n\n# Structs can be updated too:\n...\u003e initial_struct = %MyStruct{name: \"Chris\", id: :unknown}\n...\u003e ~M{%MyStruct initial_struct|id}\n%MyStruct{name: \"Chris\", id: 6}\n\n# Because the expansion happens at compile time, they can be used __anywhere__:\n\n# in function heads:\n...\u003e defmodule MyModule do\n...\u003e   def my_func(~M{name, _id}), do: {:id_present, name}\n...\u003e   def my_func(~M{name}), do: {:no_id, name}\n...\u003e end\n\n# in pattern matches:\n...\u003e ~M{age, model} = %{age: -30, model: \"Delorean\", manufacturer: \"AMC\"}\n...\u003e age\n-30\n\n```\n\n### Credits\n\nShorterMaps adds additional features to the original project, `ShortMaps`,\nlocated [here][original-repo]. The reasons for the divergence are summarized\n[here][divergent-opinion-issue].\n\n[original-repo]: https://github.com/whatyouhide/short_maps\n[divergent-opinion-issue]: https://github.com/whatyouhide/short_maps/issues/11\n\n### Quick Reference:\n\n* Atom keys: `~M{a, b}` =\u003e `%{a: a, b: b}`\n* String keys: `~m{a, b}` =\u003e `%{\"a\" =\u003e a, \"b\" =\u003e b}`\n* Structs: `~M{%Person id, name}` =\u003e `%Person{id: id, name: name}`\n* Pinned variables: `~M{^a, b}` =\u003e `%{a: ^a, b: b}`\n* Ignore matching: `~M{_a, b}` =\u003e `%{a: _a, b: b}`\n* Map update (strings or atoms): `~M{old|a, b, c}` =\u003e `%{old|a: a, b: b, c: c}`\n* Struct update: `~M{%Person old_struct|name} =\u003e %Person{old_struct|name: name}`\n* Mixed mode: `~M{a, b: b_alt}` =\u003e `%{a: a, b: b_alt}`\n* Expressions: `~M{a, b: a + 1}` =\u003e `%{a: a, b: a + 1}`\n* Zero-arity: `~M{a, b()}` =\u003e `%{a: a, b: b()}`\n* Modifiers: `~m{blah}a == ~M{blah}` or `~M{blah}s == ~m{blah}`\n\n**Note**: you must `import ShorterMaps` for the sigils to work.\n","funding_links":[],"categories":["Macros"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeyercm%2Fshorter_maps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmeyercm%2Fshorter_maps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeyercm%2Fshorter_maps/lists"}