Combining guards and Boolean "and" with two arguments of tuple type

Hello 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'"
1 Like

If 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'"
2 Likes