Skip to content

Commit

Permalink
add Cluster struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 committed May 9, 2017
1 parent 2415b61 commit 6af025a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Rock
# ROCK
[![Build Status](https://semaphoreci.com/api/v1/ayrat555/rock/branches/master/badge.svg)](https://semaphoreci.com/ayrat555/rock)

ROCK: A Robust Clustering Algorithm for Categorical Attributes
Expand Down
18 changes: 18 additions & 0 deletions lib/rock/struct/cluster.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Rock.Struct.Cluster do
defstruct points: [], size: 0

alias Rock.Struct.Cluster
alias Rock.Struct.Point

def new(points) when is_list(points) do
size = points |> Enum.count

%Cluster{points: points, size: size}
end

def add_point(%Cluster{points: points, size: size}, %Point{} = point) do
new_points = points ++ [point]

%Cluster{points: new_points, size: size + 1}
end
end
7 changes: 6 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ defmodule Rock.Mixfile do
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
deps: deps(),
elixirc_paths: elixirc_paths(Mix.env)
]
end

def application do
Expand All @@ -19,4 +21,7 @@ defmodule Rock.Mixfile do
{:credo, "~> 0.7", only: [:dev, :test]}
]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
end
23 changes: 23 additions & 0 deletions test/rock/struct/cluster_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Rock.Struct.ClusterTest do
use ExUnit.Case
alias Rock.Struct.Cluster
alias Rock.Test.TestFactory

test "adds a point to a cluster" do
point = TestFactory.from_string(:point, ["6"])
cluster = TestFactory.from_string(:cluster,
[
["1", "2", "3"],
["5"]
])

%Cluster{points: points, size: size} =
cluster
|> Cluster.add_point(point)

^size = 3
assert Enum.any?(points, fn(p) ->
p == point
end)
end
end
16 changes: 16 additions & 0 deletions test/support/test_factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Rock.Test.TestFactory do
alias Rock.Struct.Point
alias Rock.Struct.Cluster

def from_string(:cluster, string_points) do
string_points
|> Enum.map(fn(string_point) ->
from_string(:point, string_point)
end)
|> Cluster.new
end

def from_string(:point, string_attributes) do
string_attributes |> Point.new
end
end

0 comments on commit 6af025a

Please sign in to comment.