Skip to content

Commit 72c9fd5

Browse files
Merge pull request #13 from Hi-Angel/fix-sytnax-highlight
2021-10-07-PureScript-React.md: Haskellize syntax highlight workaround
2 parents bd0775b + ef689f9 commit 72c9fd5

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

_posts/2021-10-07-PureScript-React.md

+21-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: single
33
title: How to write PureScript React to replace TypeScript React in 2021
44
issue-date: 2021.10.07
55
author: James Brock
6-
tags: PureScript TypeScript React
6+
tags: PureScript TypeScript React
77
excerpt: Motivation of switching TypeScript to PureScript
88
---
99

@@ -40,7 +40,7 @@ external domain-specific languages:
4040
* *TSX*
4141
* *styled-components* or *emotion* templates
4242

43-
Also, the TypeScript type system is underpowered and has too many escape
43+
Also, the TypeScript type system is underpowered and has too many escape
4444
hatches. It’s been my
4545
observation that when TypeScript programmers are passing a string to a function
4646
they will dutifully annotate it as type `:string`, but when the types get
@@ -77,7 +77,7 @@ is a very good framework choice.
7777

7878
There are many other PureScript immediate-mode GUI libraries. Most of them
7979
were written by Phil Freeman. __purescript-react-basic-hooks__ was written by
80-
Madeline Trotter, and it's the best PureScript immediate-mode GUI library.
80+
Madeline Trotter, and it's the best PureScript immediate-mode GUI library.
8181

