Skip to content

Instantly share code, notes, and snippets.

View AlexeySoshin's full-sized avatar

Alexey Soshin AlexeySoshin

View GitHub Profile
@AlexeySoshin
AlexeySoshin / Table from string.sql
Last active November 2, 2020 17:35
Creates virtual table from single string
select a[1]::integer as col1, a[2]::integer as col2, a[3]::float as col3, a[4] as col4, a[5] as col5
from (
select string_to_array(nest.l, ',') as a
from (
select unnest('{"3,2,82.38,null,World","4,1,43.12,Hello,null"}'::text[]) as l
) nest
) b;
import io.ktor.network.tls.certificates.buildKeyStore
import io.ktor.network.tls.certificates.saveToFile
import io.ktor.network.tls.extensions.HashAlgorithm
import io.ktor.network.tls.extensions.SignatureAlgorithm
import io.ktor.routing.routing
import io.ktor.server.engine.applicationEngineEnvironment
import io.ktor.server.engine.embeddedServer
import io.ktor.server.engine.sslConnector
import io.ktor.server.netty.Netty
import io.ktor.server.netty.NettyApplicationEngine
@AlexeySoshin
AlexeySoshin / go_future.go
Last active August 15, 2020 12:32
Part of "Build your own Future in Go" article
func (this *future[T]) Get() Result[T] {
if this.completed {
return this.result
} else {
fmt.Println("Need to wait...")
select {
case <-this.wait:
return this.result
case <-this.ctx.Done():
return &result(T){failure: this.ctx.Err()}
sealed class KIO<out A : Any?> {
...
fun <B> recover(f: (Throwable) -> KIO<B>): Recovery<B> {
return Recovery(this, f)
}
}
...
fun main() {
Runtime.unsafeRunSync(
Effect {
// Generate some random UUID
if (Random().nextBoolean()) {
UUID.randomUUID().toString()
}
else {
null
}
fun main() {
Runtime.unsafeRunSync(
Effect {
// Generate some random UUID
UUID.randomUUID().toString()
}.map {
// Print it
println("UUID: $it")
}
)
object Runtime {
fun <A> unsafeRunSync(kio: KIO<A>): A = eval(kio)
fun <A> eval(kio: KIO<A>): A {
return when (kio) {
is Effect<A> -> kio.f()
is FlatMap<*, A> -> {
val res: Any? = eval(kio.input)
val f = kio.f as (Any?) -> KIO<A>
eval(f(res))
sealed class KIO<out A> {
fun <B> flatMap(f: (A) -> KIO<B>): KIO<B> {
return FlatMap(this, f)
}
fun <B> map(f: (A) -> B): KIO<B> {
return flatMap { a ->
Effect {
f(a)
}
for (var i = 0; i < 1_000_000; i++) {
Thread.startVirtualThread(() -> {
c.incrementAndGet();
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
var threads = new ArrayList<Thread>();
var cores = 10;
for (var i = 0; i < cores; i++) {
var t = Thread.startVirtualThread(() -> {
var bestUUID = "";
for (var j = 0; j < 1_000_000; j++) {
var currentUUID = UUID.randomUUID().toString();
if (currentUUID.compareTo(bestUUID) > 0) {
bestUUID = currentUUID;
}