google / google/codeworld

Consider event-specific handlers for `activityOf`

Aperta
#1,244 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
discussion
Lingua principale
Haskell
Stelle
1.3k
Fork
201
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Non vengono indicati file o test. Inizia individuando activityOf e il tipo Event, quindi confronta i gestori specifici per evento proposti con il design alternativo on/onExact e l’utilizzo di esempio. Il lavoro sarebbe completato quando sarà stato deciso un design dell’API e sarà stato implementato e convalidato il comportamento risultante.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
haskell
Ambito
api, developer-experience
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.