8282
The most remarkable thing about __purescript-react-basic-hooks__ is the
8383
[`Hook`](https://pursuit.purescript.org/packages/purescript-react-basic-hooks/docs/React.Basic.Hooks#t:Hook) indexed monad.
@@ -96,7 +96,7 @@ What gives?
9696
__[Robert Porter](https://github.com/robertdp):__
9797
Calling a `Hook` in a loop is forbidden by the
9898
[*Rules of Hooks*](https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level).
99-
That is why `Hook` is an indexed monad; because
99+
That is why `Hook` is an indexed monad; because
100100
Madeline Trotter noticed that
101101
the algebraic structure of the indexed monad matches neatly with
102102
the *Rules of Hooks*, so she created the `Hook` indexed monad.
@@ -215,8 +215,8 @@ type ArtworksResponse = ArtworksData & ErrorHandling;
215215
We can use PureScript [Row Polymorphism](https://github.com/purescript/documentation/blob/master/language/Types.md#row-polymorphism) to create equivalent PureScript types
216216
with the same runtime representation.
217217

218-
[//]: # as of 2022.07.30 Jekyll highligher rouge does not support purescript. So typescript is used instead below. Adding purescript.rb in plugins will not work
219-
```typescript
218+
<!-- as of 2022.07.30 Jekyll highligher rouge does not support purescript. So haskell is used instead below. Adding purescript.rb in plugins will not work -->
219+
```haskell
220220
type ErrorHandlingRow r =
221221
( success :: Boolean
222222
, error :: Nullable { message :: String }
@@ -237,7 +237,7 @@ Suppose we have this TypeScript React component, and we want to wrap it
237237
so that we can call it from PureScript.
238238

239239
__src/Tags.tsx__
240-
```typescript
240+
```haskell
241241
export interface Props_tags {
242242
tags: [string]
243243
}
@@ -250,8 +250,8 @@ To wrap the foreign `Tags` component in PureScript,
250250
create files `Tags_.purs` and `Tags_.js`.
251251

252252
__src/Tags_.purs__
253-
[//]: # as of 2022.07.30 Jekyll highligher rouge does not support purescript. So typescript is used instead below. Adding purescript.rb in plugins will not work
254-
```typescript
253+
<!-- as of 2022.07.30 Jekyll highligher rouge does not support purescript. So haskell is used instead below. Adding purescript.rb in plugins will not work -->
254+
```haskell
255255
module Tags (tsxTags) where
256256

257257
import React.Basic (ReactComponent)
@@ -276,8 +276,8 @@ files are foreign.)
276276

277277
Then we can use the foreign component:
278278

279-
[//]: # as of 2022.07.30 Jekyll highligher rouge does not support purescript. So typescript is used instead below. Adding purescript.rb in plugins will not work
280-
```typescript
279+
<!-- as of 2022.07.30 Jekyll highligher rouge does not support purescript. So haskell is used instead below. Adding purescript.rb in plugins will not work -->
280+
```haskell
281281
import React.Basic.DOM (div_)
282282
import React.Basic.Hooks (element)
283283
import Tags (tsxTags)
@@ -288,7 +288,7 @@ div_ [ element tsxTags {tags:["one"]} ]
288288
### React diffing algorithm and `foreign import`
289289

290290
Here is more helpful advice from [Robert Porter](https://github.com/robertdp),
291-
about typeclass
291+
about typeclass
292292
constraints on foreign imports of React components. Recent versions of PureScript
293293
have [deprecated typeclass constraints on foreign import](https://github.com/purescript/purescript/pull/3829),
294294
so you probably don't have to worry about this, but here it is just in case.
@@ -370,8 +370,8 @@ so that we can `push` to a __react-router-dom__ `History` object.
370370

371371
A bit tricky, so here is [the trick](https://lobste.rs/s/wa99yt/coming_purescript_from_haskell_reflex#c_faof1j):
372372

373-
[//]: # as of 2022.07.30 Jekyll highligher rouge does not support purescript. So typescript is used instead below. Adding purescript.rb in plugins will not work
374-
```typescript
373+
<!-- as of 2022.07.30 Jekyll highligher rouge does not support purescript. So haskell is used instead below. Adding purescript.rb in plugins will not work -->
374+
```haskell
375375
import Web.DOM.Document (toNonElementParentNode)
376376
import Web.DOM.NonElementParentNode (getElementById)
377377
import Web.HTML (window)
@@ -396,8 +396,8 @@ to get the same feature.
396396

397397
Suppose we want to remove an icon after a 20-second vanishing animation.
398398

399-
[//]: # as of 2022.07.30 Jekyll highligher rouge does not support purescript. So typescript is used instead below. Adding purescript.rb in plugins will not work
400-
```typescript
399+
<!-- as of 2022.07.30 Jekyll highligher rouge does not support purescript. So haskell is used instead below. Adding purescript.rb in plugins will not work -->
400+
```haskell
401401
React.do
402402

403403
icon /\ setIcon <- useState true
@@ -439,8 +439,8 @@ The classic essay on the general problem of how to read unstructured untyped dat
439439

440440
The [`decodeJson`](https://pursuit.purescript.org/packages/purescript-argonaut/docs/Data.Argonaut#t:DecodeJson) function from __argonaut__ can infer the structure of the JSON you're expecting from the type of the data that you want to cast it to. If the structure of the JSON doesn't match the type, then it returns an error in `Left`.
441441

442-
[//]: # as of 2022.07.30 Jekyll highligher rouge does not support purescript. So typescript is used instead below. Adding purescript.rb in plugins will not work
443-
```typescript
442+
<!-- as of 2022.07.30 Jekyll highligher rouge does not support purescript. So haskell is used instead below. Adding purescript.rb in plugins will not work -->
443+
```haskell
444444
show $ do
445445
x :: Array {a::Int,b::String} <- decodeJson =<< parseJson """[{"a":2,"b":"stuff"}]"""
446446
pure x
@@ -466,12 +466,12 @@ The automatic decoding in `Simple.JSON` is based on the[`ReadForeign`](https://p
466466

467467
#### 3. F Monad
468468

469-
The most powerful and general way to read foreign data is by writing monadic parsers for the [`F` monad](https://pursuit.purescript.org/packages/purescript-foreign/docs/Foreign#t:F). You run the parser with [`runExcept`](https://pursuit.purescript.org/packages/purescript-transformers/docs/Control.Monad.Except#v:runExcept).
469+
The most powerful and general way to read foreign data is by writing monadic parsers for the [`F` monad](https://pursuit.purescript.org/packages/purescript-foreign/docs/Foreign#t:F). You run the parser with [`runExcept`](https://pursuit.purescript.org/packages/purescript-transformers/docs/Control.Monad.Except#v:runExcept).
470470

471471
If `blob :: Foreign` is a JSON object which we expect to be an array of records, each with a string field named `"thing"`, then we can parse it into PureScript with the `F` monad like this:
472472

473-
[//]: # as of 2022.07.30 Jekyll highligher rouge does not support purescript. So typescript is used instead below. Adding purescript.rb in plugins will not work
474-
```typescript
473+
<!-- as of 2022.07.30 Jekyll highligher rouge does not support purescript. So haskell is used instead below. Adding purescript.rb in plugins will not work -->
474+
```haskell
475475
import Foreign (Foreign, readArray, readString)
476476
import Foreign.Index (readProp)
477477
import Control.Monad.Except (runExcept)
@@ -571,5 +571,3 @@ __emotion__.
571571
| String interpolation | | [__interpolate__](https://pursuit.purescript.org/packages/purescript-interpolate/) |
572572
| Loader for WebPack | | [__purs-loader__](https://github.com/ethul/purs-loader) [__craco-purscript-loader__](https://github.com/andys8/craco-purescript-loader) |
573573
| State Management | [__@types/react-redux__](https://www.npmjs.com/package/@types/react-redux) | React is already a state management framework, you don’t need __redux__ or anything else. Use a State Hook instead.|
574-
575-

0 commit comments

Comments
 (0)