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 6891695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:26:33+00:00 2026-05-27T06:26:33+00:00

Say I have a method that turns a (function on two elements) into a

  • 0

Say I have a method that turns a (function on two elements) into a (function on two sequences):

def seqed[T](f: (T,T) => T): (Seq[T], Seq[T]) => Seq[T] = (_,_).zipped map f

In words, the resulting function takes two sequences xs and ys, and creates a new sequence consisting of (xs(0) f ys(0), xs(1) f ys(1), ...)
So, for example, if xss is Seq(Seq(1,2),Seq(3,4)) and f is (a: Int, b: Int) => a + b, we can invoke it thus:

xss reduceLeft seqed(f)         // Seq(4, 6)

or with an anonymous function:

xss reduceLeft seqed[Int](_+_)

This is pretty good; it would be nice to get rid of the [Int] type argument but I don’t see how (any ideas?).

To make it feel a bit more like the tupled method, I also tried the enrich-my-library pattern:

class SeqFunction[T](f: (T,T) => T) {
  def seqed: (Seq[T], Seq[T]) => Seq[T] = (_,_).zipped map f
}
implicit def seqFunction[T](f: (T,T) => T) = new SeqFunction(f)

For a pre-defined function this works great, but it’s ugly with anonymous ones

xss reduceLeft f.seqed
xss reduceLeft ((_:Int) + (_:Int)).seqed

Is there another way I can reformulate this so that the types are inferred, and I can use syntax something like:

// pseudocode
xss reduceLeft (_+_).seqed         // ... or failing that
xss reduceLeft (_+_).seqed[Int]

? Or am I asking too much of type inference?

  • 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-27T06:26:33+00:00Added an answer on May 27, 2026 at 6:26 am

    The reason why a type annotation is required in

    xss reduceLeft seqed[Int](_+_)
    

    but not in

    xs zip ys map Function.tupled(_+_)
    

    is due to the difference in type requirements between map and reduceLeft.

    def reduceLeft [B >: A] (f: (B, A) ⇒ B): B 
    def map        [B]      (f: (A) ⇒ B): Seq[B]   // simple version!
    

    reduceLeft expects seqed to return type B where B >: Int. It seems that therefore the precise type for seqed cannot be known, so we have to provide the annotation. More info in this question.

    One way to overcome this is to re-implement reduceLeft without the lower bound.

    implicit def withReduceL[T](xs: Seq[T]) = new {
      def reduceL(f: (T, T) => T) = xs reduceLeft f
    }
    

    Test:

    scala> Seq(Seq(1,2,3), Seq(2,2,2)) reduceL seqed(_+_)
    res1: Seq[Int] = List(3, 4, 5)
    

    The problem now is that this now doesn’t work on subtypes of Seq (e.g. List), with or without the [Int] parameter:

    scala> Seq(List(1,2,3), List(2,2,2)) reduceL seqed(_+_)
    <console>:11: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
                  Seq(List(1,2,3), List(2,2,2)) reduceL seqed(_+_)
                                                              ^
    

    reduceL expects a function of type (List[Int], List[Int]) => List[Int]. Because Function2 is defined as Function2 [-T1, -T2, +R], (Seq[Int], Seq[Int]) => Seq[Int] is not a valid substitution.

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

Sidebar

Related Questions

Let's say I have a structure named vertex with a method that adds two
I'm using .NET 3.5. Say I have a method that accesses a specific file,
Say I have a method, and within that method it instantiates a Person class:
Let's say I have a subroutine/method that a user can call to test some
Say I want to have a method that takes any kind of number, is
Let's say you have a business logic method that can perform some operation across
Let's say we have a method signature like public static function explodeDn($dn, array &$keys
Say you have a method that returns a newly generated NSArray instance that is
Let's say I have a javascript method that takes a little to long to
Let's say that you have overridden an object's equals() and hashCode() methods, so that

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.