Shorthand v0.0.2 Shorthand View Source
Convenience macros to eliminate laborious typing.
Examples:
These examples use variable arguments (default is 10, see configuration below)
Instead of %{one: one, two: two, three: three}
, you can type map(one, two, three)
Instead of my_func(one: one, two: two, three: three)
, you can type my_func(keywords(one, two, three))
Instead of %MyStruct(one: one, two: two, three: three)
, you can type bulid_struct(MyStruct, one, two, three)
Without variable arguemnts,
Instead of %{one: one, two: two, three: three}
, you can type map([one, two, three])
Instead of my_func(one: one, two: two, three: three)
, you can type my_func(keywords([one, two, three]))
Instead of %MyStruct(one: one, two: two, three: three)
, you can type bulid_struct(MyStruct, [one, two, three])
Configuration
Because Shorthand is about convenience, the macros can be configured
config :shorthand,
map: :m,
str_map: :sm,
keywords: :k,
build_struct: :s,
variable_args: 10 # false to remove variable arguemnts
Then you can use them as:
m(a, _b, ^c) == %{a: a, b: _b, c: ^c}
sm(a, _b, ^c) == %{"a" => a, "b" => _b, "c" => ^c}
k(a, b, c) == [a: a, b: b, c: c]
s(MyStruct, name, age) == %MyStruct{name: name, age: age}
Link to this section Summary
Functions
Builds a struct where the field names are the same as the arguments supplied
Builds a keyword list where the keys and value arguments are the same name
Builds a map where the keys and values have the same name
Builds a map where the string keys and values have the same name
Link to this section Functions
Builds a struct where the field names are the same as the arguments supplied
Example:
iex> scheme = "https"
iex> host = "elixir-lang.org"
iex> path = "/docs.html"
iex> build_struct(URI, scheme, host, path)
%URI{scheme: "https", host: "elixir-lang.org", path: "/docs.html"}
Builds a keyword list where the keys and value arguments are the same name
Example:
iex> a = 1
iex> b = 2
iex> c = 3
iex> keywords(a, b, c)
[a: 1, b: 2, c: 3]
Examples
iex> c = 3
iex> keywords(a, _b, ^c) = [a: 1, b: 3, c: 3]
iex> a
1
iex> match?(keywords(^a), [a: 2])
false
Builds a map where the keys and values have the same name
Example:
iex> a = 1
iex> b = 2
iex> map(a, b)
%{a: 1, b: 2}
Example:
iex> a = 1
iex> c = 2
iex> map(a, b: map(c), d: nil)
%{a: 1, b: %{c: 2}, d: nil}
Example:
iex> a = 1
iex> map(^a, _b, c) = %{a: 1, b: 3, c: 2}
iex> c
2
iex> match?(map(^a), %{a: 2})
false
Builds a map where the string keys and values have the same name
Example:
iex> a = 1
iex> b = 2
iex> str_map(a, b)
%{"a" => 1, "b" => 2}
Example:
iex> a = 1
iex> b = 2
iex> str_map(a, other: str_map(b))
%{"a" => 1, "other" => %{"b" => 2}}
Example:
iex> a = 1
iex> str_map(^a, _b, c) = %{"a" => 1, "b" => 3, "c" => 2}
iex> c
2
iex> match?(str_map(^a), %{"a" => 2})
false