0

任何人都知道如何在以下函数中减少模式匹配语句中的重复?具体来说,我想概括 if 语句。我在想如果我能以某种方式将对象属性传递给一个函数,那么我就可以做到,但我不知道怎么做。任何帮助深表感谢。谢谢!!

 Ex: if (ASC) _.uid.asc else _.uid.desc

下面是函数

  /**
   * Finds all the users and sorts by column ascending or descending. Ascending or Descending is determined by
   * if the orderBy value is positive or negative. These values are held in constants  in @package models.AdminPage
   * @param orderBy The column to be sorted.
   * @return a list of sorted users.
   */
   def findAll(orderBy: Int = DefaultSortByUid) =  DB.withSession {
     implicit s: Session =>
       val q = for(u <- User) yield u

       // Get the absolute value to determine the column to be sorted. A valid value will be an Int that corresponds
       // to one of the constants defined in @package models.AdminPage.
       val sortByColumn = orderBy.abs
       val ASC = orderBy > 0

       val users = q.sortBy(sortByColumn match  {
         case `SortByUid` => if (ASC) _.uid.asc else _.uid.desc
         case `SortByUserName` => if(ASC) _.userId.asc else _.userId.desc
         case `SortByAuthMethod` => if(ASC) _.authMethod.asc  else _.authMethod.desc
         case `SortByRole` =>  if(ASC) _.role.asc else _.role.desc
         case `SortByEmail` => if(ASC) _.email.asc else _.email.desc
         case `SortByFirstName` => if(ASC) _.firstName.asc else _.firstName.desc
         case `SortByLastName` => if(ASC) _.lastName.asc else  _.lastName.desc
         //An invalid value just goes to table main page and to default sort of ascending by uid
         case _ => _.uid.asc
       }).list

       users
   }
4

3 回答 3

1

列对象是否共享一个 .desc 和 .asc 可用的公共父级如果是这样,您创建一个辅助函数:

def orderBy(direction, column) {
    if (direction) column.asc else column.desc
}

并在每种情况下都像这样调用它:

case ... => orderBy(ASC, _.uid)

或者甚至更好地反转它并使 de match 返回 _.uid 并将其用作 orderBy 的输入

于 2013-08-23T07:10:41.243 回答
1

我认为这可能是您正在寻找的:

/** Find all users and sort by column, ascending or descending.
  *
  * @param column  Column to be sorted.
  * @param asc     Sort ascending.
  * @return        Sorted list of users.
  */
def findAll[T <% Ordered](column: User => T = _.uid, asc: Boolean = true) = DB withSession {
  def sort(r: Records) = if (asc) column(r).asc else column(r).desc
  val q = for(u <- User) yield u
  q.sortBy(sort).list
}

你会像这样使用它:

val usersByEmailDesc = findAll(_.email, asc = false)

请让我知道这是否适合您——下一步是制作一个生成器来概括这一点,这样就可以将它添加到每个新表中。

于 2013-12-18T19:41:34.110 回答
1

这是我能做的最好的。谁能做得更好?

 /**
  * Find all users and sort by column, ascending or descending.
  *
  * @param orderBy Column to be sorted.
  * @return Sorted list of users.
  */
   def findAll(orderBy: Int = DefaultSortByUid) =  DB.withSession {
    implicit s: Session =>
    val q = for(u <- User) yield u

  // Ascending or Descending is determined by the sign of orderBy.
  def sort[T] (column: Column[T]) = if (orderBy > 0) column.asc else column.desc

  // Case values are defined as constants in @class models.AdminPage
  q.sortBy(c => sort( orderBy.abs match  {
    case `SortByUserName` =>  c.userId
    case `SortByAuthMethod` => c.authMethod
    case `SortByRole` =>  c.role
    case `SortByEmail` => c.email
    case `SortByFirstName` => c.firstName
    case `SortByLastName` =>  c.lastName
    case _ => c.uid
  })).list
  }
于 2013-08-23T14:05:06.567 回答