Skip to content

Examples Observers

Luis Llamas edited this page Apr 25, 2019 · 11 revisions

★ = important

Do ★

#include "ReactiveArduinoLib.h"

auto observableInt = Reactive::FromProperty<int>();

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	Reactive::FromRange<int>(1, 10, 2)
	>> Reactive::Do<int>([](int x) { Serial.println(2 * x); });
}

void loop()
{
}

DoAndFinally ★

#include "ReactiveArduinoLib.h"

auto observableInt = Reactive::FromProperty<int>();

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	Reactive::FromRange<int>(1, 10, 2)
	>> Reactive::DoAndFinally<int>( [](int x) { Serial.println(2 * x); },
					[]() { Serial.println("No more items"); });
}

void loop()
{
}

Finally

#include "ReactiveArduinoLib.h"

auto observableInt = Reactive::FromProperty<int>();

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	Reactive::FromRange<int>(1, 10, 2)
	>> Reactive::Finally<int>([]() { Serial.println("No more items"); });
}

void loop()
{
}

DoNothing ★

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 }; 
size_t valuesLength = sizeof(values) / sizeof(values[0]);

auto observableArray = Reactive::FromArray<int>(values, valuesLength);

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	observableArray
		>> Reactive::If<int>([](int x) { return x > 2; }, 
				     [](int x) {Serial.println(10 * x); })
		>> Reactive::If<int>([](int x) { return x <= 2; }, 
				     [](int x) {Serial.println(5 * x); })
		>> Reactive::DoNothing<int>();
}

void loop()
{
}

Property

#include "ReactiveArduinoLib.h"

int myProperty;

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	Reactive::FromRange<int>(1, 10, 2)
	>> Reactive::ToProperty<int>(myProperty);
}

void loop()
{
}

ToArray

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 };
const size_t valuesLength = sizeof(values) / sizeof(values[0]);

int finalValues[valuesLength];

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	Reactive::FromArray<int>(values, valuesLength)
	>> Reactive::Select<int>([](int x) {return 2 * x; })
	>> Reactive::ToArray<int>(finalValues, valuesLength);

	for (size_t i = 0; i < valuesLength; i++)
		Serial.println(finalValues[i]);
}

void loop()
{
}

ToCircularBuffer

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 };
const size_t valuesLength = sizeof(values) / sizeof(values[0]);

const size_t finalValuesLength = 5;
int finalValues[finalValuesLength];

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	Reactive::FromArray<int>(values, valuesLength)
	>> Reactive::Select<int>([](int x) {return 2 * x; })
	>> Reactive::ToCircularBuffer<int>(finalValues, finalValuesLength);

	for (size_t i = 0; i < finalValuesLength; i++)
		Serial.println(finalValues[i]);
}

void loop()
{
}

ToSerial

#include "ReactiveArduinoLib.h"

int values[] = { 0, 1, 1, 2, 1, 5, 4 };
const size_t valuesLength = sizeof(values) / sizeof(values[0]);

void setup()
{
	Serial.begin(9600);
	while (!Serial) delay(1);

	Reactive::FromArray<int>(values, valuesLength)
	>> Reactive::ToSerial<int>();
}

void loop()
{
}
Clone this wiki locally