Skip to content

Instantly share code, notes, and snippets.

@javadude
Created May 16, 2018 19:01
Show Gist options
  • Save javadude/5adafd93cc5a319531ef1e5fdff5c93a to your computer and use it in GitHub Desktop.
Save javadude/5adafd93cc5a319531ef1e5fdff5c93a to your computer and use it in GitHub Desktop.
Kotlin Lambda talk source code
package lambdas;
import kotlin.Unit;
import javax.swing.*;
import java.awt.*;
interface Foo<T> {
T doSomething();
}
class Test1 implements Foo<Void> {
@Override
public Void doSomething() {
return null;
}
}
public class GUI1 {
public static void go(Foo foo) {
foo.doSomething();
}
public static void main(String[] args) {
new JFrame() {{
setLayout(new FlowLayout());
final int x[] = {42};
add(new JButton("Press me") {{
addActionListener(e -> {
System.out.print("Pressed! " + x[0]);
});
}});
SomethingDoer somethingDoer = new SomethingDoer();
somethingDoer.simplerDoSomething(value -> {
System.out.println(value);
return value + " foo";
});
somethingDoer.simplerDoSomething2(value -> {
System.out.println(value);
return Unit.INSTANCE;
});
// x = 10;
// add(new JButton("Press me") {{
// addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// System.out.print("Pressed!");
// }
// });
// }});
pack();
setVisible(true);
}};
}
}
package lambdas
import org.jetbrains.annotations.NotNull
import java.awt.FlowLayout
import javax.swing.JButton
import javax.swing.JFrame
import kotlin.concurrent.thread
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
// val list = listOf(1,2,3)
// JFrame().apply {
// layout = FlowLayout()
// val button = JButton("Press me kotlin").apply {
// addActionListener {event ->
// println("kotlin pressed")
// list.forEach {item ->
// println("$event $item")
// }
// }
//// addActionListener(object : ActionListener {
//// override fun actionPerformed(e: ActionEvent?) {
//// println("kotlin pressed")
//// }
//// })
// }
// add(button)
// pack()
// isVisible = true
// }
val runnable = Runnable {
// do something
}
doSomething(10) {offset, value ->
(offset + value).toString()
}
doSomething2(20) {offset, value ->
(offset + value).toString()
}
doSomething2(20) { _, value ->
(10 + value).toString()
}
doSomething2(20) { _, value ->
"Hello"
}
val list = List<String>()
list.add("Scott")
list.add("Steve")
list.add("Claire")
list.forEach {
println(it)
}
// logExceptions {
// val x = "foo".toInt()
// println(x)
// }
println("time=" + time {
for(i in 0..100) {
println(i)
}
})
var x = 10
println("time=" + measureNanoTime {
for(i in 0..100) {
println("$x $i")
}
})
x = 20
thread {
for(i in 0..100) {
Thread.sleep(10)
println(x++)
}
}
Thread.sleep(40)
println("OUT: $x")
x = 30
Thread.sleep(40)
println("OUT: $x")
x = 40
Thread.sleep(40)
println("OUT: $x")
x = 50
println(x)
walk(
preOrder = {println(it)}
)
walk(
postOrder = {println(it)}
)
walk(
preOrder = {println(it)},
postOrder = {println(it)}
)
val stuff = { x : Int, y : Int ->
"$x $y"
}
val foo = Foo()
doSomething(10, foo::go)
doSomething(10, stuff)
}
typealias IntToString = (Int, Int) -> String
fun walk(preOrder : (String) -> Unit = {_ -> },
inOrder : (String) -> Unit = {_ -> },
postOrder : (String) -> Unit = {_ -> }) {
preOrder("SS")
inOrder("SS")
postOrder("SS")
}
inline fun moreEfficientWalk(preOrder : (String) -> Unit = {_ -> },
inOrder : (String) -> Unit = {_ -> },
postOrder : (String) -> Unit = {_ -> }) {
preOrder("SS")
inOrder("SS")
postOrder("SS")
}
fun walk2(preOrder : ((String) -> Unit)? = null,
inOrder : ((String) -> Unit)? = null,
postOrder : ((String) -> Unit)? = null) {
preOrder?.invoke("SS")
inOrder?.invoke("SS")
postOrder?.invoke("SS")
}
class SomethingDoer {
fun simplerDoSomething(f : (Int) -> String) {
println(f(10))
}
fun simplerDoSomething2(f : (Int) -> Unit) {
println(f(10))
}
}
fun doSomething(offset : Int, f : (Int, Int) -> String) {
println(f(offset, 10))
}
fun doSomething2(offset : Int, f : IntToString) {
println(f(offset, 10))
}
class Foo {
fun go(x : Int, y : Int) : String {
return "";
}
}
class List<ITEM> {
val data = mutableListOf<ITEM>()
fun get(n : Int) : ITEM = data[n]
fun add(item : ITEM) {data.add(item)}
fun forEach(action : (ITEM) -> Unit) {
for(item in data) {
action(item)
}
}
}
inline fun logExceptions(f : () -> Unit) {
try {
f()
} catch (t : Throwable) {
t.printStackTrace()
throw t
}
}
fun time(f : () -> Unit) : Long {
val start = System.nanoTime()
f()
val stop = System.nanoTime()
return stop - start
}
@Suppress("NOTHING_TO_INLINE")
inline fun getName() = "Hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment