Chapter 1: FRP is like Spreadsheet
Another familiar example of something fundamentally designed to keep its versions perfectly synchronized is the spreadsheet.
And the FRP library being introduced here, Timeline , will be easier to understand if this spreadsheet principle is kept in mind.
Timeline provides binary operations to utilize the state management
Section titled βTimeline provides binary operations to utilize the state managementβIn Functional Programming, everything is an expression or operation. Accordingly, Timeline library provides binary operations for the reactive state management .
This binary operation corresponds to an operation in spreadsheet apps.
π Functions
Section titled βπ Functionsββ Function to initialize Timeline<'a>
Section titled ββ Function to initialize Timeline<'a>βTimeline
Section titled βTimelineβ'a -> Timeline<'a>
let counter = Timeline 0
Consider the Timeline
as a specific container for a value, similar to a Cell in spreadsheet apps.
β‘ Functions for the binary operations
Section titled ββ‘ Functions for the binary operationsβ('a -> 'b) -> (Timeline<'a> -> Timeline<'b>)
TL.bind
Section titled βTL.bindβ('a -> Timeline<'b>) -> (Timeline<'a> -> Timeline<'b>)
When the binary operator: TL.map
,
let double = fun a -> a * 2
let timelineA = Timeline 1
let timelineB = timelineA |> TL.map double
log (timelineB |> TL.at Now)// 2
This code for the binary operation simply corresponds to the basic usage of spreadsheet apps
This is the identical structure of:
let double = a => a * 2;
let listA = [1];
let listB = listA.map(double);
console.log(listB);// [2]
We could recognize the array [2]
is identical to the Cell and Value 2
of a spreadsheet; however, the spreadsheet and Timeline maintain a double
relationship as values change over the timeline .
β’ Function to update Timeline<'a>
Section titled ββ’ Function to update Timeline<'a>βTL.next
Section titled βTL.nextβ'a -> Timeline<'a> -> unit
let timelineA = Timeline 1
timelineA |> TL.next 3
log (timelineA |> TL.at Now)// 3
β β‘β’ action of Timeline<'a>
Section titled ββ β‘β’ action of Timeline<'a>βThe update to timelineA
will trigger a reactive update of timelineB
according to the rule defined by the binary operation.
let double = fun a -> a * 2
// β initialize timelineAlet timelineA = Timeline 1
// confirm the lastVal of timelineAlog (timelineA |> TL.at Now)// 1
// β‘ the binary operationlet timelineB = timelineA |> TL.map double
// confirm the lastVal of timelineBlog (timelineB |> TL.at Now)// 2
//=====================================// β’ update the lastVal of timelineAtimelineA|> TL.next 3// update to timelineA will trigger// a reactive update of timelineB
// confirm the lastVal of timelineA & timelineBlog (timelineA |> TL.at Now)// 3log (timelineB |> TL.at Now)// 6