Unify functional parameter name for 'forEachLine', 'forEachBlock', 'repeat' methods with 'forEach'

This commit is contained in:
Ilya Gorbunov
2015-12-22 21:07:10 +03:00
parent c9ddea6d03
commit 4eb3cff129
3 changed files with 20 additions and 20 deletions
+13 -13
View File
@@ -119,26 +119,26 @@ public fun File.appendText(text: String, charset: Charset = Charsets.UTF_8): Uni
public fun File.appendText(text: String, charset: String): Unit = appendBytes(text.toByteArray(charset)) public fun File.appendText(text: String, charset: String): Unit = appendBytes(text.toByteArray(charset))
/** /**
* Reads file by byte blocks and calls [operation] for each block read. * Reads file by byte blocks and calls [action] for each block read.
* Block has default size which is implementation-dependent. * Block has default size which is implementation-dependent.
* This functions passes the byte array and amount of bytes in the array to the [operation] function. * This functions passes the byte array and amount of bytes in the array to the [action] function.
* *
* You can use this function for huge files. * You can use this function for huge files.
* *
* @param operation function to process file blocks. * @param action function to process file blocks.
*/ */
public fun File.forEachBlock(operation: (ByteArray, Int) -> Unit): Unit = forEachBlock(operation, defaultBlockSize) public fun File.forEachBlock(action: (ByteArray, Int) -> Unit): Unit = forEachBlock(action, defaultBlockSize)
/** /**
* Reads file by byte blocks and calls [operation] for each block read. * Reads file by byte blocks and calls [action] for each block read.
* This functions passes the byte array and amount of bytes in the array to the [operation] function. * This functions passes the byte array and amount of bytes in the array to the [action] function.
* *
* You can use this function for huge files. * You can use this function for huge files.
* *
* @param operation function to process file blocks. * @param action function to process file blocks.
* @param blockSize size of a block, replaced by 512 if it's less, 4096 by default. * @param blockSize size of a block, replaced by 512 if it's less, 4096 by default.
*/ */
public fun File.forEachBlock(operation: (ByteArray, Int) -> Unit, blockSize: Int): Unit { public fun File.forEachBlock(action: (ByteArray, Int) -> Unit, blockSize: Int): Unit {
val arr = ByteArray(if (blockSize < minimumBlockSize) minimumBlockSize else blockSize) val arr = ByteArray(if (blockSize < minimumBlockSize) minimumBlockSize else blockSize)
val fis = FileInputStream(this) val fis = FileInputStream(this)
@@ -148,7 +148,7 @@ public fun File.forEachBlock(operation: (ByteArray, Int) -> Unit, blockSize: Int
if (size <= 0) { if (size <= 0) {
break break
} else { } else {
operation(arr, size) action(arr, size)
} }
} while (true) } while (true)
} finally { } finally {
@@ -157,17 +157,17 @@ public fun File.forEachBlock(operation: (ByteArray, Int) -> Unit, blockSize: Int
} }
/** /**
* Reads this file line by line using the specified [charset] and calls [operation] for each line. * Reads this file line by line using the specified [charset] and calls [action] for each line.
* Default charset is UTF-8. * Default charset is UTF-8.
* *
* You may use this function on huge files. * You may use this function on huge files.
* *
* @param charset character set to use. * @param charset character set to use.
* @param operation function to process file lines. * @param action function to process file lines.
*/ */
public fun File.forEachLine(charset: Charset = Charsets.UTF_8, operation: (line: String) -> Unit): Unit { public fun File.forEachLine(charset: Charset = Charsets.UTF_8, action: (line: String) -> Unit): Unit {
// Note: close is called at forEachLine // Note: close is called at forEachLine
BufferedReader(InputStreamReader(FileInputStream(this), charset)).forEachLine(operation) BufferedReader(InputStreamReader(FileInputStream(this), charset)).forEachLine(action)
} }
/** /**
+3 -3
View File
@@ -17,12 +17,12 @@ public fun Writer.buffered(bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferedWrite
= if (this is BufferedWriter) this else BufferedWriter(this, bufferSize) = if (this is BufferedWriter) this else BufferedWriter(this, bufferSize)
/** /**
* Iterates through each line of this reader, calls [block] for each line read * Iterates through each line of this reader, calls [action] for each line read
* and closes the [Reader] when it's completed. * and closes the [Reader] when it's completed.
* *
* @param block function to process file lines. * @param action function to process file lines.
*/ */
public fun Reader.forEachLine(block: (String) -> Unit): Unit = useLines { it.forEach(block) } public fun Reader.forEachLine(action: (String) -> Unit): Unit = useLines { it.forEach(action) }
/** /**
* Reads this reader content as a list of lines. * Reads this reader content as a list of lines.
+4 -4
View File
@@ -3,12 +3,12 @@
package kotlin package kotlin
/** /**
* Executes the given function [body] specified number of [times]. * Executes the given function [action] specified number of [times].
* *
* A zero-based index of current iteration is passed as a parameter to [body]. * A zero-based index of current iteration is passed as a parameter to [action].
*/ */
public inline fun repeat(times: Int, body: (Int) -> Unit) { public inline fun repeat(times: Int, action: (Int) -> Unit) {
for (index in 0..times - 1) { for (index in 0..times - 1) {
body(index) action(index)
} }
} }