Skip to content

Instantly share code, notes, and snippets.

@silesky
Last active December 6, 2016 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silesky/af088b1d49600717f282ce15dd8dd30a to your computer and use it in GitHub Desktop.
Save silesky/af088b1d49600717f282ce15dd8dd30a to your computer and use it in GitHub Desktop.
dictionary.ex after feedback
defmodule Dictionary do
@moduledoc """
usage: the command "dict -f "atom" should return:
"Atoms are constants with itself as value."
etc...
"""
def main(args) do
IO.puts "main method called..."
args
|> parse_args
|> lookup
|> IO.inspect
end
defp find_term(term) do # given a term, searches terms.json and returns the matching term
{ :ok, terms } = "./lib/terms.json"
|> File.read!
|> JSON.decode
Enum.find(terms, &Map.get(&1, term))
end
defp parse_args(args) do # defp i.e. private
{_, _, [{_, word}]} = OptionParser.parse(args);
word
end
def lookup(args) do
args
|> find_term
|> Map.get(args)
|> Map.get("def")
end
end
[
{"atom": {
"def": ["constants with itself as value."],
"eg": null
}
},
{ "char list": {
"def": ["list of the 'code points' of each characters, rather than the chars themselves"],
"eg": "[100, 200, 300]"
}
},
{ "guard clause": {
"def": ["a way to augment pattern matching with more complex checks"],
"eg": "def hello(name) when is_string(name) do..."
}
},
{ "plug": {
"def": ["The spec that lets different frameworks talk to eachother in the erling vim", "a type of dependency (i.e goes in our mix.exs file", "a piece of code that that recieves a data structure, does some sort of transformation, and returns this same data structure, slightly modified"],
"eg": null
}
},
{ "directive": {
"def": ["a type of module with lexical scope"],
"eg": "require Foo, import Foo"
}
},
{
"require": {
"def": ["We use import whenever we want to easily access functions or macros from other modules without using the fully-qualified name."],
"eg": "require Foo"
}
}
]
@bryanjos
Copy link

bryanjos commented Dec 3, 2016

Looks good! Only a couple of points:

You can have your comment at the top of the module as a moduledoc inside of the module like so:

defmodule Dictionary do
  @moduledoc """
  Simple Dictionary (CL)
  the command "dict -f "atom" should return: 
  "Atoms are constants with itself as value."
  the command "dict -f "char list" should return:
  " a list of the 'code points' of the characters.
  etc...
  """

This would make it available for help in iex when someone typed h Dictionary as well as in generated from from ex_doc

The other thing would be that for find_term, you don't have to define a variable at the end. You can do it like so:

  defp find_term(term) do # given a term, searches terms.json and returns the matching term
    { :ok, terms } = "./lib/terms.json"
      |> File.read!
      |> JSON.decode
    
    Enum.find(terms, fn(el) -> Map.get(el, term) end)
  end

Same feedback for lookup as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment