Skip to content

Commit 796aaf2

Browse files
committed
Updated Library.properties to library.json
Distinct -> DistunctUntilChanged Added Distinct operator
1 parent 3c9a527 commit 796aaf2

File tree

8 files changed

+105
-29
lines changed

8 files changed

+105
-29
lines changed

keywords.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.

library.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
3+
"name": "ReactiveArduino",
4+
"description": "ReactiveArduino implements observable-observer pattern on a processor like Arduino",
5+
"keywords": [
6+
"Reactive",
7+
"Arduino",
8+
"Observer",
9+
"Observable",
10+
"Event"
11+
],
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/rzeldent/Arduino-ReactiveArduino"
15+
},
16+
"frameworks": [
17+
"arduino"
18+
],
19+
"platforms": "*",
20+
"build": {
21+
"srcDir": "src/",
22+
"includeDir": "include/"
23+
},
24+
"version": "2.0.0",
25+
"authors": [
26+
{
27+
"name": "Luis Llamas",
28+
"maintainer": true
29+
},
30+
{
31+
"name": "Rene Zeldenthuis",
32+
"maintainer": true
33+
}
34+
35+
],
36+
"category": "Other"
37+
}

library.properties

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Operators/OperatorDistinct.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class OperatorDistinct : public Operator<T, T>
1919
void OnNext(T value) override;
2020

2121
private:
22-
T _last = T();
22+
std::set<T> _seen;
2323
bool _any = false;
2424
};
2525

@@ -31,10 +31,12 @@ OperatorDistinct<T>::OperatorDistinct()
3131
template <typename T>
3232
void OperatorDistinct<T>::OnNext(T value)
3333
{
34-
if (!_any || (_any && _last != value))
34+
if (!_any || (_any && _seen.find(value) == _seen.end()))
35+
{
36+
_seen.insert(value);
3537
this->_childObservers.OnNext(value);
38+
}
3639

37-
_last = value;
3840
_any = true;
3941
}
4042

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************
2+
Copyright (c) 2019 Luis Llamas
3+
(www.luisllamas.es)
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License
8+
****************************************************/
9+
10+
#ifndef _REACTIVEOPERATORDISTINCTUNTILCHANGED_h
11+
#define _REACTIVEOPERATORDISTINCTUNTILCHANGED_h
12+
13+
template <typename T>
14+
class OperatorDistinctUntilChanged : public Operator<T, T>
15+
{
16+
public:
17+
OperatorDistinctUntilChanged();
18+
19+
void OnNext(T value) override;
20+
21+
private:
22+
T _last = T();
23+
bool _any = false;
24+
};
25+
26+
template <typename T>
27+
OperatorDistinctUntilChanged<T>::OperatorDistinctUntilChanged()
28+
{
29+
}
30+
31+
template <typename T>
32+
void OperatorDistinctUntilChanged<T>::OnNext(T value)
33+
{
34+
if (!_any || (_any && _last != value))
35+
{
36+
_last = value;
37+
this->_childObservers.OnNext(value);
38+
}
39+
40+
_any = true;
41+
}
42+
43+
#endif

src/Operators/Operators.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Unless required by applicable law or agreed to in writing, software distributed
1212

1313
#include "OperatorWhere.h"
1414
#include "OperatorDistinct.h"
15+
#include "OperatorDistinctUntilChanged.h"
1516

1617
#include "OperatorLast.h"
1718
#include "OperatorFirst.h"

src/ReactiveArduinoCore.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ template <typename T> class FilterIsZero;
5050

5151
template <typename T> class OperatorWhere;
5252
template <typename T> class OperatorDistinct;
53+
template <typename T> class OperatorDistinctUntilChanged;
5354
template <typename T> class OperatorLast;
5455
template <typename T> class OperatorFirst;
5556
template <typename T> class OperatorTake;
@@ -154,6 +155,7 @@ class Observable : IObservable<T>, IResetable<T>
154155
// "Fluent" behavior
155156
OperatorWhere<T>& Where(ReactivePredicate<T> condition);
156157
OperatorDistinct<T>& Distinct();
158+
OperatorDistinctUntilChanged<T>& DistinctUntilChanged();
157159
OperatorFirst<T>& First();
158160
OperatorLast<T>& Last();
159161
OperatorSkip<T>& Skip(size_t num);
@@ -266,6 +268,14 @@ auto Observable<T>::Distinct() -> OperatorDistinct<T>&
266268
return *newOp;
267269
}
268270

271+
template <typename T>
272+
auto Observable<T>::DistinctUntilChanged() -> OperatorDistinctUntilChanged<T>&
273+
{
274+
auto newOp = new OperatorDistinctUntilChanged<T>();
275+
Compound(*this, *newOp);
276+
return *newOp;
277+
}
278+
269279
template <typename T>
270280
auto Observable<T>::First() -> OperatorFirst<T>&
271281
{

src/ReactiveArduinoLib.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Copyright (c) 2019 Luis Llamas
55
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66
77
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License
8-
****************************************************/
8+
****************************************************/
99

1010
#ifndef _REACTIVEARDUINOLIB_h
1111
#define _REACTIVEARDUINOLIB_h
@@ -18,6 +18,8 @@ Unless required by applicable law or agreed to in writing, software distributed
1818
#include "WProgram.h"
1919
#endif
2020

21+
#include <set>
22+
2123
namespace Reactive
2224
{
2325
#include "ReactiveArduinoCore.h"
@@ -223,6 +225,12 @@ namespace Reactive
223225
return *(new OperatorDistinct<T>());
224226
}
225227

228+
template <typename T>
229+
OperatorDistinctUntilChanged<T>& DistinctUntilChanged()
230+
{
231+
return *(new OperatorDistinctUntilChanged<T>());
232+
}
233+
226234
template <typename T>
227235
OperatorFirst<T>& First()
228236
{

0 commit comments

Comments
 (0)