Skip to content

Commit

Permalink
fixed mistake in duc
Browse files Browse the repository at this point in the history
  • Loading branch information
m1dnight committed Jun 6, 2018
1 parent 9493732 commit 8ddf653
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/obs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ defmodule Observables.Obs do
Take,
CombineLatest,
CombineLatestSilent,
Delay
Delay,
DistinctUntilChanged
}

alias Enum
Expand Down Expand Up @@ -185,7 +186,7 @@ defmodule Observables.Obs do
More information: http://reactivex.io/documentation/operators/distinct.html and http://rxmarbles.com/#distinctUntilChanged
"""
def distinctuntilchanged(observable, f \\ fn x, y -> x == y end) do
{:ok, pid} = GenObservable.start_link(Distinct, [f])
{:ok, pid} = GenObservable.start_link(DistinctUntilChanged, [f])

GenObservable.send_to(observable, pid)

Expand Down
10 changes: 5 additions & 5 deletions lib/observables/distinct_until_changed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ defmodule Observables.Operator.DistinctUntilChanged do

def init([comparator]) do
Logger.debug("Distinct Until Changed: #{inspect(self())}")
{:ok, %{:comp => comparator, :seen => []}}
{:ok, %{:comp => comparator, :last => []}}
end

def handle_event(v, state = %{:comp => f, :seen => xs}) do
seen? = Enum.any?(xs, fn seen -> f.(v, seen) end)
def handle_event(v, state = %{:comp => f, :last => last}) do
same? = f.(v, last)

if not seen? do
{:value, v, %{:comp => f, :seen => []}}
if not same? do
{:value, v, %{:comp => f, :last => last}}
else
{:novalue, state}
end
Expand Down
13 changes: 4 additions & 9 deletions test/distinctuntilchanged_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ defmodule DistinctUntilChangedTest do
test "distinctuntilchanged" do
testproc = self()

xs = [1, 1, 2, 2, 3, 3]
xs = [1,2,3,4,1,2,3,4]

expected = [1, 2, 3]
expected = [1,2,3,4,1,2,3,4]

xs
|> Obs.from_enum(100)
|> Obs.distinctuntilchanged()
|> Obs.inspect()
|> Obs.map(fn v -> send(testproc, v) end)

expected
|> Enum.map(fn x ->
assert_receive(^x, 1000, "did not get this message!")

receive do
^x -> assert "duplicates" == ""
after
1000 -> :ok
end
assert_receive(^x, 1000, "did not get this message! #{inspect x}")
end)

receive do
Expand Down

0 comments on commit 8ddf653

Please sign in to comment.