-
Notifications
You must be signed in to change notification settings - Fork 34
Ревью нового ДЗ =) #74
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
Open
bylevskijSV
wants to merge
4
commits into
yzh44yzh:master
Choose a base branch
from
bylevskijSV:lesson_07/done_homework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,29 @@ | ||
| defmodule MyMusicBand.Band do | ||
| alias MyMusicBand.Player | ||
|
|
||
| def init(), do: nil | ||
| defstruct players: [] | ||
|
|
||
| def add_player(arg1, arg2), do: nil | ||
| @type t() :: %__MODULE__{ | ||
| players: list(Player.t()) | ||
| } | ||
|
|
||
| def next(arg), do: nil | ||
| @spec init() :: t() | ||
| def init(), do: %__MODULE__{} | ||
|
|
||
| end | ||
| @spec add_player(t(), Player.t()) :: t() | ||
| def add_player(band, player) do | ||
| %{band | players: band.players ++ [player]} | ||
| end | ||
|
|
||
| @spec next(t()) :: {list(atom), t()} | ||
| def next(band) do | ||
| {sounds, updated_players} = | ||
| Enum.map(band.players, fn player -> | ||
| Player.next(player) | ||
| end) | ||
| |> Enum.unzip() | ||
|
|
||
| new_band = %{band | players: updated_players} | ||
| {sounds, new_band} | ||
| end | ||
| end |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| defmodule MyMusicBand.Drummer do | ||
| alias MyMusicBand.Model.Sound | ||
| alias MyMusicBand.PlayerLogic | ||
|
|
||
| def init(arg), do: nil | ||
| @derive MyMusicBand.Player | ||
|
|
||
| def next(arg), do: nil | ||
| defstruct [:sounds, :current_stream] | ||
|
|
||
| end | ||
| @type t() :: %__MODULE__{ | ||
| sounds: [Sound.drum_sound()], | ||
| current_stream: Enumerable.t() | ||
| } | ||
|
|
||
| @spec init([Sound.drum_sound()]) :: {:ok, t()} | {:error, [{integer(), :atom}]} | ||
| def init(sounds) do | ||
| PlayerLogic.init(%__MODULE__{}, sounds, &Sound.is_drum/1) | ||
| end | ||
|
|
||
| @spec next(t()) :: {atom(), t()} | ||
| def next(drummer), do: PlayerLogic.next(drummer) | ||
| end |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| defmodule MyMusicBand.Guitarist do | ||
| alias MyMusicBand.Model.Sound | ||
| alias MyMusicBand.PlayerLogic | ||
|
|
||
| def init(arg), do: nil | ||
| @derive MyMusicBand.Player | ||
|
|
||
| def next(arg), do: nil | ||
| defstruct [:sounds, :current_stream] | ||
|
|
||
| end | ||
| @type t() :: %__MODULE__{ | ||
| sounds: [Sound.guitar_sound()], | ||
| current_stream: Enumerable.t() | ||
| } | ||
|
|
||
| @spec init([Sound.guitar_sound()]) :: {:ok, t()} | {:error, [{integer(), :atom}]} | ||
| def init(sounds) do | ||
| PlayerLogic.init(%__MODULE__{}, sounds, &Sound.is_guitar/1) | ||
| end | ||
|
|
||
| @spec next(t()) :: {atom(), t()} | ||
| def next(guitaris), do: PlayerLogic.next(guitaris) | ||
| end |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,32 @@ | ||
| defmodule MyMusicBand.Model.Sound do | ||
| @moduledoc """ | ||
| Identifies valid sounds for each type of musician. | ||
| """ | ||
|
|
||
| @type silence() :: :' ' | ||
| @type silence() :: :" " | ||
| @silence :" " | ||
|
|
||
| @type note_a() :: :'A-a-a' | ||
| @type note_c() :: :'O-o-o' | ||
| @type note_e() :: :'E-e-e' | ||
| @type note_g() :: :'Wooo' | ||
| @type note_a() :: :"A-a-a" | ||
| @type note_c() :: :"O-o-o" | ||
| @type note_e() :: :"E-e-e" | ||
| @type note_g() :: :Wooo | ||
| @type vocal_sound() :: note_a() | note_c() | note_e() | note_g() | silence() | ||
| @vocal_sounds [:"A-a-a", :"O-o-o", :"E-e-e", :Wooo, @silence] | ||
| def is_vocal(sound), do: sound in @vocal_sounds | ||
|
|
||
| @type accord_a() :: :A | ||
| @type accord_d() :: :D | ||
| @type accord_e() :: :E | ||
| @type guitar_sound:: accord_a() | accord_d() | accord_e() | silence() | ||
| @type guitar_sound() :: accord_a() | accord_d() | accord_e() | silence() | ||
| @guitar_sounds [:A, :D, :E, @silence] | ||
| def is_guitar(sound), do: sound in @guitar_sounds | ||
|
|
||
| @type kick() :: :BOOM | ||
| @type snare() :: :Doom | ||
| @type hi_hat() :: :Ts | ||
| @type drum_sound :: kick() | snare() | hi_hat() | silence() | ||
| @type drum_sound() :: kick() | snare() | hi_hat() | silence() | ||
| @drum_sound [:BOOM, :Doom, :Ts, @silence] | ||
| def is_drum(sound), do: sound in @drum_sound | ||
|
|
||
| @type sound() :: vocal_sound() | guitar_sound() | drum_sound() | ||
|
|
||
| end |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| defprotocol MyMusicBand.Player do | ||
| @moduledoc """ | ||
| Define the protocol for all musicians in the band. | ||
| """ | ||
| alias MyMusicBand.{Vocalist, Drummer, Guitarist} | ||
|
|
||
| @type player() :: Guitarist.t() | Drummer.t() | Vocalist.t() | ||
|
|
||
| @doc "Returns the next sound and the updated state" | ||
| @spec next(player()) :: {atom(), player()} | ||
| def next(player) | ||
| end | ||
|
|
||
| defimpl MyMusicBand.Player, for: Any do | ||
| alias MyMusicBand.{Vocalist, Drummer, Guitarist, Player} | ||
|
|
||
| @spec next(Player.player()) :: {atom(), Player.player()} | ||
| def next(player) do | ||
| MyMusicBand.PlayerLogic.next(player) | ||
| end | ||
| end | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| defmodule MyMusicBand.PlayerLogic do | ||
| @moduledoc """ | ||
| Contains common logic for all modules that implement Player behavior. | ||
| """ | ||
|
|
||
| alias MyMusicBand.Player | ||
|
|
||
| @spec init(struct(), list(atom), (atom() -> boolean())) :: | ||
| {:ok, Player.t()} | {:error, list({pos_integer(), atom})} | ||
| def init(struct, sounds, validator_fun) do | ||
| errors = | ||
| sounds | ||
| |> Enum.with_index(1) | ||
| |> Enum.reduce([], fn {sound, index}, acc -> | ||
| if validator_fun.(sound), do: acc, else: [{index, sound} | acc] | ||
| end) | ||
| |> Enum.reverse() | ||
|
|
||
| case errors do | ||
| [] -> {:ok, %{struct | sounds: sounds, current_stream: Stream.cycle(sounds)}} | ||
| _ -> {:error, errors} | ||
| end | ||
| end | ||
|
|
||
| @spec next(Player.t()) :: {atom, Player.t()} | ||
| def next(%{current_stream: stream} = player) do | ||
| {sound, new_stream} = get_sound(stream) | ||
| new_player = %{player | current_stream: new_stream} | ||
| {sound, new_player} | ||
| end | ||
|
|
||
| defp get_sound(stream) do | ||
| case stream |> Stream.take(1) |> Enum.to_list() do | ||
| [sound] -> | ||
| {sound, stream |> Stream.drop(1)} | ||
|
|
||
| _ -> | ||
| {nil, stream} | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| defmodule MyMusicBand.Vocalist do | ||
| alias MyMusicBand.Model.Sound | ||
| alias MyMusicBand.PlayerLogic | ||
|
|
||
| def init(arg), do: nil | ||
| @derive MyMusicBand.Player | ||
|
|
||
| def next(arg), do: nil | ||
| defstruct [:sounds, :current_stream] | ||
|
|
||
| end | ||
| @type t() :: %__MODULE__{ | ||
| sounds: [Sound.vocal_sound()], | ||
| current_stream: Enumerable.t() | ||
| } | ||
|
|
||
| @spec init([Sound.vocal_sound()]) :: {:ok, t()} | {:error, [{integer(), :atom}]} | ||
| def init(sounds) do | ||
| PlayerLogic.init(%__MODULE__{}, sounds, &Sound.is_vocal/1) | ||
| end | ||
|
|
||
| @spec next(t()) :: {atom(), t()} | ||
| def next(vocalist), do: PlayerLogic.next(vocalist) | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Доброго вечера! Прошу прощения за мои 5 копеек!
Какой смысл объявлять протокол для Any? Protocol == полиморфизм. Его суть как раз-таки в том, чтобы написать различные имплементации для одной и той же функции в зависимости от типа входного параметра.
Пример из документации https://hexdocs.pm/elixir/1.12.3/Protocol.html
Вы можете сделать импорт
PlayerLogicи будет то же самое.Вероятно, нужно посмотреть в сторону
@behaviour.Behaviour обычно и используется для абстрагирования логики, причём вызываемый модуль при помощи коллбеков
@callbackговорит вызывающему коду, как нужно работать с данным вызываемым модулем.P.S. Ещё раз прошу прощения, что влез 🙏
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Привет, никаких проблем) Можно и нужно высказываться)
Я понимаю про что Вы говорите, но в данном случае просто проба пера протокола и я тут согласен - он не нужен)
А Any и derive это просто лень =) Чтобы для каждого, опять, не писать одно и тоже, а вернее меньше одного и того же)))
Поэтому есть реализация через callback, которая мне больше нравится.