└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Functional Syntax in JS, Elm, and Haskell 2 | 3 | Elm developers often come from one of two opposite directions. 4 | 5 | 1. JavaScript programmers who know front-end web dev but want to introduce typed FP 6 | 2. Haskell programmers who know typed FP but want to use it for front-end web dev 7 | 8 | This leads to lots of comparisons of Elm functions to similar ones in both 9 | JavaScript and Haskell. Now you can have all of them in one place. 10 | 11 | ## Collections 12 | 13 | Collection data structures are core to most of programming. Here's how the 14 | various collections in Elm compare to equivalents in JavaScript and Haskell. 15 | 16 | | Purpose | JavaScript | Elm | Haskell | 17 | |-----------------------------------------|------------|---------|---------| 18 | | Random access via key - dynamic keys | Map | Dict | Map | 19 | | Random access via key - static keys | N/A | records | records | 20 | | Unique values | Set | Set | Set | 21 | | Preserve order, random access via index | Array | Array | Array | 22 | | Preserve order, no random access | N/A | List | List | 23 | 24 | ## List transformations 25 | 26 | The default collection in Elm is the list. This is a breakdown of some of the 27 | more common functional list operations. 28 | 29 | | Purpose | JavaScript | Elm | Haskell | 30 | |---------------------------------------|--------------------------|---------------|----------| 31 | | Combine all elements into a new value | `Array.prototype.reduce` | `List.foldl` | `foldl` | 32 | | Apply a function to each element | `Array.prototype.map` | `List.map` | `fmap` | 33 | | Only keep elements that match | `Array.prototype.filter` | `List.filter` | `filter` | 34 | | Get the first item | `array[0]` | `List.head` | `head` | 35 | | Get the first element that matches | `Array.prototype.find` | N/A | `find` | 36 | 37 | ## Other Common Data Structures 38 | 39 | | Purpose | JavaScript | Elm | Haskell | 40 | |-----------------|-----------------|----------------|----------| 41 | | Optional values | `null` | `Maybe` | `Maybe` | 42 | | Error handling | `try` / `catch` | `Result` | `Either` | 43 | | Async work | `Promise` | `Task` | `IO` | 44 | | Side effects | N/A | `Task` / `Cmd` | `IO` | 45 | 46 | ## Operators 47 | 48 | Haskell is notorious for its use of operators. Here's how Elm concepts line up 49 | with some of the most common Haskell operators. Most of these FP operations 50 | don't exist in JavaScript (yet). 51 | 52 | For more details on the Haskell operators, check out this [guide]. 53 | 54 | | Description | JavaScript | Elm | Haskell | 55 | |-----------------------|--------------------|---------------------------------------------|---------| 56 | | Forwards pipe | \|> (experimental) | \|> | `&` | 57 | | Backwards pipe | | <\| | `$` | 58 | | Mapping | | various `map` functions | `<$>` | 59 | | Applying | | various (third party) `andMap` functions | `<*>` | 60 | | Chaining | | various `andThen` and `concatMap` functions | `>>=` | 61 | | Appending | `+` | `++` | `<>` | 62 | | Backwards composition | | `<<` | `.` | 63 | | Forwards composition | | `>>` | | 64 | | Alternative | | various (third party) `or` functions | <\|> | 65 | 66 | [guide]: https://haskell-lang.org/tutorial/operators 67 | 68 | ## Convenience functions 69 | 70 | | Purpose | JavaScript | Elm | Haskell | 71 | |----------------------------------|------------|------------|---------| 72 | | return the argument | N/A | `identity` | `id` | 73 | | ignore the second argument | N/A | `always` | `const` | 74 | | flip the arguments of a function | N/A | `flip` | `flip` | 75 | | treat two-tuple as two args | N/A | `curry` | `curry` | 76 | --------------------------------------------------------------------------------