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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:38:11+00:00 2026-06-17T20:38:11+00:00

I was just looking into scala, because most people seem to like it. So

  • 0

I was just looking into scala, because most people seem to like it. So I thought I’d write FizzBuzz (because it’s so much more fun than HelloWorld), but am kinda stuck because no matter what I put into them, if I define my PartialFunctions with the match/case syntax their isDefinedAt method always seems to return true. Can anyone explain to me what I’m missing (or is this a bug?). I’m using the Scala 2.10.0 release.

Code:

object Main extends App {
  def applyAndJoin[A, B](f: PartialFunction[A, B],
    g: PartialFunction[A, B])(h: (B, B) => B): PartialFunction[A, B] = {
    PartialFunction[A, B] {
      case a if (f isDefinedAt a) && (g isDefinedAt a) => h(f(a), g(a))
    } orElse f orElse g
  }

  val fizz = PartialFunction[Int, String] { case a if a % 3 == 0 => "Fizz" }
  val buzz = PartialFunction[Int, String] { case a if a % 5 == 0 => "Buzz" }
  val makeString = PartialFunction[Int, String] { case a => a.toString }
  val fizzBuzz = applyAndJoin(fizz, buzz)((a, b) => a + b) orElse makeString
  1 to 100 collect fizzBuzz foreach println //this fails because scala thinks
                                            //that fizz is defined at 1
}

Stacktrace:

Exception in thread "main" scala.MatchError: 1 (of class java.lang.Integer)
    at Main$$anonfun$1.apply(Main.scala:9)
    at Main$$anonfun$1.apply(Main.scala:9)
    at scala.PartialFunction$$anonfun$apply$1.applyOrElse(PartialFunction.scala:242)
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33)
    at Main$$anonfun$applyAndJoin$1.apply(Main.scala:5)
    at scala.PartialFunction$$anonfun$apply$1.applyOrElse(PartialFunction.scala:242)
    at scala.PartialFunction$OrElse.apply(PartialFunction.scala:162)
    at scala.collection.TraversableLike$$anonfun$collect$1.apply(TraversableLike.scala:278)
    at scala.collection.immutable.Range.foreach(Range.scala:142)
    at scala.collection.TraversableLike$class.collect(TraversableLike.scala:278)
    at scala.collection.AbstractTraversable.collect(Traversable.scala:105)
    at Main$delayedInit$body.apply(Main.scala:13)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.App$$anonfun$main$1.apply(App.scala:71)
    at scala.collection.immutable.List.foreach(List.scala:309)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
    at scala.App$class.main(App.scala:71)
    at Main$.main(Main.scala:1)
    at Main.main(Main.scala)
  • 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-06-17T20:38:12+00:00Added an answer on June 17, 2026 at 8:38 pm

    Your PartialFunction definition is not doing what you think, you should write

    val fizz: PartialFunction[Int, String] = { case a if a % 3 == 0 => "Fizz" }
    val buzz: PartialFunction[Int, String] = { case a if a % 5 == 0 => "Buzz" }
    val makeString: PartialFunction[Int, String] = { case a => a.toString }
    

    In this way you define a variable of type PartialFunction[Int, String] whose value is the anonymous definition { case a if ...}


    Instead you are defining a variable

    val fizz = ...
    

    using a call to the PartialFunction object apply() method, defined in scala 2.10 as

    def apply[A, B](f: A => B): PartialFunction[A, B] = { case x => f(x) }
    

    in this case the resulting function fizzis defined for every x, but when the “wrapped” f(x)is called, an error ensues.

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

Sidebar

Related Questions

I just finished Programming in Scala , and I've been looking into the changes
I was just looking into the memory allocation of a program in C .
I just began looking into source control.... And installed subversion from collabnet... I also
I have just been looking into Linq with ASP.Net. It is very neat indeed.
I am just looking at sorting an NSArray of NSNumbers into numeric order but
I am just getting started couchdb and have been looking into writing couch apps.
I was just going to use the FileUpload.FileBytes property but looking into the respective
I am just looking into compilers and I was wondering is it possible to
I'm just getting into graph databases, and I seem to keep running into a
I am just looking into using HTML Purifier to ensure that a user-inputed string

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.