Skip to content
This repository has been archived by the owner on May 14, 2018. It is now read-only.

Commit

Permalink
Problem: range variables 'a' and 'b' are hard to understand
Browse files Browse the repository at this point in the history
Solution: refactor them into a single 'range' variable
  • Loading branch information
c-rack committed Dec 7, 2015
1 parent 3dc97d0 commit 1daf3cd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
10 changes: 5 additions & 5 deletions lib/quantum/executor.ex
Expand Up @@ -31,11 +31,11 @@ defmodule Quantum.Executor do
[m, h, d, n, w] = e |> String.split(" ")
{_, cur_mon, cur_day} = state.d
cond do
!match(m, state.m, 0, 59) -> false
!match(h, state.h, 0, 23) -> false
!match(d, cur_day, 1, 31) -> false
!match(n, cur_mon, 1, 12) -> false
!match(w, state.w, 0, 6) -> false
!match(m, state.m, 0..59) -> false
!match(h, state.h, 0..23) -> false
!match(d, cur_day, 1..31) -> false
!match(n, cur_mon, 1..12) -> false
!match(w, state.w, 0..6) -> false
true -> execute_fun(fun, args)
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/quantum/matcher.ex
Expand Up @@ -4,20 +4,20 @@ defmodule Quantum.Matcher do

alias Quantum.Parser

def match("*", _, _, _) do
def match("*", _, _) do
true
end

def match(e, v, a, b) do
do_match(e |> String.split(","), v, a, b)
def match(e, v, range) do
e |> String.split(",") |> do_match(v, range)
end

defp do_match([], _, _, _) do
defp do_match([], _, _) do
false
end

defp do_match([e|t], v, a, b) do
Enum.any?(Parser.parse(e, a, b), &(&1 == v)) or do_match(t, v, a, b)
defp do_match([e|t], v, range) do
Enum.any?(Parser.parse(e, range), &(&1 == v)) or do_match(t, v, range)
end

end
20 changes: 10 additions & 10 deletions lib/quantum/parser.ex
Expand Up @@ -2,26 +2,26 @@ defmodule Quantum.Parser do

@moduledoc false

def parse("*/" <> i, a, b) do
a..b |> only_multiplier_of(i)
def parse("*/" <> i, range) do
range |> only_multiplier_of(i)
end

def parse(e, a, b) do
def parse(e, range) do
[r|i] = e |> String.split("/")
[x|y] = r |> String.split("-")
x
|> String.to_integer
|> do_parse(y, i, a, b)
|> Enum.filter(&(&1 in a..b))
|> do_parse(y, i, range)
|> Enum.filter(&(&1 in range))
end

defp do_parse(v, [], [], _, _), do: [v]
defp do_parse(v, [], [], _), do: [v]

defp do_parse(v, [], [i], _, _) do
defp do_parse(v, [], [i], _) do
[rem(v, i |> String.to_integer)]
end

defp do_parse(v, [y], [], a, b) do
defp do_parse(v, [y], [], a..b) do
t = y |> String.to_integer
if v < t do
Enum.to_list(v..t)
Expand All @@ -30,8 +30,8 @@ defmodule Quantum.Parser do
end
end

defp do_parse(v, y, [i], a, b) do
v |> do_parse(y, [], a, b) |> only_multiplier_of(i)
defp do_parse(v, y, [i], range) do
v |> do_parse(y, [], range) |> only_multiplier_of(i)
end

defp only_multiplier_of(coll, i) do
Expand Down
18 changes: 9 additions & 9 deletions test/matcher_test.exs
Expand Up @@ -4,18 +4,18 @@ defmodule Quantum.MatcherTest do
import Quantum.Matcher

test "should always match" do
assert match("*", nil, nil, nil) == true
assert match("*", nil, 0..59) == true
end

test "should match list" do
assert match("1,2", 1, 0, 59) == true
assert match("1,2", 2, 0, 59) == true
assert match("1,2", 3, 0, 59) == false
assert match("*/20,30", 0, 0, 59) == true
assert match("*/20,30", 20, 0, 59) == true
assert match("*/20,30", 30, 0, 59) == true
assert match("*/20,30", 40, 0, 59) == true
assert match("*/20,30", 50, 0, 59) == false
assert match("1,2", 1, 0..59) == true
assert match("1,2", 2, 0..59) == true
assert match("1,2", 3, 0..59) == false
assert match("*/20,30", 0, 0..59) == true
assert match("*/20,30", 20, 0..59) == true
assert match("*/20,30", 30, 0..59) == true
assert match("*/20,30", 40, 0..59) == true
assert match("*/20,30", 50, 0..59) == false
end

end
12 changes: 6 additions & 6 deletions test/parser_test.exs
Expand Up @@ -4,12 +4,12 @@ defmodule Quantum.ParserTest do
import Quantum.Parser

test "parse" do
assert parse("*/20", 0, 59) == [0, 20, 40]
assert parse("0/20", 0, 59) == [0]
assert parse("15-45/5", 0, 59) == [15, 20, 25, 30, 35, 40, 45]
assert parse("10-15", 0, 59) == [10, 11, 12, 13, 14, 15]
assert parse("55-100", 0, 59) == [55, 56, 57, 58, 59]
assert parse("55-5", 0, 59) == [0, 1, 2, 3, 4, 5, 55, 56, 57, 58, 59]
assert parse("*/20", 0..59) == [0, 20, 40]
assert parse("0/20", 0..59) == [0]
assert parse("15-45/5", 0..59) == [15, 20, 25, 30, 35, 40, 45]
assert parse("10-15", 0..59) == [10, 11, 12, 13, 14, 15]
assert parse("55-100", 0..59) == [55, 56, 57, 58, 59]
assert parse("55-5", 0..59) == [0, 1, 2, 3, 4, 5, 55, 56, 57, 58, 59]
end

end

0 comments on commit 1daf3cd

Please sign in to comment.