Mexicovic/expat
Reusable, composable patterns across Elixir libraries
🧩 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_parrotchứamexicanbên trong, tự động bind biến cần thiết (owner_name: name). - Guard support: Gắn
whenguard vàodefpat(ví dụadultcheckage >= 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 defcho 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.
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
A complete computer science study plan to become a software engineer.
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.
Python tool for converting files and office documents to Markdown.
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...
Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.