Skip to content

Commit

Permalink
Add examples for how to customize an engine customization using featu…
Browse files Browse the repository at this point in the history
…re flags
  • Loading branch information
zcohan committed Mar 21, 2020
1 parent 147cd87 commit 48922b0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions MacSample/MacSample/SoulverCoreExamples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SoulverCoreExamples {
SoulverCoreExamples().showingAnAnswerTo2dp()
SoulverCoreExamples().creatingACustomUnit()
SoulverCoreExamples().usingAEuropeanLocale()
SoulverCoreExamples().disablingBracketComments()
SoulverCoreExamples().customizingHowAmbiguousExpressionsAreHandled()

// Multi-line examples
SoulverCoreExamples().simpleMultiLineCalculation()
Expand Down Expand Up @@ -151,6 +153,46 @@ class SoulverCoreExamples {

}

func disablingBracketComments() {

// An engine customization includes a list of feature flags that can be toggled to change calculator behaviour

// SoulverCore has a feature called bracket comments, which instructs the calculator to ignore single numbers in brackets.

// If we want to return to a more traditional evaluation style, we need to set the feature flags property on the customization

var flags = EngineFeatureFlags()
flags.bracketComments = false

var customization: EngineCustomization = .standard
customization.featureFlags = flags

let calculator = Calculator(customization: customization)
let result = calculator.calculate("5 (10)")
print(result.stringValue) // 50
}



func customizingHowAmbiguousExpressionsAreHandled() {

// When faced with an ambiguous expression, like "123 456", SoulverCore will (by default) select the last number as the answer.

// If you prefer to have no answer in ambiguous cases like these, there's a feature flag for that

var flags = EngineFeatureFlags()
flags.inAmbiguityPreferSomethingToNothing = false

var customization: EngineCustomization = .standard
customization.featureFlags = flags

let calculator = Calculator(customization: customization)
let result = calculator.calculate("123 456")

print(result.isEmptyResult) // true
print(result.stringValue) // empty string

}


// MARK: - Multi-Line Calculations
Expand Down

0 comments on commit 48922b0

Please sign in to comment.