This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Serialize,Deserialize)] | |
struct Posts { | |
children: Vec<Post> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"kind": "Listing", | |
"data": { | |
"children": [....] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Sheet.row(rowNumber: Int? = null, block: Row.() -> Unit): Row { | |
return createRow(rowNumber ?: physicalNumberOfRows).apply(block) | |
} | |
fun Row.cell(value: Any? = null, column: Int? = null, block: Cell.() -> Unit = {}): Cell { | |
val cell = createCell(column ?: physicalNumberOfCells) | |
when (value) { | |
is Calendar -> cell.setCellValue(value) | |
is Date -> cell.setCellValue(value) | |
is Boolean -> cell.setCellValue(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Workbook.sheet(name: String? = null, block: Sheet.() -> Unit = {}): Sheet { | |
return if (name != null) { | |
createSheet(name) | |
} else { | |
createSheet() | |
}.apply(block) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void createWorkbook() { | |
Workbook wb = new HSSFWorkbook(); | |
Sheet sheet = wb.createSheet(); | |
Row row = sheet.createRow(0); | |
row.createCell(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun workbook(wb: Workbook = HSSFWorkbook(), block: Workbook.() -> Unit = {}): Workbook { | |
block(wb) | |
return wb | |
} | |
fun tryItOut() { | |
// use the default HSSFWorkbook | |
workbook { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun tryItOut() { | |
// Also since the block is the only parameter we can even drop the parenthesis all together! | |
workbook { | |
// We can directly set the value for isHidden! | |
isHidden = true | |
// We cannot change setSelectedTab because Workbook only has a setter for this and no getter (because POI is a PITA). | |
// This prevents Kotlin from autogenerating the "synthetic accessor" | |
setSelectedTab(2) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example is lovingly lifted directly from the Kotlin documentation | |
class Foo() { | |
// Note that in Kotlin variable types are inferred, you can say "var" (or val if you want it immutable) instead of specifying the type. | |
// Here we are defining a field named "counter" with a value of 1. | |
var counter = 0 | |
// Here we are providing a setter that only updates the field if the new value is positive. | |
set(value) { | |
if (value >= 0) field = value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Notice the parameter is now of type "Workbook.() -> Unit" rather than "(Workbook) -> Unit" | |
fun workbook(block: Workbook.() -> Unit): Workbook { | |
var wb = HSSFWorkbook() | |
block(wb) | |
return wb | |
} | |
fun tryItOut() { | |
// Since we defined the lambda as using a Receiver Type of "Workbook.()" we can now directly call the methods without referencing the paramter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In Kotlin function definitions start with the keywork "fun" | |
// Additionally Kotlin lets you have "top level" functions that do not need to be inside a class | |
// "(Workbook) -> Unit" means "a function that takes a workbook and returns nothing (Unit) | |
fun workbook(block: (Workbook) -> Unit): Workbook { | |
var wb = HSSFWorkbook() | |
// Note that in Kotlin we can direcly call "block" rather than needing to call "accept" on it | |
block(wb) | |
return wb | |
} |
NewerOlder