Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d30956
commit b2745fe
Showing
4 changed files
with
232 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,96 @@ | ||
defmodule Math.Enum do | ||
@moduledoc """ | ||
Math functions that work on any Enumerable objects, such as lists, streams, maps, sets, etc. | ||
Math.Enum defines Math-functions that work on any collection extending the Enumerable protocol. | ||
This means Maps, Lists, Sets, etc., and any custom collection types as well. | ||
""" | ||
|
||
@doc """ | ||
Calculates the product, obtained by multiplying all elements in *collection* with eachother. | ||
## Examples | ||
iex> Math.Enum.product [1,2,3] | ||
6 | ||
iex> Math.Enum.product 1..10 | ||
3628800 | ||
iex> Math.Enum.product [1,2,3,4,5, -100] | ||
-12000 | ||
""" | ||
def product(collection) | ||
|
||
# General implementation for any enumerable. | ||
def product(collection) do | ||
Enum.reduce(collection, &(&1*&2)) | ||
Enum.reduce(collection, &(&1 * &2)) | ||
end | ||
|
||
@doc """ | ||
Calculates the mean of a collection of numbers. | ||
This is the sum, divided by the amount of elements in the collection. | ||
If the collection is empty, returns `nil` | ||
Also see `Math.Enum.median/1` | ||
## Examples | ||
iex> Math.Enum.mean [1,2,3] | ||
2.0 | ||
iex> Math.Enum.mean 1..10 | ||
5.5 | ||
iex> Math.Enum.mean [1,2,3,4,5, -100] | ||
-14.166666666666666 | ||
iex> Math.Enum.mean [] | ||
nil | ||
""" | ||
@spec mean(Enum.t) :: number | ||
def mean(collection) | ||
|
||
def mean(collection) do | ||
count = Enum.count(collection) | ||
case count do | ||
0 -> nil | ||
_ -> Enum.sum(collection) / count | ||
end | ||
end | ||
|
||
@doc """ | ||
Calculates the median of a given collection of numbers. | ||
- If the collection has an odd number of elements, this will be the middle-most element of the (sorted) collection. | ||
- If the collection has an even number of elements, this will be mean of the middle-most two elements of the (sorted) collection. | ||
If the collection is empty, returns `nil` | ||
Also see `Math.Enum.mean/1` | ||
## Examples | ||
iex> Math.Enum.median [1,2,3] | ||
2 | ||
iex> Math.Enum.median 1..10 | ||
6.5 | ||
iex> Math.Enum.median [1,2,3,4,5, -100] | ||
3.5 | ||
iex> Math.Enum.median [] | ||
nil | ||
""" | ||
@spec median(Enum.t) :: number | nil | ||
def median(collection) | ||
|
||
def median(collection) do | ||
count = Enum.count(collection) | ||
cond do | ||
count == 0 -> nil | ||
rem(count, 2) == 1 -> # Middle element exists | ||
Enum.sort(collection) |> Enum.at(div(count, 2)) | ||
true -> | ||
# Take two middle-most elements. | ||
sorted_collection = Enum.sort(collection) | ||
[ | ||
Enum.at(sorted_collection, div(count, 2)), | ||
Enum.at(sorted_collection, div(count, 2) + 1) | ||
] | ||
|> Math.Enum.mean | ||
end | ||
end | ||
end |
Oops, something went wrong.