Resettable battery behavior?

I'm trying to wrap my head around FRP and I'm not sure if I'm doing it right. I want to create a string from keypress events prior to pressing enter. After pressing a key, the string will be written out and the accumulator will be reset to an empty string.

I have an event source that emits Char

every time you press a key on the keyboard ePressed

. First, I share two types of presses that excite me:

eWritable = filterE (`elem` ['A'..'z']) ePressed
eEnter = filterE (== '\n') ePressed

      

Now I know how to put them together into what I want to send:

eToPrint = accumE "" (fmap (:) eWritable)

      

But I'm not sure how to "hold" this until a key is pressed, or how to reset after that. What's the correct, idotic way to do this?

+3


source to share


1 answer


The idea is, which eToPrint

is a combination of two events: when you press characters and when you press enter. Here's an example (0.8 reactive banana):

eToPrint = accumE "" $ unions [(:) <$> eWritable, const "" <$> eEnter]

      

To "hold" it, you can use Behavior

.




Here's the complete solution:

bString = accumB "" $ unions [(:) <$> eWritable, const "" <$> eEnter]
eOut    = bString <@ eEnter

      

The behavior bString

contains the accumulated value String

. The event eOut

returns the last string value whenever the event occurs eEnter

. Note, in particular, the semantics accumB

: At the point in time that eEnter

occurs, the value bString

is still the old value.

+2


source







All Articles