{"id":21564617,"url":"https://github.com/taiansu/dextruct","last_synced_at":"2025-03-18T05:16:27.487Z","repository":{"id":57489804,"uuid":"100692582","full_name":"taiansu/dextruct","owner":"taiansu","description":"Destructing assignment with `\u003c~`for Elixir","archived":false,"fork":false,"pushed_at":"2017-08-30T17:03:08.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T00:14:22.757Z","etag":null,"topics":["elixir"],"latest_commit_sha":null,"homepage":"","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/taiansu.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":"2017-08-18T08:48:30.000Z","updated_at":"2017-10-21T09:44:44.000Z","dependencies_parsed_at":"2022-08-29T15:12:01.212Z","dependency_job_id":null,"html_url":"https://github.com/taiansu/dextruct","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/taiansu%2Fdextruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taiansu%2Fdextruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taiansu%2Fdextruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taiansu%2Fdextruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taiansu","download_url":"https://codeload.github.com/taiansu/dextruct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244160053,"owners_count":20408021,"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":["elixir"],"created_at":"2024-11-24T10:16:34.770Z","updated_at":"2025-03-18T05:16:27.465Z","avatar_url":"https://github.com/taiansu.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dextruct\n\n### Destructing assignment with `\u003c~`\n\nIt's so obvious that pattern matching with `=` is _just awesome_. But less then occasionally, we still want some destructing assignment, witout `MatchError`, works like in other languge. `Dextruct` library provides a `\u003c~` operator which imitates similar behavior, with some other goodies.\n\n## Installation\n\nThe package can be installed by adding `dextruct` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:dextruct, \"~\u003e 1.0.0\"}\n  ]\nend\n```\n\nAfter excute `mix deps.get` command, add `use Dextruct` in your module:\n```elixir\ndef YourModule do\n  use Dextruct\nend\n```\n\n## Destruct assignment\n\nThe `\u003c~` operator works on two senarios, `List` and `Map`.\n\n### List\nFor destructing assignment on `List`, it simply fills up the list on left hand side with filler (`nil` by default).\n\n```elixir\niex\u003e [a, b, c] \u003c~ [1, 2]\n[1, 2, nil]\n\niex\u003e c\nnil\n```\n\nOne of the useful example is use with `Regex`'s optional group matching. Originally it will omit the unmatched groups in the end.\n```elixir\niex\u003e Regex.run(~r/(a)?(b)?(c)?(d)?/, \"ab\")\n[\"ab\", \"a\", \"b\"]\n\niex\u003e [matched, a, b, c, d] \u003c~ Regex.run(~r/(a)?(b)?(c)?(d)?/, \"ab\")\n[\"ab\", \"a\", \"b\", nil, nil]\n```\n\n### Map\nDestructing assignment on `Map`, then as well, fill the keys missing in the right hand side map,\nwith filler.\n```elixir\niex\u003e %{a: a, b: b, c: c} \u003c~ %{a: 1}\niex\u003e a\n1\niex\u003e b\nnil\niex\u003e c\nnil\n```\n\n### Different filler\n\nYou can specify the filler by `fill` option while `use Dextruct`. That means you can only have one filler for each module.\n\n```elixir\ndef YourModule do\n  use Dextruct, fill: 0\nend\n```\n\n## Map short-hand literal\n\nDextractor also come with a short-hand literal for `Map`, which is `sigil_m`.\n```elixir\niex\u003e ~m{a, b, c: foo} = %{a: 1, b: 2, c: 3}\n%{a: 1, b: 2, c: 3}\n```\n\nIt works well with `\u003c~`, of course.\n```elixir\niex\u003e ~m{a, b, c: foo} \u003c~ %{a: 1}\niex\u003e a\n1\niex\u003e b\nnil\niex\u003e foo\nnil\n```\n\n### Exclude `sigil_m`\nTo be honest, if you use shorthand map literal quite often, without change the binding variable in between, i.e.,  `c: foo` in the previous code demo, you should consider using [ShortMaps](https://github.com/whatyouhide/short_maps) instead. Which is more like the consensus of the community for now.\n\nTo do so, you'll like to `except` the `sigil_m` while using `Dextruct`.\n```elixir\ndefmodule YourModule do\n  use Dextruct, except: [sigil_m: 2]\nend\n```\n\nActually, the `sigil_m` comes with `Dextruct` will be exclude by default or even deprecate, once the\n`ShortMap` literal becomes the de facto standard anytime in the future.\n\n## Is it any good?\n\nHopefully.\n\n## License\n\nApache 2\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaiansu%2Fdextruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaiansu%2Fdextruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaiansu%2Fdextruct/lists"}