1// Generates src/Data/Currency.purs from Intl.supportedValuesOf('currency')
3const currencies = Intl.supportedValuesOf("currency").sort()
4const n = currencies.length
7lines.push(`module Data.Currency where`)
9lines.push(`import Prelude`)
11lines.push(`import Control.Monad.Except (throwError)`)
12lines.push(`import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), enumFromTo)`)
13lines.push(`import Data.Maybe (Maybe(..))`)
14lines.push(`import Foreign (ForeignError(..))`)
15lines.push(`import Yoga.JSON (class ReadForeign, class WriteForeign, readImpl, writeImpl)`)
19lines.push(`data Currency`)
20currencies.forEach((c, i) => {
21 const prefix = i === 0 ? " = " : " | "
22 lines.push(`${prefix}${c}`)
25lines.push(`derive instance Eq Currency`)
26lines.push(`derive instance Ord Currency`)
28lines.push(`instance Show Currency where`)
29lines.push(` show = toString`)
33lines.push(`instance Bounded Currency where`)
34lines.push(` bottom = ${currencies[0]}`)
35lines.push(` top = ${currencies[n - 1]}`)
39lines.push(`instance Enum Currency where`)
40lines.push(` succ = case _ of`)
41currencies.forEach((c, i) => {
42 if (i < n - 1) lines.push(` ${c} -> Just ${currencies[i + 1]}`)
43 else lines.push(` ${c} -> Nothing`)
45lines.push(` pred = case _ of`)
46currencies.forEach((c, i) => {
47 if (i === 0) lines.push(` ${c} -> Nothing`)
48 else lines.push(` ${c} -> Just ${currencies[i - 1]}`)
53lines.push(`instance BoundedEnum Currency where`)
54lines.push(` cardinality = Cardinality ${n}`)
55lines.push(` toEnum = case _ of`)
56currencies.forEach((c, i) => lines.push(` ${i} -> Just ${c}`))
57lines.push(` _ -> Nothing`)
58lines.push(` fromEnum = case _ of`)
59currencies.forEach((c, i) => lines.push(` ${c} -> ${i}`))
63lines.push(`toString :: Currency -> String`)
64lines.push(`toString = case _ of`)
65currencies.forEach(c => lines.push(` ${c} -> "${c}"`))
69lines.push(`fromString :: String -> Maybe Currency`)
70lines.push(`fromString = case _ of`)
71currencies.forEach(c => lines.push(` "${c}" -> Just ${c}`))
72lines.push(` _ -> Nothing`)
76lines.push(`allCurrencies :: Array Currency`)
77lines.push(`allCurrencies = enumFromTo bottom top`)
80// ReadForeign / WriteForeign
81lines.push(`instance WriteForeign Currency where`)
82lines.push(` writeImpl = toString >>> writeImpl`)
84lines.push(`instance ReadForeign Currency where`)
85lines.push(` readImpl f = do`)
86lines.push(` let parse s = case fromString s of`)
87lines.push(` Just c -> pure c`)
88lines.push(` Nothing -> throwError $ pure $ ForeignError $ "Unknown currency: " <> s`)
89lines.push(` s <- readImpl f`)
93process.stdout.write(lines.join("\n") + "\n")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list