RepositoryElixirMXMexico
vic

vic/expat

Reusable, composable patterns across Elixir libraries

175sao
5fork
3issue
3lượt xem

🧩 Expat — Pattern Matching Tái Sử Dụng, Tổ Hợp Cho Elixir

Thay vì lặp lại pattern dài dòng khắp codebase, hãy đặt tên cho chúng, ghép chồng lên nhau, kèm guard — và dùng ngay ở function head hay constructor.

Expat (vic/expat) là thư viện Elixir cho phép định nghĩa named pattern — những pattern matcher có tên, có thể import, lồng nhau, gắn guard, và khi bind đủ biến còn đóng vai constructor dữ liệu. Thay vì viết pattern struct/tuple phức tạp lặp đi lặp lại trong Phoenix params, business logic hay test, bạn tách thành từng mảnh nhỏ (defpat), tổ hợp thành pattern lớn (mexican_parrot lồng mexican), thậm chí dùng expat def để expand pattern + guard ngay tại function head. Kết quả: code đọc dễ hiểu intent, ít lặp, dễ bảo trì.

✨ Điểm nổi bật

  • Named patterns (defpat): Đặt tên cho pattern tuple/struct/map bất kỳ, dùng lại ở đâu cũng được (ok(), error(reason: x)).
  • Pattern composition: Pattern lớn lồng pattern nhỏ — ví dụ mexican_parrot chứa mexican bên trong, tự động bind biến cần thiết (owner_name: name).
  • Guard support: Gắn when guard vào defpat (ví dụ adult check age >= 18), guard được expand đúng chỗ khi dùng.
  • Data constructor: Khi bind đủ mọi biến trong pattern, macro biến thành constructor (ok(result: "done") → {:ok, "done"}).
  • expat def cho function head: Expand pattern + guard ngay tại clause, giữ function head gọn mà vẫn match đầy đủ cấu trúc phức tạp.
  • Exportable across libs/apps: Pattern định nghĩa ở lib A có thể import vào lib B — tách rule matching dữ liệu ra khỏi logic nghiệp vụ.
  • Zero runtime overhead: Toàn bộ expand tại compile-time qua macro, không chi phí runtime.
  • Works anywhere pattern valid: Dùng trong match?, assert, case, function head, comprehension...

💡 Vì sao đáng chú ý

Expat giải quyết nỗi đau thực tế: pattern matching Elixir mạnh nhưng khi struct lồng nhau sâu, params Phoenix to, hoặc cùng một pattern xuất hiện ở nhiều chỗ, code trở nên lặp, khó đọc, khó đổi. Thay vì viết helper function check, Expat cho phép trừu tượng hóa pattern như một first-class citizen — đặt tên, tổ hợp, guard, export. Đặc biệt phù hợp cho codebase lớn, umbrella apps, hoặc library cần công khai "hình dạng" dữ liệu hợp lệ cho consumer. Cú pháp expat def là điểm tinh tế: nó không chỉ macro sinh pattern, mà còn đặt guard đúng ngữ nghĩa (guard của pattern con expand vào clause cha), tránh sai lệch so với viết tay.


#Elixir #PatternMatching #Macros #ComposablePatterns #OpenSource #TiniX

Dự án tương tự

Khám phá các dự án và mô hình AI khác có sự tương đồng.

RepositoryRust
adam-mcdaniel/sage

A programming language that's wise beyond its bytes!🌱🌿🪴

559
May 18, 2026
RepositoryJulia
MasonProtter/SumTypes.jl

An implementation of sum types in Julia

122
Nov 11, 2026
RepositoryJulia
Roger-luo/Moshi.jl

nextgen MLStyle: Generic Algebraic Data Type + Pattern Match

114
Nov 21, 2026
RepositoryTypeScript
openclaw/openclaw

The AI that really does things. Any OS. Any Platform. The lobster way. 🦞

395.6k
Dec 2, 2026
Repository
jwasham/coding-interview-university

A complete computer science study plan to become a software engineer.

365.2k
Nov 7, 2026
RepositoryTypeScript
firecrawl/firecrawl

Firecrawl is the knowledge infrastructure to make your agents smarter. It connects your agents to the web, and to Alexandria, our library of everything else.

216.1k
Dec 3, 2026
RepositoryPython
microsoft/markitdown

Python tool for converting files and office documents to Markdown.

199.6k
Dec 3, 2026
Modelsentence-similarity
sentence-transformers/all-MiniLM-L6-v2

all-MiniLM-L6-v2 This is a sentence-transformers(https://www.SBERT.net) model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search. Usage (Sentence-Transformers) Using t...

♥0
156.6M
Jun 1, 2026
RepositoryElixir
plausible/analytics

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

30.0k
Nov 27, 2026