pselm / signals

Purescript implementation of Elm 0.16's signals modules
Other
91 stars 2 forks source link

Tips for converting Javascript to Purescript #7

Open rgrempel opened 8 years ago

rgrempel commented 8 years ago

In my README, I've been writing down a few tips for converting from Elm to Purescript.

Lately, I've actually been converting a fair bit of Javascript as well (from the parts of Elm implemented in Javascript). So, I should really start documenting some tips for doing that.

For instance, I have a few ideas about dealing with mutability, and could give some examples. E.g., I'd approach the possibilities in about this order (from most attractive to least attractive):

  1. See if you can trivially rewrite without mutability ... often possible.
  2. See if you can use a fold of some kind to keep some state updated.
  3. Consider using recursion ... sometimes you can recurse in situations that you might otherwise mutate.
  4. If the mutation is purely local (i.e. you mutate, but don't need to keep the mutated state), then consider runST, newSTRef etc.
  5. If the mutation occurs across multiple functions, but the mutation takes place inside a larger operation using those functions that doesn't escape, consider using StateT etc. Talk about how this is sort of similar to generating and using closures.
  6. If the mutation is truly chaotic, and references need to be passed around like spaghetti, then try newRef etc.

There are things that could also probably be said about converting object-oriented Javascript code to Purescript.