Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Converting the array of values into the record

$
0
0

I would create default records and reduce the array:

let defaultSettings = {
  startDate: "2025-08-24",
  untilDate: "2025-08-25",
  defaultCurrency: Currency("EUR"),
  rates: Map.make(),
}

let defaultLedger = {
  settings: defaultSettings,
}

let make: array<ParserResult.t> => t = lines =>
  lines->Array.reduce(defaultLedger, (ledger, parserResult) => {
    let settings = switch parserResult {
    | Setting(DefaultCurrency(defaultCurrency)) => {...ledger.settings, defaultCurrency}
 // | others
    | _ => ledger.settings
    }

    {...ledger, settings}
  })

Playground example

You might want to make some fields optional instead:

type settings = {
  startDate?: string,
  untilDate?: string,
  defaultCurrency: currency,
  rates: Map.t<currency, float>,
}

Of course this comes at the cost of worse DX but you get the compiler to check if the dates are not just dummy ones.


Viewing all articles
Browse latest Browse all 2592

Trending Articles