Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[in-review] Code optimisations via adding the credo #285

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/blanks.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Blanks do
@moduledoc false
def replace(ast, replacements) do
replacements = List.wrap(replacements)

Expand Down
3 changes: 2 additions & 1 deletion lib/display.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display do
@moduledoc false
use GenServer

alias IO.ANSI
Expand All @@ -20,7 +21,7 @@ defmodule Display do
{:noreply, %{state | clear_screen: false}}
end

def handle_cast(:clear_screen, state = %{clear_screen: true}) do
def handle_cast(:clear_screen, %{clear_screen: true} = state) do
IO.puts(ANSI.clear())
IO.puts(ANSI.home())

Expand Down
3 changes: 3 additions & 0 deletions lib/display/colours.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.Paint do
@moduledoc false
def red(str), do: painter().red(str)
def cyan(str), do: painter().cyan(str)
def green(str), do: painter().green(str)
Expand All @@ -13,6 +14,7 @@ defmodule Display.Paint do
end

defmodule Display.Colours do
@moduledoc false
alias IO.ANSI

def red(str), do: colourize(ANSI.red(), str)
Expand All @@ -26,6 +28,7 @@ defmodule Display.Colours do
end

defmodule Display.Uncoloured do
@moduledoc false
def red(str), do: str
def cyan(str), do: str
def green(str), do: str
Expand Down
1 change: 1 addition & 0 deletions lib/display/failure.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.Failure do
@moduledoc false
alias Display.Paint

@no_value :ex_unit_no_meaningful_value
Expand Down
7 changes: 4 additions & 3 deletions lib/display/intro.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule Display.Intro do
@moduledoc false
alias Display.Paint

def intro(module, modules) do
if not (module in modules) do
show_intro(module.intro)
else
if module in modules do
""
else
show_intro(module.intro)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/display/notification.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.Notifications do
@moduledoc false
alias Display.Paint

def congratulate do
Expand All @@ -13,8 +14,7 @@ defmodule Display.Notifications do
defp module_names(modules) do
modules
|> Enum.map(&Atom.to_string/1)
|> Enum.map(&name/1)
|> Enum.join(", ")
|> Enum.map_join(", ", &name/1)
|> Paint.red()
end

Expand Down
1 change: 1 addition & 0 deletions lib/display/progress_bar.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Display.ProgressBar do
@moduledoc false
@progress_bar_length 30

def progress_bar(%{current: current, total: total}) do
Expand Down
1 change: 1 addition & 0 deletions lib/elixir_koans.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule ElixirKoans do
@moduledoc false
use Application

