Skip to content

jerometerry/potassium

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

alt text Potassium

Potassium is a Functional Rective Programming (FRP) Library for .Net, based on Sodium.

FRP was introduced by Conal Elliott and Paul Hudak in the ICFP 97 paper Functional Reactive Animation (Fran).

Conal Elliott's answer to What is (Functional) Reactive Programming?' is a great resource for learning about FRP.

Potassium API documentation is located here.

Installing

Potassium is available for download on NuGet

To install Potassium, run the following command in the Package Manager Console

Install-Package Potassium

Overview

At the heart of FRP are Events and Behaviors.

Events should be familiar to programmers who've done any sort of Event Driven Programming. Mouse clicks, key presses, etc., are examples of Events. Event is technically an event stream of discrete occurrences.

The type signagure for an Event of integers is Event<int>.

Behaviors are simply time varying values. Put another way, a Behavior is a reactive value. Behavior is simply a container for a value, where the value gets updated by firings of an Event.

The type signature for a Behavior of integers is Behavior<int>.

Here's an example illustrating the use of Events and Behaviors. The values 1 through 10 are fired through evt, while values 0 through 10 are observed.

	FirableEvent<int> evt = new FirableEvent<int>();
	Behavior<int> beh = evt.Hold(0);
	Event<int> vals = beh.ToEventWithInitialFire();
	ISubscription<int> s = vals.Subscribe((v) => {
		Console.WriteLine("{0}", v);
	});
	for (int i = 1; i <= 10; i++) {
		evt.Fire(i);
	}

0 is written to the console because the Behavior.ToEventWithInitialFire() generates an Event that, on subscription, fires the value of the Behavior.

The operations on Events and Behaviors are what make FRP interesting.

Take Map for example. Below is an example that doubles the fired values.

	FirableEvent<int> evt = new FirableEvent<int>();
	Event<int> timesTwo = evt.Map(v => 2*v);
	ISubscription<int> s = timesTwo.Subscribe((v) => {
		Console.WriteLine("{0}", v);
	});
	for (int i = 1; i <= 10; i++) {
		evt.Fire(i);
	}