Last active
December 6, 2016 18:21
-
-
Save silesky/af088b1d49600717f282ce15dd8dd30a to your computer and use it in GitHub Desktop.
dictionary.ex after feedback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"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" | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
This would make it available for help in
iex
when someone typedh Dictionary
as well as in generated from from ex_docThe 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:Same feedback for
lookup
as well.