четверг, 19 сентября 2013 г.

Functional Reactive Programming: Introduction to SynapseGrid

Functional programming paradigm opens new ways of building complex software systems. We wish to keep all operations side-effect-free and have all benefits of immutability. Also we want a way to compose functional building blocks into bigger systems that retain the same properties.

Traditional functional composition

Constructing a complex system from functions in a traditional way looks something like:
    def f3(x) = f2(f1(x))
    def g3(xs:List) = xs.map(f1).flatMap(f2)
    def h3(xs:List) = 
      for { x <- xs
            ys = f1(x)
            y <- ys
         yield f2(y)

What's wrong with it? Well, not too much. This approach works pretty good in many situations. However, what to do if we want
  1. to split the flow of data into two chains?
  2. to implement an arbitrary DataFlow processing?
  3. to modularise the construction of processing function?
The construction code becomes a bit uglier.

Builder / Runtime system separation

During the construction phase of a complex system we may tolerate mutable state, because the construction is usually single-threaded, but during runtime we want to avoid it as much as possible.
The steps looks as follows:
  1. We construct a system using an advanced DSL.
  2. Convert the system into runtime representation.
  3. Run the system on some data.
What can advanced DSL give us?
  1. Arbitrary DataFlow graph of a system can be constructed incrementally.
  2. The construction can be done in separate (/nested) modules.

System construction DSL

Of course for simple cases we wish to retain the usual DSL with maps and flatMaps. But in order to have builder/runtime separation the map and flatMap methods are replaced. Now they do not immediately execute their operation but instead defer execution to runtime.
This system can be constructed with
    val len = myContact.map( _.length )
    Input >> myContact
    len >> Output

Runtime system

The system is constructed within a mutable Builder and can be converted to static immutable system definition with the method toStaticSystem. The system definition can the be statically analyzed (converted to the above picture for instance). Or further converted to a simple function.
    val s = sb.toStaticSystem
    val fun = s.toDynamicSystem.toMapTransducer(Input, Output)The fun has type of a function and can be immediately used in other parts of the program:

More info

More examples on GitHub.

Ярлыки: , , ,

Комментарии: 2:

В 31 октября 2022 г. в 07:14 , Blogger jacquelinesaalfeld сказал(а)...

[리포트]서울지방노동청 근로감독관은 특별사법경찰로서, 노사관계에 있어서 발생한 위법 사항에 카지노 바카라 대해 수사를 할 수 있습니다. 귀하께서는 회사의 서비스를 이용하시며 발생하는 모든 개인정보보호 관련 민원을 개인정보관리책임자 혹은 담당부서로 신고하실 수 있습니다. 회사는 이용자들의 신고사항에 대해 신속하게 충분한 답변을 드릴 것입니다.

В 7 ноября 2022 г. в 07:41 , Blogger waldanqualley сказал(а)...

If you have an interest in a challenge, strive half in} King of the North and see should you can win. Regardless of what on line casino you choose, checking certain elements first at all times makes issues easy. So, here are are|listed below are} some necessary elements want to} think about for on line casino video games. No need to attend as you enter the website site}, deposit some money, and start playing. There is not any 정카지노 limit on how a lot have the ability to|you possibly can} bet; if you have what it takes, bet greater and earn more. However, that is the only portal with the lowest favorable odds, so watch out when half in} a giant hand.


Отправить комментарий

Подпишитесь на каналы Комментарии к сообщению [Atom]

<< Главная страница