Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Elixir
  3. shorter_maps

shorter_maps

MITElixir

Elixir sigil for concise map and struct creation with automatic key-value inference from variable names.

GitHubGitHub
234 stars10 forks0 contributors

What is shorter_maps?

ShorterMaps is an Elixir library that provides the `~M` and `~m` sigils as shorthand syntax for creating maps and structs. It reduces repetitive boilerplate code by inferring keys from variable names, similar to JavaScript's ES6 object shorthand, and supports pattern matching, updates, and nested usage.

Target Audience

Elixir developers who frequently work with maps and structs, especially those writing code with repetitive key-value pairs like `%{name: name, id: id}` or using pattern matching for destructuring.

Value Proposition

Developers choose ShorterMaps because it makes Elixir code more concise and readable by eliminating verbose map syntax while maintaining full compatibility with Elixir's semantics, and it works anywhere in code due to compile-time macro expansion.

Overview

Elixir ~M sigil for map shorthand. ~M{id, name} ~> %{id: id, name: name}

Use Cases

Best For

  • Reducing repetitive map syntax like `%{name: name, id: id}` in Elixir codebases
  • Creating and updating structs with shorthand syntax, e.g., `~M{%Person id, name}`
  • Pattern matching and destructuring maps in function heads or assignments
  • Mixing variables, expressions, and nested maps in a concise format
  • Updating existing maps or structs with the `~M{old_map|key}` syntax
  • Using string-keyed maps (`~m`) or atom-keyed maps (`~M`) interchangeably with modifiers

Not Ideal For

  • Teams enforcing strict Elixir style guides that prohibit custom sigils or macros
  • Projects where map keys are complex expressions not directly derived from variable names
  • Codebases prioritizing zero dependencies and vanilla syntax for maintainability

Pros & Cons

Pros

Concise Syntax

Eliminates repetitive map boilerplate by inferring keys from variable names, e.g., ~M{name} expands to %{name: name}, similar to JavaScript ES6 shorthand as noted in the README.

Full Language Integration

Works anywhere in Elixir code due to compile-time macro expansion, including function heads for pattern matching and nested usage, demonstrated in the examples.

Struct Support

Enables shorthand creation and updates for structs with syntax like ~M{%Person id, name}, integrating directly with Elixir's struct semantics without extra steps.

Flexible Expressions

Allows mixing shorthand variables with explicit key-value pairs and expressions, such as ~M{name, id: id + 200}, for versatile map construction.

Cons

Import Dependency

Requires importing ShorterMaps in every module, adding boilerplate setup and potential namespace pollution, which the README explicitly mandates.

Non-Standard Syntax

Introduces custom sigils that deviate from vanilla Elixir, potentially confusing developers unfamiliar with the library and hindering code readability in mixed teams.

Macro Complexity

Relies on compile-time macro expansion, which can lead to cryptic error messages and subtle debugging challenges, a common trade-off in macro-based libraries.

Frequently Asked Questions

Quick Stats

Stars234
Forks10
Contributors0
Open Issues7
Last commit2 years ago
CreatedSince 2016

Tags

#macro#elixir#productivity#structs#pattern-matching

Built With

E
Elixir

Included in

Elixir13.1k
Auto-fetched 17 hours ago

Related Projects

typed_structtyped_struct

An Elixir library for defining structs with a type without writing boilerplate code.

Stars768
Forks37
Last commit7 months ago
opusopus

A framework for pluggable business logic components

Stars371
Forks21
Last commit2 years ago
pathexpathex

Fastest tool to access data in Elixir

Stars364
Forks17
Last commit11 months ago
pipespipes

Macros for more flexible composition with the Elixir Pipe operator

Stars323
Forks22
Last commit7 years ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub