https://github.com/daggerok/fold-java
Fold operator
https://github.com/daggerok/fold-java
fold fold-left gradle-kotlin-dsl java kotlin spek spekframework
Last synced: 11 months ago
JSON representation
Fold operator
- Host: GitHub
- URL: https://github.com/daggerok/fold-java
- Owner: daggerok
- License: mit
- Created: 2019-02-17T00:15:29.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-17T23:13:00.000Z (about 1 year ago)
- Last Synced: 2025-01-10T00:37:00.987Z (about 1 year ago)
- Topics: fold, fold-left, gradle-kotlin-dsl, java, kotlin, spek, spekframework
- Language: Java
- Homepage:
- Size: 85.9 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# left fold [](https://github.com/daggerok/fold-java/actions/workflows/ci.yaml)
Kotlin to the rescue
[fold the universe: reduction with java 8 streams API](https://dzone.com/articles/folding-the-universe-part-iii-java-8-list-and-stre)
_DomainEvents.kt_
```kotlin
@file:JvmName("DomainEvents")
interface DomainEvent {
val type: Class
get() = this.javaClass
}
class CounterCreatedEvent : DomainEvent
class CounterIncrementedEvent : DomainEvent
class CounterDecrementedEvent : DomainEvent
fun Aggregate.apply(event: DomainEvent): Aggregate = when (event) {
is CounterCreatedEvent -> this.on(event)
is CounterIncrementedEvent -> this.on(event)
is CounterDecrementedEvent -> this.on(event)
else -> throw IllegalStateException("unexpected unsupported domain event occur: $event")
}
```
_Some.java_
```java
Optional foldLeft(Aggregate initialState, DomainEvent... events) {
if (events.length <= 0) return Optional.empty();
Aggregate accumulator = initialState;
for (DomainEvent event : events)
accumulator = DomainEvents.apply(accumulator, event);
return Optional.of(accumulator);
}
```
## Run tests
```bash
./gradlew clean test
```
## Links
- [lazy-java](https://github.com/daggerok/lazy-java)
- [java-examples](https://github.com/daggerok/java-examples)
- [learn-jvm](https://github.com/daggerok/learn-jvm)
- [fold-java](https://github.com/daggerok/fold-java)