{"id":17248947,"url":"https://github.com/pakoito/komprehensions","last_synced_at":"2025-04-14T05:31:40.710Z","repository":{"id":139227738,"uuid":"66873147","full_name":"pakoito/Komprehensions","owner":"pakoito","description":"Do comprehensions for Kotlin and 3rd party libraries [STABLE]","archived":false,"fork":false,"pushed_at":"2019-04-16T19:48:41.000Z","size":113,"stargazers_count":118,"open_issues_count":1,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T19:51:11.041Z","etag":null,"topics":["functional-programming","kotlin","rxjava"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pakoito.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-08-29T19:17:14.000Z","updated_at":"2024-12-08T14:19:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"65ef4f6a-b34a-4ff5-acc9-e57f622fa669","html_url":"https://github.com/pakoito/Komprehensions","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pakoito%2FKomprehensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pakoito%2FKomprehensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pakoito%2FKomprehensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pakoito%2FKomprehensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pakoito","download_url":"https://codeload.github.com/pakoito/Komprehensions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248826601,"owners_count":21167719,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functional-programming","kotlin","rxjava"],"created_at":"2024-10-15T06:42:31.645Z","updated_at":"2025-04-14T05:31:35.700Z","avatar_url":"https://github.com/pakoito.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Komprehensions\n\nKomprehensions is a library to reduce boilerplate and simplify your call chains.\n\n## Rationale\n\nAs your code starts getting more and more functional, you find that you want to chain multiple statements by helpers like `let`. This causes indentation levels to go quite high, and would often require that you split the code in several methods just to keep it readable.\n\n```java\nfun calculateDoubles(calcParams: Params) =\n    calcParams\n            .let { params -\u003e\n                defrombulate(params.first, param.second)\n                        .let { result -\u003e\n                            gaussianRoots(result)\n                                    .let { grtts -\u003e\n                                        storeResult(params.first, params.second, result, grtts)\n                                    }\n                        }\n            }\n```\n\n**Comprehensions** are a language feature that allow you to define such a chain in a way where every observable is a function at topmost indentation, yet still contains all the parameters received in the previous functions.\n\n## Usage\n\n### Let\n\nKomprehensions contains functions `doLet()` for `let()`. Each takes from 2 to 9 function each with an increasing number of parameters, and returns an object of the type of the return of the last function.\n\n```java\nfun calculateDoubles(calcParams: Params) =\n    // chained with let()\n    doLet(\n        { calcParams },\n        { params -\u003e defrombulate(params.first, param.second) },\n        { params, result -\u003e gaussianRoots(result) },\n        { params, result, grtts -\u003e storeResult(params.first, params.second, result, grtts) }\n    )\n```\n\n### Chainable\n\nKomprehensions contains functions `doChainable()` for interface `Chainable`. Each takes from 2 to 9 function each with an increasing number of parameters, and returns a Chainable.\n\nIt's functionally similar to [let](https://github.com/pakoito/Komprehensions#let) but it's limited to types that are marked as Chainable. The recommended usage is to annotate sealed classes with it to indicate that they can be transformed between them.\nAn example is available in [this link](https://gist.github.com/pakoito/8043a42c2381112753cfdaab128cdc49) with a longer description given by [\"A Domain Driven approach to Kotlin's new types\"](http://www.pacoworks.com/2016/10/03/new-talk-a-domain-driven-approach-to-kotlins-new-types-at-mobilization-2016/).\n\nA special thanks to [@Takhion](https://github.com/Takhion) for simplifying my initial implementation.\n\n### map\n\nKomprehensions contains functions `doMapIterable()` for `map()` chaining of `Iterable`. Each takes from 1 to 9 function each with an increasing number of parameters, and returns an `Iterable` of the type of the return of the last function.\n\n### flatMap\n\nKomprehensions contains functions `doFlatMapIterable()` for `flatMap()` chaining of `Iterable`. Each takes from 1 to 9 function each with an increasing number of parameters and returns an `Iterable`, then flattens the return into an `Iterable` of the type of the return of the last function.\n\n## Komprehensions-rx\n\nKomprehensions-rx is an extension module that allows chaining of [RxJava](https://github.com/ReactiveX/RxJava) `Observables`. It is available for both RxJava 1 and 2.\n\n### Map comprehensions\n\nKomprehensions-rx contains functions `doFlatMap()` for `flatMap()`, `doConcatMap()` for `concatMap()`, `doSwitchMap()` for `switchMap()`. Each takes from 1 to 9 function each with an increasing number of parameters, and returns an `Observable` of the type of the return of the last function.\n\n```java\nObservable\u003cString\u003e getUserFriends =\n    // chained with flatMap()\n    KomprehensionsRx.doFlatMap(\n        { profileClicks() },\n        { position -\u003e getUserFromProfile(position) },\n        { position, user -\u003e requestFriendListForUser(position, user.id) },\n        { position, user, friends -\u003e storeUserAndFriends(user, friends) },\n        { position, user, friends, result -\u003e toUserDisplayString(position, user, friends, result) }\n    );\n```\n\n### Compose comprehensions\n\nKomprehensions-rx contains functions `doCompose()` for `compose()`. Each takes from 1 to 9 `Transformer\u003cT, U\u003e` (RxJava 1.X) or `ObservableTransformer\u003cT, U\u003e` (RxJava 2.X), and returns an `Observable` of the type of the return of the last one.\n\n```java\nObservable\u003cList\u003cSiblings\u003e\u003e getRelatives =\n    // chained with compose()\n    KomprehensionsRx.doCompose(\n        { requestRelative(\"12345\") },\n        validate(),\n        assureThreads(Schedulers.io(), AndroidSchedulers.main()),\n        respectLifecycle(activity),\n        toUILayerModel(),\n        groupSiblings()\n    );\n\nObservable\u003cRelativeDto\u003e requestRelative(String id) { /* ... */ }\n\nObservableTransformer\u003cRelativeDto, RelativeDto\u003e validate() { /* ... */ }\n\nObservableTransformer\u003cRelativeDto, RelativeDto\u003e assureThreads(Scheduler in, Scheduler out) { /* ... */ }\n\nObservableTransformer\u003cRelativeDto, RelativeDto\u003e respectLifecycle(Activity activity) { /* ... */ }\n\nObservableTransformer\u003cRelativeDto, Relative\u003e toUILayerModel() { /* ... */ }\n\nObservableTransformer\u003cRelative, List\u003cSiblings\u003e\u003e groupSiblings() { /* ... */ }\n```\n\n## Komprehensions-reactor\n\nKomprehensions-reactor is an extension module that allows chaining of [Project Reactor](https://projectreactor.io/) `Flux` and `Mono` operators.\n\n### Map comprehensions\n\nKomprehensions-reactor contains functions `doFlatMap()` for `flatMap()`, `doConcatMap()` for `concatMap()`, `doSwitchMap()` for `switchMap()`. Each takes from 1 to 9 function each with an increasing number of parameters, and returns an `Flux` of the type of the return of the last function. It also contains functions `doFlatMapMono()` for `flatMap()` on the `Mono` operator. Each takes from 1 to 8 function each with an increasing number of parameters, and returns a `Mono` of the type of the return of the last function.\n\n```java\nFlux\u003cString\u003e getUserFriends =\n    // chained with flatMap()\n    KomprehensionsReactor.doFlatMap(\n        { profileClicks() },\n        { position -\u003e getUserFromProfile(position) },\n        { position, user -\u003e requestFriendListForUser(position, user.id) },\n        { position, user, friends -\u003e storeUserAndFriends(user, friends) },\n        { position, user, friends, result -\u003e toUserDisplayString(position, user, friends, result) }\n    );\n```\n\n## Distribution\n\nAdd as a dependency to your `build.gradle`\n```groovy\nrepositories {\n    ...\n    maven { url \"https://jitpack.io\" }\n    ...\n}\n    \ndependencies {\n    ...\n    compile 'com.github.pakoito.Komprehensions:komprehensions:1.3.2'\n\n    // Extensions for RxJava 1.X\n    compile 'com.github.pakoito.Komprehensions:komprehensions-rx:1.3.2'\n\n    // Extensions for RxJava 2.X\n    compile 'com.github.pakoito.Komprehensions:komprehensions-rx2:1.3.2'\n\n    // Extensions for Reactor\n    compile 'com.github.pakoito.Komprehensions:komprehensions-reactor:1.3.2'\n    ...\n}\n```\nor to your `pom.xml`\n\n```xml\n\u003crepositories\u003e\n    \u003crepository\u003e\n        \u003cid\u003ejitpack.io\u003c/id\u003e\n        \u003curl\u003ehttps://jitpack.io\u003c/url\u003e\n    \u003c/repository\u003e\n\u003c/repositories\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.pakoito.Komprehensions\u003c/groupId\u003e\n    \u003cartifactId\u003ekomprehensions\u003c/artifactId\u003e\n    \u003cversion\u003e1.3.2\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.pakoito.Komprehensions\u003c/groupId\u003e\n    \u003cartifactId\u003ekomprehensions-rx\u003c/artifactId\u003e\n    \u003cversion\u003e1.3.2\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.pakoito.Komprehensions\u003c/groupId\u003e\n    \u003cartifactId\u003ekomprehensions-rx2\u003c/artifactId\u003e\n    \u003cversion\u003e1.3.2\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.pakoito.Komprehensions\u003c/groupId\u003e\n    \u003cartifactId\u003ekomprehensions-reactor\u003c/artifactId\u003e\n    \u003cversion\u003e1.3.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Contributions\n\nPRs and suggestions for new features welcome.\n\nIf you have any core function that is chainable, please PR against the main module. If the function is contained in any 3rd party dependency, create a separate module and PR it instead.\n\nFor any error report please send an issue with a full stack trace and reproduction steps.\n\n## License\n\nCopyright (c) pakoito 2016\n\nThe Apache Software License, Version 2.0\n\nSee LICENSE.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpakoito%2Fkomprehensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpakoito%2Fkomprehensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpakoito%2Fkomprehensions/lists"}