Giter Club home page Giter Club logo

match_spec's Introduction

MatchSpec

tests

:ets matchspec helper library for elixir. Exposes the fun2ms/1 macro which transforms elixir-style function code into ets matchspecs.

iex> require MatchSpec
iex> MatchSpec.fun2ms(fn {key, value} when key === :foo -> value end)
[{{:"$1", :"$2"}, [{:"=:=", :"$1", {:const, :foo}}], [:"$2"]}]

Also exposes the ms2fun/2 function which converts a matchspec to function code or ast which represents a function that performs the same task as the ets matchspec

iex> MatchSpec.ms2fun([{{:"$1", :"$2"}, [{:"=:=", :"$1", {:const, :foo}}], [:"$2"]}], :code)
"fn {v1, v2} when v1 === :foo -> v2 end"
iex> :ets.test_ms({:foo, :bar}, MatchSpec.fun2ms(fn {key, value} when key === :foo -> value end))
{:ok, :bar}

Provides fun2msfun/4 macro which can be used to parametrize the matchspec:

iex> require MatchSpec
iex> lambda = MatchSpec.fun2msfun(fn {^key, value} -> value end, [key])
iex> lambda.(:key)
[{{:"$1", :"$2"}, [{:"=:=", :"$1", {:const, :key}}], [:"$2"]}]

Provides defmatchspec/2 and defmatchspecp/2 macros which can be used to directly generate functions in your module

defmodule MyModule do
  use MatchSpec
  
  defmatchspec my_matchspec(key) do
    {^key, value} -> value
  end
end

MyModule.my_matchspec(:key)
[{{:"$1", :"$2"}, [{:"=:=", :"$1", {:const, :key}}], [:"$2"]}]

Installation

The package can be installed by adding match_spec to your list of dependencies in mix.exs:

def deps do
  [
    {:match_spec, "~> 0.3.1"}
  ]
end

Documentation can be found at https://hexdocs.pm/match_spec.

match_spec's People

Contributors

ityonemo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

match_spec's Issues

bugfix

{location = {^row, _}, cell} when cell.char !== "\n" -> {location, cell}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.