Consider event-specific handlers for `activityOf`
- Vorherrschende Sprache
- Haskell
- Sterne
- 1.3k
- Forks
- 201
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
Inspired by @alphalambda's discussion of handling state at NY Haskell.
We could add these to CodeWorld:
``` haskell
onKeyPress :: (Text, state -> state) -> ((state, Event) -> state)
onAnyKeyPress :: ((state, Text) -> state) -> ((state, Event) -> state)
onKeyRelease :: (Text, state -> state) -> ((state, Event) -> state)
onAnyKeyRelease :: ((state, Text) -> state) -> ((state, Event) -> state)
onPointerPress :: ((state, Point) -> state) -> ((state, Event) -> state)
onPointerRelease :: ((state, Point) -> state) -> ((state, Event) -> state)
onPointerMovement :: ((state, Point) -> state) -> ((state, Event) -> state)
onTimePassing :: ((state, Number) -> state) -> ((state, Event) -> state)
onTextEntry :: ((state, Text) -> state) -> ((state, Event) -> state)
onAll :: [(state, Event) -> state] -> ((state, Event) -> state)
```
Implementation is trivial. Sample usage like this:
``` haskell
program = activityOf(initial, change, picture)
change = onAll([
onKeyPress("Enter", fire),
onKeyPress("W", moveUp),
onKeyPress("A", moveLeft),
onKeyPress("S", moveDown),
onKeyPress("D", moveRight),
onTimePassing(time)
])
```
Advantages:
* Encourages students to break down event handling with functions.
* Autocomplete with `on` prefix is powerful
Disadvantages:
* This is a kind of magic that applies to one specific function. Doesn't apply to functions in general.
I initially thought about something like, where the first parameter is the constructor:
``` haskell
on :: (a -> Event, (state, a) -> state) -> ((state, Event) -> state)
onExact :: (Event, state -> state) -> ((state, Event) -> state)
```
However, `on` isn't cleanly implementable. I suppose one could do something like:
``` haskell
on(constructor, f)(state, ev) = case (constructor undefined, ev) of
(PointerPress _, PointerPress pt) -> f(state, pt)
(PointerRelease _, PointerRelease pt) -> f(state, pt)
...
```
but that feels very hacky. The function will accept a bunch of non-constructor-like functions that look at their argument, and will crash at runtime. Maybe that's okay?
Beitragsleitfaden
Rechercherichtung
Es werden keine Dateien oder Tests genannt. Beginne damit, activityOf und den Typ Event zu finden, und vergleiche dann die vorgeschlagenen ereignisspezifischen Handler mit dem alternativen on/onExact-Design und der Beispielverwendung. Als abgeschlossen würde gelten, wenn ein API-Design festgelegt sowie das resultierende Verhalten implementiert und validiert wurde.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- haskell
- Bereich
- api, developer-experience
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100