1

My aim: To minimize lines of code.

Have a few function like f1, f2 ...

f1(A: String)

f2(A: String, B: Long, C: User)

Want to process it with high order function approach.

def processf1(request: Request[AnyContent], f: (String) => String)

def processf2(request: Request[AnyContent], f: (String, Long, User) => String) = {...}

Can I create ONE common process function for f1, f2 ?

Any ideas ?

Thanks.

2 Answers 2

1

You could parametrize processf types:

def processf[S,T](request: Request[S], f: T => String) = ...

Examples of use:

processf(new Request[Int], (x: String) => x)

processf(new Request[Int], (x: (String, Long, User)) => x._1)
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the answer, but not so clear for me, could please clarify

for example 3 function which have duplicate like RequestUtil.getRequestParams

  private def regUser(request: Request[AnyContent], f: (String) => String): String = {
    RequestUtil.getRequestParams(request, APPConst.USER) match {
      case Some(map) => f(map(APPConst.USER))
      case None => JsonUtil.toJson(APPConst.ERROR -> APPConst.POST_PARAMS_EMPTY_MISMATCH)
    }
  }

  private def regDog(request: Request[AnyContent], f: (Dog, Enum, String, String) => String): String = {
    RequestUtil.getRequestParams(request, APPConst.Dog) match {
      case Some(m) => process(m, f)
      case None => JsonUtil.toJson(APPConst.ERROR -> APPConst.POST_PARAMS_EMPTY_MISMATCH)
    }
  }

  private def regCat[T](request: Request[AnyContent], f: (Cat, Enum) => String): String = {
    RequestUtil.getRequestParams(request, APPConst.CAT) match {
      case Some(map) => process(map, f)
      case None => JsonUtil.toJson(APPConst.ERROR -> APPConst.POST_PARAMS_EMPTY_MISMATCH)
    }
  }

and executions

 def regUser = Action { request => Ok(views.html.index(regUserProcess(request, UserService.regUser)))}

  def regDog = Action { request => Ok(views.html.index(regCurrProcess(request, UserService.regDog)))}

  def regCat = Action { request => Ok(views.html.index(mainProcess(request, UserService.regCat)))}

As you can see 3 different functions with different count parameters UserService.regUser, UserService.regDog, UserService.regCat functions

3 Comments

In Scala you can parametrize functions by type. e.g: f(x: Int): String = x.toString and g(x: Int): x.toString can by expressed as an unique function f[T](x: T): = x.toString. Thanks to Scala's type inference you can call f as f(1) and f("1"). My answer applies the same strategy but to the function you are passing type.
In the case of your methods, they could be merged into a type-parametrized single method: private def regUser[T, S](request: Request[AnyContent], f: T => String, params: S): String = { params match { case Some(x) => f(x); case None => JsonUtil.toJson(APPConst.ERROR -> APPConst.POST_PARAMS_EMPTY_MISMATCH); } }
btw, You should't answer your question to add more information. You should edit your original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.