Skip to content

Instantly share code, notes, and snippets.

@pambrose
pambrose / pom.xml
Last active July 16, 2022 21:25
kslides-maven pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>kslides-maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
@pambrose
pambrose / inc.kt
Last active July 2, 2022 22:05
Kotlin pre and post conditions
// Out-of-the-box require() stmts
fun increment1(x: Int): Int {
require(x > 0) { "value must be positive" }
return (x + 1).also { require(it > 0) { "result is positive" } }
}
// Consolidate the post also/require() stmts into ensure()
fun increment2(x: Int): Int {
require(x > 0) { "value must be positive" }
return (x + 1).ensure({ it > 0 }) { "result is positive" }
public String frontBack(String str) {
if (str.length() < 2)
return str;
String b = str.substring(0, 1);
String e = str.substring(str.length() - 1, str.length());
String m = str.substring(1, str.length() - 1);
return e + m + b;
}
@pambrose
pambrose / main.dart
Created December 20, 2019 05:58
flutter_switch
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(SwitchDisplayApp());
class SwitchDisplayApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(home: SwitchDisplay());
}
@pambrose
pambrose / typeParameterCount.kts
Created December 8, 2019 02:31
Return type parameter count
val Any.typeParameterCount: Int
get() =
when {
this is Array<*> -> 1
!javaClass.genericSuperclass.javaClass.kotlin.isSubclassOf(ParameterizedType::class) -> 0
else -> (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.size
}
4.typeParameterCount shouldEqual 0
"dd".typeParameterCount shouldEqual 0
@pambrose
pambrose / KtsScriptExmple.kt
Created December 6, 2019 02:46
Example of KtsScript
class IncClass(var i: Int = 9) {
fun inc() {
i++
}
override fun toString() = "Value: $i"
}
fun main() {
KtsScript()
# Add these to your .zshrc or .cshrc files
j8='export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home; java -version'
j11='export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home; java -version'
j13='export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home; java -version'
@pambrose
pambrose / SimpleMutexSelect.kt
Last active September 11, 2019 15:18
Example of select on mutext.onLock
package org.athenian.select
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.selects.select
import kotlinx.coroutines.sync.Mutex
fun selectOnUnlock() {
// Initialize mutexes as locked
@pambrose
pambrose / Nullable Kotlin GEF Receivers
Last active May 30, 2018 03:48
Nullability in Kotlin Generic Extension Function Receivers
fun main(args: Array<String>) {
val nonnNullStr = "Str-"
val nullStr: String? = null
println(nonnNullStr.nullableConcat("1"))
println(nullStr.nullableConcat("2"))
println(nonnNullStr.nonnullableConcat("3"))
// Why is this legal?
public static void main(String[] args)
throws Exception {
// Create a JmxProxyLauncher
JmxProxyLauncher jmxProxyLauncher =
new JmxProxyLauncher.Builder()
.setUserName("pambrose")
.setPassword("topsecret")
.build();