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

  • Home
  • SEARCH
  • 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 623347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:05:00+00:00 2026-05-13T19:05:00+00:00

I was trying to write a testing/timing function for answers provided in this SO

  • 0

I was trying to write a testing/timing function for answers provided in this SO question. Some answers work on Array[T], some on List[T], one on Iterable[T] and one on String!

What I’d like to write is a function that takes the shift* functions from the question or the answers, an input list, a predicate and an expected output and run the function. Sort of like:

def test[T](
  func:(Seq[T], T=>Boolean) => Seq[T],
  input:Seq[T],
  predicate:T=>Boolean,
  expected:Seq[T]): Unit = {
    // may be some warm up
    // ... time start, run func, time stop, 
    // check output against expected
}

Except I can figure out the signature, as Array seems to have mutable Seq properties, while List seems to have immutable Seq properties.

What’s the best way to handle that?

Edit: Using Thomas’ suggestion this is how close I can get (works on Array[Char], List[T] but not on Array[T]):

val inputArr = Array('a', 'b', 'C', 'D')
val expectArr = Array('a', 'C', 'D', 'b')
val inputList = inputArr.toList
val expectList = expectArr.toList

def test[I, T](
  func:(I, T=>Boolean) => Traversable[T],
  input: I,
  predicate: T=>Boolean,
  expected: Traversable[T]): Boolean = {
  val result = func(input, predicate)
  if (result.size == expected.size) {
    result.toIterable.zip(expected.toIterable).forall(x => x._1 == x._2)
  } else {
    false
  }
}

// this method is from Geoff [there][2] 
def shiftElements[A](l: List[A], pred: A => Boolean): List[A] = {
  def aux(lx: List[A], accum: List[A]): List[A] = {
    lx match {
      case Nil => accum
      case a::b::xs if pred(b) && !pred(a) => aux(a::xs, b::accum)
      case x::xs => aux(xs, x::accum)
    }
  }
  aux(l, Nil).reverse
}

def shiftWithFor[T](a: Array[T], p: T => Boolean):Array[T] = {
  for (i <- 0 until a.length - 1; if !p(a(i)) && p(a(i+1))) {
    val tmp = a(i); a(i) = a(i+1); a(i+1) = tmp
  }
  a
}

def shiftWithFor2(a: Array[Char], p: Char => Boolean):Array[Char] = {
  for (i <- 0 until a.length - 1; if !p(a(i)) && p(a(i+1))) {
    val tmp = a(i); a(i) = a(i+1); a(i+1) = tmp
  }
  a
}

def shiftMe_?(c:Char): Boolean = c.isUpper

println(test(shiftElements[Char], inputList, shiftMe_?, expectList))
println(test(shiftWithFor2, inputArr, shiftMe_?, expectArr))
//following line does not compile
println(test(shiftWithFor, inputArr, shiftMe_?, expectArr))
//found   : [T](Array[T], (T) => Boolean) => Array[T]
//required: (?, (?) => Boolean) => Traversable[?]

//following line does not compile
println(test(shiftWithFor[Char], inputArr, shiftMe_?, expectArr))
//found   : => (Array[Char], (Char) => Boolean) => Array[Char]
//required: (?, (?) => Boolean) => Traversable[?]

//following line does not compile
println(test[Array[Char], Char](shiftWithFor[Char], inputArr, shiftMe_?, expectArr))
//found   : => (Array[Char], (Char) => Boolean) => Array[Char]
//required: (Array[Char], (Char) => Boolean) => Traversable[Char]

I will mark Daniel’s answer as accepted as it compiles and provides me a different way to achieve what I wanted – unless the method on Array[T] creates a new array (and brings in Manifest issues).

(2): How would be a functional approach to shifting certain array elements?

  • 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-13T19:05:00+00:00Added an answer on May 13, 2026 at 7:05 pm

    I’d use scala.collection.Seq on Scala 2.8, as this type is parent to all ordered collections. Except Array and String, unfortunately. One can get around that with view bounds, like this:

    def test
      [A, CC <% scala.collection.Seq[A]]
      (input: CC, expected: CC)
      (func: (CC, A => Boolean) => CC, predicate: A => Boolean): Unit = {
      def times(n: Int)(f: => Unit) = 1 to n foreach { count => f }
      def testFunction = assert(func(input, predicate) == expected)
      def warm = times(50) { testFunction }
      def test = times(50) { testFunction }
    
      warm
      val start = System.currentTimeMillis()
      test
      val end = System.currentTimeMillis()
      println("Total time "+(end - start))
    }
    

    I’m currying this function so that the input (and expected) can be used to infer the type. Anyway, this won’t work with your own Array version on Scala 2.8, because that requires a Manifest. I’m sure it can be provided here somehow, but I don’t see quite how.

    But say you ignore all that stuff about sequences, arrays, etc. Just remove the view bound from the function, and you get this:

    def test
      [A, CC]
      (input: CC, expected: CC)
      (func: (CC, A => Boolean) => CC, predicate: A => Boolean): Unit = {
      def times(n: Int)(f: => Unit) = 1 to n foreach { count => f }
      def testFunction = assert(func(input, predicate) == expected)
      def warm = times(50) { testFunction }
      def test = times(50) { testFunction }
    
      warm
      val start = System.currentTimeMillis()
      test
      val end = System.currentTimeMillis()
      println("Total time "+(end - start))
    }
    

    Which will work just as find. As long the type match, it isn’t really relevant for this program to know what a CC is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I am trying to implement performance testing on ActiveMQ, so have setup a basic
Any ideas on this one? I'm trying to write a unit test that will
I'm trying to create a web application project template for everyone to use here
I am looking for a way to determine what the Name/IP Address of the
I am trying to write a unit test to cover code found in our

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.