Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在Kotlin中将元素前置到列表ifNotEmpty()的最惯用方法_List_Kotlin_Idioms - Fatal编程技术网

List 在Kotlin中将元素前置到列表ifNotEmpty()的最惯用方法

List 在Kotlin中将元素前置到列表ifNotEmpty()的最惯用方法,list,kotlin,idioms,List,Kotlin,Idioms,我想在列表前面加一个元素,但前提是列表不是空的 我在考虑一个组合takeIf{it.isNotEmpty()},orEmpty(),和flatMap 在科特林做这件事最惯用的方式是什么?这就是我想到的 val myEmptyList = listOf<String>() val myNotEmptyList = listOf<String>("is", "the", "worst") listOf("first").takeIf { myEmptyList.isNotE

我想在
列表
前面加一个元素,但前提是列表不是空的

我在考虑一个组合
takeIf{it.isNotEmpty()}
orEmpty()
,和
flatMap


在科特林做这件事最惯用的方式是什么?

这就是我想到的

val myEmptyList = listOf<String>()
val myNotEmptyList = listOf<String>("is", "the", "worst")

listOf("first").takeIf { myEmptyList.isNotEmpty() }.orEmpty() + myEmptyList
listOf("first").takeIf { myNotEmptyList.isNotEmpty() }.orEmpty() + myNotEmptyList

这就是我想到的

val myEmptyList = listOf<String>()
val myNotEmptyList = listOf<String>("is", "the", "worst")

listOf("first").takeIf { myEmptyList.isNotEmpty() }.orEmpty() + myEmptyList
listOf("first").takeIf { myNotEmptyList.isNotEmpty() }.orEmpty() + myNotEmptyList

下面是一段非常惯用的代码:

infix fun <T> Collection<T>?.prependIfNotEmpty(other: Collection<T>): Collection<T>? =
        if (this?.isNotEmpty() == true)
            other + this
        else
            this

// or for non nullable lists your approach
infix fun <T> Collection<T>.prependIfNotEmptyNonNull(other: Collection<T>): Collection<T> = other.takeIf { isNotEmpty() }.orEmpty() + this

infix乐趣收藏?.prependIfNotEmpty(其他:收藏):收藏=
if(this?.isNotEmpty()==true)
其他+这个
其他的
这
//或者对于不可为空的列表,请使用您的方法
infix fun Collection.prependIfNotEmptyNonNull(其他:集合):Collection=other.takeIf{isNotEmpty()}.orEmpty()+此
用法

listOf(1, 2, 3) prependIfNotEmpty listOf(-1, 0) // [-1,0,1,2,3]

listOf("two", "three", "four").prependIfNotEmpty(listOf("zero", "one")) // ["zero", "one", "two", "three", "four"]

val list: List<Float>? = null

list prependIfNotEmpty listOf(3.5, 5.6) // null

//for prependIfNotEmptyNonNull the same usage
listOf(1,2,3)prependIfNotEmpty listOf(-1,0)/[-1,0,1,2,3]
listOf(“二”、“三”、“四”)。prependIfNotEmpty(listOf(“零”、“一”))/[“零”、“一”、“二”、“三”、“四”]
val列表:列表?=无效的
list prependIfNotEmpty listOf(3.5,5.6)//null
//对于prependIfNotEmptyNonNull,使用相同的用法
也许有一种更惯用的方式,但我仍然没有弄明白


希望能有所帮助。

这里有一段代码非常地道:

infix fun <T> Collection<T>?.prependIfNotEmpty(other: Collection<T>): Collection<T>? =
        if (this?.isNotEmpty() == true)
            other + this
        else
            this

// or for non nullable lists your approach
infix fun <T> Collection<T>.prependIfNotEmptyNonNull(other: Collection<T>): Collection<T> = other.takeIf { isNotEmpty() }.orEmpty() + this

infix乐趣收藏?.prependIfNotEmpty(其他:收藏):收藏=
if(this?.isNotEmpty()==true)
其他+这个
其他的
这
//或者对于不可为空的列表,请使用您的方法
infix fun Collection.prependIfNotEmptyNonNull(其他:集合):Collection=other.takeIf{isNotEmpty()}.orEmpty()+此
用法

listOf(1, 2, 3) prependIfNotEmpty listOf(-1, 0) // [-1,0,1,2,3]

listOf("two", "three", "four").prependIfNotEmpty(listOf("zero", "one")) // ["zero", "one", "two", "three", "four"]

val list: List<Float>? = null

list prependIfNotEmpty listOf(3.5, 5.6) // null

//for prependIfNotEmptyNonNull the same usage
listOf(1,2,3)prependIfNotEmpty listOf(-1,0)/[-1,0,1,2,3]
listOf(“二”、“三”、“四”)。prependIfNotEmpty(listOf(“零”、“一”))/[“零”、“一”、“二”、“三”、“四”]
val列表:列表?=无效的
list prependIfNotEmpty listOf(3.5,5.6)//null
//对于prependIfNotEmptyNonNull,使用相同的用法
也许有一种更惯用的方式,但我仍然没有弄明白


希望有帮助。

不需要花哨的东西:
if(list.isEmpty())list else(e)+list
。如果您不喜欢
list
复制,请使用这个函数体创建一个函数。不需要花哨的东西:
If(list.isEmpty())list else listOf(e)+list
。如果您不喜欢
列表
复制,请使用此正文创建一个函数。