Combining guards and Boolean "and" with two arguments of tuple type
App Development2 posts305 views3 likesLast activity Jul 2020
CH
Chris_RiversOP
Jul 2020Hello Community! I’d like to return the result when both arguments are present and evaluate to ‘True’: (1) fixedRate and otherValueA, (2) floatingRate and otherValueB or (3) error.
How should this function be represented?:
setInterestRate : CalculationPeriod -> ([Text], CalculationPeriod)
setInterestRate cp
| Some fixedRate <- irp.rateSpecification.fixedRate =
([], cp { fixedRate = Some fixedRate.initialValue })
| Some otherValue <- irp.rateSpecification.otherValue =
([], cp { otherValueA = Some otherValueA.initialValue })
| Some floatingRate <- irp.rateSpecification.floatingRate =
let frDef = get "floatingRateDefinition" cp.floatingRateDefinition
frdNew = FR.calcFloatingRate existingResets floatingRate frDef
in (fst frdNew, cp { floatingRateDefinition = Some (snd frdNew) })
| Some otherValueB <- irp.rateSpecification.otherValueB =
([], cp { otherValueB = Some otherValueB.initialValue })
| otherwise = error "expecting exactly two 'rateSpecification'"BE
bernhard
Jul 2020If you want to stick with guards, you can use just combine them:
setInterestRate : CalculationPeriod -> ([Text], CalculationPeriod)
setInterestRate cp
| Some fixedRate <- irp.rateSpecification.fixedRate
, Some otherValue <- irp.rateSpecification.otherValue
= ([], cp { otherValueA = Some otherValueA.initialValue
; fixedRate = Some fixedRate.initialValue })
| Some floatingRate <- irp.rateSpecification.floatingRate
, Some otherValueB <- irp.rateSpecification.otherValueB
= let frDef = get "floatingRateDefinition" cp.floatingRateDefinition
frdNew = FR.calcFloatingRate existingResets floatingRate frDef
in (fst frdNew, cp { floatingRateDefinition = Some (snd frdNew)
; otherValueB = Some otherValueB.initialValue })
| otherwise = error "expecting exactly two 'rateSpecification'"