vic / expat Goto Github PK
View Code? Open in Web Editor NEWReusable, composable patterns across Elixir libraries
Home Page: https://hex.pm/packages/expat
License: Apache License 2.0
Reusable, composable patterns across Elixir libraries
Home Page: https://hex.pm/packages/expat
License: Apache License 2.0
Hey,
there is already a popular C library libexpat, which is also spelled Expat in short or Expat XML parser in full, so it might make sense to rename this project to something else to avoid confusion.
Following @OvermindDL1's idea:
Currently when you define a named pattern
def var(name, meta, context) when is_atom(name) and is_list(meta) and is_atom(context)
The following macros are generated:
defmacro var()
defmacro var(bindings)
defmacro var(name, bindings)
defmacro var(name, meta, bindings)
defmacro var(name, meta, context, bindings)
var()
will underscore all pattern variables not used in guard.
bindings
is a Keyword of names to expressions, used to bind variables inside the pattern.
All other functions are just there for providing positional arguments (vs named ones via the last keyword)
Now, I'm thiking about adding a couple more:
defmacro var?(expr)
defmacro var!(bindings)
The first one will just check that expr conforms - that is it matches the pattern and all its guards are satisfied
and the second will raise an error unless all variables inside the patter are bound by name from bindings, it will be used for as data constructor, even for those patterns with guards.
Hi! First of all: fantastic project!
I was writing a credo check. I needed to match on "pipe operator where the second param is function application". Those kinds of patterns are pretty big:
{:|>, a,
[b, {{:., c, [{:__aliases__, meta, [module_name]}, function_name]}, d, params}]},
but using the lib makes them more manageable:
defpat(pipe({:>, meta, [before_pipe, after_pipe]} = ast))
defpat(
function_call(
{{:., dot_meta, [{:__aliases__, module_meta, [module_name]}, function_name]}, params_meta,
params} = ast
)
)
defp traverse(
pipe(
meta: a,
before_pipe: b,
after_pipe:
function_call(
dot_meta: c,
module_meta: meta,
module_name: module_name,
function_name: function_name,
params_meta: d,
params: params
)
),
...) do
Thank you very much for making composing pattern matches easier! I wanted to share with you how much easier it makes writing those checks :)
If I make a pattern like this:
defpat ok({:ok, val})
but then misuse it like this:
def my_fun(ok(value: value)) do
value
end
Notice that the binding is called val
but used :value
as the key in the keyword list. The error I get is:
warning: variable "value" does not exist and is being expanded to "value()", please use parentheses to remove the ambiguity or change the variable name
If I understand correctly, it means that the key value
is ignored when there is no binding called this way. It might be a little bit confusing in case of typos to understand what is happening.
Would you consider it a good idea to raise in case usage of a given pattern tries to access non-existent binding?
If yes, I could try digging into it over the weekend and checking if I understand the lib enough to make that change.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.