{"id":18894301,"url":"https://github.com/derive4j/derive4j-fj","last_synced_at":"2025-10-28T12:41:17.658Z","repository":{"id":144610253,"uuid":"44429023","full_name":"derive4j/derive4j-fj","owner":"derive4j","description":"Automatic derivation of functional-java typeclasse instances and optics","archived":false,"fork":false,"pushed_at":"2020-02-06T17:55:52.000Z","size":140,"stargazers_count":21,"open_issues_count":3,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-22T01:24:56.297Z","etag":null,"topics":["derive4j","functional-programming","functionaljava","java","java-8","typeclasses"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/derive4j.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-10-17T08:10:44.000Z","updated_at":"2020-03-24T14:00:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"23462d6b-ad4a-426a-a53a-eca1b4242243","html_url":"https://github.com/derive4j/derive4j-fj","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/derive4j/derive4j-fj","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derive4j%2Fderive4j-fj","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derive4j%2Fderive4j-fj/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derive4j%2Fderive4j-fj/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derive4j%2Fderive4j-fj/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derive4j","download_url":"https://codeload.github.com/derive4j/derive4j-fj/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derive4j%2Fderive4j-fj/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270856775,"owners_count":24657700,"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","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["derive4j","functional-programming","functionaljava","java","java-8","typeclasses"],"created_at":"2024-11-08T08:20:39.902Z","updated_at":"2025-10-07T02:06:39.525Z","avatar_url":"https://github.com/derive4j.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FunctionalJava on Derive4J steroids!\n\nThis [Derive4J](https://github.com/derive4j/derive4j) extension permits automatic derivation of [FunctionalJava](https://github.com/functionaljava/functionaljava) instances for the following type classes:\n\n - [Equal](https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/Equal.java)\n - [Hash](https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/Hash.java)\n - [Show](https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/Show.java)\n - [Ord](https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/Ord.java)\n\n(generation of optics is planned).\n\nSo, now, no excuse to write [unsafe code that breaks parametricity](https://github.com/derive4j/derive4j/issues/50)! ;)\n\n# \"Implicit\" resolution of instances\n\nThe implementation of a type class instance may often depends on the instances of the data type fields.\nDerive4J will try its best to resolve those dependencies for you, following rules similar to scala implicit resolution.\nIn some cases, Derive4J can fail to find a valid instance for a field, for one of the following two reasons:\n\n - because it does not exist (in the expected classes, ie. its an \"orphan instance\")\n - or because the instance for that field is not yet generated AND the field type is parametrized.\n\nIn both cases you will have to provide a static field or method (in the class of the ADT under derivation) that returns the missing type class instance.\nThe implementation can simply be a forward to a generated instance (for the second case).\n\n# Example Usage:\n```java\n\nimport fj.*;\nimport org.derive4j.*;\n\n@Data(flavour = Flavour.FJ)\n@Derive(@Instances({ Show.class, Hash.class, Equal.class, Ord.class}))\npublic abstract class Either\u003cA, B\u003e {\n\n  /**\n   * The catamorphism for either. Folds over this either breaking into left or right.\n   *\n   * @param left The function to call if this is left.\n   * @param right The function to call if this is right.\n   * @return The reduced value.\n   */\n  public abstract \u003cX\u003e X either(F\u003cA, X\u003e left, F\u003cB, X\u003e right);\n\n\n  // In case you need to interact with unsafe code that\n  // expects hashCode/equal/toString to be implemented:\n\n  @Deprecated\n  @Override\n  public final boolean equals(Object obj) {\n    return Equal.equals0(Either.class, this, obj,\n        Eithers.eitherEqual(Equal.anyEqual(), Equal.anyEqual()));\n  }\n\n  @Deprecated\n  @Override\n  public final int hashCode() {\n    return Eithers.\u003cA, B\u003eeitherHash(Hash.anyHash(), Hash.anyHash()).hash(this);\n  }\n\n  @Deprecated\n  @Override\n  public final String toString() {\n    return Eithers.\u003cA, B\u003eeitherShow(Show.anyShow(), Show.anyShow()).showS(this);\n  }\n}\n```\nDerive4J, through this extension will then derive the following code in the generated `Eithers.java` file:\n```java\n  \n  public static \u003cA, B\u003e Show\u003cEither\u003cA, B\u003e\u003e eitherShow(Show\u003cA\u003e aShow, Show\u003cB\u003e bShow) {\n    return Show.show(either -\u003e either.either(\n      (left) -\u003e Stream.fromString(\"left(\").append(() -\u003e aShow.show(left)).append(Stream.fromString(\")\")),\n      (right) -\u003e Stream.fromString(\"right(\").append(() -\u003e bShow.show(right)).append(Stream.fromString(\")\"))\n    ));\n  }\n\n  public static \u003cA, B\u003e Ord\u003cEither\u003cA, B\u003e\u003e eitherOrd(Ord\u003cA\u003e aOrd, Ord\u003cB\u003e bOrd) {\n    return Ord.ordDef(either1 -\u003e either1.either(\n      (left1) -\u003e either2 -\u003e either2.either(\n        (left2) -\u003e {\n          Ordering o = Ordering.EQ;\n          o = aOrd.compare(left1, left2);\n          if (o != Ordering.EQ) return o;\n          return o;\n        },\n        (right2) -\u003e Ordering.LT\n      ),\n      (right1) -\u003e either2 -\u003e either2.either(\n        (left2) -\u003e Ordering.GT,\n        (right2) -\u003e {\n          Ordering o = Ordering.EQ;\n          o = bOrd.compare(right1, right2);\n          if (o != Ordering.EQ) return o;\n          return o;\n        }\n      )\n    ));\n  }\n\n  public static \u003cA, B\u003e Equal\u003cEither\u003cA, B\u003e\u003e eitherEqual(Equal\u003cA\u003e aEqual, Equal\u003cB\u003e bEqual) {\n    return Equal.equalDef(either1 -\u003e either1.either(\n      (left1) -\u003e either2 -\u003e either2.either(\n        (left2) -\u003e aEqual.eq(left1, left2),\n        (right2) -\u003e false\n      ),\n      (right1) -\u003e either2 -\u003e either2.either(\n        (left2) -\u003e false,\n        (right2) -\u003e bEqual.eq(right1, right2)\n      )\n    ));\n  }\n\n  public static \u003cA, B\u003e Hash\u003cEither\u003cA, B\u003e\u003e eitherHash(Hash\u003cA\u003e aHash, Hash\u003cB\u003e bHash) {\n    return Hash.hash(either -\u003e either.either(\n      (left) -\u003e 23 + aHash.hash(left),\n      (right) -\u003e 29 + bHash.hash(right)\n    ));\n}\n```\n\n# Use it in your project\nDerive4J-FJ is a \"plugin\" of [Derive4J](https://github.com/derive4j/derive4j) and should be declared (as well) as a apt/compile-time only\ndependency (not needed at runtime). So while derive4j and derive4j-fj are (L)GPL-licensed, the generated code is not linked to derive4j, and thus __derive4j can be used in any project (proprietary or not)__.\nAlso you will need jdk8 version of FunctionalJava artifacts (4.7+).\n\n## Maven:\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.functionaljava\u003c/groupId\u003e\n    \u003cartifactId\u003efunctionaljava_1.8\u003c/artifactId\u003e\n    \u003cversion\u003e4.7\u003c/version\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eorg.derive4j\u003c/groupId\u003e\n  \u003cartifactId\u003ederive4j-fj\u003c/artifactId\u003e\n  \u003cversion\u003e0.2\u003c/version\u003e\n  \u003coptional\u003etrue\u003c/optional\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eorg.derive4j\u003c/groupId\u003e\n  \u003cartifactId\u003ederive4j\u003c/artifactId\u003e\n  \u003cversion\u003e0.12.4\u003c/version\u003e\n  \u003coptional\u003etrue\u003c/optional\u003e\n\u003c/dependency\u003e\n```\n[search.maven]: http://search.maven.org/#search|ga|1|org.derive4j.derive4j-fj\n\n## Gradle\n```\ncompile \"org.functionaljava:functionaljava_1.8:4.7\"\ncompile(group: 'org.derive4j', name: 'derive4j', version: '0.12.4', ext: 'jar')\ncompile(group: 'org.derive4j', name: 'derive4j-fj', version: '0.2', ext: 'jar')\n```\nor better using the [gradle-apt-plugin](https://github.com/tbroyer/gradle-apt-plugin):\n```\ncompile \"org.functionaljava:functionaljava_1.8:4.7\"\ncompileOnly \"org.derive4j:derive4j-annotation:0.12.4\"\napt \"org.derive4j:derive4j:0.12.4\"\napt \"org.derive4j:derive4j-fj:0.2\"\n```\n\n## Contributing\n\nBug reports, feature requests and pull requests are welcome, as well as contributions to improve documentation.\n\n## Contact\njb@giraudeau.info, [@jb9i](https://twitter.com/jb9i) or use the project GitHub issues.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderive4j%2Fderive4j-fj","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderive4j%2Fderive4j-fj","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderive4j%2Fderive4j-fj/lists"}