Skip to content

Instantly share code, notes, and snippets.

Avatar
☯️

Glenn pencelab

☯️
View GitHub Profile
View shapes-dsl-composedshape-with-inner-shapes-sample-output.txt
z*aa y * bb b x * ccc cc w * dddd dd v * eeeee e e
y ***bbbb xxx *** cccccc www *** dddd ddd vvv *** eeeee eeee
x *****cccccc www ***** dddd ddd vvvvv ***** eeeeeeeeee
w *******dddddddd vvv ******* eeeee eeee
v *********eeeeeeeeee
View shapes-dsl-composedshape-with-inner-shapes-sample.kt
panel {
repeat(5) {
composed {
val rhombus = rhombus {
lines = it + 1
char = 'z' - it
}
val triangle = triangle {
lines = it + 1
}
View shapes-dsl-collection-reduce-sample-output.txt
a bbbbb ccccc d eeeee
aaa bbbbb ccccc ddd eeeee d
aaaaa bbbbb ccccc ddddd eeeee ddd
aaaaaaa bbbbb ccccc ddd eeeee ddd
aaaaaaaaa bbbbb ccccc d eeeee d
View shapes-dsl-collection-reduce-sample.kt
panel {
val shapes = mutableListOf<Shape>()
repeat(5) {
shapes.add(when ((1..3).random()) {
1 -> square {
lines = 5
char = 'a' + it
}
2 -> triangle {
View shapes-dsl-loop-triangles-sample-output.txt
a b c d e f g h i j
bbb ccc ddd eee fff ggg hhh iii jjj
ccccc ddddd eeeee fffff ggggg hhhhh iiiii jjjjj
ddddddd eeeeeee fffffff ggggggg hhhhhhh iiiiiii jjjjjjj
eeeeeeeee fffffffff ggggggggg hhhhhhhhh iiiiiiiii jjjjjjjjj
fffffffffff ggggggggggg hhhhhhhhhhh iiiiiiiiiii jjjjjjjjjjj
ggggggggggggg hhhhhhhhhhhhh iiiiiiiiiiiii jjjjjjjjjjjjj
hhhhhhhhhhhhhhh iiiiiiiiiiiiiii
View shapes-dsl-loop-triangles-sample.kt
panel {
for (i in 0..9) {
triangle {
lines = i + 1
char = 'a' + i
}
space()
}
View Extension.kt
inline fun panel(init: (@ShapeDsl Panel).() -> Unit): Panel {
return Panel().apply { init() }
}
inline fun Panel.square(init: SquareBuilder.() -> Unit): Square {
return SquareBuilder().apply { init() }.build().also { this.addShape(it) }
}
inline fun Panel.triangle(init: TriangleBuilder.() -> Unit): Triangle {
return TriangleBuilder().apply { init() }.build().also { this.addShape(it) }
View shapes-dsl-explicit-receiver-usage-sample.kt
panel {
square {
lines = 8
char = 'd'
this@panel.triangle {
lines = 10
char = 'x'
}
}
View shapes-dsl-shapebuilder.kt
@ShapeDsl
abstract class ShapeBuilder {
private companion object {
const val DEFAULT_CHAR = '*'
}
var char = DEFAULT_CHAR
var lines = 0