fellshard
03/05/2016, 8:33 AMvoddan
03/05/2016, 8:38 AMforEach
to return the collection instead of Unit
voddan
03/05/2016, 8:38 AMvoddan
03/05/2016, 8:39 AMvmironov
03/05/2016, 9:37 AMvmironov
03/05/2016, 9:38 AMpeek
is totally wrong:
1. It will evaluate sequence eagerly
2. It will evaluate sequence multiple times
3. It will apply side effects in a wrong ordervmironov
03/05/2016, 9:39 AMfun main(args: Array<String>) {
val sequence = sequenceOf("1", "2", "3")
sequence
.peekRight { println("[peekRight] $it") }
.forEach { println("[forEach] $it") }
println("=".repeat(13))
sequence
.peekWrong { println("[peekWrong] $it") }
.forEach { println("[forEach] $it") }
}
inline fun <T> Sequence<T>.peekRight(crossinline action: (T) -> Unit) = map { action(it); it }
inline fun <T> Sequence<T>.peekWrong(crossinline action: (T) -> Unit) = apply { forEach(action) }
vmironov
03/05/2016, 9:40 AM[peekRight] 1
[forEach] 1
[peekRight] 2
[forEach] 2
[peekRight] 3
[forEach] 3
=============
[peekWrong] 1
[peekWrong] 2
[peekWrong] 3
[forEach] 1
[forEach] 2
[forEach] 3
vmironov
03/05/2016, 9:42 AMpeek
, it can be easily implemented for Sequence
, but not for List
or Iterable
voddan
03/05/2016, 11:16 AMforEach
evaluates everything eagerly, periodvoddan
03/05/2016, 11:17 AMpeek
is a lazy forEach
voddan
03/05/2016, 11:20 AMvoddan
03/05/2016, 11:21 AMpeek
in Scala, Haskell's peek
means something completely differentvoddan
03/05/2016, 11:22 AMmap {f(it); it}
can be improvedvmironov
03/05/2016, 11:29 AMvoddan
03/05/2016, 12:02 PMkirillrakhman
03/07/2016, 12:57 PMkirillrakhman
03/07/2016, 12:57 PMFile()
constructor callsmikehearn
03/07/2016, 1:53 PMmikehearn
03/07/2016, 1:53 PMPaths.get()
. it's java 7+ so if you're on android, no use.mikehearn
03/07/2016, 1:53 PMevanchooly
03/07/2016, 2:11 PMkirillrakhman
03/07/2016, 2:38 PMkirillrakhman
03/07/2016, 2:38 PMkirillrakhman
03/07/2016, 2:39 PMFile
constructor accepts Strings or Files as the first argument, but only Strings as the second argumentjw
03/07/2016, 2:40 PMfun paths(base: File, vararg pathSegments: String): File {
var result = base
for (pathSegment in pathSegments) {
result = File(result, pathSegment)
}
return result
}
jw
03/07/2016, 2:40 PMFile
kirillrakhman
03/07/2016, 2:41 PMvmironov
03/07/2016, 2:48 PMfun paths(base: File, vararg pathSegments: String): File {
return pathSegments.fold(base) { file, segment -> File(file, segment) }
}
kirillrakhman
03/07/2016, 2:48 PM