Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional Event Driven Programming

I'm having trouble writing event driven GUI code in a functional style, using Clojure and Seesaw. Specifically, I can't figure out how to pass the state of the program around without using globals, or some other unpleasant hack. My current approach is something like this:

(defn event-handler [gui-state event]
   (update-gui! (get-new-state gui-state event)))

(defn update-gui! [gui-state]
   (remove-all-listeners (gui-state :button))
   (seesaw.core/listen (gui-state :button)
                       :action
                       (partial event-handler gui-state)))

It sets an event listener on the relevant component, with a partially applied function to advance the state and update the gui, including removing the old listener. Although this seems to be working, I don't really like it, partly because I can't pass the listener itself in the state (since it's not constructed until after I've already defined the state), so removing the old listener requires removing all listeners, which could cause problems as the program grows.

The closest solution I've found online is in this answer, but I don't know how to handle events as a stream like it shows. I'm sure there must be a better solution than my current approach, but I can't figure out what.

Can anyone show me how I can respond to user input events while still following a functional style?

like image 275
resueman Avatar asked Nov 14 '14 01:11

resueman


People also ask

What are event-driven functions?

Event-driven functions such as Background Functions and CloudEvent Functions can be used when Cloud Functions are invoked indirectly in response to an event, such as a message on a Pub/Sub topic, a change in a Cloud Storage bucket, or a Firebase event.

What is an event in functional programming?

Events—Simple, immutable data objects capturing what happened. States—Data objects representing the state of an entity at a certain point in time. State transitions—Functions that take a state and an event, and produce a new state.

What is event-driven programming and how it is different from functional programming?

Unsourced material may be challenged and removed. In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or message passing from other programs or threads.

What is an example of event-driven programming?

An event-driven application is designed to detect events as they occur, and then deal with them using some event-handling procedure. Examples of events include: An HTML message has been received (web server) A key has been pressed (text editor)


1 Answers

The Streams from the linked answer seem like an analog of core.async channels.

Instead of removing all listeners each event maybe pass in a channel that has event details put to it. The same channel should go to the button's logic handler where it will repeatedly be taken from.

like image 101
A Sammich Avatar answered Oct 15 '22 20:10

A Sammich