{"id":15045209,"url":"https://github.com/serg-maximchuk/function-utils","last_synced_at":"2025-05-15T08:32:12.867Z","repository":{"id":57734771,"uuid":"196358301","full_name":"Serg-Maximchuk/function-utils","owner":"Serg-Maximchuk","description":"Java functional utils","archived":false,"fork":false,"pushed_at":"2020-07-27T08:32:13.000Z","size":217,"stargazers_count":3,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T05:51:15.816Z","etag":null,"topics":["functional","java","java-11","java-8","lambdas","library","utils","utils-library"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Serg-Maximchuk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-11T09:00:50.000Z","updated_at":"2020-07-27T08:32:15.000Z","dependencies_parsed_at":"2022-09-26T22:11:04.243Z","dependency_job_id":null,"html_url":"https://github.com/Serg-Maximchuk/function-utils","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serg-Maximchuk%2Ffunction-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serg-Maximchuk%2Ffunction-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serg-Maximchuk%2Ffunction-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serg-Maximchuk%2Ffunction-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Serg-Maximchuk","download_url":"https://codeload.github.com/Serg-Maximchuk/function-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254304641,"owners_count":22048446,"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","java","java-11","java-8","lambdas","library","utils","utils-library"],"created_at":"2024-09-24T20:51:35.418Z","updated_at":"2025-05-15T08:32:07.850Z","avatar_url":"https://github.com/Serg-Maximchuk.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Java functional utils\n\n![](https://img.shields.io/maven-central/v/io.github.serg-maximchuk/function-utils)\n\nhttps://mvnrepository.com/artifact/io.github.serg-maximchuk/function-utils\n\nJava 11\n\n```groovy\ncompile 'io.github.serg-maximchuk:function-utils:1.1.6'\n```\nOr\n```groovy\ncompile group: 'io.github.serg-maximchuk', name: 'function-utils', version: '1.1.6'\n```\nOr\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.serg-maximchuk\u003c/groupId\u003e\n  \u003cartifactId\u003efunction-utils\u003c/artifactId\u003e\n  \u003cversion\u003e1.1.6\u003c/version\u003e\n\u003c/dependency\u003e\n```\nJava 8 suport:\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.serg-maximchuk\u003c/groupId\u003e\n  \u003cartifactId\u003efunction-utils\u003c/artifactId\u003e\n  \u003cversion\u003e1.1.1-java8\u003c/version\u003e\n\u003c/dependency\u003e\n```\n```groovy\ncompile group: 'io.github.serg-maximchuk', name: 'function-utils', version: '1.1.1-java8'\n```\n\nDependency-free utility library born to reduce the code size and increase it's readability\nwhen dealing with lambdas, and some more! It will help you to write java code in more\nfunctional/imperative style.\n\nThrowing lambdas as a bonus!\n\nFeel free to create a PR.\n\n# Value::with\nThe main function, it will help you to write java code in more functional/imperative style.\n\nHow many times you've been writing code like these pieces:\n\n```\nHttpHeaders headers = new HttpHeaders();\nheaders.add(AUTHORIZATION, \"Basic ...\");\nreturn headers;\n```\n\n\n```\nUser user = new User();\nuser.setEmail(email);\nreturn repository.save(user);\n```\n\n```\nUser user = new User();\nusers.add(user);\nreturn user;\n```\n\nThe point is to shorten such cases, when you want to get/create something, made an action with\nit, and return it back.\n\n\nWhole idea is to transform next common-used structure:\n```\nT t = new T();\nt.f();\nreturn t;\n```\nOr:\n```\nT t = new T();\nouter.f(t);\nreturn t;\n```\n\nInto this:\n```\nreturn with(new T(), t -\u003e t.f());\nreturn with(new T(), t -\u003e outer.f(t));\n\n// which also can be simplified\nreturn with(new T(), T::f);\nreturn with(new T(), outer::f);\n```\n\nMeet the `Value` class and it's function `with`!\n\nMain actors:\n   - `T value`, an operand of generic type, which will be returned in the end;\n   - void `Consumer\u003cT\u003e action` whose the only argument would be the `value`.\n\nTake a look how it can reduce the code (`with` method is statically imported):\n\n```\nreturn with(new HttpHeaders(), headers -\u003e headers.add(AUTHORIZATION, \"Basic ...\"));\n```\n\n```\nreturn repository.save(with(new User(), user -\u003e user.setEmail(email)));\n```\n\n```\nreturn with(new User(), users::add);\n```\n\nYou may not be good enough in lambdas, so you may not understand these pieces of code,\ndon't panic! Try this approach few times, and you will start seeing potential places where\nyou can use this awesome function. You will start hating such code constructions:\n```\nSome some = new Some();\nsome.doSomething();\nreturn some;\n```\n\n**Note**, the `action` is a consumer - void function. The `Value::with` thing is **not** for\nsuch case:\n```\nT t = new T();\nreturn outer.f(t);\n```\nWhich may be something like this in real life:\n```\nUser user = new User();\nreturn repo.save(user);\n```\n**BUT** the `Value::with` can help you in next case:\n```\nUser user = new User();\nuser.setActive();\nreturn repo.save(user);\n```\nJust don't forget to call `repo::save` method **outside** of a `Value::with`,\nI believe you understand why.\n\n# Bonus: Throwing lambdas\nNot only in the `Value::with` context, sometimes action in a lambda may throw some checked\nexceptions. Throwing lambdas to the rescue:\n\n```\nThrowingBiConsumer\nThrowingBiFunction\nThrowingBinaryOperator\nThrowingBiPredicate\nThrowingBooleanSupplier\nThrowingConsumer\nThrowingDoubleBinaryOperator\nThrowingDoubleConsumer\nThrowingDoubleFunction\nThrowingDoublePredicate\nThrowingDoubleSupplier\nThrowingDoubleToIntFunction\nThrowingDoubleToLongFunction\nThrowingDoubleUnaryOperator\nThrowingFunction\nThrowingIntBinaryOperator\nThrowingIntConsumer\nThrowingIntFunction\nThrowingIntPredicate\nThrowingIntSupplier\nThrowingIntToDoubleFunction\nThrowingIntToLongFunction\nThrowingIntUnaryOperator\nThrowingLongBinaryOperator\nThrowingLongConsumer\nThrowingLongFunction\nThrowingLongPredicate\nThrowingLongSupplier\nThrowingLongToDoubleFunction\nThrowingLongToIntFunction\nThrowingLongUnaryOperator\nThrowingObjDoubleConsumer\nThrowingObjIntConsumer\nThrowingObjLongConsumer\nThrowingPredicate\nThrowingRunnable\nThrowingSupplier\nThrowingToDoubleBiFunction\nThrowingToDoubleFunction\nThrowingToIntBiFunction\nThrowingToIntFunction\nThrowingToLongBiFunction\nThrowingToLongFunction\nThrowingUnaryOperator\n```\nYou can't compile such a thing without wrapping it into `try/catch` or using\n`sneakyThrow` approach:\n```\nValue.with(t, t -\u003e outer.thisMethodThrows(t));\n```\nBut you may use built-in method with throwing lambda:\n```\nValue.withThrowing(t, t -\u003e outer.thisMethodThrows(t));\n```\n\nThrowing lambdas may be used as a stand-alone function whenever you want.\n\n#\n\n### Coming soon:\n\nIn throwing lambdas context: comparison why this library is better than others\n```\nhttps://www.javadoc.io/doc/io.vavr/vavr/1.0.0-alpha-2\nhttps://github.com/pivovarit/throwing-function\nhttps://github.com/SeregaLBN/StreamUnthrower\nhttps://github.com/fge/throwing-lambdas\nhttps://github.com/mscharhag/ET\nhttps://github.com/jOOQ/jOOL\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserg-maximchuk%2Ffunction-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserg-maximchuk%2Ffunction-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserg-maximchuk%2Ffunction-utils/lists"}