From b87c0755ad073457becadb3f78eac5497990adc2 Mon Sep 17 00:00:00 2001 From: "alexander.nutz" Date: Thu, 28 Mar 2024 17:55:28 +0100 Subject: [PATCH] fix --- README.md | 84 ++++++++++++++++++++++++++++++++- build.gradle.kts | 2 +- src/main/kotlin/blitz/io/Raw.kt | 2 +- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c2e2ffc..9a2597d 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,86 @@ repositories { } dependencies { - implementation("me.alex_s168:blitz:0.5") + implementation("me.alex_s168:blitz:0.6") } -``` \ No newline at end of file +``` + +## Examples +### Fibonacci sequence +```kotlin +val fib = lazySequence(0 to 1) { i, f -> + f(i-1) + f(i-2) +} + +println(fib[10]) +``` +### Unix `uniq` +```kotlin +val inp = sequenceOf("AAA", "BBB", "AAA", "AAA", "AAA", "BBB") +val out = inp.easyMappingSequence { i, s, m -> + if (s(i-1) == m(i)) null + else m(i) +} +println(out.contents) +``` +### Reading files +```kotlin +val file = Path.of("test.txt") // Path + .getFile() // File + +val text = file.read() // ByteBatchSequence + .stringify() // Sequence // (NOT lines!!) + .flatten() // String +``` +### Bit fields +```kotlin +class Flags: BitField() { + var direction by bit(0) + var moving by bit(1) + var frontLight by bit(2) +} + +val byte = getByteFromSomewhere() +val flags = Flags().decode(byte) +flags.direction = !flags.direction +putByteSomewhere(flags.encode()) +``` +### Unix `cat` with monads (pure) +```kotlin +fun pureCat(args: Array): Monad = + args + .ifEmpty { arrayOf("-") } + .map { + if (it == "-") readIn() + else unit(it) + .asPath() + .read() + .stringify() + } + .rewrap() + .flatten() + .reduce { s -> print(s) } +``` +### Numbers to bytes +```kotlin +val num: Short = 5 +val bytes = num.toBytes(Endian.LITTLE) +``` +### Caching delegate property +```kotlin +class Label { + val font = "Arial 11" + val fontWith by caching(::font) { + someFunctionToCalculate(it) + } +} +``` +### Contents +```kotlin +val a = listOf(1, 2, 3, 4) +val b = arrayOf(1, 2, 3, 4) +println(a.contents == b.contents) // true +println(b.contents) // [1, 2, 3, 4] +``` +### Either +No example yet \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 7f944e7..7041777 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "me.alex_s168" -version = "0.5" +version = "0.6" repositories { mavenCentral() diff --git a/src/main/kotlin/blitz/io/Raw.kt b/src/main/kotlin/blitz/io/Raw.kt index 8d09840..865ecaf 100644 --- a/src/main/kotlin/blitz/io/Raw.kt +++ b/src/main/kotlin/blitz/io/Raw.kt @@ -16,7 +16,7 @@ fun Provider.readerSequence(): ByteBatchSequence = var i = 0 while (!(buffered.exhausted() || i == limit - 1)) out[i ++] = buffered.readByte() - return out.sliceArray(0..i) + return out.sliceArray(0..