Avatar of britto

britto's solution

to Two Fer in the Elixir Track

Published at Aug 13 2018 · 6 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Two-fer or 2-fer is short for two for one. One for you and one for me.

$ elixir "One for X, one for me."
```


When X is a name or "you".

If the given name is "Dunk", the result should be "One for Dunk, one for me." If no name is given, the result should be "One for you, one for me."

## Running tests

Execute the tests with:


```bash
$ elixir two_fer_test.exs
```


### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by
commenting out the relevant `@tag :pending` with a `#` symbol.

For example:


```elixir
# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```


Or, you can enable all the tests by commenting out the
`ExUnit.configure` line in the test suite.


```elixir
# ExUnit.configure exclude: :pending, trace: true
```


For more detailed information about the Elixir track, please
see the [help page](http://exercism.io/languages/elixir).

## Source

This is an exercise to introduce users to basic programming constructs, just after Hello World. https://en.wikipedia.org/wiki/Two-fer

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

two_fer_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("two_fer.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule TwoFerTest do
  use ExUnit.Case

  test "no name given" do
    assert TwoFer.two_fer() == "One for you, one for me"
  end

  @tag :pending
  test "a name given" do
    assert TwoFer.two_fer("Gilberto Barros") == "One for Gilberto Barros, one for me"
  end

  @tag :pending
  test "when the parameter is a number" do
    assert_raise FunctionClauseError, fn ->
      TwoFer.two_fer(10)
    end
  end

  @tag :pending
  test "when the parameter is an atom" do
    assert_raise FunctionClauseError, fn ->
      TwoFer.two_fer(:bob)
    end
  end

  @tag :pending
  test "when the parameter is a charlist" do
    assert_raise FunctionClauseError, fn ->
      refute TwoFer.two_fer('Jon Snow')
    end
  end
end
defmodule TwoFer do
  @doc """
  Two-fer or 2-fer is short for two for one. One for you and one for me.
  """
  @spec two_fer(String.t()) :: String.t()
  def two_fer(name \\ "you") when is_binary(name) do
    "One for #{name}, one for me"
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of theoneandonlywoj
theoneandonlywoj
commented 342 days ago

It does not pass all the tests...

Avatar of britto
Solution Author
commented 341 days ago

Hi, @arcyfelix! Thanks for reporting. Could you please point me to the broken test so I can fix it? All tests seem to be green locally.

Avatar of theoneandonlywoj
theoneandonlywoj
commented 341 days ago

@britto test "when the parameter is an atom"

Avatar of britto
Solution Author
commented 341 days ago

Weird. It works for me both in the test and in iex.

iex> TwoFer.two_fer :abc 
** (FunctionClauseError) no function clause matching in TwoFer.two_fer/1    
    
    The following arguments were given to TwoFer.two_fer/1:
    
        # 1
        :abc
    
    two_fer.exs:6: TwoFer.two_fer/1
  test "when the parameter is an atom" do
    assert_raise FunctionClauseError, fn ->
      TwoFer.two_fer(:bob)
    end
  end
TwoFerTest
  * test when the parameter is a charlist (3.1ms)
  * test no name given (0.00ms)
  * test a name given (0.00ms)
  * test when the parameter is an atom (0.02ms)
  * test when the parameter is a number (0.01ms)


Finished in 0.03 seconds (0.03s on load, 0.00s on tests)
5 tests, 0 failures

Am I missing something? πŸ€”

Avatar of theoneandonlywoj
theoneandonlywoj
commented 340 days ago

@britto Apologies, my mistake. It works.

Avatar of britto
Solution Author
commented 339 days ago

No worries. Thanks for reviewing. πŸ™‡β€β™‚οΈ

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?