我怎样才能在不重复的情况下以功能方式正确编写此代码
How could I correctly write this code in functional way without duplication
在下面的函数中,我传递了一个 Option
。根据 Option
是 Some
还是 None
,我需要调用特定的 API,但其余代码对于两者都是相同的 Some
和 None
。我不知道如何删除代码重复。我怎样才能以函数式编程风格重写代码?
def getRowsByPartitionKeyId(id:I, pagingStateOption:Option[PagingState]):Tuple2[Option[List[M]],Option[PagingState]] = {
pagingStateOption match {
case Some(pagingState:PagingState) => {
val resultSet = session.execute(whereClause
.setFetchSize(1)
.setPagingState(pagingState)) //THIS IS THE ONLY DIFFERENCE IN THE TWO CODE LEGS
val it = resultSet.iterator();//resultSet is an iterator
val newPagingState:PagingState = resultSet.getExecutionInfo.getPagingState
if(it.hasNext){
val resultSetAsList:List[Row] = asScalaIterator(it).toList
val resultSetAsModelList = rowToModel(resultSetAsList.head)
Tuple2(Some(List(resultSetAsModelList)),Some(pagingState))
}
else {
Tuple2(None, None)
}
}
case None =>{
val resultSet = session.execute(whereClause
.setFetchSize(1)) //get one row from ResultSet. Cassandra might return more or less though
val it = resultSet.iterator();//resultSet is an iterator
val pagingState:PagingState = resultSet.getExecutionInfo.getPagingState
if(it.hasNext){
val resultSetAsList:List[Row] = asScalaIterator(it).toList
val resultSetAsModelList = rowToModel(resultSetAsList.head)
Tuple2(Some(List(resultSetAsModelList)),Some(pagingState))
}
else {
Tuple2(None, None)
}
}
}
def getRowsByPartitionKeyId(
id:I,
pagingStateOption:Option[PagingState]
): (Option[List[M]], Option[PagingState]) = {
val resultSet = session.execute(pagingStateOption match {
case Some(pagingState: PagingState) =>
whereClause.setFetchSize(1).setPagingState(pagingState)
case None =>
whereClause.setFetchSize(1)
})
val it = resultSet.iterator();//resultSet is an iterator
val newPagingState:PagingState = resultSet.getExecutionInfo.getPagingState
if (it.hasNext) {
val resultSetAsList:List[Row] = asScalaIterator(it).toList
val resultSetAsModelList = rowToModel(resultSetAsList.head)
Tuple2(Some(List(resultSetAsModelList)),Some(pagingState))
} else {
Tuple2(None, None)
}
}
知道了。我忘记了 Scala
returns 中的所有值,甚至 match
,所以我可以这样做
val resultSet = pagingStateOption match {
case Some(pagingState: PagingState) => {
println("got paging state:" +pagingState)
session.execute(whereClause
.setFetchSize(1)
.setPagingState(pagingState)) //get one row from ResultSet. Cassandra might return more or less though
}
case None => {
session.execute(whereClause
.setFetchSize(1)) //get one row from ResultSet. Cassandra might return more or less though
}
}
在下面的函数中,我传递了一个 Option
。根据 Option
是 Some
还是 None
,我需要调用特定的 API,但其余代码对于两者都是相同的 Some
和 None
。我不知道如何删除代码重复。我怎样才能以函数式编程风格重写代码?
def getRowsByPartitionKeyId(id:I, pagingStateOption:Option[PagingState]):Tuple2[Option[List[M]],Option[PagingState]] = {
pagingStateOption match {
case Some(pagingState:PagingState) => {
val resultSet = session.execute(whereClause
.setFetchSize(1)
.setPagingState(pagingState)) //THIS IS THE ONLY DIFFERENCE IN THE TWO CODE LEGS
val it = resultSet.iterator();//resultSet is an iterator
val newPagingState:PagingState = resultSet.getExecutionInfo.getPagingState
if(it.hasNext){
val resultSetAsList:List[Row] = asScalaIterator(it).toList
val resultSetAsModelList = rowToModel(resultSetAsList.head)
Tuple2(Some(List(resultSetAsModelList)),Some(pagingState))
}
else {
Tuple2(None, None)
}
}
case None =>{
val resultSet = session.execute(whereClause
.setFetchSize(1)) //get one row from ResultSet. Cassandra might return more or less though
val it = resultSet.iterator();//resultSet is an iterator
val pagingState:PagingState = resultSet.getExecutionInfo.getPagingState
if(it.hasNext){
val resultSetAsList:List[Row] = asScalaIterator(it).toList
val resultSetAsModelList = rowToModel(resultSetAsList.head)
Tuple2(Some(List(resultSetAsModelList)),Some(pagingState))
}
else {
Tuple2(None, None)
}
}
}
def getRowsByPartitionKeyId(
id:I,
pagingStateOption:Option[PagingState]
): (Option[List[M]], Option[PagingState]) = {
val resultSet = session.execute(pagingStateOption match {
case Some(pagingState: PagingState) =>
whereClause.setFetchSize(1).setPagingState(pagingState)
case None =>
whereClause.setFetchSize(1)
})
val it = resultSet.iterator();//resultSet is an iterator
val newPagingState:PagingState = resultSet.getExecutionInfo.getPagingState
if (it.hasNext) {
val resultSetAsList:List[Row] = asScalaIterator(it).toList
val resultSetAsModelList = rowToModel(resultSetAsList.head)
Tuple2(Some(List(resultSetAsModelList)),Some(pagingState))
} else {
Tuple2(None, None)
}
}
知道了。我忘记了 Scala
returns 中的所有值,甚至 match
,所以我可以这样做
val resultSet = pagingStateOption match {
case Some(pagingState: PagingState) => {
println("got paging state:" +pagingState)
session.execute(whereClause
.setFetchSize(1)
.setPagingState(pagingState)) //get one row from ResultSet. Cassandra might return more or less though
}
case None => {
session.execute(whereClause
.setFetchSize(1)) //get one row from ResultSet. Cassandra might return more or less though
}
}