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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:10:44+00:00 2026-06-17T11:10:44+00:00

I have a pattern to process web service requests using chained partial functions (this

  • 0

I have a pattern to process web service requests using chained partial functions (this is a chain of responsibility pattern, I think?). In my example, let’s say there are two parameters for the request, a string Id and a date. There’s a verification step involving the id, a verification step checking the date, and finally some business logic that use both. So I have them implemented like so:

object Controller {
  val OK = 200
  val BAD_REQUEST = 400

  type ResponseGenerator = PartialFunction[(String, DateTime), (String, Int)]

  val errorIfInvalidId:ResponseGenerator = {
    case (id, _) if (id == "invalid") => ("Error, Invalid ID!", BAD_REQUEST)
  }

  val errorIfFutureDate:ResponseGenerator = {
    case (_, date) if (date.isAfter(DateTime.now)) => ("Error, date in future!", BAD_REQUEST)
  }

  val businessLogic:ResponseGenerator = {
    case (id, date) => {
      // ... do stuff
      ("Success!", OK)
    }
  }

  def handleRequest(id:String, date:DateTime) = {
    val chained = errorIfInvalidId orElse errorIfFutureDate orElse businessLogic
    val result: (String, Int) = chained(id, date)

    // make some sort of a response out of the message and status code
    // e.g. in the Play framework...
    Status(result._2)(result._1)
  }
}

I like this pattern because it’s very expressive – you can easily grasp what the controller method logic is just by looking at the chained functions. And, I can easily mix and match different verification steps for different requests.

The problem is that as I try to expand this pattern it starts to break down. Suppose my next controller takes an id I want to validate, but does not have the date parameter, and maybe it has some new parameter of a third type that does need validation. I don’t want to keep expanding that tuple to (String, DateTime, Other) and have to pass in a dummy DateTime or Other. I want to have partial functions that accept different types of arguments (they can still return the same type). But I can’t figure out how to compose them.

For a concrete question – suppose the example validator methods are changed to look like this:

val errorIfInvalidId:PartialFunction[String, (String, Int)] = {
  case id if (id == "invalid") => ("Error, Invalid ID!", BAD_REQUEST)
}

val errorIfInvalidDate:PartialFunction[DateTime, (String, Int)] = {
  case date if (date.isAfter(DateTime.now)) => ("Error, date in future!", BAD_REQUEST)
}

Can I still chain them together? It seems like I should be able to map the tuples to them, but I can’t figure out how.

  • 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-17T11:10:46+00:00Added an answer on June 17, 2026 at 11:10 am

    I’m a big fan of using scalaz’s Validation for things like this. It gives you quite a bit of control over what you want to do with errors and how to handle them. Here’s an example using you’re controller:

    import scalaz._
    import Scalaz._
    
    object Controller {
      val OK = 200
      val BAD_REQUEST = 400
    
      case class Response(response: String, status: Int)
    
      def validateIfInvalidId(id: String) = (id == "invalid") ?
        Response("Error, Invalid ID!", BAD_REQUEST).fail[String] |
        id.success[Response]
    
    
      def validateIfFutureDate(date: DateTime, currentDate: DateTime = DateTime.now) = (date.isAfter(currentDate)) ?
        Response("Error, date in future!", BAD_REQUEST).fail[DateTime] |
        date.success[Response]
    
      def handleRequest(id: String, date: DateTime) = {
        val response = for {
          validatedId <- validateIfInvalidId(id)
          validatedDate <- validateIfFutureDate(date)
        } yield {
          // ... do stuff
          Response("Success!", OK)
        }
    
        // make some sort of a response out of the message and status code
        // e.g. in the Play framework...
        response.fold(
          failure => Status(failure.response, failure.status),
          success => Status(success.response, success.status)
        )
      }
    }
    

    You can move the different validation functions off into their own world and then compose them anytime you want with the for comprehension in scala.

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

Sidebar

Related Questions

I am trying to implement a jax-rs web service using jersey framework. I have
I have problems with pattern. I need to process some text for example: <div>{text1}</div><span>{text2}</span>
I have a multistep process where each step does some network IO (web service
I have this pattern: ~^([a-z0-9]+[a-z0-9-]+[a-z0-9]+\.([a-z]+)(\.[a-z]+)?)$~i It will match the following: xxx.xxx or xxx.xxx.xxx (number
I have this pattern written ^.*\.(?!jpg$|png$).+$ However there is a problem - this pattern
Ok so if I have this pattern: ab&bc&cd&de&ef And I need to replace all
I have this module pattern that stores a bunch of vars. I want to
I may have the wrong pattern here, but I think it's a fair topic.
I'm in the process of building my first web application using ASP.NET MVC 2
I have simple web application based on JSP. Root of application looks like this:

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.