Skip to content

Examples Transformations

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

★ = important

Select ★

#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::Select<int>([](int x) {return x * 2; })
		>> Reactive::ToSerial<int>();
}

void loop()
{
}

Cast ★

#include "ReactiveArduinoLib.h"

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

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

	observableInt
		>> Reactive::Cast<int, float>()
		>> Reactive::ToSerial<float>();
}

void loop() 
{
	delay(1000);
	observableInt = 1;
	delay(1000);
	observableInt = 2;
	delay(1000);
	observableInt = 2;
	delay(1000);
	observableInt = 3;
}

Map ★

#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::Map<int, double>([](int x) { return 2.0 * x; })
		>> Reactive::ToSerial<double>();
}

void loop()
{
}

Reduce

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange<int>(0, 10)
	>> Reactive::Reduce<int>([](int acum, int current) { return acum + current; })
	>> Reactive::ToSerial<int>();
}

void loop()
{
}

Limit

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange<int>(0, 10)
	>> Reactive::Limit<int>(2, 5)
	>> Reactive::ToSerial<int>();
}

void loop()
{
}

Scale

#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange<int>(0, 10)
	>> Reactive::Scale<int>(0, 10, 100, 200)
	>> Reactive::ToSerial<int>();
}

void loop()
{
}
#include "ReactiveArduinoLib.h"

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

	Reactive::FromRange<int>(0, 10)
	>> Reactive::Scale<int>(100)
	>> Reactive::ToSerial<int>();
}

void loop()
{
}

Timestamp (millis or micros)

#include "ReactiveArduinoLib.h"

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

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

	observableInt
		>> Reactive::Millis<int>()  // Millis() or Micros()
		>> Reactive::ToSerial<unsigned long>();
}

void loop()
{
	delay(1000);
	observableInt = 1;
	delay(1000);
	observableInt = 2;
	delay(1000);
	observableInt = 2;
	delay(1000);
	observableInt = 3;
}

Frequency

#include "ReactiveArduinoLib.h"

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

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

	observableInt
		>> Reactive::Frequency<int>()
		>> Reactive::ToSerial<float>();
}

void loop() 
{
	delay(2000);
	observableInt = 1;
}
Clone this wiki locally