def start(_type, _args) do
Expand Down
1 change: 1 addition & 0 deletions lib/execute.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Execute do
@moduledoc false
def run_module(module, callback \\ fn _result, _module, _koan -> nil end) do
Enum.reduce_while(module.all_koans, :passed, fn koan, _ ->
module
Expand Down
1 change: 1 addition & 0 deletions lib/koans.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Koans do
@moduledoc false
defp valid_name(name) do
Regex.match?(~r/([A-Z]|\.\.\.).+/, name)
end
Expand Down
1 change: 1 addition & 0 deletions lib/koans/01_equalities.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Equalities do
@moduledoc false
use Koans

@intro """
Expand Down
1 change: 1 addition & 0 deletions lib/koans/02_strings.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Strings do
@moduledoc false
use Koans

@intro "Strings"
Expand Down
3 changes: 2 additions & 1 deletion lib/koans/03_numbers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Numbers do
@moduledoc false
require Integer
use Koans

Expand Down Expand Up @@ -49,7 +50,7 @@ defmodule Numbers do
end

koan "Let's grab the individual digits in a list" do
individual_digits = Integer.digits(58127)
individual_digits = Integer.digits(58_127)
assert individual_digits == ___
end

Expand Down
1 change: 1 addition & 0 deletions lib/koans/04_atoms.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Atoms do
@moduledoc false
use Koans

@intro "Atoms"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/05_tuples.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Tuples do
@moduledoc false
use Koans

@intro "Tuples"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/06_lists.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Lists do
@moduledoc false
use Koans

@intro "Lists"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/07_keyword_lists.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule KeywordLists do
@moduledoc false
use Koans

@intro "KeywordLists"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/08_maps.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Maps do
@moduledoc false
use Koans

@intro "Maps"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/09_map_sets.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule MapSets do
@moduledoc false
use Koans

@intro "My name is Set, MapSet."
Expand Down
4 changes: 4 additions & 0 deletions lib/koans/10_structs.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule Structs do
@moduledoc false
use Koans

@intro "Structs"

defmodule Person do
@moduledoc false
defstruct [:name, :age]
end

Expand Down Expand Up @@ -35,10 +37,12 @@ defmodule Structs do
end

defmodule Plane do
@moduledoc false
defstruct passengers: 0, maker: :boeing
end

defmodule Airline do
@moduledoc false
defstruct plane: %Plane{}, name: "Southwest"
end

Expand Down
1 change: 1 addition & 0 deletions lib/koans/11_sigils.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Sigils do
@moduledoc false
use Koans

@intro "Sigils"
Expand Down
3 changes: 3 additions & 0 deletions lib/koans/12_pattern_matching.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PatternMatching do
@moduledoc false
use Koans

@intro "PatternMatching"
Expand Down Expand Up @@ -94,6 +95,7 @@ defmodule PatternMatching do
end

defmodule Animal do
@moduledoc false
defstruct [:kind, :name]
end

Expand All @@ -103,6 +105,7 @@ defmodule PatternMatching do
end

defmodule Plane do
@moduledoc false
defstruct passengers: 0, maker: :boeing
end

Expand Down
4 changes: 2 additions & 2 deletions lib/koans/13_functions.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Functions do
@moduledoc false
use Koans

@intro "Functions"
Expand Down Expand Up @@ -104,8 +105,7 @@ defmodule Functions do
result =
"full-name"
|> String.split("-")
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")
|> Enum.map_join(" ", &String.capitalize/1)

assert result == ___
end
Expand Down
1 change: 1 addition & 0 deletions lib/koans/14_enums.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Enums do
@moduledoc false
use Koans

@intro "Enums"
Expand Down
9 changes: 7 additions & 2 deletions lib/koans/15_processes.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Processes do
@moduledoc false
use Koans

@intro "Processes"
Expand Down Expand Up @@ -88,8 +89,12 @@ defmodule Processes do

send(pid, {:hello, self()})

timeout = 100 # ms
failure_message = "Sorry, I didn't get the right message. Look at the message that is sent back very closely, and try again"
# ms
timeout = 100

failure_message =
"Sorry, I didn't get the right message. Look at the message that is sent back very closely, and try again"

assert_receive ___, timeout, failure_message
end

Expand Down
1 change: 1 addition & 0 deletions lib/koans/16_tasks.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Tasks do
@moduledoc false
use Koans

@intro "Tasks"
Expand Down
1 change: 1 addition & 0 deletions lib/koans/17_agents.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Agents do
@moduledoc false
use Koans

@intro "Agents"
Expand Down
2 changes: 2 additions & 0 deletions lib/koans/18_genservers.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule GenServers do
@moduledoc false
use Koans

@intro "GenServers"

defmodule Laptop do
@moduledoc false
use GenServer

#####
Expand Down
19 changes: 16 additions & 3 deletions lib/koans/19_protocols.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Protocols do
@moduledoc false
use Koans

@intro "Want to follow the rules? Adhere to the protocol!"
Expand All @@ -12,13 +13,25 @@ defmodule Protocols do
end

defmodule Painter do
@moduledoc false
@derive Artist
defstruct name: ""
end

defmodule(Musician, do: defstruct(name: "", instrument: ""))
defmodule(Dancer, do: defstruct(name: "", dance_style: ""))
defmodule(Physicist, do: defstruct(name: ""))
defmodule Musician do
@moduledoc false
defstruct(name: "", instrument: "")
end

defmodule Dancer do
@moduledoc false
defstruct(name: "", dance_style: "")
end

defmodule Physicist do
@moduledoc false
defstruct(name: "")
end

defimpl Artist, for: Musician do
def perform(musician) do
Expand Down
14 changes: 7 additions & 7 deletions lib/koans/20_comprehensions.ex
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
defmodule Comprehensions do
@moduledoc false
use Koans

@intro "A comprehension is made of three parts: generators, filters, and collectibles. We will look at how these interact with each other"

koan "The generator, `n <- [1, 2, 3, 4]`, is providing the values for our comprehension" do
assert (for n <- [1, 2, 3, 4], do: n * n) == ___
assert for(n <- [1, 2, 3, 4], do: n * n) == ___
end

koan "Any enumerable can be a generator" do
assert (for n <- 1..4, do: n * n) == ___
assert for(n <- 1..4, do: n * n) == ___
end

koan "A generator specifies how to extract values from a collection" do
collection = [["Hello","World"], ["Apple", "Pie"]]
assert (for [a, b] <- collection, do: "#{a} #{b}") == ___
collection = [["Hello", "World"], ["Apple", "Pie"]]
assert for([a, b] <- collection, do: "#{a} #{b}") == ___
end

koan "You can use multiple generators at once" do
assert (for x <- ["little", "big"], y <- ["dogs", "cats"], do: "#{x} #{y}") == ___
assert for(x <- ["little", "big"], y <- ["dogs", "cats"], do: "#{x} #{y}") == ___
end

koan "Use a filter to reduce your work" do
assert (for n <- [1, 2, 3, 4, 5, 6], n > 3, do: n) == ___
assert for(n <- [1, 2, 3, 4, 5, 6], n > 3, do: n) == ___
end

koan "Add the result of a comprehension to an existing collection" do
collection = for x <- ["Pecan", "Pumpkin"], into: %{}, do: {x, "#{x} Pie"}
assert collection == ___
end

end
1 change: 1 addition & 0 deletions lib/meditate.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Mix.Tasks.Meditate do
@moduledoc false
use Mix.Task

@shortdoc "Start the koans"
Expand Down
1 change: 1 addition & 0 deletions lib/runner.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Runner do
@moduledoc false
use GenServer

def koan?(koan) do
Expand Down
1 change: 1 addition & 0 deletions lib/tracker.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Tracker do
@moduledoc false
alias __MODULE__

defstruct total: 0,
Expand Down
1 change: 1 addition & 0 deletions lib/watcher.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Watcher do
@moduledoc false
use GenServer

def start_link() do
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ defmodule Koans.Mixfile do
end

defp deps do
[{:file_system, "~> 0.2"}]
[{:file_system, "~> 0.2"},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false}]
end

defp elixirc_path(:test), do: ["lib/", "test/support"]
Expand Down
Loading