Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:25:43+00:00 2026-05-26T10:25:43+00:00

I have functions A => Double . I want to check whether two such

  • 0

I have functions A => Double. I want to check whether two such functions give the same results (up to a tolerance, using the existing beCloseTo matcher) for a given set of values.

I want to be able to write:

type TF = A => Double
(f: TF) must computeSameResultsAs(g: TF,tolerance: Double, tests: Set[A])

I want to build this matcher in a modular way, not simply writing a Matcher[TF] from scratch.

It might be even nicer if I could write:

(f: TF) must computeSameResultsAs(g: TF)
               .withTolerance(tolerance)
               .onValues(tests: Set[A])

Also I want to get a reasonable description when the matcher fails.

Edit

After sleeping over it I came up with the following.

def computeSameResultsAs[A](ref: A => Double, tolerance: Double, args: Set[A]): Matcher[A => Double] = 
  args.map(beCloseOnArg(ref, tolerance, _)).reduce(_ and _)

def beCloseOnArg[A](ref: A => Double, tolerance: Double, arg: A): Matcher[A => Double] = 
  closeTo(ref(arg), tolerance) ^^ ((_: A => Double).apply(arg))

This is much shorter than Eric’s solution but doesn’t provide a good failure message. What I’d love to be able is rename the mapped value in the second method. Something like the following (which does not compile).

def beCloseOnArg[A](ref: A => Double, tolerance: Double, arg: A): Matcher[A => Double] = 
  closeTo(ref(arg), tolerance) ^^ ((_: A => Double).apply(arg) aka "result on argument " + arg)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:25:44+00:00Added an answer on May 26, 2026 at 10:25 am

    If you want to write things with the second version you need to create a new Matcher class encapsulating the functionality of the beCloseTo matcher:

    def computeSameResultsAs[A](g: A => Double, 
                                tolerance: Double = 0.0, 
                                values: Seq[A] = Seq()) = TFMatcher(g, tolerance, values)
    
    case class TFMatcher[A](g: A => Double, 
                            tolerance: Double = 0.0, 
                            values: Seq[A] = Seq()) extends Matcher[A => Double] {
    
      def apply[S <: A => Double](f: Expectable[S]) = {
        // see definition below
      }
    
      def withTolerance(t: Double) = TFMatcher(g, t, values)
      def onValues(tests: A*) = TFMatcher(g, tolerance, tests)
    }
    

    This class allows to use the syntax you’re after:

    val f = (i: Int) => i.toDouble
    val g = (i: Int) => i.toDouble + 0.1
    
    "f must be close to another similar function with a tolerance" in {
      f must computeSameResultsAs[Int](g).withTolerance(0.5).onValues(1, 2, 3)          
    }
    

    Now, let’s see how to reuse the beCloseTo matcher in the apply method:

    def apply[S <: A => Double](f: Expectable[S]) = {
      val res = ((v: A) => beCloseTo(g(v) +/- tolerance).apply(theValue(f.value(v)))).forall(values)
    
      val message = "f is "+(if (res.isSuccess) "" else "not ")+
                    "close to g with a tolerance of "+tolerance+" "+
                    "on values "+values.mkString(",")+": "+res.message
       result(res.isSuccess, message, message, f)
     }
    

    In the code above, we apply a function returning a MatcherResult to a sequence of values:

    ((v: A) => beCloseTo(g(v) +/- tolerance).apply(theValue(f.value(v)))).forall(values)
    

    Note that:

    1. f is an Expectable[A => Double] so we need to take its actual value to be able to use it

    2. similarly we can only apply an Expectable[T] to a Matcher[T] so we need to use the method theValue to transform f.value(v) to an Expectable[Double] (from the MustExpectations trait)

    Finally, we when have the result of the forall matching, we can customize the result messages by using:

    1. the inherited result method building a MatchResult (what the apply method of any Matcher should return

    2. passing it a boolean saying if the execution of beCloseTo was successful: .isSuccess

    3. passing it nicely formatted “ok” and “ko” messages, based on the input and on the result message of the beCloseTo matching

    4. passing it the Expectable which was used to do the matching in the first place: f, so that the final result has a type of MatchResult[A => Double]

    I’m not sure how more modular we can get given your requirements. It looks to me that the best we can do here is to reuse beCloseTo with forall.

    UPDATE

    A shorter answer might be something like this:

    val f = (i: Int) => i.toDouble
    val g = (i: Int) => i.toDouble + 1.0
    
    "f must be close to another similar function with a tolerance" in {
      f must computeSameResultsAs[Int](g, tolerance = 0.5, values = Seq(1, 2, 3))          
    }
    
    def computeSameResultsAs[A](ref: A => Double, tolerance: Double, values: Seq[A]): Matcher[A => Double] = (f: A => Double) => {
      verifyFunction((a: A) => (beCloseTo(ref(a) +/- tolerance)).apply(theValue(f(a)))).forall(values)
    }
    

    The code above creates a failure message like:

    In the sequence '1, 2, 3', the 1st element is failing: 1.0 is not close to 2.0 +/- 0.5
    

    This should almost work out-of-the-box. The missing part is an implicit conversion from A => MatchResult[_] to Matcher[A] (which I’m going to add to the next version):

    implicit def functionResultToMatcher[T](f: T => MatchResult[_]): Matcher[T] = (t: T) => {
      val result = f(t)
      (result.isSuccess, result.message)
    }
    

    You can use foreach instead of forall if you want to get all the failures:

    1.0 is not close to 2.0 +/- 0.5; 2.0 is not close to 3.0 +/- 0.5; 3.0 is not close to 4.0 +/- 0.5
    

    UPDATE 2

    This gets better everyday. With the latest specs2 snapshot you can write:

    def computeSameResultsAs[A](ref: A => Double, tolerance: Double, values: Seq[A]): Matcher[A => Double] = (f: A => Double) => {
      ((a: A) => beCloseTo(ref(a) +/- tolerance) ^^ f).forall(values)
    }   
    

    UPDATE 3

    And now with the latest specs2 snapshot you can write:

    def computeSameResultsAs[A](ref: A => Double, tolerance: Double, values: Seq[A]): Matcher[A => Double] = (f: A => Double) => {
      ((a: A) => beCloseTo(ref(a) +/- tolerance) ^^ ((a1: A) => f(a) aka "the value")).forall(values)
    }   
    

    The failure message will be:

    In the sequence '1, 2, 3', the 1st element is failing: the value '1.0' is not close to 2.0 +/- 0.5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions: double fullFingerPrinting(string location1, string location2, int nGrams) double AllSubstrings(string location1,
I have two JavaScript functions that I want to call from inside of a
I have functions in place that will convert the results of sql queries into
I have a char pointer returned from a function, and i want to double
Of course most languages have library functions for this, but suppose I want to
I have variables which are of double type I want them to be float
I have seperate functions for reading from a text file (depending on whether its
Suppose I have written such a class (number of functions doesn't really matter, but
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
I have the function prototype here: extern C void __stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.