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

New concept exercise city-office (docs and typespecs) #693

Merged
merged 19 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Empty file.
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions exercises/concept/city-office/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions exercises/concept/city-office/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
match_binary-*.tar

27 changes: 27 additions & 0 deletions exercises/concept/city-office/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"blurb": "TODO",
"authors": [
{
"github_username": "angelikatyborska",
"exercism_username": "angelikatyborska"
}
],
"contributors": [
{
"github_username": "neenjaw",
"exercism_username": "neenjaw"
}
],
"files": {
"solution": [
"lib/form.ex"
],
"test": [
"test/form_test.exs"
],
"exemplar": [
".meta/exemplar.ex"
]
},
"language_versions": ">=1.10"
}
11 changes: 11 additions & 0 deletions exercises/concept/city-office/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Design

## Learning objectives

## Out of scope

## Prerequisites

## Concepts

## Analyzer
75 changes: 75 additions & 0 deletions exercises/concept/city-office/.meta/exemplar.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule Form do
@moduledoc """
A collection of loosely related functions helpful for filling out various forms at the city office.
"""

defmodule Address do
defstruct [:street, :postal_code, :city]

@type t() ::
%__MODULE__{
street: String.t(),
postal_code: String.t(),
city: String.t()
}
end

@doc """
Generates a string of a given length.

This string can be used to fill out a form field that is supposed to have no value.
Such fields cannot be left empty because a malicious third party could fill them out with false data.
"""
@spec blanks(non_neg_integer()) :: String.t()
def blanks(n) do
String.duplicate("X", n)
end

@doc """
Splits the string into a list of uppercase letters.

This is needed for form fields that don't offer a single input for the whole string,
but instead require splitting the string into a predefined number of single-letter inputs.
"""
@spec letters(String.t()) :: list(String.t())
def letters(word) do
word
|> String.upcase()
|> String.split("", trim: true)
end

@doc """
Checks if the value has no more than the maximum allowed number of letters.

This is needed for form fields that don't offer a single input for the whole string,
but instead require splitting the string into a predefined number of single-letter inputs.
"""
@spec check_length(String.t(), non_neg_integer()) :: :ok | {:error, pos_integer()}
def check_length(word, length) do
diff = String.length(word) - length

if diff <= 0 do
:ok
else
{:error, diff}
end
end

@doc """
Formats the address as an uppercase multiline string.
"""
@spec format_address(
Address.t()
| {street :: String.t(), postal_code :: String.t(), city :: String.t()}
) :: String.t()
def format_address(%Address{} = address) do
format_address({address.street, address.postal_code, address.city})
end

def format_address({street, postal_code, city}) do
"""
#{String.upcase(street)}
#{String.upcase(postal_code)} #{String.upcase(city)}
"""
end
end
36 changes: 36 additions & 0 deletions exercises/concept/city-office/lib/form.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Form do
defmodule Address do
defstruct [:street, :postal_code, :city]
end

def blanks(n) do
String.duplicate("X", n)
end

def letters(word) do
word
|> String.upcase()
|> String.split("", trim: true)
end

def check_length(word, length) do
diff = String.length(word) - length

if diff <= 0 do
:ok
else
{:error, diff}
end
end

def format_address(%Address{} = address) do
format_address({address.street, address.postal_code, address.city})
end

def format_address({street, postal_code, city}) do
"""
#{String.upcase(street)}
#{String.upcase(postal_code)} #{String.upcase(city)}
"""
end
end
28 changes: 28 additions & 0 deletions exercises/concept/city-office/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Form.MixProject do
use Mix.Project

def project do
[
app: :city_office,
version: "0.1.0",
# elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
Loading