diff --git a/.travis.yml b/.travis.yml index 7c55b120..2fb30bf7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,24 +4,28 @@ sudo: false language: java jdk: - - openjdk11 + - openjdk8 matrix: + fast_finish: true include: - - jdk: openjdk8 - script: - - ./gradlew build coverage -s -i - after_success: - - bash <(curl -s https://codecov.io/bash) - - '[ "$TRAVIS_BRANCH" = "series/4.x" -a "$TRAVIS_PULL_REQUEST" = "false" -a -z "$TRAVIS_TAG" ] - && ./gradlew uploadArchives' -script: - - ./gradlew clean test + allow_failures: +script: + - ./gradlew build --no-daemon env: global: - secure: Bun+1FZ29Q3dR9gZ/5brxcSf+zcY5tWrsqOA4GUb5bYCMyORuXQB0FYXuhKR4wB1pFrk1a9EYwRwSu3GwRJVWb+UzF0CNOWF/QG5tGPx32IOXScwlL/KonI4Vhs7Oc0fF4Wdb7euNrT27BU61jbUugjJ642b3n0VBYFYDdquprU= - secure: QAxhjqLRa+WHKIzgIJPZ/rM5a5uzqG7E5rsC0YvB25cO712oYXmzsYPia/oSp0chXlYLYMfk2UnLeQCSx2e6ogXRRRa977Q+B33Nt0Hd9SGLtduv6DBrbA2ehLU12Ib4DWe5VhF5eueAunycYcllTvqA5h+pzTtEVbd68ZHncM4= + +before_cache: + - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock + - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ + +cache: + directories: + - $HOME/.gradle/caches/ + - $HOME/.gradle/wrapper/ diff --git a/ChangeLog.md b/ChangeLog.md deleted file mode 100644 index acdbedea..00000000 --- a/ChangeLog.md +++ /dev/null @@ -1,52 +0,0 @@ -4.8.1 ------ - -### Enhancements - -- Add Trampoline.suspend(final F0> a). (see [#367](https://github.com/functionaljava/functionaljava/pull/367)); - -### Fixes - -- Fix regression in lifted semigroup sum. Fix #365 (see [#366](https://github.com/functionaljava/functionaljava/pull/366)); - -### Internal - -- Fix compile under jdk11. Enable jdk11 travis build. (see [#361](https://github.com/functionaljava/functionaljava/pull/361)); -- Fix warnings (see [#369](https://github.com/functionaljava/functionaljava/pull/369)); -- Add P tests (see [#360](https://github.com/functionaljava/functionaljava/pull/360)); -- Exclude consume/ from coverage (see [#357](https://github.com/functionaljava/functionaljava/pull/357)); -- Add DList tests (see [#356](https://github.com/functionaljava/functionaljava/pull/356)); -- Add Visitor tests (see [#354](https://github.com/functionaljava/functionaljava/pull/354)); - -4.8 ---- - -### Enhancements - -- Enable upload of snapshot artifacts. (see [`e834e8b`](https://github.com/functionaljava/functionaljava/commit/e834e8b)); -- Add append methods to all Px classes. Fix #326 (see [`065ed43`](https://github.com/functionaljava/functionaljava/commit/065ed43)); -- Introduce the Eval monad (see [`98294fc`](https://github.com/functionaljava/functionaljava/commit/98294fc)); -- Fluent Equal/Ord construction (see [#333](https://github.com/functionaljava/functionaljava/pull/333)); -- Implement Zipper Eq and Hash and add tests (see [#343](https://github.com/functionaljava/functionaljava/pull/343)); -- Implement Vector equals (see [#350](https://github.com/functionaljava/functionaljava/pull/350)); - -### Fixes - -- Fixed a bug in the NonEmptyList Semigroup implementation that resulted in the same NonEmptyList appended to itself. (Regression in 4.7, see [`07f94fa`](https://github.com/functionaljava/functionaljava/commit/07f94fa)); -- Fixes #334: exception in Either.LeftProjection.traverseIO (see [#335](https://github.com/functionaljava/functionaljava/pull/335)); - -### Internal - -- Added Scalacheck Arbitrary implementations for Natural and NonEmptyList. (see [`405c3ec`](https://github.com/functionaljava/functionaljava/commit/405c3ec)); -- Added unit test coverage for Semigroup implementations. The StringBuffer and StringBuilder tests fail because both of those types are mutable. The IO test fails because the ArbitraryIO implementation does not implement equals. (see [`ef81130`](https://github.com/functionaljava/functionaljava/commit/ef81130)); -- Fixed the ArbitraryIO implementation and created a Properties object for testing the IO semigroup. (see [`a8e979f`](https://github.com/functionaljava/functionaljava/commit/a8e979f)); -- Equal: remove reference to static field of LazyString. Fix #321 (see [`6c6dabd`](https://github.com/functionaljava/functionaljava/commit/6c6dabd)); -- Add IOFunctions tests (see [#340](https://github.com/functionaljava/functionaljava/pull/340)); -- Add Stream tests (see [#341](https://github.com/functionaljava/functionaljava/pull/341)); -- Add tests for Try, F, FW, Digit (see [#346](https://github.com/functionaljava/functionaljava/pull/346)); -- Add Vector tests (see [#347](https://github.com/functionaljava/functionaljava/pull/347)); -- Add Optic tests (see [#348](https://github.com/functionaljava/functionaljava/pull/348)); -- Add Parser tests (see [#349](https://github.com/functionaljava/functionaljava/pull/349)); -- Add FingerTree tests (see [#351](https://github.com/functionaljava/functionaljava/pull/351)); -- Add TreeZipper tests (see [#352](https://github.com/functionaljava/functionaljava/pull/352)); -- Add Reader/Writer tests (see [#353](https://github.com/functionaljava/functionaljava/pull/353)); diff --git a/README.adoc b/README.adoc index 8a3ad347..ecf403e2 100644 --- a/README.adoc +++ b/README.adoc @@ -1,6 +1,6 @@ = Functional Java -image:https://travis-ci.org/functionaljava/functionaljava.svg?branch=master["Build Status", link="https://travis-ci.org/functionaljava/functionaljava"] +image:https://travis-ci.org/functionaljava/functionaljava.svg?branch=master["Build Status", link="https://app.travis-ci.com/github/functionaljava/functionaljava"] image:https://codecov.io/gh/functionaljava/functionaljava/branch/master/graph/badge.svg["Code Coverage", link="https://codecov.io/gh/functionaljava/functionaljava"] image:https://badges.gitter.im/functionaljava/functionaljava.svg[link="https://gitter.im/functionaljava/functionaljava?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"] @@ -8,7 +8,7 @@ image::http://www.functionaljava.org/img/logo-600x144.png[] Functional Java is an open source library facilitating functional programming in Java. The library implements numerous basic and advanced programming abstractions that assist composition oriented development. Functional Java also serves as a platform for learning functional programming concepts by introducing these concepts using a familiar language. -The library is intended for use in production applications and is thoroughly tested using the technique of automated specification-based testing with ScalaCheck and Functional Java's quickcheck module. Functional Java is compiled with Java 8 targeting Java 7 bytecode. The use of lambdas within the project are backported with the Retro Lambda library, supporting Java versions 6 to 8. +The library is intended for use in production applications and is thoroughly tested using the technique of automated specification-based testing with ScalaCheck and Functional Java's quickcheck module. Functional Java provides abstractions for the following types: @@ -22,8 +22,9 @@ Important URLs for the project are: * Website, http://www.functionaljava.org * Website repository, http://github.com/functionaljava/functionaljava.github.io -* Travis continuous integration build, https://travis-ci.org/functionaljava/functionaljava -* Sonatype Repository, https://oss.sonatype.org/content/groups/public/org/functionaljava/ +* Travis continuous integration build, https://app.travis-ci.com/github/functionaljava/functionaljava +* Sonatype repository, https://oss.sonatype.org/content/groups/public/org/functionaljava/ +* Maven Central repository, https://mvnrepository.com/artifact/org.functionaljava/functionaljava == Downloading @@ -31,16 +32,15 @@ The recommended way to download and use the project is through your build tool. The Functional Java artifact is published to Maven Central using the group `org.functionaljava` with three published artifacts: -* the core library (`functionaljava` or `functionaljava_1.8` if you use Java 8+) -* Java 8 specific support (`functionaljava-java8`) -* property based testing (`functionaljava-quickcheck` or `functionaljava-quickcheck_1.8` if you use Java 8+) +* the core library (`functionaljava`) +* property based testing (`functionaljava-quickcheck`) +* a small amount of Java 8 support (`functionaljava-java-core`) -The latest stable version is `4.8.1`. This can be added to your Gradle project by adding the dependencies: +The latest stable version is `5.0`. This can be added to your Gradle project by adding the dependencies: ---- -compile "org.functionaljava:functionaljava:4.8.1" -compile "org.functionaljava:functionaljava-java8:4.8.1" -compile "org.functionaljava:functionaljava-quickcheck:4.8.1" -compile "org.functionaljava:functionaljava-java-core:4.8.1" +compile "org.functionaljava:functionaljava:5.0" +compile "org.functionaljava:functionaljava-quickcheck:5.0" +compile "org.functionaljava:functionaljava-java-core:5.0" ---- and in Maven: @@ -48,34 +48,27 @@ and in Maven: org.functionaljava functionaljava - 4.8.1 - - - org.functionaljava - functionaljava-java8 - 4.8.1 + 5.0 org.functionaljava functionaljava-quickcheck - 4.8.1 + 5.0 org.functionaljava functionaljava-java-core - 4.8.1 + 5.0 ---- == Building -FunctionalJava uses the Retro Lambda project to backport Java 8 lambdas to Java 6 bytecode. This requires access to both JDK 6 and 8. The build system requires the environment variables `JAVA6_HOME` and `JAVA8_HOME` to refer to the appropriate directories. - -Building is done using Gradle 2.13. In the root directory run: +Building is done using Java 8 and Gradle 7.4. In the root directory run: ---- ./gradlew ---- -This requires access to Java and will download the Gradle build tool and necessary dependencies and build FunctionalJava. +This requires access to Java 8 and will download the Gradle build tool and necessary dependencies and build FunctionalJava. == Features @@ -122,3 +115,7 @@ A more complete description of the features mentioned above are: == License link:etc/LICENCE[The Functional Java license] uses the BSD 3 license (3-clause license) available at https://en.wikipedia.org/wiki/BSD_licenses[]. + +== Release Notes + +For release notes for each version, see the directory link:etc/release-notes. diff --git a/build.gradle b/build.gradle index c9d8be40..c89c9536 100644 --- a/build.gradle +++ b/build.gradle @@ -1,33 +1,27 @@ defaultTasks 'build' -ext { -} +apply plugin: "com.github.ben-manes.versions" buildscript { - ext { - uptodateVersion = "1.6.3" - retrolambdaPluginVersion = "3.7.0" - retrolambdaVersion = "2.5.4" - } - repositories { mavenLocal() - jcenter() mavenCentral() + gradlePluginPortal() } dependencies { - classpath "com.ofg:uptodate-gradle-plugin:$uptodateVersion" - classpath "me.tatarka:gradle-retrolambda:$retrolambdaPluginVersion" + classpath "com.github.ben-manes:gradle-versions-plugin:0.42.0" + classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:6.1.0" } wrapper { - gradleVersion = "4.10.2" + gradleVersion = "7.4" distributionType = Wrapper.DistributionType.ALL } } + if (JavaVersion.current().isJava8Compatible()) { allprojects { tasks.withType(Javadoc) { @@ -41,21 +35,21 @@ allprojects { apply plugin: "jacoco" jacoco { - toolVersion = "0.8.2" + toolVersion = "0.8.7" } defaultTasks "build" ext { isSnapshot = true - fjBaseVersion = "4.9" + fjBaseVersion = "5.1" snapshotAppendix = "-SNAPSHOT" fjVersion = fjBaseVersion + (isSnapshot ? snapshotAppendix : "") - fjConsumeVersion = "4.8.1" + fjConsumeVersion = "5.0" signModule = false - useRetroLambda = false + uploadModule = false projectTitle = "Functional Java" projectName = "functionaljava" @@ -65,27 +59,32 @@ allprojects { projectUrl = "http://functionaljava.org/" scmUrl = "git://github.com/functionaljava/functionaljava.git" scmGitFile = "scm:git@github.com:functionaljava/functionaljava.git" + scmSshGitFile = "scm:git:ssh://git@github.com/functionaljava/functionaljava.git" + licenseUrl = "https://github.com/functionaljava/functionaljava/blob/master/etc/LICENCE" + licenseName = "The BSD3 License" + + issueUrl = "https://github.com/functionaljava/functionaljava/issues" + githubUrl = "https://github.com/functionaljava/functionaljava" sonatypeBaseUrl = "https://oss.sonatype.org" sonatypeSnapshotUrl = "$sonatypeBaseUrl/content/repositories/snapshots/" sonatypeRepositoryUrl = "$sonatypeBaseUrl/content/groups/public" sonatypeReleaseUrl = "$sonatypeBaseUrl/service/local/staging/deploy/maven2/" + sonatypeUploadUrl = isSnapshot ? sonatypeSnapshotUrl : sonatypeReleaseUrl + primaryEmail = "functionaljava@googlegroups.com" - dependencyJunit = "org.junit.vintage:junit-vintage-engine:5.2.0" + junitCompile = "junit:junit:4.13.2" + junitRuntime = "org.junit.vintage:junit-vintage-engine:5.8.2" displayCompilerWarnings = true - - newJdkEnvVar = "JAVA8_HOME" - oldJdkEnvVar = "JAVA7_HOME" - retroLambdaTarget = JavaVersion.VERSION_1_6 + generateTestReports = false } repositories { - jcenter() - mavenCentral() mavenLocal() + mavenCentral() } version = fjVersion @@ -98,29 +97,37 @@ subprojects { buildscript { repositories { + mavenLocal() mavenCentral() } } apply from: "$rootDir/lib.gradle" - apply plugin: "java" + apply plugin: "java-library" apply plugin: "eclipse" - apply plugin: "com.ofg.uptodate" repositories { mavenLocal() - jcenter() mavenCentral() maven { url sonatypeRepositoryUrl } } - if (displayCompilerWarnings) { - tasks.withType(JavaCompile) { + tasks.withType(JavaCompile) { + if (displayCompilerWarnings) { options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" - } - } + } + } + + tasks.withType(Test).configureEach { + maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1 + if (!generateTestReports) { + reports.html.required = false + reports.junitXml.required = false + } + } + } @@ -128,24 +135,26 @@ task coverage(type: org.gradle.testing.jacoco.tasks.JacocoReport) { dependsOn = subprojects*.test executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec") // We only care about coverage of: - def projectForFoverage = ["core", "java8", "quickcheck", "java-core"] - classDirectories = files(subprojects.findAll {subproject -> subproject.name in projectForFoverage} .sourceSets.main.output) - sourceDirectories = files(subprojects.findAll {subproject -> subproject.name in projectForFoverage} .sourceSets.main.allSource.srcDirs) + def projectForFoverage = ["core", "quickcheck", "java-core"] + getClassDirectories().from(files(subprojects.findAll {subproject -> subproject.name in projectForFoverage} .sourceSets.main.output)) + getSourceDirectories().from(files(subprojects.findAll {subproject -> subproject.name in projectForFoverage} .sourceSets.main.allSource.srcDirs)) reports { - html.enabled = true - xml.enabled = true + html.required = true + xml.required = true } } configure(subprojects.findAll { it.name != "props-core" }) { - apply plugin: "maven" + apply plugin: "maven-publish" apply plugin: "signing" - apply plugin: "osgi" - + apply plugin: "biz.aQute.bnd.builder" sourceCompatibility = "1.8" + javadoc { + } + task javadocJar(type: Jar, dependsOn: "javadoc") { classifier = 'javadoc' from "build/docs/javadoc" @@ -163,17 +172,14 @@ configure(subprojects.findAll { it.name != "props-core" }) { } jar { - version project.fjVersion - manifest { - name = 'Functional Java' - instruction 'Signature-Version', project.fjVersion - instruction 'Bundle-ActivationPolicy', 'lazy' - instruction 'Bundle-Vendor', 'functionaljava.org' - if(project.name != "core") { - instruction 'Require-Bundle', 'org.functionaljava;bundle-version="'+project.fjBaseVersion+'"' - } - instruction 'Automatic-Module-Name', "functionaljava${project.name == 'core' ? '' : ".$project.name"}" - } + archiveVersion = project.fjVersion + bnd ( + 'Bundle-Name': 'Functional Java', + 'Signature-Version': project.fjVersion, + 'Bundle-ActivationPolicy': 'lazy', + 'Bundle-Vendor': 'functionaljava.org', + 'Automatic-Module-Name': "functionaljava${project.name == 'core' ? '' : ".$project.name"}", + ) } eclipse { diff --git a/consume/build.gradle b/consume/build.gradle index ff92a601..45dadefb 100644 --- a/consume/build.gradle +++ b/consume/build.gradle @@ -2,7 +2,8 @@ archivesBaseName = "${project.projectName}-${project.name}" dependencies { - compile("$group:$projectName:$fjConsumeVersion") + api "$group:$projectName:$fjConsumeVersion" - testCompile dependencyJunit + testImplementation junitCompile + testRuntimeOnly junitRuntime } diff --git a/consume/src/test/java/fj/EmptyTest.java b/consume/src/test/java/fj/EmptyTest.java index e112a97a..13675822 100644 --- a/consume/src/test/java/fj/EmptyTest.java +++ b/consume/src/test/java/fj/EmptyTest.java @@ -5,9 +5,6 @@ import org.junit.Assert; -/** - * Created by MarkPerry on 30/08/2015. - */ public class EmptyTest { @Ignore @Test diff --git a/core/build.gradle b/core/build.gradle index 71dae913..282ec191 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,19 +1,18 @@ ext { signModule = true + uploadModule = true + } archivesBaseName = project.projectName dependencies { - testCompile dependencyJunit - testCompile 'com.h2database:h2:1.4.197' - testCompile 'commons-dbutils:commons-dbutils:1.7' + testImplementation junitCompile + testRuntimeOnly junitRuntime + testImplementation 'com.h2database:h2:2.1.210' + testImplementation 'commons-dbutils:commons-dbutils:1.7' } performSigning(signingEnabled, signModule) -configureUpload(signingEnabled, signModule) - -uploadArchives.enabled = true - -configureAllRetroLambda() +configureUpload(signingEnabled, signModule, uploadModule) diff --git a/core/src/main/java/fj/Bottom.java b/core/src/main/java/fj/Bottom.java index a70629f1..5cc7f4e4 100644 --- a/core/src/main/java/fj/Bottom.java +++ b/core/src/main/java/fj/Bottom.java @@ -2,8 +2,6 @@ /** * Represents the bottom _|_ value. - * - * @version %build.number% */ public final class Bottom { private Bottom() { diff --git a/core/src/main/java/fj/Bounded.java b/core/src/main/java/fj/Bounded.java new file mode 100644 index 00000000..e100b6d6 --- /dev/null +++ b/core/src/main/java/fj/Bounded.java @@ -0,0 +1,50 @@ +package fj; + +/** + * The Bounded class is used to name the upper and lower limits of a type. + * Ord is not a superclass of Bounded since types that are not totally ordered may also have upper and lower bounds. + */ +public final class Bounded { + + private final Definition def; + + /** + * Minimal definition of Bounded + */ + public interface Definition { + A min(); + + A max(); + } + + private Bounded(Definition definition) { + this.def = definition; + } + + public A min() { + return def.min(); + } + + public A max() { + return def.max(); + } + + public static Bounded boundedDef(Definition def) { + return new Bounded<>(def); + } + + public static Bounded bounded(A min, A max) { + return boundedDef(new Definition() { + @Override + public A min() { + return min; + } + + @Override + public A max() { + return max; + } + }); + } + +} diff --git a/core/src/main/java/fj/Class.java b/core/src/main/java/fj/Class.java index 56222de1..8bee1951 100644 --- a/core/src/main/java/fj/Class.java +++ b/core/src/main/java/fj/Class.java @@ -11,8 +11,6 @@ /** * A wrapper for a {@link java.lang.Class} that provides additional methods. - * - * @version %build.number% */ public final class Class { private final java.lang.Class c; diff --git a/core/src/main/java/fj/Digit.java b/core/src/main/java/fj/Digit.java index 7213e278..4fec7b0c 100644 --- a/core/src/main/java/fj/Digit.java +++ b/core/src/main/java/fj/Digit.java @@ -6,8 +6,6 @@ /** * The digits zero to nine. - * - * @version %build.number% */ public enum Digit { /** diff --git a/core/src/main/java/fj/Effect.java b/core/src/main/java/fj/Effect.java index 80680cac..2b5dcd6f 100644 --- a/core/src/main/java/fj/Effect.java +++ b/core/src/main/java/fj/Effect.java @@ -14,8 +14,6 @@ /** * Represents a side-effect. - * - * @version %build.number% */ public final class Effect { diff --git a/core/src/main/java/fj/Equal.java b/core/src/main/java/fj/Equal.java index 5cd31aeb..cba4a589 100644 --- a/core/src/main/java/fj/Equal.java +++ b/core/src/main/java/fj/Equal.java @@ -22,8 +22,6 @@ /** * Tests for equality between two objects. - * - * @version %build.number% */ public final class Equal { @@ -357,6 +355,14 @@ public static Equal> eitherEqual(final Equal ea, final Eq )); } + public static Equal> either3Equal(Equal ea, Equal eb, Equal ec) { + return equalDef((e1, e2) -> + optionEqual(ea).eq(e1.leftOption(), e2.leftOption()) && + optionEqual(eb).eq(e1.middleOption(), e2.middleOption()) && + optionEqual(ec).eq(e1.rightOption(), e2.rightOption()) + ); + } + public static Equal> resultEqual(final Equal ea, final Equal ei) { Definition eaDef = ea.def; Definition eiDef= ei.def; diff --git a/core/src/main/java/fj/F.java b/core/src/main/java/fj/F.java index e2db316e..1736f97b 100644 --- a/core/src/main/java/fj/F.java +++ b/core/src/main/java/fj/F.java @@ -1,12 +1,25 @@ package fj; +import fj.control.parallel.Actor; +import fj.control.parallel.Promise; +import fj.control.parallel.Strategy; +import fj.data.*; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.TreeSet; +import java.util.concurrent.*; +import java.util.function.Function; + +import static fj.data.Option.some; +import static fj.data.Stream.iterableStream; +import static fj.data.Zipper.fromStream; + /** - * A transformation or function from A to B. This type can be represented - * using the Java 7 closure syntax. - * - * @version %build.number% + * A transformation or function from A to B. */ -public interface F { +@FunctionalInterface +public interface F extends Function { /** * Transform A to B. * @@ -15,4 +28,668 @@ public interface F { */ B f(A a); + default B apply(A a) { + return f(a); + } + + /** + * Function composition + * + * @param g A function to compose with this one. + * @return The composed function such that this function is applied last. + */ + default F o(final F g) { + return c -> f(g.f(c)); + } + + /** + * First-class function composition + * + * @return A function that composes this function with another. + */ + default F, F> o() { + return g -> o(g); + } + + /** + * Function composition flipped. + * + * @param g A function with which to compose this one. + * @return The composed function such that this function is applied first. + */ + @SuppressWarnings("unchecked") + default F andThen(final F g) { + return g.o(this); + } + + /** + * First-class composition flipped. + * + * @return A function that invokes this function and then a given function on the result. + */ + default F, F> andThen() { + return g -> andThen(g); + } + + /** + * Binds a given function across this function (Reader Monad). + * + * @param g A function that takes the return value of this function as an argument, yielding a new function. + * @return A function that invokes this function on its argument and then the given function on the result. + */ + default F bind(final F> g) { + return a -> g.f(f(a)).f(a); + } + + /** + * First-class function binding. + * + * @return A function that binds another function across this function. + */ + default F>, F> bind() { + return g -> bind(g); + } + + /** + * Function application in an environment (Applicative Functor). + * + * @param g A function with the same argument type as this function, yielding a function that takes the return + * value of this function. + * @return A new function that invokes the given function on its argument, yielding a new function that is then + * applied to the result of applying this function to the argument. + */ + default F apply(final F> g) { + return a -> g.f(a).f(f(a)); + } + + /** + * First-class function application in an environment. + * + * @return A function that applies a given function within the environment of this function. + */ + default F>, F> apply() { + return g -> apply(g); + } + + /** + * Applies this function over the arguments of another function. + * + * @param g The function over whose arguments to apply this function. + * @return A new function that invokes this function on its arguments before invoking the given function. + */ + default F> on(final F> g) { + return a1 -> a2 -> g.f(f(a1)).f(f(a2)); + } + + + + /** + * Applies this function over the arguments of another function. + * + * @return A function that applies this function over the arguments of another function. + */ + default F>, F>> on() { + return g -> on(g); + } + + /** + * Promotes this function so that it returns its result in a product-1. Kleisli arrow for P1. + * + * @return This function promoted to return its result in a product-1. + */ + default F> lazy() { + return a -> P.lazy(() -> f(a)); + } + + /** + * Partial application. + * + * @param a The A to which to apply this function. + * @return The function partially applied to the given argument to return a lazy value. + */ + default P1 partial(final A a) { + return P.lazy(() -> f(a)); + } + + /** + * Promotes this function to map over a product-1. + * + * @return This function promoted to map over a product-1. + */ + default F, P1> mapP1() { + return p -> p.map(this); + } + + /** + * Promotes this function so that it returns its result in an Option. Kleisli arrow for Option. + * + * @return This function promoted to return its result in an Option. + */ + default F> optionK() { + return a -> some(f(a)); + } + + /** + * Promotes this function to map over an optional value. + * + * @return This function promoted to map over an optional value. + */ + default F, Option> mapOption() { + return o -> o.map(this); + } + + /** + * Promotes this function so that it returns its result in a List. Kleisli arrow for List. + * + * @return This function promoted to return its result in a List. + */ + default F> listK() { + return a -> List.single(f(a)); + } + + /** + * Promotes this function to map over a List. + * + * @return This function promoted to map over a List. + */ + default F, List> mapList() { + return x -> x.map(this); + } + + /** + * Promotes this function so that it returns its result in a Stream. Kleisli arrow for Stream. + * + * @return This function promoted to return its result in a Stream. + */ + default F> streamK() { + return a -> Stream.single(f(a)); + } + + /** + * Promotes this function to map over a Stream. + * + * @return This function promoted to map over a Stream. + */ + default F, Stream> mapStream() { + return x -> x.map(this); + } + + /** + * Promotes this function so that it returns its result in a Array. Kleisli arrow for Array. + * + * @return This function promoted to return its result in a Array. + */ + default F> arrayK() { + return a -> Array.single(f(a)); + + } + + /** + * Promotes this function to map over a Array. + * + * @return This function promoted to map over a Array. + */ + default F, Array> mapArray() { + return x -> x.map(this); + } + + /** + * Returns a function that contramaps over a given actor. + * + * @return A function that contramaps over a given actor. + */ + default F, Actor> contramapActor() { + return a -> a.contramap(this); + } + + /** + * Promotes this function to a concurrent function that returns a Promise of a value. + * + * @param s A parallel strategy for concurrent execution. + * @return A concurrent function that returns a Promise of a value. + */ + default F> promiseK(final Strategy s) { + return Promise.promise(s, this); + } + + /** + * Promotes this function to map over a Promise. + * + * @return This function promoted to map over Promises. + */ + default F, Promise> mapPromise() { + return p -> p.fmap(this); + } + + /** + * Promotes this function so that it returns its result on the left side of an Either. + * Kleisli arrow for the Either left projection. + * + * @return This function promoted to return its result on the left side of an Either. + */ + @SuppressWarnings("unchecked") + default F> eitherLeftK() { + return Either.left_().o(this); + } + + /** + * Promotes this function so that it returns its result on the right side of an Either. + * Kleisli arrow for the Either right projection. + * + * @return This function promoted to return its result on the right side of an Either. + */ + @SuppressWarnings("unchecked") + default F> eitherRightK() { + return Either.right_().o(this); + } + + /** + * Promotes this function to map over the left side of an Either. + * + * @return This function promoted to map over the left side of an Either. + */ + @SuppressWarnings("unchecked") + default F, Either> mapLeft() { + return Either.leftMap_().f(this); + } + + /** + * Promotes this function to map over the right side of an Either. + * + * @return This function promoted to map over the right side of an Either. + */ + @SuppressWarnings("unchecked") + default F, Either> mapRight() { + return Either.rightMap_().f(this); + } + + /** + * Returns a function that returns the left side of a given Either, or this function applied to the right side. + * + * @return a function that returns the left side of a given Either, or this function applied to the right side. + */ + default F, B> onLeft() { + return e -> e.left().on(this); + } + + /** + * Returns a function that returns the right side of a given Either, or this function applied to the left side. + * + * @return a function that returns the right side of a given Either, or this function applied to the left side. + */ + default F, B> onRight() { + return e -> e.right().on(this); + } + + /** + * Promotes this function to return its value in an Iterable. + * + * @return This function promoted to return its value in an Iterable. + */ + @SuppressWarnings("unchecked") + default F> iterableK() { + return IterableW.arrow().f(this); + } + + /** + * Promotes this function to map over Iterables. + * + * @return This function promoted to map over Iterables. + */ + @SuppressWarnings("unchecked") + default F, IterableW> mapIterable() { + return IterableW.map().f(this).o(IterableW.wrap()); + } + + /** + * Promotes this function to return its value in a NonEmptyList. + * + * @return This function promoted to return its value in a NonEmptyList. + */ + @SuppressWarnings("unchecked") + default F> nelK() { + return NonEmptyList.nel().o(this); + } + + /** + * Promotes this function to map over a NonEmptyList. + * + * @return This function promoted to map over a NonEmptyList. + */ + default F, NonEmptyList> mapNel() { + return list -> list.map(this); + } + + /** + * Promotes this function to return its value in a Set. + * + * @param o An order for the set. + * @return This function promoted to return its value in a Set. + */ + default F> setK(final Ord o) { + return a -> Set.single(o, f(a)); + } + + /** + * Promotes this function to map over a Set. + * + * @param o An order for the resulting set. + * @return This function promoted to map over a Set. + */ + default F, Set> mapSet(final Ord o) { + return s -> s.map(o, this); + } + + /** + * Promotes this function to return its value in a Tree. + * + * @return This function promoted to return its value in a Tree. + */ + default F> treeK() { + return a -> Tree.leaf(f(a)); + } + + /** + * Promotes this function to map over a Tree. + * + * @return This function promoted to map over a Tree. + */ + @SuppressWarnings("unchecked") + default F, Tree> mapTree() { + return Tree.fmap_().f(this); + } + + /** + * Returns a function that maps this function over a tree and folds it with the given monoid. + * + * @param m The monoid with which to fold the mapped tree. + * @return a function that maps this function over a tree and folds it with the given monoid. + */ + default F, B> foldMapTree(final Monoid m) { + return Tree.foldMap_(this, m); + } + + /** + * Promotes this function to return its value in a TreeZipper. + * + * @return This function promoted to return its value in a TreeZipper. + */ + default F> treeZipperK() { + return treeK().andThen(TreeZipper.fromTree()); + } + + /** + * Promotes this function to map over a TreeZipper. + * + * @return This function promoted to map over a TreeZipper. + */ + default F, TreeZipper> mapTreeZipper() { + return z -> z.map(this); + } + + /** + * Promotes this function so that it returns its result on the failure side of a Validation. + * Kleisli arrow for the Validation failure projection. + * + * @return This function promoted to return its result on the failure side of a Validation. + */ + default F> failK() { + return a -> Validation.fail(f(a)); + + } + + /** + * Promotes this function so that it returns its result on the success side of an Validation. + * Kleisli arrow for the Validation success projection. + * + * @return This function promoted to return its result on the success side of an Validation. + */ + default F> successK() { + return a -> Validation.success(f(a)); + } + + /** + * Promotes this function to map over the failure side of a Validation. + * + * @return This function promoted to map over the failure side of a Validation. + */ + default F, Validation> mapFail() { + return v -> v.f().map(this); + } + + /** + * Promotes this function to map over the success side of a Validation. + * + * @return This function promoted to map over the success side of a Validation. + */ + default F, Validation> mapSuccess() { + return v -> v.map(this); + } + + /** + * Returns a function that returns the failure side of a given Validation, + * or this function applied to the success side. + * + * @return a function that returns the failure side of a given Validation, + * or this function applied to the success side. + */ + default F, B> onFail() { + return v -> v.f().on(this); + } + + /** + * Returns a function that returns the success side of a given Validation, + * or this function applied to the failure side. + * + * @return a function that returns the success side of a given Validation, + * or this function applied to the failure side. + */ + default F, B> onSuccess() { + return v -> v.on(this); + } + + /** + * Promotes this function to return its value in a Zipper. + * + * @return This function promoted to return its value in a Zipper. + */ + default F> zipperK() { + return streamK().andThen(s -> fromStream(s).some()); + } + + /** + * Promotes this function to map over a Zipper. + * + * @return This function promoted to map over a Zipper. + */ + default F, Zipper> mapZipper() { + return z -> z.map(this); + } + + /** + * Promotes this function to map over an Equal as a contravariant functor. + * + * @return This function promoted to map over an Equal as a contravariant functor. + */ + default F, Equal> contramapEqual() { + return e -> e.contramap(this); + } + + /** + * Promotes this function to map over a Hash as a contravariant functor. + * + * @return This function promoted to map over a Hash as a contravariant functor. + */ + default F, Hash> contramapHash() { + return h -> h.contramap(this); + } + + /** + * Promotes this function to map over a Show as a contravariant functor. + * + * @return This function promoted to map over a Show as a contravariant functor. + */ + default F, Show> contramapShow() { + return s -> s.contramap(this); + } + + /** + * Promotes this function to map over the first element of a pair. + * + * @return This function promoted to map over the first element of a pair. + */ + default F, P2> mapFst() { + return P2.map1_(this); + } + + /** + * Promotes this function to map over the second element of a pair. + * + * @return This function promoted to map over the second element of a pair. + */ + default F, P2> mapSnd() { + return P2.map2_(this); + } + + /** + * Promotes this function to map over both elements of a pair. + * + * @return This function promoted to map over both elements of a pair. + */ + default F, P2> mapBoth() { + return p2 -> P2.map(this, p2); + } + + /** + * Maps this function over a SynchronousQueue. + * + * @param as A SynchronousQueue to map this function over. + * @return A new SynchronousQueue with this function applied to each element. + */ + default SynchronousQueue mapJ(final SynchronousQueue as) { + final SynchronousQueue bs = new SynchronousQueue<>(); + bs.addAll(iterableStream(as).map(this).toCollection()); + return bs; + } + + + /** + * Maps this function over a PriorityBlockingQueue. + * + * @param as A PriorityBlockingQueue to map this function over. + * @return A new PriorityBlockingQueue with this function applied to each element. + */ + default PriorityBlockingQueue mapJ(final PriorityBlockingQueue as) { + return new PriorityBlockingQueue<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over a LinkedBlockingQueue. + * + * @param as A LinkedBlockingQueue to map this function over. + * @return A new LinkedBlockingQueue with this function applied to each element. + */ + default LinkedBlockingQueue mapJ(final LinkedBlockingQueue as) { + return new LinkedBlockingQueue<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over a CopyOnWriteArraySet. + * + * @param as A CopyOnWriteArraySet to map this function over. + * @return A new CopyOnWriteArraySet with this function applied to each element. + */ + default CopyOnWriteArraySet mapJ(final CopyOnWriteArraySet as) { + return new CopyOnWriteArraySet<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over a CopyOnWriteArrayList. + * + * @param as A CopyOnWriteArrayList to map this function over. + * @return A new CopyOnWriteArrayList with this function applied to each element. + */ + default CopyOnWriteArrayList mapJ(final CopyOnWriteArrayList as) { + return new CopyOnWriteArrayList<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over a ConcurrentLinkedQueue. + * + * @param as A ConcurrentLinkedQueue to map this function over. + * @return A new ConcurrentLinkedQueue with this function applied to each element. + */ + default ConcurrentLinkedQueue mapJ(final ConcurrentLinkedQueue as) { + return new ConcurrentLinkedQueue<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over an ArrayBlockingQueue. + * + * @param as An ArrayBlockingQueue to map this function over. + * @return A new ArrayBlockingQueue with this function applied to each element. + */ + default ArrayBlockingQueue mapJ(final ArrayBlockingQueue as) { + final ArrayBlockingQueue bs = new ArrayBlockingQueue<>(as.size()); + bs.addAll(iterableStream(as).map(this).toCollection()); + return bs; + } + + + /** + * Maps this function over a TreeSet. + * + * @param as A TreeSet to map this function over. + * @return A new TreeSet with this function applied to each element. + */ + default TreeSet mapJ(final TreeSet as) { + return new TreeSet<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over a PriorityQueue. + * + * @param as A PriorityQueue to map this function over. + * @return A new PriorityQueue with this function applied to each element. + */ + default java.util.PriorityQueue mapJ(final java.util.PriorityQueue as) { + return new java.util.PriorityQueue<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over a LinkedList. + * + * @param as A LinkedList to map this function over. + * @return A new LinkedList with this function applied to each element. + */ + default LinkedList mapJ(final LinkedList as) { + return new LinkedList<>(iterableStream(as).map(this).toCollection()); + } + + /** + * Maps this function over an ArrayList. + * + * @param as An ArrayList to map this function over. + * @return A new ArrayList with this function applied to each element. + */ + default ArrayList mapJ(final ArrayList as) { + return new ArrayList<>(iterableStream(as).map(this).toCollection()); + } + + default F map(F f) { + return f.o(this); + } + + default F contramap(F f) { + return o(f); + } + + /** + * Both map (with g) and contramap (with f) the target function. (Profunctor pattern) + */ + default F dimap(F f, F g) { + return c -> g.f(f(f.f(c))); + } + + } diff --git a/core/src/main/java/fj/F0.java b/core/src/main/java/fj/F0.java index eab82a01..88d60a9e 100644 --- a/core/src/main/java/fj/F0.java +++ b/core/src/main/java/fj/F0.java @@ -1,10 +1,34 @@ package fj; -/** - * Created by MarkPerry on 21/01/2015. - */ -public interface F0 { +import fj.function.Effect0; +import fj.function.Try0; +import fj.function.TryEffect0; + +import java.util.function.Supplier; + +@FunctionalInterface +public interface F0 extends Supplier { A f(); + default A get() { + return f(); + } + + default Effect0 toEffect0() { + return () -> f(); + } + + default TryEffect0 toTryEffect0() { + return () -> f(); + } + + default Try0 toTry0() { + return () -> f(); + } + + default P1 toP1() { + return P.lazy(() -> f()); + } + } diff --git a/core/src/main/java/fj/F1Functions.java b/core/src/main/java/fj/F1Functions.java deleted file mode 100644 index 00bdf515..00000000 --- a/core/src/main/java/fj/F1Functions.java +++ /dev/null @@ -1,687 +0,0 @@ -package fj; - -import fj.control.parallel.Actor; -import fj.control.parallel.Promise; -import fj.control.parallel.Strategy; -import fj.data.*; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.PriorityQueue; -import java.util.TreeSet; -import java.util.concurrent.*; - -import static fj.data.Option.some; -import static fj.data.Stream.iterableStream; -import static fj.data.Zipper.fromStream; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F1Functions { - - - private F1Functions() { - } - - /** - * Function composition - * - * @param g A function to compose with this one. - * @return The composed function such that this function is applied last. - */ - public static F o(final F f, final F g) { - return c -> f.f(g.f(c)); - } - - /** - * First-class function composition - * - * @return A function that composes this function with another. - */ - public static F, F> o(final F f) { - return g -> o(f, g); - } - - /** - * Function composition flipped. - * - * @param g A function with which to compose this one. - * @return The composed function such that this function is applied first. - */ - @SuppressWarnings("unchecked") - public static F andThen(final F f, final F g) { - return o(g, f); - } - - /** - * First-class composition flipped. - * - * @return A function that invokes this function and then a given function on the result. - */ - public static F, F> andThen(final F f) { - return g -> andThen(f, g); - } - - /** - * Binds a given function across this function (Reader Monad). - * - * @param g A function that takes the return value of this function as an argument, yielding a new function. - * @return A function that invokes this function on its argument and then the given function on the result. - */ - public static F bind(final F f, final F> g) { - return a -> g.f(f.f(a)).f(a); - } - - /** - * First-class function binding. - * - * @return A function that binds another function across this function. - */ - public static F>, F> bind(final F f) { - return g -> bind(f, g); - } - - /** - * Function application in an environment (Applicative Functor). - * - * @param g A function with the same argument type as this function, yielding a function that takes the return - * value of this function. - * @return A new function that invokes the given function on its argument, yielding a new function that is then - * applied to the result of applying this function to the argument. - */ - public static F apply(final F f, final F> g) { - return a -> g.f(a).f(f.f(a)); - } - - /** - * First-class function application in an environment. - * - * @return A function that applies a given function within the environment of this function. - */ - public static F>, F> apply(final F f) { - return g -> apply(f, g); - } - - /** - * Applies this function over the arguments of another function. - * - * @param g The function over whose arguments to apply this function. - * @return A new function that invokes this function on its arguments before invoking the given function. - */ - public static F> on(final F f, final F> g) { - return a1 -> a2 -> g.f(f.f(a1)).f(f.f(a2)); - } - - - - /** - * Applies this function over the arguments of another function. - * - * @return A function that applies this function over the arguments of another function. - */ - public static F>, F>> on(final F f) { - return g -> on(f, g); - } - - /** - * Promotes this function so that it returns its result in a product-1. Kleisli arrow for P1. - * - * @return This function promoted to return its result in a product-1. - */ - public static F> lazy(final F f) { - return a -> P.lazy(() -> f.f(a)); - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument to return a lazy value. - */ - public static P1 f(final F f, final A a) { - return P.lazy(() -> f.f(a)); - } - - /** - * Promotes this function to map over a product-1. - * - * @return This function promoted to map over a product-1. - */ - public static F, P1> mapP1(final F f) { - return p -> p.map(f); - } - - /** - * Promotes this function so that it returns its result in an Option. Kleisli arrow for Option. - * - * @return This function promoted to return its result in an Option. - */ - public static F> optionK(final F f) { - return a -> some(f.f(a)); - } - - /** - * Promotes this function to map over an optional value. - * - * @return This function promoted to map over an optional value. - */ - public static F, Option> mapOption(final F f) { - return o -> o.map(f); - } - - /** - * Promotes this function so that it returns its result in a List. Kleisli arrow for List. - * - * @return This function promoted to return its result in a List. - */ - public static F> listK(final F f) { - return a -> List.single(f.f(a)); - } - - /** - * Promotes this function to map over a List. - * - * @return This function promoted to map over a List. - */ - public static F, List> mapList(final F f) { - return x -> x.map(f); - } - - /** - * Promotes this function so that it returns its result in a Stream. Kleisli arrow for Stream. - * - * @return This function promoted to return its result in a Stream. - */ - public static F> streamK(final F f) { - return a -> Stream.single(f.f(a)); - } - - /** - * Promotes this function to map over a Stream. - * - * @return This function promoted to map over a Stream. - */ - public static F, Stream> mapStream(final F f) { - return x -> x.map(f); - } - - /** - * Promotes this function so that it returns its result in a Array. Kleisli arrow for Array. - * - * @return This function promoted to return its result in a Array. - */ - public static F> arrayK(final F f) { - return a -> Array.single(f.f(a)); - - } - - /** - * Promotes this function to map over a Array. - * - * @return This function promoted to map over a Array. - */ - public static F, Array> mapArray(final F f) { - return x -> x.map(f); - } - - /** - * Returns a function that contramaps over a given actor. - * - * @return A function that contramaps over a given actor. - */ - public static F, Actor> contramapActor(final F f) { - return a -> a.contramap(f); - } - - /** - * Promotes this function to a concurrent function that returns a Promise of a value. - * - * @param s A parallel strategy for concurrent execution. - * @return A concurrent function that returns a Promise of a value. - */ - public static F> promiseK(final F f, final Strategy s) { - return Promise.promise(s, f); - } - - /** - * Promotes this function to map over a Promise. - * - * @return This function promoted to map over Promises. - */ - public static F, Promise> mapPromise(final F f) { - return p -> p.fmap(f); - } - - /** - * Promotes this function so that it returns its result on the left side of an Either. - * Kleisli arrow for the Either left projection. - * - * @return This function promoted to return its result on the left side of an Either. - */ - @SuppressWarnings("unchecked") - public static F> eitherLeftK(final F f) { - return o(Either.left_(), f); - } - - /** - * Promotes this function so that it returns its result on the right side of an Either. - * Kleisli arrow for the Either right projection. - * - * @return This function promoted to return its result on the right side of an Either. - */ - @SuppressWarnings("unchecked") - public static F> eitherRightK(final F f) { - return o(Either.right_(), f); - } - - /** - * Promotes this function to map over the left side of an Either. - * - * @return This function promoted to map over the left side of an Either. - */ - @SuppressWarnings("unchecked") - public static F, Either> mapLeft(final F f) { - return Either.leftMap_().f(f); - } - - /** - * Promotes this function to map over the right side of an Either. - * - * @return This function promoted to map over the right side of an Either. - */ - @SuppressWarnings("unchecked") - public static F, Either> mapRight(final F f) { - return Either.rightMap_().f(f); - } - - /** - * Returns a function that returns the left side of a given Either, or this function applied to the right side. - * - * @return a function that returns the left side of a given Either, or this function applied to the right side. - */ - public static F, B> onLeft(final F f) { - return e -> e.left().on(f); - } - - /** - * Returns a function that returns the right side of a given Either, or this function applied to the left side. - * - * @return a function that returns the right side of a given Either, or this function applied to the left side. - */ - public static F, B> onRight(final F f) { - return e -> e.right().on(f); - } - - /** - * Promotes this function to return its value in an Iterable. - * - * @return This function promoted to return its value in an Iterable. - */ - @SuppressWarnings("unchecked") - public static F> iterableK(final F f) { - return IterableW.arrow().f(f); - } - - /** - * Promotes this function to map over Iterables. - * - * @return This function promoted to map over Iterables. - */ - @SuppressWarnings("unchecked") - public static F, IterableW> mapIterable(final F f) { - return o(IterableW.map().f(f), IterableW.wrap()); - } - - /** - * Promotes this function to return its value in a NonEmptyList. - * - * @return This function promoted to return its value in a NonEmptyList. - */ - @SuppressWarnings("unchecked") - public static F> nelK(final F f) { - return o(NonEmptyList.nel(), f); - } - - /** - * Promotes this function to map over a NonEmptyList. - * - * @return This function promoted to map over a NonEmptyList. - */ - public static F, NonEmptyList> mapNel(final F f) { - return list -> list.map(f); - } - - /** - * Promotes this function to return its value in a Set. - * - * @param o An order for the set. - * @return This function promoted to return its value in a Set. - */ - public static F> setK(final F f, final Ord o - ) { - return a -> Set.single(o, f.f(a)); - } - - /** - * Promotes this function to map over a Set. - * - * @param o An order for the resulting set. - * @return This function promoted to map over a Set. - */ - public static F, Set> mapSet(final F f, final Ord o) { - return s -> s.map(o, f); - } - - /** - * Promotes this function to return its value in a Tree. - * - * @return This function promoted to return its value in a Tree. - */ - public static F> treeK(final F f) { - return a -> Tree.leaf(f.f(a)); - } - - /** - * Promotes this function to map over a Tree. - * - * @return This function promoted to map over a Tree. - */ - @SuppressWarnings("unchecked") - public static F, Tree> mapTree(final F f) { - return Tree.fmap_().f(f); - } - - /** - * Returns a function that maps this function over a tree and folds it with the given monoid. - * - * @param m The monoid with which to fold the mapped tree. - * @return a function that maps this function over a tree and folds it with the given monoid. - */ - public static F, B> foldMapTree(final F f, final Monoid m) { - return Tree.foldMap_(f, m); - } - - /** - * Promotes this function to return its value in a TreeZipper. - * - * @return This function promoted to return its value in a TreeZipper. - */ - public static F> treeZipperK(final F f) { - return andThen(treeK(f), TreeZipper.fromTree()); - } - - /** - * Promotes this function to map over a TreeZipper. - * - * @return This function promoted to map over a TreeZipper. - */ - public static F, TreeZipper> mapTreeZipper(final F f) { - return (z) -> z.map(f); - } - - /** - * Promotes this function so that it returns its result on the failure side of a Validation. - * Kleisli arrow for the Validation failure projection. - * - * @return This function promoted to return its result on the failure side of a Validation. - */ - public static F> failK(final F f) { - return a -> Validation.fail(f.f(a)); - - } - - /** - * Promotes this function so that it returns its result on the success side of an Validation. - * Kleisli arrow for the Validation success projection. - * - * @return This function promoted to return its result on the success side of an Validation. - */ - public static F> successK(final F f) { - return a -> Validation.success(f.f(a)); - } - - /** - * Promotes this function to map over the failure side of a Validation. - * - * @return This function promoted to map over the failure side of a Validation. - */ - public static F, Validation> mapFail(final F f) { - return v -> v.f().map(f); - } - - /** - * Promotes this function to map over the success side of a Validation. - * - * @return This function promoted to map over the success side of a Validation. - */ - public static F, Validation> mapSuccess(final F f) { - return v -> v.map(f); - } - - /** - * Returns a function that returns the failure side of a given Validation, - * or this function applied to the success side. - * - * @return a function that returns the failure side of a given Validation, - * or this function applied to the success side. - */ - public static F, B> onFail(final F f) { - return v -> v.f().on(f); - } - - /** - * Returns a function that returns the success side of a given Validation, - * or this function applied to the failure side. - * - * @return a function that returns the success side of a given Validation, - * or this function applied to the failure side. - */ - public static F, B> onSuccess(final F f) { - return v -> v.on(f); - } - - /** - * Promotes this function to return its value in a Zipper. - * - * @return This function promoted to return its value in a Zipper. - */ - public static F> zipperK(final F f) { - return andThen(streamK(f), s -> fromStream(s).some()); - } - - /** - * Promotes this function to map over a Zipper. - * - * @return This function promoted to map over a Zipper. - */ - public static F, Zipper> mapZipper(final F f) { - return z -> z.map(f); - } - - /** - * Promotes this function to map over an Equal as a contravariant functor. - * - * @return This function promoted to map over an Equal as a contravariant functor. - */ - public static F, Equal> contramapEqual(final F f) { - return e -> e.contramap(f); - } - - /** - * Promotes this function to map over a Hash as a contravariant functor. - * - * @return This function promoted to map over a Hash as a contravariant functor. - */ - public static F, Hash> contramapHash(final F f) { - return h -> h.contramap(f); - } - - /** - * Promotes this function to map over a Show as a contravariant functor. - * - * @return This function promoted to map over a Show as a contravariant functor. - */ - public static F, Show> contramapShow(final F f) { - return s -> s.contramap(f); - } - - /** - * Promotes this function to map over the first element of a pair. - * - * @return This function promoted to map over the first element of a pair. - */ - public static F, P2> mapFst(final F f) { - return P2.map1_(f); - } - - /** - * Promotes this function to map over the second element of a pair. - * - * @return This function promoted to map over the second element of a pair. - */ - public static F, P2> mapSnd(final F f) { - return P2.map2_(f); - } - - /** - * Promotes this function to map over both elements of a pair. - * - * @return This function promoted to map over both elements of a pair. - */ - public static F, P2> mapBoth(final F f) { - return p2 -> P2.map(f, p2); - } - - /** - * Maps this function over a SynchronousQueue. - * - * @param as A SynchronousQueue to map this function over. - * @return A new SynchronousQueue with this function applied to each element. - */ - public static SynchronousQueue mapJ(final F f, final SynchronousQueue as) { - final SynchronousQueue bs = new SynchronousQueue<>(); - bs.addAll(iterableStream(as).map(f).toCollection()); - return bs; - } - - - /** - * Maps this function over a PriorityBlockingQueue. - * - * @param as A PriorityBlockingQueue to map this function over. - * @return A new PriorityBlockingQueue with this function applied to each element. - */ - public static PriorityBlockingQueue mapJ(final F f, final PriorityBlockingQueue as) { - return new PriorityBlockingQueue<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over a LinkedBlockingQueue. - * - * @param as A LinkedBlockingQueue to map this function over. - * @return A new LinkedBlockingQueue with this function applied to each element. - */ - public static LinkedBlockingQueue mapJ(final F f, final LinkedBlockingQueue as) { - return new LinkedBlockingQueue<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over a CopyOnWriteArraySet. - * - * @param as A CopyOnWriteArraySet to map this function over. - * @return A new CopyOnWriteArraySet with this function applied to each element. - */ - public static CopyOnWriteArraySet mapJ(final F f, final CopyOnWriteArraySet as) { - return new CopyOnWriteArraySet<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over a CopyOnWriteArrayList. - * - * @param as A CopyOnWriteArrayList to map this function over. - * @return A new CopyOnWriteArrayList with this function applied to each element. - */ - public static CopyOnWriteArrayList mapJ(final F f, final CopyOnWriteArrayList as) { - return new CopyOnWriteArrayList<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over a ConcurrentLinkedQueue. - * - * @param as A ConcurrentLinkedQueue to map this function over. - * @return A new ConcurrentLinkedQueue with this function applied to each element. - */ - public static ConcurrentLinkedQueue mapJ(final F f, final ConcurrentLinkedQueue as) { - return new ConcurrentLinkedQueue<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over an ArrayBlockingQueue. - * - * @param as An ArrayBlockingQueue to map this function over. - * @return A new ArrayBlockingQueue with this function applied to each element. - */ - public static ArrayBlockingQueue mapJ(final F f, final ArrayBlockingQueue as) { - final ArrayBlockingQueue bs = new ArrayBlockingQueue<>(as.size()); - bs.addAll(iterableStream(as).map(f).toCollection()); - return bs; - } - - - /** - * Maps this function over a TreeSet. - * - * @param as A TreeSet to map this function over. - * @return A new TreeSet with this function applied to each element. - */ - public static TreeSet mapJ(final F f, final TreeSet as) { - return new TreeSet<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over a PriorityQueue. - * - * @param as A PriorityQueue to map this function over. - * @return A new PriorityQueue with this function applied to each element. - */ - public static PriorityQueue mapJ(final F f, final PriorityQueue as) { - return new PriorityQueue<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over a LinkedList. - * - * @param as A LinkedList to map this function over. - * @return A new LinkedList with this function applied to each element. - */ - public static LinkedList mapJ(final F f, final LinkedList as) { - return new LinkedList<>(iterableStream(as).map(f).toCollection()); - } - - /** - * Maps this function over an ArrayList. - * - * @param as An ArrayList to map this function over. - * @return A new ArrayList with this function applied to each element. - */ - public static ArrayList mapJ(final F f, final ArrayList as) { - return new ArrayList<>(iterableStream(as).map(f).toCollection()); - } - - public static F map(F target, F f) { - return o(f, target); - } - - public static F contramap(F target, F f) { - return o(target, f); - } - - /** - * Both map (with g) and contramap (with f) the target function. (Profunctor pattern) - */ - public static F dimap(F target, F f, F g) { - return c -> g.f(target.f(f.f(c))); - } - -} diff --git a/core/src/main/java/fj/F1W.java b/core/src/main/java/fj/F1W.java deleted file mode 100644 index 611c9e01..00000000 --- a/core/src/main/java/fj/F1W.java +++ /dev/null @@ -1,687 +0,0 @@ -package fj; - -import fj.control.parallel.Actor; -import fj.control.parallel.Promise; -import fj.control.parallel.Strategy; -import fj.data.*; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.PriorityQueue; -import java.util.TreeSet; -import java.util.concurrent.*; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F1W implements F { - - /** - * Function composition - * - * @param g A function to compose with this one. - * @return The composed function such that this function is applied last. - */ - public final F1W o(final F g) { - return lift(F1Functions.o(this, g)); - } - - /** - * First-class function composition - * - * @return A function that composes this function with another. - */ - public final F1W, F> o() { - return lift(F1Functions.o(this)); - } - - /** - * Function composition flipped. - * - * @param g A function with which to compose this one. - * @return The composed function such that this function is applied first. - */ - @SuppressWarnings("unchecked") - public final F1W andThen(final F g) { - return lift(F1Functions.andThen(this, g)); - } - - /** - * First-class composition flipped. - * - * @return A function that invokes this function and then a given function on the result. - */ - public final F1W, F> andThen() { - return lift( F1Functions.andThen(this)); - } - - /** - * Binds a given function across this function (Reader Monad). - * - * @param g A function that takes the return value of this function as an argument, yielding a new function. - * @return A function that invokes this function on its argument and then the given function on the result. - */ - public final F1W bind(final F> g) { - return lift(F1Functions.bind(this, g)); - } - - - /** - * First-class function binding. - * - * @return A function that binds another function across this function. - */ - public final F1W>, F> bind() { - return lift(F1Functions.bind(this)); - } - - /** - * Function application in an environment (Applicative Functor). - * - * @param g A function with the same argument type as this function, yielding a function that takes the return - * value of this function. - * @return A new function that invokes the given function on its argument, yielding a new function that is then - * applied to the result of applying this function to the argument. - */ - public final F1W apply(final F> g) { - return lift(F1Functions.apply(this, g)); - } - - - /** - * First-class function application in an environment. - * - * @return A function that applies a given function within the environment of this function. - */ - public final F1W>, F> apply() { - return lift(F1Functions.apply(this)); - } - - /** - * Applies this function over the arguments of another function. - * - * @param g The function over whose arguments to apply this function. - * @return A new function that invokes this function on its arguments before invoking the given function. - */ - public final F1W> on(final F> g) { - return lift(F1Functions.on(this, g)); - } - - - /** - * Applies this function over the arguments of another function. - * - * @return A function that applies this function over the arguments of another function. - */ - public final F1W>, F>> on() { - return lift(F1Functions.on(this)); - } - - /** - * Promotes this function so that it returns its result in a product-1. Kleisli arrow for P1. - * - * @return This function promoted to return its result in a product-1. - */ - public final F1W> lazy() { - return lift(F1Functions.lazy(this)); - } - - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument to return a lazy value. - */ - public final P1 lazy(final A a) { - return F1Functions.f(this, a); - } - - /** - * Promotes this function to map over a product-1. - * - * @return This function promoted to map over a product-1. - */ - public final F1W, P1> mapP1() { - return lift(F1Functions.mapP1(this)); - } - - /** - * Promotes this function so that it returns its result in an Option. Kleisli arrow for Option. - * - * @return This function promoted to return its result in an Option. - */ - public final F1W> optionK() { - return lift(F1Functions.optionK(this)); - } - - - /** - * Promotes this function to map over an optional value. - * - * @return This function promoted to map over an optional value. - */ - public final F1W, Option> mapOption() { - return lift(F1Functions.mapOption(this)); - } - - /** - * Promotes this function so that it returns its result in a List. Kleisli arrow for List. - * - * @return This function promoted to return its result in a List. - */ - public final F1W> listK() { - return lift( F1Functions.listK(this)); - } - - /** - * Promotes this function to map over a List. - * - * @return This function promoted to map over a List. - */ - public final F1W, List> mapList() { - return lift(F1Functions.mapList(this)); - } - - /** - * Promotes this function so that it returns its result in a Stream. Kleisli arrow for Stream. - * - * @return This function promoted to return its result in a Stream. - */ - public final F1W> streamK() { - return lift(F1Functions.streamK(this)); - } - - /** - * Promotes this function to map over a Stream. - * - * @return This function promoted to map over a Stream. - */ - public final F1W, Stream> mapStream() { - return lift(F1Functions.mapStream(this)); - } - - /** - * Promotes this function so that it returns its result in a Array. Kleisli arrow for Array. - * - * @return This function promoted to return its result in a Array. - */ - public final F1W> arrayK() { - return lift(F1Functions.arrayK(this)); - - } - - /** - * Promotes this function to map over a Array. - * - * @return This function promoted to map over a Array. - */ - public final F1W, Array> mapArray() { - return lift(F1Functions.mapArray(this)); - } - - /** - * Returns a function that contramaps over a given actor. - * - * @return A function that contramaps over a given actor. - */ - public final F1W, Actor> contramapActor() { - return lift(F1Functions.contramapActor(this)); - } - - /** - * Promotes this function to a concurrent function that returns a Promise of a value. - * - * @param s A parallel strategy for concurrent execution. - * @return A concurrent function that returns a Promise of a value. - */ - public final F1W> promiseK(final Strategy s) { - return lift(F1Functions.promiseK(this, s)); - } - - /** - * Promotes this function to map over a Promise. - * - * @return This function promoted to map over Promises. - */ - public final F1W, Promise> mapPromise() { - return lift(F1Functions.mapPromise(this)); - } - - /** - * Promotes this function so that it returns its result on the left side of an Either. - * Kleisli arrow for the Either left projection. - * - * @return This function promoted to return its result on the left side of an Either. - */ - @SuppressWarnings("unchecked") - public final F1W> eitherLeftK() { - return lift(F1Functions.eitherLeftK(this)); - } - - /** - * Promotes this function so that it returns its result on the right side of an Either. - * Kleisli arrow for the Either right projection. - * - * @return This function promoted to return its result on the right side of an Either. - */ - @SuppressWarnings("unchecked") - public final F1W> eitherRightK() { - return lift(F1Functions.eitherRightK(this)); - } - - /** - * Promotes this function to map over the left side of an Either. - * - * @return This function promoted to map over the left side of an Either. - */ - @SuppressWarnings("unchecked") - public final F1W, Either> mapLeft() { - return lift(F1Functions.mapLeft(this)); - } - - /** - * Promotes this function to map over the right side of an Either. - * - * @return This function promoted to map over the right side of an Either. - */ - @SuppressWarnings("unchecked") - public final F1W, Either> mapRight() { - return lift(F1Functions.mapRight(this)); - } - - /** - * Returns a function that returns the left side of a given Either, or this function applied to the right side. - * - * @return a function that returns the left side of a given Either, or this function applied to the right side. - */ - public final F1W, B> onLeft() { - return lift(F1Functions.onLeft(this)); - } - - /** - * Returns a function that returns the right side of a given Either, or this function applied to the left side. - * - * @return a function that returns the right side of a given Either, or this function applied to the left side. - */ - public final F1W, B> onRight() { - return lift(F1Functions.onRight(this)); - } - - /** - * Promotes this function to return its value in an Iterable. - * - * @return This function promoted to return its value in an Iterable. - */ - @SuppressWarnings("unchecked") - public final F1W> iterableK() { - return lift( F1Functions.iterableK(this)); - } - - /** - * Promotes this function to map over Iterables. - * - * @return This function promoted to map over Iterables. - */ - @SuppressWarnings("unchecked") - public final F1W, IterableW> mapIterable() { - return lift( F1Functions.mapIterable(this)); - } - - /** - * Promotes this function to return its value in a NonEmptyList. - * - * @return This function promoted to return its value in a NonEmptyList. - */ - @SuppressWarnings("unchecked") - public final F1W> nelK() { - return lift(F1Functions.nelK(this)); - } - - /** - * Promotes this function to map over a NonEmptyList. - * - * @return This function promoted to map over a NonEmptyList. - */ - public final F1W, NonEmptyList> mapNel() { - return lift(F1Functions.mapNel(this)); - } - - /** - * Promotes this function to return its value in a Set. - * - * @param o An order for the set. - * @return This function promoted to return its value in a Set. - */ - public final F1W> setK(final Ord o) { - return lift(F1Functions.setK(this, o)); - } - - /** - * Promotes this function to map over a Set. - * - * @param o An order for the resulting set. - * @return This function promoted to map over a Set. - */ - public final F1W, Set> mapSet(final Ord o) { - return lift(F1Functions.mapSet(this, o)); - } - - /** - * Promotes this function to return its value in a Tree. - * - * @return This function promoted to return its value in a Tree. - */ - public final F1W> treeK() { - return lift(F1Functions.treeK(this)); - } - - /** - * Promotes this function to map over a Tree. - * - * @return This function promoted to map over a Tree. - */ - @SuppressWarnings("unchecked") - public final F1W, Tree> mapTree() { - return lift(F1Functions.mapTree(this)); - } - - /** - * Returns a function that maps this function over a tree and folds it with the given monoid. - * - * @param m The monoid with which to fold the mapped tree. - * @return a function that maps this function over a tree and folds it with the given monoid. - */ - public final F1W, B> foldMapTree(final Monoid m) { - return lift(F1Functions.foldMapTree(this, m)); - } - - /** - * Promotes this function to return its value in a TreeZipper. - * - * @return This function promoted to return its value in a TreeZipper. - */ - public final F1W> treeZipperK() { - return lift(F1Functions.treeZipperK(this)); - } - - /** - * Promotes this function to map over a TreeZipper. - * - * @return This function promoted to map over a TreeZipper. - */ - public final F1W, TreeZipper> mapTreeZipper() { - return lift(F1Functions.mapTreeZipper(this)); - } - - /** - * Promotes this function so that it returns its result on the failure side of a Validation. - * Kleisli arrow for the Validation failure projection. - * - * @return This function promoted to return its result on the failure side of a Validation. - */ - public final F1W> failK() { - return lift(F1Functions.failK(this)); - } - - /** - * Promotes this function so that it returns its result on the success side of an Validation. - * Kleisli arrow for the Validation success projection. - * - * @return This function promoted to return its result on the success side of an Validation. - */ - public final F1W> successK() { - return lift( F1Functions.successK(this)); - } - - /** - * Promotes this function to map over the failure side of a Validation. - * - * @return This function promoted to map over the failure side of a Validation. - */ - public final F1W, Validation> mapFail() { - return lift(F1Functions.mapFail(this)); - } - - /** - * Promotes this function to map over the success side of a Validation. - * - * @return This function promoted to map over the success side of a Validation. - */ - public final F1W, Validation> mapSuccess() { - return lift(F1Functions.mapSuccess(this)); - } - - /** - * Returns a function that returns the failure side of a given Validation, - * or this function applied to the success side. - * - * @return a function that returns the failure side of a given Validation, - * or this function applied to the success side. - */ - public final F1W, B> onFail() { - return lift(F1Functions.onFail(this)); - } - - /** - * Returns a function that returns the success side of a given Validation, - * or this function applied to the failure side. - * - * @return a function that returns the success side of a given Validation, - * or this function applied to the failure side. - */ - public final F1W, B> onSuccess() { - return lift(F1Functions.onSuccess(this)); - } - - /** - * Promotes this function to return its value in a Zipper. - * - * @return This function promoted to return its value in a Zipper. - */ - public final F1W> zipperK() { - return lift(F1Functions.zipperK(this)); - } - - /** - * Promotes this function to map over a Zipper. - * - * @return This function promoted to map over a Zipper. - */ - public final F1W, Zipper> mapZipper() { - return lift(F1Functions.mapZipper(this)); - } - - /** - * Promotes this function to map over an Equal as a contravariant functor. - * - * @return This function promoted to map over an Equal as a contravariant functor. - */ - public final F1W, Equal> contramapEqual() { - return lift(F1Functions.contramapEqual(this)); - } - - /** - * Promotes this function to map over a Hash as a contravariant functor. - * - * @return This function promoted to map over a Hash as a contravariant functor. - */ - public final F1W, Hash> contramapHash() { - return lift(F1Functions.contramapHash(this)); - } - - /** - * Promotes this function to map over a Show as a contravariant functor. - * - * @return This function promoted to map over a Show as a contravariant functor. - */ - public final F1W, Show> contramapShow() { - return lift(F1Functions.contramapShow(this)); - } - - /** - * Promotes this function to map over the first element of a pair. - * - * @return This function promoted to map over the first element of a pair. - */ - public final F1W, P2> mapFst() { - return lift(F1Functions.mapFst(this)); - } - - /** - * Promotes this function to map over the second element of a pair. - * - * @return This function promoted to map over the second element of a pair. - */ - public final F1W, P2> mapSnd() { - return lift(F1Functions.mapSnd(this)); - } - - /** - * Promotes this function to map over both elements of a pair. - * - * @return This function promoted to map over both elements of a pair. - */ - public final F1W, P2> mapBoth() { - return lift(F1Functions.mapBoth(this)); - } - - /** - * Maps this function over a SynchronousQueue. - * - * @param as A SynchronousQueue to map this function over. - * @return A new SynchronousQueue with this function applied to each element. - */ - public final SynchronousQueue mapJ(final SynchronousQueue as) { - return F1Functions.mapJ(this, as); - } - - - /** - * Maps this function over a PriorityBlockingQueue. - * - * @param as A PriorityBlockingQueue to map this function over. - * @return A new PriorityBlockingQueue with this function applied to each element. - */ - public final PriorityBlockingQueue mapJ(final PriorityBlockingQueue as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over a LinkedBlockingQueue. - * - * @param as A LinkedBlockingQueue to map this function over. - * @return A new LinkedBlockingQueue with this function applied to each element. - */ - public final LinkedBlockingQueue mapJ(final LinkedBlockingQueue as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over a CopyOnWriteArraySet. - * - * @param as A CopyOnWriteArraySet to map this function over. - * @return A new CopyOnWriteArraySet with this function applied to each element. - */ - public final CopyOnWriteArraySet mapJ(final CopyOnWriteArraySet as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over a CopyOnWriteArrayList. - * - * @param as A CopyOnWriteArrayList to map this function over. - * @return A new CopyOnWriteArrayList with this function applied to each element. - */ - public final CopyOnWriteArrayList mapJ(final CopyOnWriteArrayList as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over a ConcurrentLinkedQueue. - * - * @param as A ConcurrentLinkedQueue to map this function over. - * @return A new ConcurrentLinkedQueue with this function applied to each element. - */ - public final ConcurrentLinkedQueue mapJ(final ConcurrentLinkedQueue as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over an ArrayBlockingQueue. - * - * @param as An ArrayBlockingQueue to map this function over. - * @return A new ArrayBlockingQueue with this function applied to each element. - */ - public final ArrayBlockingQueue mapJ(final ArrayBlockingQueue as) { - return F1Functions.mapJ(this, as); - } - - - /** - * Maps this function over a TreeSet. - * - * @param as A TreeSet to map this function over. - * @return A new TreeSet with this function applied to each element. - */ - public final TreeSet mapJ(final TreeSet as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over a PriorityQueue. - * - * @param as A PriorityQueue to map this function over. - * @return A new PriorityQueue with this function applied to each element. - */ - public final PriorityQueue mapJ(final PriorityQueue as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over a LinkedList. - * - * @param as A LinkedList to map this function over. - * @return A new LinkedList with this function applied to each element. - */ - public final LinkedList mapJ(final LinkedList as) { - return F1Functions.mapJ(this, as); - } - - /** - * Maps this function over an ArrayList. - * - * @param as An ArrayList to map this function over. - * @return A new ArrayList with this function applied to each element. - */ - public final ArrayList mapJ(final ArrayList as) { - return F1Functions.mapJ(this, as); - } - - public final F1W map(F f) { - return lift(F1Functions.map(this, f)); - } - - public final F1W contramap(F f) { - return lift(F1Functions.contramap(this, f)); - } - - public static class F1WFunc extends F1W { - final F func; - public F1WFunc(F f) { - func = f; - } - - @Override - public final B f(A a) { - return func.f(a); - } - } - - /** - * Lifts the function into the fully featured function wrapper - */ - public static F1W lift(final F f) { - return new F1WFunc<>(f); - } -} diff --git a/core/src/main/java/fj/F2.java b/core/src/main/java/fj/F2.java index 63555fd5..8b1aac20 100644 --- a/core/src/main/java/fj/F2.java +++ b/core/src/main/java/fj/F2.java @@ -1,12 +1,22 @@ package fj; +import fj.control.parallel.Promise; +import fj.data.*; + +import java.util.function.BiFunction; + +import static fj.P.p; +import static fj.data.IterableW.wrap; +import static fj.data.Set.iterableSet; +import static fj.data.Tree.node; +import static fj.data.TreeZipper.treeZipper; +import static fj.data.Zipper.zipper; + /** * A transformation function of arity-2 from A and B to C. - * This type can be represented using the Java 7 closure syntax. - * - * @version %build.number% */ -public interface F2 { +@FunctionalInterface +public interface F2 extends BiFunction { /** * Transform A and B to C. * @@ -16,4 +26,264 @@ public interface F2 { */ C f(A a, B b); + default C apply(A a, B b) { + return f(a, b); + } + + /** + * Partial application. + * + * @param a The A to which to apply this function. + * @return The function partially applied to the given argument. + */ + default F f(final A a) { + return b -> f(a, b); + } + + /** + * Curries this wrapped function to a wrapped function of arity-1 that returns another wrapped function. + * + * @return a wrapped function of arity-1 that returns another wrapped function. + */ + default F> curry() { + return a -> b -> f(a, b); + } + + /** + * Flips the arguments of this function. + * + * @return A new function with the arguments of this function flipped. + */ + default F2 flip() { + return (b, a) -> f(a, b); + } + + /** + * Uncurries this function to a function on tuples. + * + * @return A new function that calls this function with the elements of a given tuple. + */ + default F, C> tuple() { + return p -> f(p._1(), p._2()); + } + + /** + * Promotes this function to a function on Arrays. + * + * @return This function promoted to transform Arrays. + */ + default F2, Array, Array> arrayM() { + return (a, b) -> a.bind(b, curry()); + } + + /** + * Promotes this function to a function on Promises. + * + * @return This function promoted to transform Promises. + */ + default F2, Promise, Promise> promiseM() { + return (a, b) -> a.bind(b, curry()); + } + + /** + * Promotes this function to a function on Iterables. + * + * @return This function promoted to transform Iterables. + */ + default F2, Iterable, IterableW> iterableM() { + return (a, b) -> IterableW.liftM2(curry()).f(a).f(b); + } + + /** + * Promotes this function to a function on Lists. + * + * @return This function promoted to transform Lists. + */ + default F2, List, List> listM() { + return (a, b) -> List.liftM2(curry()).f(a).f(b); + } + + /** + * Promotes this function to a function on non-empty lists. + * + * @return This function promoted to transform non-empty lists. + */ + default F2, NonEmptyList, NonEmptyList> nelM() { + return (as, bs) -> NonEmptyList.fromList(as.toList().bind(bs.toList(), this)).some(); + } + + /** + * Promotes this function to a function on Options. + * + * @return This function promoted to transform Options. + */ + default F2, Option, Option> optionM() { + return (a, b) -> Option.liftM2(curry()).f(a).f(b); + } + + /** + * Promotes this function to a function on Sets. + * + * @param o An ordering for the result of the promoted function. + * @return This function promoted to transform Sets. + */ + default F2, Set, Set> setM(final Ord o) { + return (as, bs) -> { + Set cs = Set.empty(o); + for (final A a : as) + for (final B b : bs) + cs = cs.insert(f(a, b)); + return cs; + }; + } + + /** + * Promotes this function to a function on Streams. + * + * @return This function promoted to transform Streams. + */ + default F2, Stream, Stream> streamM() { + return (as, bs) -> as.bind(bs, this); + } + + /** + * Promotes this function to a function on Trees. + * + * @return This function promoted to transform Trees. + */ + default F2, Tree, Tree> treeM() { + F2 outer = this; + return new F2, Tree, Tree>() { + public Tree f(final Tree as, final Tree bs) { + final F2, Tree, Tree> self = this; + return node(outer.f(as.root(), bs.root()), P.lazy(() -> self.streamM().f(as.subForest()._1(), bs.subForest()._1()))); + } + }; + } + + /** + * Promotes this function to zip two arrays, applying the function lock-step over both Arrays. + * + * @return A function that zips two arrays with this function. + */ + default F2, Array, Array> zipArrayM() { + return (as, bs) -> as.zipWith(bs, this); + } + + /** + * Promotes this function to zip two iterables, applying the function lock-step over both iterables. + * + * @return A function that zips two iterables with this function. + */ + default F2, Iterable, Iterable> zipIterableM() { + return (as, bs) -> wrap(as).zipWith(bs, this); + } + + /** + * Promotes this function to zip two lists, applying the function lock-step over both lists. + * + * @return A function that zips two lists with this function. + */ + default F2, List, List> zipListM() { + return (as, bs) -> as.zipWith(bs, this); + } + + + /** + * Promotes this function to zip two streams, applying the function lock-step over both streams. + * + * @return A function that zips two streams with this function. + */ + default F2, Stream, Stream> zipStreamM() { + return (as, bs) -> as.zipWith(bs, this); + } + + /** + * Promotes this function to zip two non-empty lists, applying the function lock-step over both lists. + * + * @return A function that zips two non-empty lists with this function. + */ + default F2, NonEmptyList, NonEmptyList> zipNelM() { + return (as, bs) -> NonEmptyList.fromList(as.toList().zipWith(bs.toList(), this)).some(); + } + + /** + * Promotes this function to zip two sets, applying the function lock-step over both sets. + * + * @param o An ordering for the resulting set. + * @return A function that zips two sets with this function. + */ + default F2, Set, Set> zipSetM(final Ord o) { + return (as, bs) -> iterableSet(o, as.toStream().zipWith(bs.toStream(), this)); + } + + /** + * Promotes this function to zip two trees, applying the function lock-step over both trees. + * The structure of the resulting tree is the structural intersection of the two trees. + * + * @return A function that zips two trees with this function. + */ + default F2, Tree, Tree> zipTreeM() { + F2 outer = this; + return new F2, Tree, Tree>() { + public Tree f(final Tree ta, final Tree tb) { + final F2, Tree, Tree> self = this; + return node(outer.f(ta.root(), tb.root()), P.lazy(() -> self.zipStreamM().f(ta.subForest()._1(), tb.subForest()._1()))); + } + }; + } + + /** + * Promotes this function to zip two zippers, applying the function lock-step over both zippers in both directions. + * The structure of the resulting zipper is the structural intersection of the two zippers. + * + * @return A function that zips two zippers with this function. + */ + default F2, Zipper, Zipper> zipZipperM() { + return (ta, tb) -> { + final F2, Stream, Stream> sf = this.zipStreamM(); + return zipper(sf.f(ta.lefts(), tb.lefts()), f(ta.focus(), tb.focus()), sf.f(ta.rights(), tb.rights())); + }; + } + + /** + * Promotes this function to zip two TreeZippers, applying the function lock-step over both zippers in all directions. + * The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers. + * + * @return A function that zips two TreeZippers with this function. + */ + default F2, TreeZipper, TreeZipper> zipTreeZipperM() { + F2 outer = this; + return (ta, tb) -> { + final F2>, Stream>, Stream>> sf = outer.treeM().zipStreamM(); + F2>, A, Stream>>, P3>, B, Stream>>, P3>, C, Stream>>> g = + (pa, pb) -> p(outer.treeM().zipStreamM().f(pa._1(), pb._1()), outer.f(pa._2(), pb._2()), + outer.treeM().zipStreamM().f(pa._3(), pb._3())); + final + F2>, A, Stream>>>, + Stream>, B, Stream>>>, + Stream>, C, Stream>>>> + pf = g.zipStreamM(); + return treeZipper(outer.treeM().f(ta.p()._1(), tb.p()._1()), sf.f(ta.lefts(), tb.lefts()), + sf.f(ta.rights(), tb.rights()), pf.f(ta.p()._4(), tb.p()._4())); + }; + } + + default F2 contramapFirst(F f) { + return (z, b) -> f(f.f(z), b); + } + + default F2 contramapSecond(F f) { + return (a, z) -> f(a, f.f(z)); + } + + default F2 contramap(F f, F g) { + return contramapFirst(f).contramapSecond(g); + } + + default F2 map(F f) { + return (a, b) -> f.f(f(a, b)); + } + + } diff --git a/core/src/main/java/fj/F2Functions.java b/core/src/main/java/fj/F2Functions.java deleted file mode 100644 index f69e02ed..00000000 --- a/core/src/main/java/fj/F2Functions.java +++ /dev/null @@ -1,273 +0,0 @@ -package fj; - -import fj.control.parallel.Promise; -import fj.data.*; - -import static fj.P.p; -import static fj.data.IterableW.wrap; -import static fj.data.Set.iterableSet; -import static fj.data.Tree.node; -import static fj.data.TreeZipper.treeZipper; -import static fj.data.Zipper.zipper; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F2Functions { - - - private F2Functions() { - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument. - */ - public static F f(final F2 f, final A a) { - return b -> f.f(a, b); - } - - /** - * Curries this wrapped function to a wrapped function of arity-1 that returns another wrapped function. - * - * @return a wrapped function of arity-1 that returns another wrapped function. - */ - public static F> curry(final F2 f) { - return a -> b -> f.f(a, b); - } - - /** - * Flips the arguments of this function. - * - * @return A new function with the arguments of this function flipped. - */ - public static F2 flip(final F2 f) { - return (b, a) -> f.f(a, b); - } - - /** - * Uncurries this function to a function on tuples. - * - * @return A new function that calls this function with the elements of a given tuple. - */ - public static F, C> tuple(final F2 f) { - return p -> f.f(p._1(), p._2()); - } - - /** - * Promotes this function to a function on Arrays. - * - * @return This function promoted to transform Arrays. - */ - public static F2, Array, Array> arrayM(final F2 f) { - return (a, b) -> a.bind(b, curry(f)); - } - - /** - * Promotes this function to a function on Promises. - * - * @return This function promoted to transform Promises. - */ - public static F2, Promise, Promise> promiseM(final F2 f) { - return (a, b) -> a.bind(b, curry(f)); - } - - /** - * Promotes this function to a function on Iterables. - * - * @return This function promoted to transform Iterables. - */ - public static F2, Iterable, IterableW> iterableM(final F2 f) { - return (a, b) -> IterableW.liftM2(curry(f)).f(a).f(b); - } - - /** - * Promotes this function to a function on Lists. - * - * @return This function promoted to transform Lists. - */ - public static F2, List, List> listM(final F2 f) { - return (a, b) -> List.liftM2(curry(f)).f(a).f(b); - } - - /** - * Promotes this function to a function on non-empty lists. - * - * @return This function promoted to transform non-empty lists. - */ - public static F2, NonEmptyList, NonEmptyList> nelM(final F2 f) { - return (as, bs) -> NonEmptyList.fromList(as.toList().bind(bs.toList(), f)).some(); - } - - /** - * Promotes this function to a function on Options. - * - * @return This function promoted to transform Options. - */ - public static F2, Option, Option> optionM(final F2 f) { - return (a, b) -> Option.liftM2(curry(f)).f(a).f(b); - } - - /** - * Promotes this function to a function on Sets. - * - * @param o An ordering for the result of the promoted function. - * @return This function promoted to transform Sets. - */ - public static F2, Set, Set> setM(final F2 f, final Ord o) { - return (as, bs) -> { - Set cs = Set.empty(o); - for (final A a : as) - for (final B b : bs) - cs = cs.insert(f.f(a, b)); - return cs; - }; - } - - /** - * Promotes this function to a function on Streams. - * - * @return This function promoted to transform Streams. - */ - public static F2, Stream, Stream> streamM(final F2 f) { - return (as, bs) -> as.bind(bs, f); - } - - /** - * Promotes this function to a function on Trees. - * - * @return This function promoted to transform Trees. - */ - public static F2, Tree, Tree> treeM(final F2 f) { - return new F2, Tree, Tree>() { - public Tree f(final Tree as, final Tree bs) { - final F2, Tree, Tree> self = this; - return node(f.f(as.root(), bs.root()), P.lazy(() -> streamM(self).f(as.subForest()._1(), bs.subForest()._1()))); - } - }; - } - - /** - * Promotes this function to zip two arrays, applying the function lock-step over both Arrays. - * - * @return A function that zips two arrays with this function. - */ - public static F2, Array, Array> zipArrayM(final F2 f) { - return (as, bs) -> as.zipWith(bs, f); - } - - /** - * Promotes this function to zip two iterables, applying the function lock-step over both iterables. - * - * @return A function that zips two iterables with this function. - */ - public static F2, Iterable, Iterable> zipIterableM(final F2 f) { - return (as, bs) -> wrap(as).zipWith(bs, f); - } - - /** - * Promotes this function to zip two lists, applying the function lock-step over both lists. - * - * @return A function that zips two lists with this function. - */ - public static F2, List, List> zipListM(final F2 f) { - return (as, bs) -> as.zipWith(bs, f); - } - - - /** - * Promotes this function to zip two streams, applying the function lock-step over both streams. - * - * @return A function that zips two streams with this function. - */ - public static F2, Stream, Stream> zipStreamM(final F2 f) { - return (as, bs) -> as.zipWith(bs, f); - } - - /** - * Promotes this function to zip two non-empty lists, applying the function lock-step over both lists. - * - * @return A function that zips two non-empty lists with this function. - */ - public static F2, NonEmptyList, NonEmptyList> zipNelM(final F2 f) { - return (as, bs) -> NonEmptyList.fromList(as.toList().zipWith(bs.toList(), f)).some(); - } - - /** - * Promotes this function to zip two sets, applying the function lock-step over both sets. - * - * @param o An ordering for the resulting set. - * @return A function that zips two sets with this function. - */ - public static F2, Set, Set> zipSetM(final F2 f, final Ord o) { - return (as, bs) -> iterableSet(o, as.toStream().zipWith(bs.toStream(), f)); - } - - /** - * Promotes this function to zip two trees, applying the function lock-step over both trees. - * The structure of the resulting tree is the structural intersection of the two trees. - * - * @return A function that zips two trees with this function. - */ - public static F2, Tree, Tree> zipTreeM(final F2 f) { - return new F2, Tree, Tree>() { - public Tree f(final Tree ta, final Tree tb) { - final F2, Tree, Tree> self = this; - return node(f.f(ta.root(), tb.root()), P.lazy(() -> zipStreamM(self).f(ta.subForest()._1(), tb.subForest()._1()))); - } - }; - } - - /** - * Promotes this function to zip two zippers, applying the function lock-step over both zippers in both directions. - * The structure of the resulting zipper is the structural intersection of the two zippers. - * - * @return A function that zips two zippers with this function. - */ - public static F2, Zipper, Zipper> zipZipperM(final F2 f) { - return (ta, tb) -> { - final F2, Stream, Stream> sf = zipStreamM(f); - return zipper(sf.f(ta.lefts(), tb.lefts()), f.f(ta.focus(), tb.focus()), sf.f(ta.rights(), tb.rights())); - }; - } - - /** - * Promotes this function to zip two TreeZippers, applying the function lock-step over both zippers in all directions. - * The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers. - * - * @return A function that zips two TreeZippers with this function. - */ - public static F2, TreeZipper, TreeZipper> zipTreeZipperM(final F2 f) { - return (ta, tb) -> { - final F2>, Stream>, Stream>> sf = zipStreamM(treeM(f)); - final - F2>, A, Stream>>>, - Stream>, B, Stream>>>, - Stream>, C, Stream>>>> - pf = - zipStreamM((pa, pb) -> p(zipStreamM(treeM(f)).f(pa._1(), pb._1()), f.f(pa._2(), pb._2()), - zipStreamM(treeM(f)).f(pa._3(), pb._3()))); - return treeZipper(treeM(f).f(ta.p()._1(), tb.p()._1()), sf.f(ta.lefts(), tb.lefts()), - sf.f(ta.rights(), tb.rights()), pf.f(ta.p()._4(), tb.p()._4())); - }; - } - - public static F2 contramapFirst(F2 target, F f) { - return (z, b) -> target.f(f.f(z), b); - } - - public static F2 contramapSecond(F2 target, F f) { - return (a, z) -> target.f(a, f.f(z)); - } - - public static F2 contramap(F2 target, F f, F g) { - return contramapSecond(contramapFirst(target, f), g); - } - - public static F2 map(F2 target, F f) { - return (a, b) -> f.f(target.f(a, b)); - } - -} diff --git a/core/src/main/java/fj/F2W.java b/core/src/main/java/fj/F2W.java deleted file mode 100644 index 1f35855a..00000000 --- a/core/src/main/java/fj/F2W.java +++ /dev/null @@ -1,254 +0,0 @@ -package fj; - -import fj.control.parallel.Promise; -import fj.data.*; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F2W implements F2 { - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument. - */ - public final F1W f(final A a) { - return F1W.lift(F2Functions.f(this, a)); - } - - /** - * Curries this wrapped function to a wrapped function of arity-1 that returns another wrapped function. - * - * @return a wrapped function of arity-1 that returns another wrapped function. - */ - public final F1W> curry() { - return F1W.lift(F2Functions.curry(this)); - } - - /** - * Flips the arguments of this function. - * - * @return A new function with the arguments of this function flipped. - */ - public final F2W flip() { - return lift(F2Functions.flip(this)); - } - - /** - * Uncurries this function to a function on tuples. - * - * @return A new function that calls this function with the elements of a given tuple. - */ - public final F1W, C> tuple() { - return F1W.lift(F2Functions.tuple(this)); - } - - /** - * Promotes this function to a function on Arrays. - * - * @return This function promoted to transform Arrays. - */ - public final F2W, Array, Array> arrayM() { - return lift(F2Functions.arrayM(this)); - } - - /** - * Promotes this function to a function on Promises. - * - * @return This function promoted to transform Promises. - */ - public final F2W, Promise, Promise> promiseM() { - return lift(F2Functions.promiseM(this)); - } - - /** - * Promotes this function to a function on Iterables. - * - * @return This function promoted to transform Iterables. - */ - public final F2W, Iterable, IterableW> iterableM() { - return lift(F2Functions.iterableM(this)); - } - - /** - * Promotes this function to a function on Lists. - * - * @return This function promoted to transform Lists. - */ - public final F2W, List, List> listM() { - return lift(F2Functions.listM(this)); - } - - /** - * Promotes this function to a function on non-empty lists. - * - * @return This function promoted to transform non-empty lists. - */ - public final F2W, NonEmptyList, NonEmptyList> nelM() { - return lift(F2Functions.nelM(this)); - } - - /** - * Promotes this function to a function on Options. - * - * @return This function promoted to transform Options. - */ - public final F2W, Option, Option> optionM() { - return lift(F2Functions.optionM(this)); - } - - /** - * Promotes this function to a function on Sets. - * - * @param o An ordering for the result of the promoted function. - * @return This function promoted to transform Sets. - */ - public final F2W, Set, Set> setM(final Ord o) { - return lift(F2Functions.setM(this, o)); - } - - /** - * Promotes this function to a function on Streams. - * - * @return This function promoted to transform Streams. - */ - public final F2W, Stream, Stream> streamM() { - return lift(F2Functions.streamM(this)); - } - - /** - * Promotes this function to a function on Trees. - * - * @return This function promoted to transform Trees. - */ - public final F2W, Tree, Tree> treeM() { - return lift(F2Functions.treeM(this)); - } - - /** - * Promotes this function to zip two arrays, applying the function lock-step over both Arrays. - * - * @return A function that zips two arrays with this function. - */ - public final F2W, Array, Array> zipArrayM() { - return lift(F2Functions.zipArrayM(this)); - } - - /** - * Promotes this function to zip two iterables, applying the function lock-step over both iterables. - * - * @return A function that zips two iterables with this function. - */ - public final F2W, Iterable, Iterable> zipIterableM() { - return lift(F2Functions.zipIterableM(this)); - } - - /** - * Promotes this function to zip two lists, applying the function lock-step over both lists. - * - * @return A function that zips two lists with this function. - */ - public final F2W, List, List> zipListM() { - return lift(F2Functions.zipListM(this)); - } - - - /** - * Promotes this function to zip two streams, applying the function lock-step over both streams. - * - * @return A function that zips two streams with this function. - */ - public final F2W, Stream, Stream> zipStreamM() { - return lift(F2Functions.zipStreamM(this)); - } - - /** - * Promotes this function to zip two non-empty lists, applying the function lock-step over both lists. - * - * @return A function that zips two non-empty lists with this function. - */ - public final F2W, NonEmptyList, NonEmptyList> zipNelM() { - return lift(F2Functions.zipNelM(this)); - } - - /** - * Promotes this function to zip two sets, applying the function lock-step over both sets. - * - * @param o An ordering for the resulting set. - * @return A function that zips two sets with this function. - */ - public final F2W, Set, Set> zipSetM(final Ord o) { - return lift(F2Functions.zipSetM(this, o)); - } - - /** - * Promotes this function to zip two trees, applying the function lock-step over both trees. - * The structure of the resulting tree is the structural intersection of the two trees. - * - * @return A function that zips two trees with this function. - */ - public final F2W, Tree, Tree> zipTreeM() { - return lift(F2Functions.zipTreeM(this)); - } - - /** - * Promotes this function to zip two zippers, applying the function lock-step over both zippers in both directions. - * The structure of the resulting zipper is the structural intersection of the two zippers. - * - * @return A function that zips two zippers with this function. - */ - public final F2W, Zipper, Zipper> zipZipperM() { - return lift(F2Functions.zipZipperM(this)); - } - - /** - * Promotes this function to zip two TreeZippers, applying the function lock-step over both zippers in all directions. - * The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers. - * - * @return A function that zips two TreeZippers with this function. - */ - public final F2W, TreeZipper, TreeZipper> zipTreeZipperM() { - return lift(F2Functions.zipTreeZipperM(this)); - } - - public final F2W contramapFirst(F f) { - return lift(F2Functions.contramapFirst(this, f)); - } - - public final F2W contramapSecond(F f) { - return lift(F2Functions.contramapSecond(this, f)); - } - - public final F2W contramap(F f, F g) { - return lift(F2Functions.contramap(this, f, g)); - } - - public final F2W map(F f) { - return lift(F2Functions.map(this, f)); - } - - - public static class F2WFunc extends F2W { - final F2 func; - public F2WFunc(F2 f) { - func = f; - } - - @Override - public final C f(A a, B b) { - return func.f(a, b); - } - } - - /** - * Lifts the function into the fully featured function wrapper - */ - public static F2W lift(final F2 f) { - return new F2WFunc<>(f); - } - - - -} diff --git a/core/src/main/java/fj/F3.java b/core/src/main/java/fj/F3.java index 540afdaa..b7efeb3a 100644 --- a/core/src/main/java/fj/F3.java +++ b/core/src/main/java/fj/F3.java @@ -2,10 +2,9 @@ /** * A transformation function of arity-3 from A, B and C to - * D. This type can be represented using the Java 7 closure syntax. - * - * @version %build.number% + * D. */ +@FunctionalInterface public interface F3 { /** * Transform A, B and C to D. diff --git a/core/src/main/java/fj/F3Functions.java b/core/src/main/java/fj/F3Functions.java deleted file mode 100644 index 18e811d9..00000000 --- a/core/src/main/java/fj/F3Functions.java +++ /dev/null @@ -1,22 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F3Functions { - - - private F3Functions() { - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument. - */ - public static F2 f(final F3 f, final A a) { - return (b, c) -> f.f(a, b, c); - } - -} diff --git a/core/src/main/java/fj/F3W.java b/core/src/main/java/fj/F3W.java deleted file mode 100644 index d6ffa80b..00000000 --- a/core/src/main/java/fj/F3W.java +++ /dev/null @@ -1,8 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F3W implements F3 { - -} diff --git a/core/src/main/java/fj/F4.java b/core/src/main/java/fj/F4.java index 84dcfafa..c3c5f882 100644 --- a/core/src/main/java/fj/F4.java +++ b/core/src/main/java/fj/F4.java @@ -2,10 +2,9 @@ /** * A transformation function of arity-4 from A, B, C and - * D to E. This type can be represented using the Java 7 closure syntax. - * - * @version %build.number% + * D to E. */ +@FunctionalInterface public interface F4 { /** * Transform A, B, C and D to E. diff --git a/core/src/main/java/fj/F4Functions.java b/core/src/main/java/fj/F4Functions.java deleted file mode 100644 index cb353764..00000000 --- a/core/src/main/java/fj/F4Functions.java +++ /dev/null @@ -1,21 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F4Functions { - - private F4Functions() { - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument. - */ - public static F3 f(final F4 f, final A a) { - return (b, c, d) -> f.f(a, b, c, d); - } - -} diff --git a/core/src/main/java/fj/F4W.java b/core/src/main/java/fj/F4W.java deleted file mode 100644 index 3cc92e55..00000000 --- a/core/src/main/java/fj/F4W.java +++ /dev/null @@ -1,9 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F4W implements F4 { - - -} diff --git a/core/src/main/java/fj/F5.java b/core/src/main/java/fj/F5.java index 5fea676b..574afb15 100644 --- a/core/src/main/java/fj/F5.java +++ b/core/src/main/java/fj/F5.java @@ -2,11 +2,9 @@ /** * A transformation function of arity-5 from A, B, C, - * D and E to F$. This type can be represented using the Java - * 7 closure syntax. - * - * @version %build.number% + * D and E to F$. */ +@FunctionalInterface public interface F5 { /** * Transform A, B, C, D and E to diff --git a/core/src/main/java/fj/F5Functions.java b/core/src/main/java/fj/F5Functions.java deleted file mode 100644 index c39b228f..00000000 --- a/core/src/main/java/fj/F5Functions.java +++ /dev/null @@ -1,21 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F5Functions { - - private F5Functions() { - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument. - */ - public static F4 f(final F5 f, final A a) { - return (b, c, d, e) -> f.f(a, b, c, d, e); - } - -} diff --git a/core/src/main/java/fj/F5W.java b/core/src/main/java/fj/F5W.java deleted file mode 100644 index 5fa407cd..00000000 --- a/core/src/main/java/fj/F5W.java +++ /dev/null @@ -1,9 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F5W implements F5 { - - -} diff --git a/core/src/main/java/fj/F6.java b/core/src/main/java/fj/F6.java index 20101cf7..135e28fe 100644 --- a/core/src/main/java/fj/F6.java +++ b/core/src/main/java/fj/F6.java @@ -2,11 +2,9 @@ /** * A transformation function of arity-6 from A, B, C, - * D, E and F$ to G. This type can be - * represented using the Java 7 closure syntax. - * - * @version %build.number% + * D, E and F$ to G. */ +@FunctionalInterface public interface F6 { /** * Transform A, B, C, D, E and diff --git a/core/src/main/java/fj/F6Functions.java b/core/src/main/java/fj/F6Functions.java deleted file mode 100644 index 320b95f4..00000000 --- a/core/src/main/java/fj/F6Functions.java +++ /dev/null @@ -1,22 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F6Functions { - - private F6Functions() { - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument. - */ - public static F5 f(final F6 func, final A a) { - return (b, c, d, e, f) -> func.f(a, b, c, d, e, f); - } - - -} diff --git a/core/src/main/java/fj/F6W.java b/core/src/main/java/fj/F6W.java deleted file mode 100644 index adaf723f..00000000 --- a/core/src/main/java/fj/F6W.java +++ /dev/null @@ -1,9 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F6W implements F6 { - - -} diff --git a/core/src/main/java/fj/F7.java b/core/src/main/java/fj/F7.java index 5a3ea1ef..a2542700 100644 --- a/core/src/main/java/fj/F7.java +++ b/core/src/main/java/fj/F7.java @@ -2,11 +2,9 @@ /** * A transformation function of arity-7 from A, B, C, - * D, E, F$ and G to H. This type - * can be represented using the Java 7 closure syntax. - * - * @version %build.number% + * D, E, F$ and G to H. */ +@FunctionalInterface public interface F7 { /** * Transform A, B, C, D, E, diff --git a/core/src/main/java/fj/F7Functions.java b/core/src/main/java/fj/F7Functions.java deleted file mode 100644 index e583ba4b..00000000 --- a/core/src/main/java/fj/F7Functions.java +++ /dev/null @@ -1,22 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F7Functions { - - private F7Functions() { - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @return The function partially applied to the given argument. - */ - public static F6 f(final F7 func, final A a) { - return (b, c, d, e, f, g) -> func.f(a, b, c, d, e, f, g); - } - - -} diff --git a/core/src/main/java/fj/F7W.java b/core/src/main/java/fj/F7W.java deleted file mode 100644 index fdcec3d5..00000000 --- a/core/src/main/java/fj/F7W.java +++ /dev/null @@ -1,9 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F7W implements F7 { - - -} diff --git a/core/src/main/java/fj/F8.java b/core/src/main/java/fj/F8.java index 8b986456..647fc14c 100644 --- a/core/src/main/java/fj/F8.java +++ b/core/src/main/java/fj/F8.java @@ -3,10 +3,9 @@ /** * A transformation function of arity-8 from A, B, C, * D, E, F$, G and H to - * I. This type can be represented using the Java 7 closure syntax. - * - * @version %build.number% + * I. */ +@FunctionalInterface public interface F8 { /** * Transform A, B, C, D, E, diff --git a/core/src/main/java/fj/F8Functions.java b/core/src/main/java/fj/F8Functions.java deleted file mode 100644 index 8b9883a3..00000000 --- a/core/src/main/java/fj/F8Functions.java +++ /dev/null @@ -1,20 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F8Functions { - - private F8Functions() { - } - - /** - * Partial application. - * - * @return The function partially applied to the given argument. - */ - public static F7 f(final F8 func, final A a) { - return (b, c, d, e, f, g, h) -> func.f(a, b, c, d, e, f, g, h); - } - -} diff --git a/core/src/main/java/fj/F8W.java b/core/src/main/java/fj/F8W.java deleted file mode 100644 index d0a9f861..00000000 --- a/core/src/main/java/fj/F8W.java +++ /dev/null @@ -1,9 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 22/01/2015. - */ -public abstract class F8W implements F8 { - - -} diff --git a/core/src/main/java/fj/Function.java b/core/src/main/java/fj/Function.java index 995c179d..52819377 100644 --- a/core/src/main/java/fj/Function.java +++ b/core/src/main/java/fj/Function.java @@ -4,8 +4,6 @@ /** * Transformations on functions. - * - * @version %build.number% */ public final class Function { private Function() { diff --git a/core/src/main/java/fj/Hash.java b/core/src/main/java/fj/Hash.java index 8d0a4e4c..241bcefb 100644 --- a/core/src/main/java/fj/Hash.java +++ b/core/src/main/java/fj/Hash.java @@ -17,8 +17,6 @@ /** * Produces a hash code for an object which should attempt uniqueness. - * - * @version %build.number% */ public final class Hash { private final F f; @@ -163,7 +161,12 @@ public static Hash> eitherHash(final Hash ha, final Hash< return hash(e -> e.isLeft() ? ha.hash(e.left().value()) : hb.hash(e.right().value())); } - /** + public static Hash> either3Hash(final Hash ha, final Hash hb, final Hash hc) { + return hash(e -> e.either(a -> ha.hash(a), b -> hb.hash(b), c -> hc.hash(c))); + } + + + /** * A hash instance for the {@link Result} type. * * @param ha Hash the Result value. diff --git a/core/src/main/java/fj/LcgRng.java b/core/src/main/java/fj/LcgRng.java index 215b3572..cd918f5b 100644 --- a/core/src/main/java/fj/LcgRng.java +++ b/core/src/main/java/fj/LcgRng.java @@ -1,8 +1,6 @@ package fj; /** - * Created by MarkPerry on 7/07/2014. - * * https://en.wikipedia.org/wiki/Linear_congruential_generator */ public class LcgRng extends Rng { diff --git a/core/src/main/java/fj/Monoid.java b/core/src/main/java/fj/Monoid.java index 0c09b558..bc05f33e 100644 --- a/core/src/main/java/fj/Monoid.java +++ b/core/src/main/java/fj/Monoid.java @@ -1,15 +1,6 @@ package fj; -import static fj.F1Functions.dimap; - -import fj.data.Array; -import fj.data.DList; -import fj.data.List; -import fj.data.IO; -import fj.data.Natural; -import fj.data.Option; -import fj.data.Set; -import fj.data.Stream; +import fj.data.*; import static fj.Function.*; import static fj.Semigroup.semigroupDef; @@ -31,8 +22,6 @@ *
  • Right Identity; forall x. sum(x, zero()) == x
  • *
  • Associativity; forall x y z. sum(sum(x, y), z) == sum(x, sum(y, z))
  • * - * - * @version %build.number% */ public final class Monoid
    { @@ -153,7 +142,7 @@ public B append(B a1, B a2) { @Override public F prepend(B b) { - return dimap(def.prepend(g.f(b)), g, f); + return def.prepend(g.f(b)).dimap(g, f); } @Override @@ -475,20 +464,6 @@ public A append(A a1, A a2) { }); } - /** - * Constructs a monoid from the given semigroup and zero value, which must follow the monoidal laws. - * @deprecated since 4.7. Use {@link #monoidDef(Semigroup.Definition, Object)} or {@link Semigroup#monoid(Object)} instead. - * - * @param s The semigroup for the monoid. - * @param zero The zero for the monoid. - * @return A monoid instance that uses the given sun function and zero value. - */ - @Deprecated - public static Monoid monoid(final Semigroup s, final A zero) { - return s.monoid(zero); - } - - /** * A monoid that adds integers. */ @@ -538,18 +513,6 @@ public Integer multiply(int n, Integer integer) { } }); - /** - * @deprecated Since 4.7. Due to rounding errors, addition of doubles does not comply with monoid laws - */ - @Deprecated - public static final Monoid doubleAdditionMonoid = monoidDef((d1, d2) -> d1 + d2, 0.0); - - /** - * @deprecated Since 4.7. Due to rounding errors, multiplication of doubles does not comply with monoid laws - */ - @Deprecated - public static final Monoid doubleMultiplicationMonoid = monoidDef((d1, d2) -> d1 * d2, 1.0); - /** * A monoid that adds big integers. */ @@ -898,17 +861,6 @@ public Option sum(F0>> oas) { }); } - /** - * A monoid for options. - * @deprecated since 4.7. Use {@link #firstOptionMonoid()}. - * - * @return A monoid for options. - */ - @Deprecated - public static Monoid> optionMonoid() { - return firstOptionMonoid(); - } - /** * A monoid for options that take the first available value. * @@ -1090,7 +1042,7 @@ public Unit append(Unit a1, Unit a2) { }); /** - * A monoid for sets. + * A union monoid for sets. * * @param o An order for set elements. * @return A monoid for sets whose elements have the given order. @@ -1110,15 +1062,24 @@ public Set append(Set a1, Set a2) { } /** - * A monoid for the maximum of elements with ordering o. - * @deprecated since 4.7. Use {@link Ord#maxMonoid(Object)} + * A intersection monoid for sets. * - * @param o An ordering of elements. - * @param zero The minimum element. + * @param bounded A bound for all possible elements + * @param enumerator An enumerator for all possible elements + * @return A monoid for sets whose elements have the given order. */ - @Deprecated - public static Monoid ordMaxMonoid(final Ord o, final A zero) { - return o.maxMonoid(zero); + public static Monoid> setIntersectionMonoid(final Bounded bounded, final Enumerator enumerator) { + return monoidDef(new Definition>() { + @Override + public Set empty() { + return Set.iteratorSet(enumerator.order(), enumerator.toStream(bounded).iterator()); + } + + @Override + public Set append(Set a1, Set a2) { + return a1.intersect(a2); + } + }); } } diff --git a/core/src/main/java/fj/Ord.java b/core/src/main/java/fj/Ord.java index 0a778f4d..2bf90450 100644 --- a/core/src/main/java/fj/Ord.java +++ b/core/src/main/java/fj/Ord.java @@ -1,29 +1,15 @@ package fj; -import fj.data.Array; -import fj.data.Either; -import fj.data.List; -import fj.data.Natural; -import fj.data.NonEmptyList; -import fj.data.Option; -import fj.data.Set; -import fj.data.Stream; -import fj.data.Validation; - -import java.math.BigDecimal; -import java.math.BigInteger; +import fj.data.*; + +import java.math.*; import java.util.Comparator; -import static fj.Function.apply; -import static fj.Function.compose; -import static fj.Function.curry; -import static fj.Semigroup.semigroup; +import static fj.Function.*; import static fj.Semigroup.semigroupDef; /** * Tests for ordering between two objects. - * - * @version %build.number% */ public final class Ord { @@ -541,6 +527,37 @@ public static Ord> listOrd(final Ord oa) { }); } + /** + * Return a seq ord using the given value ord. + * + * @param ord the given value ord + * @param the type of the seq value + * @return the seq ord + */ + public static Ord> seqOrd(final Ord ord) { + return ordDef((l1, l2) -> { + Seq x1 = l1; + Seq x2 = l2; + + while (x1.isNotEmpty() && x2.isNotEmpty()) { + final Ordering o = ord.compare(x1.head(), x2.head()); + if (o == Ordering.LT || o == Ordering.GT) { + return o; + } + x1 = x1.tail(); + x2 = x2.tail(); + } + + if (x1.isEmpty() && x2.isEmpty()) { + return Ordering.EQ; + } else if (x1.isEmpty()) { + return Ordering.LT; + } else { + return Ordering.GT; + } + }); + } + /** * An order instance for the {@link NonEmptyList} type. * @@ -668,44 +685,6 @@ public static > Ord comparableOrd() { return ordDef((a1, a2) -> Ordering.fromInt(a1.compareTo(a2))); } - /** - * An order instance that uses {@link Object#hashCode()} for computing the order and equality, - * thus objects returning the same hashCode are considered to be equals. - * This is not safe and therefore this method is deprecated. - * - * @return An order instance that is based on {@link Object#hashCode()}. - * - * @deprecated As of release 4.7. - */ - @Deprecated - public static Ord hashOrd() { - return ordDef(a -> { - int aHash = a.hashCode(); - return a2 -> Ordering.fromInt(Integer.valueOf(aHash).compareTo(a2.hashCode())); - }); - } - - /** - * An order instance that uses {@link Object#hashCode()} and {@link Object#equals} for computing - * the order and equality. First the hashCode is compared, if this is equal, objects are compared - * using {@link Object#equals}. - * WARNING: This ordering violate antisymmetry on hash collisions. - * - * @return An order instance that is based on {@link Object#hashCode()} and {@link Object#equals}. - * - * @deprecated As of release 4.7. - */ - @Deprecated - public static Ord hashEqualsOrd() { - return ordDef(a -> { - int aHash = a.hashCode(); - return a2 -> { - final int a2Hash = a2.hashCode(); - return aHash < a2Hash ? Ordering.LT : aHash == a2Hash && a.equals(a2) ? Ordering.EQ : Ordering.GT; - }; - }); - } - class OrdComparator implements Comparator { @Override public final int compare(A o1, A o2) { diff --git a/core/src/main/java/fj/Ordering.java b/core/src/main/java/fj/Ordering.java index b22bcfcb..1828f69a 100644 --- a/core/src/main/java/fj/Ordering.java +++ b/core/src/main/java/fj/Ordering.java @@ -3,8 +3,6 @@ /** * The comparison of two instances of a type may have one of three orderings; less than, equal or * greater than. - * - * @version %build.number% */ public enum Ordering { /** diff --git a/core/src/main/java/fj/P.java b/core/src/main/java/fj/P.java index 6d174ed7..3e6efb74 100644 --- a/core/src/main/java/fj/P.java +++ b/core/src/main/java/fj/P.java @@ -4,8 +4,6 @@ /** * Functions across products. - * - * @version %build.number% */ public final class P { private P() { @@ -65,6 +63,13 @@ public static P1 softMemo(F0 f) { return new P1.SoftReferenceMemo<>(f); } + /** + * Convert a F0 into a P1, using weak call-by-need semantic using {@link #weakMemo(F0)}. + */ + public static P1 memo(F0 f) { + return weakMemo(f); + } + /** * Convert a F0 into a P1, using call-by-name semantic: * function f is evaluated at each call to {@link P1#_1()}. diff --git a/core/src/main/java/fj/P1.java b/core/src/main/java/fj/P1.java index 5e627403..5e575a4d 100644 --- a/core/src/main/java/fj/P1.java +++ b/core/src/main/java/fj/P1.java @@ -1,20 +1,12 @@ package fj; -import fj.data.Array; -import fj.data.Either; -import fj.data.List; -import fj.data.Option; -import fj.data.Stream; -import fj.data.Validation; +import fj.data.*; import java.lang.ref.Reference; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; import static fj.P.p; -import static fj.Unit.unit; -//import fj.data.*; - public abstract class P1 implements F0 { @@ -39,18 +31,6 @@ public static F, A> __1() { return P1::_1; } - /** - * Promote any function to a transformation between P1s. - * - * @deprecated As of release 4.5, use {@link #map_} - * @param f A function to promote to a transformation between P1s. - * @return A function promoted to operate on P1s. - */ - @Deprecated - public static F, P1> fmap(final F f) { - return map_(f); - } - /** * Promote any function to a transformation between P1s. * @@ -108,7 +88,7 @@ public final P1 bind(final P1 cb, final F> f) { * Binds the given function to the values in the given P1s with a final join. */ public final P1 bind(final P1 cb, final F2 f) { - return bind(cb, F2W.lift(f).curry()); + return bind(cb, f.curry()); } /** @@ -243,9 +223,8 @@ public final P1 map(final F f) { } /** - * @deprecated since 4.7. Use {@link P1#weakMemo()} instead. + * Wrap the memoized value into a WeakReference. */ - @Deprecated public final P1 memo() { return weakMemo(); } @@ -267,22 +246,6 @@ public final P1 memo() { */ public P1 softMemo() { return new SoftReferenceMemo<>(this); } - /** - * @deprecated since 4.7. Use {@link P#weakMemo(F0)} instead. - */ - @Deprecated - public static P1 memo(F f) { - return P.weakMemo(() -> f.f(unit())); - } - - /** - * @deprecated since 4.7. Use {@link P#weakMemo(F0)} instead. - */ - @Deprecated - public static P1 memo(F0 f) { - return P.weakMemo(f); - } - static final class Memo extends P1 { private volatile F0 fa; private A value; diff --git a/core/src/main/java/fj/P2.java b/core/src/main/java/fj/P2.java index b434a0da..b4979f03 100644 --- a/core/src/main/java/fj/P2.java +++ b/core/src/main/java/fj/P2.java @@ -9,8 +9,6 @@ /** * A product-2. - * - * @version %build.number% */ public abstract class P2 { /** @@ -166,7 +164,7 @@ public final Stream sequenceW(final Stream, C>> fs) { * @return the 1-product projection over the first element. */ public final P1 _1_() { - return F1Functions.lazy(P2.__1()).f(this); + return P2.__1().lazy().f(this); } /** @@ -175,7 +173,7 @@ public final P1 _1_() { * @return the 1-product projection over the second element. */ public final P1 _2_() { - return F1Functions.lazy(P2.__2()).f(this); + return P2.__2().lazy().f(this); } /** diff --git a/core/src/main/java/fj/P3.java b/core/src/main/java/fj/P3.java index fe980059..fff39ed8 100644 --- a/core/src/main/java/fj/P3.java +++ b/core/src/main/java/fj/P3.java @@ -4,8 +4,6 @@ /** * A product-3. - * - * @version %build.number% */ public abstract class P3 { /** @@ -101,7 +99,7 @@ public X _3() { * @return the 1-product projection over the first element. */ public final P1 _1_() { - return F1Functions.lazy(P3.__1()).f(this); + return P3.__1().lazy().f(this); } /** @@ -110,7 +108,7 @@ public final P1 _1_() { * @return the 1-product projection over the second element. */ public final P1 _2_() { - return F1Functions.lazy(P3.__2()).f(this); + return P3.__2().lazy().f(this); } /** @@ -119,7 +117,7 @@ public final P1 _2_() { * @return the 1-product projection over the third element. */ public final P1 _3_() { - return F1Functions.lazy(P3.__3()).f(this); + return P3.__3().lazy().f(this); } diff --git a/core/src/main/java/fj/P4.java b/core/src/main/java/fj/P4.java index a7bf8167..d2dca64b 100644 --- a/core/src/main/java/fj/P4.java +++ b/core/src/main/java/fj/P4.java @@ -4,8 +4,6 @@ /** * A product-4. - * - * @version %build.number% */ public abstract class P4 { /** @@ -146,7 +144,7 @@ public X _4() { * @return the 1-product projection over the first element. */ public final P1 _1_() { - return F1Functions.lazy(P4.__1()).f(this); + return P4.__1().lazy().f(this); } /** @@ -155,7 +153,7 @@ public final P1 _1_() { * @return the 1-product projection over the second element. */ public final P1 _2_() { - return F1Functions.lazy(P4.__2()).f(this); + return P4.__2().lazy().f(this); } /** @@ -164,7 +162,7 @@ public final P1 _2_() { * @return the 1-product projection over the third element. */ public final P1 _3_() { - return F1Functions.lazy(P4.__3()).f(this); + return P4.__3().lazy().f(this); } /** @@ -173,7 +171,7 @@ public final P1 _3_() { * @return the 1-product projection over the fourth element. */ public final P1 _4_() { - return F1Functions.lazy(P4.__4()).f(this); + return P4.__4().lazy().f(this); } diff --git a/core/src/main/java/fj/P5.java b/core/src/main/java/fj/P5.java index 69262327..07138cb3 100644 --- a/core/src/main/java/fj/P5.java +++ b/core/src/main/java/fj/P5.java @@ -4,8 +4,6 @@ /** * A product-5. - * - * @version %build.number% */ public abstract class P5 { /** @@ -199,7 +197,7 @@ public X _5() { * @return the 1-product projection over the first element. */ public final P1 _1_() { - return F1Functions.lazy(P5.__1()).f(this); + return P5.__1().lazy().f(this); } /** @@ -208,7 +206,7 @@ public final P1 _1_() { * @return the 1-product projection over the second element. */ public final P1 _2_() { - return F1Functions.lazy(P5.__2()).f(this); + return P5.__2().lazy().f(this); } /** @@ -217,7 +215,7 @@ public final P1 _2_() { * @return the 1-product projection over the third element. */ public final P1 _3_() { - return F1Functions.lazy(P5.__3()).f(this); + return P5.__3().lazy().f(this); } /** @@ -226,7 +224,7 @@ public final P1 _3_() { * @return the 1-product projection over the fourth element. */ public final P1 _4_() { - return F1Functions.lazy(P5.__4()).f(this); + return P5.__4().lazy().f(this); } /** @@ -235,7 +233,7 @@ public final P1 _4_() { * @return the 1-product projection over the fifth element. */ public final P1 _5_() { - return F1Functions.lazy(P5.__5()).f(this); + return P5.__5().lazy().f(this); } /** diff --git a/core/src/main/java/fj/P6.java b/core/src/main/java/fj/P6.java index fbf91948..295b5427 100644 --- a/core/src/main/java/fj/P6.java +++ b/core/src/main/java/fj/P6.java @@ -4,8 +4,6 @@ /** * A product-6. - * - * @version %build.number% */ @SuppressWarnings("UnnecessaryFullyQualifiedName") public abstract class P6 { @@ -261,7 +259,7 @@ public X _6() { * @return the 1-product projection over the first element. */ public final P1 _1_() { - return F1Functions.lazy(P6.__1()).f(this); + return P6.__1().lazy().f(this); } /** @@ -270,7 +268,7 @@ public final P1 _1_() { * @return the 1-product projection over the second element. */ public final P1 _2_() { - return F1Functions.lazy(P6.__2()).f(this); + return P6.__2().lazy().f(this); } /** @@ -279,7 +277,7 @@ public final P1 _2_() { * @return the 1-product projection over the third element. */ public final P1 _3_() { - return F1Functions.lazy(P6.__3()).f(this); + return P6.__3().lazy().f(this); } /** @@ -288,7 +286,7 @@ public final P1 _3_() { * @return the 1-product projection over the fourth element. */ public final P1 _4_() { - return F1Functions.lazy(P6.__4()).f(this); + return P6.__4().lazy().f(this); } /** @@ -297,7 +295,7 @@ public final P1 _4_() { * @return the 1-product projection over the fifth element. */ public final P1 _5_() { - return F1Functions.lazy(P6.__5()).f(this); + return P6.__5().lazy().f(this); } /** @@ -306,7 +304,7 @@ public final P1 _5_() { * @return the 1-product projection over the sixth element. */ public final P1 _6_() { - return F1Functions.lazy(P6.__6()).f(this); + return P6.__6().lazy().f(this); } /** diff --git a/core/src/main/java/fj/P7.java b/core/src/main/java/fj/P7.java index b1c67b30..17161f5d 100644 --- a/core/src/main/java/fj/P7.java +++ b/core/src/main/java/fj/P7.java @@ -4,8 +4,6 @@ /** * A product-7. - * - * @version %build.number% */ @SuppressWarnings("UnnecessaryFullyQualifiedName") public abstract class P7 { @@ -330,7 +328,7 @@ public X _7() { * @return the 1-product projection over the first element. */ public final P1 _1_() { - return F1Functions.lazy(P7.__1()).f(this); + return P7.__1().lazy().f(this); } /** @@ -339,7 +337,7 @@ public final P1 _1_() { * @return the 1-product projection over the second element. */ public final P1 _2_() { - return F1Functions.lazy(P7.__2()).f(this); + return P7.__2().lazy().f(this); } /** @@ -348,7 +346,7 @@ public final P1 _2_() { * @return the 1-product projection over the third element. */ public final P1 _3_() { - return F1Functions.lazy(P7.__3()).f(this); + return P7.__3().lazy().f(this); } /** @@ -357,7 +355,7 @@ public final P1 _3_() { * @return the 1-product projection over the fourth element. */ public final P1 _4_() { - return F1Functions.lazy(P7.__4()).f(this); + return P7.__4().lazy().f(this); } /** @@ -366,7 +364,7 @@ public final P1 _4_() { * @return the 1-product projection over the fifth element. */ public final P1 _5_() { - return F1Functions.lazy(P7.__5()).f(this); + return P7.__5().lazy().f(this); } /** @@ -375,7 +373,7 @@ public final P1 _5_() { * @return the 1-product projection over the sixth element. */ public final P1 _6_() { - return F1Functions.lazy(P7.__6()).f(this); + return P7.__6().lazy().f(this); } /** @@ -384,7 +382,7 @@ public final P1 _6_() { * @return the 1-product projection over the seventh element. */ public final P1 _7_() { - return F1Functions.lazy(P7.__7()).f(this); + return P7.__7().lazy().f(this); } /** diff --git a/core/src/main/java/fj/P8.java b/core/src/main/java/fj/P8.java index f15d63a1..571d6a15 100644 --- a/core/src/main/java/fj/P8.java +++ b/core/src/main/java/fj/P8.java @@ -4,8 +4,6 @@ /** * A product-8. - * - * @version %build.number% */ @SuppressWarnings("UnnecessaryFullyQualifiedName") public abstract class P8 { @@ -408,7 +406,7 @@ public X _8() { * @return the 1-product projection over the first element. */ public final P1 _1_() { - return F1Functions.lazy(P8.__1()).f(this); + return P8.__1().lazy().f(this); } /** @@ -417,7 +415,7 @@ public final P1 _1_() { * @return the 1-product projection over the second element. */ public final P1 _2_() { - return F1Functions.lazy(P8.__2()).f(this); + return P8.__2().lazy().f(this); } /** @@ -426,7 +424,7 @@ public final P1 _2_() { * @return the 1-product projection over the third element. */ public final P1 _3_() { - return F1Functions.lazy(P8.__3()).f(this); + return P8.__3().lazy().f(this); } /** @@ -435,7 +433,7 @@ public final P1 _3_() { * @return the 1-product projection over the fourth element. */ public final P1 _4_() { - return F1Functions.lazy(P8.__4()).f(this); + return P8.__4().lazy().f(this); } /** @@ -444,7 +442,7 @@ public final P1 _4_() { * @return the 1-product projection over the fifth element. */ public final P1 _5_() { - return F1Functions.lazy(P8.__5()).f(this); + return P8.__5().lazy().f(this); } /** @@ -453,7 +451,7 @@ public final P1 _5_() { * @return the 1-product projection over the sixth element. */ public final P1 _6_() { - return F1Functions.lazy(P8.__6()).f(this); + return P8.__6().lazy().f(this); } /** @@ -462,7 +460,7 @@ public final P1 _6_() { * @return the 1-product projection over the seventh element. */ public final P1 _7_() { - return F1Functions.lazy(P8.__7()).f(this); + return P8.__7().lazy().f(this); } /** @@ -471,7 +469,7 @@ public final P1 _7_() { * @return the 1-product projection over the eighth element. */ public final P1 _8_() { - return F1Functions.lazy(P8.__8()).f(this); + return P8.__8().lazy().f(this); } /** diff --git a/core/src/main/java/fj/Primitive.java b/core/src/main/java/fj/Primitive.java index 2007c03c..6ded797e 100644 --- a/core/src/main/java/fj/Primitive.java +++ b/core/src/main/java/fj/Primitive.java @@ -2,8 +2,6 @@ /** * Functions that convert between Java primitive types. - * - * @version %build.number% */ public final class Primitive { private Primitive() { diff --git a/core/src/main/java/fj/Rng.java b/core/src/main/java/fj/Rng.java index 20b3358f..46c31b27 100644 --- a/core/src/main/java/fj/Rng.java +++ b/core/src/main/java/fj/Rng.java @@ -1,8 +1,5 @@ package fj; -/** - * Created by MarkPerry on 7/07/2014. - */ public abstract class Rng { public abstract P2 nextInt(); diff --git a/core/src/main/java/fj/Semigroup.java b/core/src/main/java/fj/Semigroup.java index 42f05f5b..cd31e1e3 100644 --- a/core/src/main/java/fj/Semigroup.java +++ b/core/src/main/java/fj/Semigroup.java @@ -13,21 +13,16 @@ import java.math.BigDecimal; import java.math.BigInteger; -import static fj.F1Functions.dimap; import static fj.Function.constant; import static fj.Function.identity; import static fj.Monoid.*; import static fj.data.DList.listDList; -import static fj.data.Option.none; -import static fj.data.Option.some; /** * Implementations must satisfy the law of associativity: *
      *
    • Associativity; forall x. forall y. forall z. sum(sum(x, y), z) == sum(x, sum(y, z))
    • *
    - * - * @version %build.number% */ public final class Semigroup
    { @@ -193,7 +188,7 @@ public B append(B a1, B a2) { @Override public F prepend(B b) { - return dimap(def.prepend(g.f(b)), g, f); + return def.prepend(g.f(b)).dimap(g, f); } @Override @@ -295,23 +290,11 @@ public static Semigroup semigroup(final F2 sum) { */ public static final Semigroup intAdditionSemigroup = intAdditionMonoid.semigroup(); - /** - * @deprecated Since 4.7. Due to rounding errors, addition of doubles does not comply with monoid laws - */ - @Deprecated - public static final Semigroup doubleAdditionSemigroup = semigroupDef((d1, d2) -> d1 + d2); - /** * A semigroup that multiplies integers. */ public static final Semigroup intMultiplicationSemigroup = intMultiplicationMonoid.semigroup(); - /** - * @deprecated Since 4.7. Due to rounding errors, addition of doubles does not comply with monoid laws - */ - @Deprecated - public static final Semigroup doubleMultiplicationSemigroup = semigroupDef((d1, d2) -> d1 * d2); - /** * A semigroup that yields the maximum of integers. */ @@ -523,17 +506,6 @@ public NonEmptyList sum(NonEmptyList nea, F0>> neas }); } - /** - * A semigroup for optional values. - * @deprecated since 4.7. Use {@link #firstOptionSemigroup()}. - * - * @return A semigroup for optional values. - */ - @Deprecated - public static Semigroup> optionSemigroup() { - return firstOptionSemigroup(); - } - /** * A semigroup for optional values that take the first available value. * @@ -621,7 +593,7 @@ public static Semigroup> ioSemigroup(final Semigroup sa) { public static final Semigroup unitSemigroup = unitMonoid.semigroup(); /** - * A semigroup for sets. + * A union semigroup for sets. * * @return a semigroup for sets. */ @@ -629,4 +601,13 @@ public static Semigroup> setSemigroup() { return semigroupDef(Set::union); } + /** + * A intersection semigroup for sets. + * + * @return a semigroup for sets. + */ + public static Semigroup> setIntersectionSemigroup() { + return semigroupDef(Set::intersect); + } + } diff --git a/core/src/main/java/fj/Show.java b/core/src/main/java/fj/Show.java index cd57897b..8a21a281 100644 --- a/core/src/main/java/fj/Show.java +++ b/core/src/main/java/fj/Show.java @@ -27,8 +27,6 @@ /** * Renders an object for display. - * - * @version %build.number% */ public final class Show { private final F> f; @@ -256,6 +254,16 @@ public static Show> eitherShow(final Show sa, final Show< fromString("Right(").append(sb.f.f(e.right().value())).append(single(')'))); } + public static Show> either3Show(final Show sa, final Show sb, final Show sc) { + return show(e -> + e.either( + a -> fromString("Left(").append(sa.f.f(a)).append(single(')')), + b -> fromString("Middle(").append(sb.f.f(b)).append(single(')')), + c -> fromString("Right(").append(sc.f.f(c)).append(single(')')) + ) + ); + } + /** * A show instance for the {@link Result} type. * diff --git a/core/src/main/java/fj/Try.java b/core/src/main/java/fj/Try.java index d8a30130..8418a621 100644 --- a/core/src/main/java/fj/Try.java +++ b/core/src/main/java/fj/Try.java @@ -10,9 +10,6 @@ import static fj.data.Validation.fail; import static fj.data.Validation.success; -/** - * Created by mperry on 24/07/2014. - */ public final class Try { private Try() { diff --git a/core/src/main/java/fj/TryEffect.java b/core/src/main/java/fj/TryEffect.java index cd1f75ab..a240e638 100644 --- a/core/src/main/java/fj/TryEffect.java +++ b/core/src/main/java/fj/TryEffect.java @@ -3,9 +3,6 @@ import fj.data.Validation; import fj.function.*; -/** - * Created by mperry on 29/08/2014. - */ public final class TryEffect { private TryEffect(){} diff --git a/core/src/main/java/fj/Unit.java b/core/src/main/java/fj/Unit.java index c7471f21..76463ec1 100644 --- a/core/src/main/java/fj/Unit.java +++ b/core/src/main/java/fj/Unit.java @@ -2,8 +2,6 @@ /** * The unit type which has only one value. - * - * @version %build.number% */ public final class Unit { private static final Unit u = new Unit(); diff --git a/core/src/main/java/fj/control/Trampoline.java b/core/src/main/java/fj/control/Trampoline.java index 47fd282b..5c787f04 100644 --- a/core/src/main/java/fj/control/Trampoline.java +++ b/core/src/main/java/fj/control/Trampoline.java @@ -9,7 +9,7 @@ /** * A Trampoline is a potentially branching computation that can be stepped through and executed in constant stack. - * It represent suspendable coroutines with subroutine calls, reified as a data structure. + * It represents suspendable coroutines with subroutine calls, reified as a data structure. */ public abstract class Trampoline { @@ -171,7 +171,7 @@ public static F>, Trampoline> suspend_() { * @return A new trampoline that runs this trampoline, then applies the given function to the result. */ public final Trampoline map(final F f) { - return bind(F1Functions.o(Trampoline.pure(), f)); + return bind(Trampoline.pure().o(f)); } /** @@ -266,10 +266,11 @@ public final Trampoline zipWith(final Trampoline b, final F2>, B> eb = b.resume(); for (final P1> x : ea.left()) { for (final P1> y : eb.left()) { - return suspend(x.bind(y, F2Functions.curry((ta, tb) -> suspend(() -> ta.zipWith(tb, f))))); + F, F, Trampoline>> z = ta -> tb -> suspend(() -> ta.zipWith(tb, f)); + return suspend(x.bind(y, z)); } for (final B y : eb.right()) { - return suspend(x.map(ta -> ta.map(F2Functions.f(F2Functions.flip(f), y)))); + return suspend(x.map(ta -> ta.map(f.flip().f(y)))); } } for (final A x : ea.right()) { @@ -277,7 +278,7 @@ public final Trampoline zipWith(final Trampoline b, final F2 pure(f.f(x, y))); } for (final P1> y : eb.left()) { - return suspend(y.map(liftM2(F2Functions.curry(f)).f(pure(x)))); + return suspend(y.map(liftM2(f.curry()).f(pure(x)))); } } throw Bottom.error("Match error: Trampoline is neither done nor suspended."); diff --git a/core/src/main/java/fj/control/parallel/ParModule.java b/core/src/main/java/fj/control/parallel/ParModule.java index 514988aa..b9eeacbf 100644 --- a/core/src/main/java/fj/control/parallel/ParModule.java +++ b/core/src/main/java/fj/control/parallel/ParModule.java @@ -67,7 +67,7 @@ public F, Promise> promise() { * that can be claimed in the future. */ public F> promise(final F f) { - return F1Functions.promiseK(f, strategy); + return f.promiseK(strategy); } /** @@ -88,7 +88,7 @@ public F, F>> promisePure() { * that can be claimed in the future. */ public F2> promise(final F2 f) { - return P2.untuple(F1Functions.promiseK(F2Functions.tuple(f), strategy)); + return P2.untuple(f.tuple().promiseK(strategy)); } diff --git a/core/src/main/java/fj/data/Array.java b/core/src/main/java/fj/data/Array.java index 7e937200..fb245f3f 100755 --- a/core/src/main/java/fj/data/Array.java +++ b/core/src/main/java/fj/data/Array.java @@ -130,19 +130,6 @@ public Object[] array() { return copyOf(a, a.length); } - /** - * To be removed in future release: - * affectation of the result of this method to a non generic array - * will result in runtime error (ClassCastException). - * - * @deprecated As of release 4.6, use {@link #array(Class)}. - */ - @SuppressWarnings("unchecked") - @Deprecated - public A[] toJavaArray() { - return (A[]) array(); - } - /** * Returns an option projection of this array; None if empty, or the first element in * Some. diff --git a/java8/src/main/java/fj/data/Collectors.java b/core/src/main/java/fj/data/Collectors.java similarity index 100% rename from java8/src/main/java/fj/data/Collectors.java rename to core/src/main/java/fj/data/Collectors.java diff --git a/core/src/main/java/fj/data/Either.java b/core/src/main/java/fj/data/Either.java index a9a746a9..e710c1d6 100644 --- a/core/src/main/java/fj/data/Either.java +++ b/core/src/main/java/fj/data/Either.java @@ -1,27 +1,16 @@ package fj.data; -import fj.Equal; -import fj.F; -import fj.F0; -import fj.Function; -import fj.Hash; -import fj.P1; -import fj.Show; -import fj.Unit; +import fj.*; import fj.function.Effect1; -import java.util.Collection; -import java.util.Iterator; +import java.util.*; import static fj.Bottom.error; -import static fj.Function.compose; -import static fj.Function.identity; +import static fj.Function.*; import static fj.P.p; import static fj.Unit.unit; import static fj.data.Array.mkArray; -import static fj.data.List.cons_; -import static fj.data.List.list; -import static fj.data.List.single; +import static fj.data.List.*; import static fj.data.Option.some; /** @@ -321,6 +310,19 @@ public IO> traverseIO(final F> f) { IOFunctions.unit(Either.right(e.right().value())); } + /** + * Traverse this left with the given function and collect the output as a p1. + * + * @param f the given function + * @param the type of the p1 value + * @return the p1 + */ + public P1> traverseP1(final F> f) { + return e.isLeft() ? + f.f(value()).map(left_()) : + p(right(e.right().value())); + } + /** * Returns None if this projection has no value or if the given predicate * p does not hold for the value, otherwise, returns a right in Some. @@ -464,6 +466,16 @@ public Either either() { return e; } + /** + * Returns the value of this projection or fails with the given error message. + * + * @param err The error message to fail with. + * @return The value of this projection + */ + public B valueE(final String err) { + return valueE(p(err)); + } + /** * Returns the value of this projection or fails with the given error message. * @@ -487,6 +499,16 @@ public B value() { return valueE(p("right.value on Left")); } + /** + * The value of this projection or the given argument. + * + * @param a The value to return if this projection has no value. + * @return The value of this projection or the given argument. + */ + public B orValue(final B a) { + return e.isRight() ? value() : a; + } + /** * The value of this projection or the given argument. * @@ -586,12 +608,26 @@ public IO> traverseIO(final F> f) { IOFunctions.lazy(() -> left(e.left().value())); } + /** + * Traverse this right with the given function and collect the output as a p1. + * + * @param f the given function + * @param the type of the p1 value + * @return the p1 + */ public P1> traverseP1(final F> f) { return e.isRight() ? f.f(value()).map(right_()) : p(left(e.left().value())); } + /** + * Traverse this right with the given function and collect the output as an option. + * + * @param f the given function + * @param the type of the option value + * @return the option + */ public Option> traverseOption(final F> f) { return e.isRight() ? f.f(value()).map(right_()) : @@ -758,6 +794,22 @@ public static F, X> either_(final F left, final F the type of the function output + * @return the either + */ + public final Either leftMap(final F f) { + return left().map(f); + } + + /** + * Return a function that maps a given function across this either's left projection. + * + * @param the type of the right value + * @param the type of the left value + * @param the type of the function output * @return A function that maps another function across an either's left projection. */ public static F, F, Either>> leftMap_() { @@ -765,6 +817,22 @@ public static F, F, Either>> leftMap_() { } /** + * Map the given function across this either's right. + * + * @param f the given function + * @param the type of the function output + * @return the either + */ + public final Either rightMap(final F f) { + return right().map(f); + } + + /** + * Return a function that maps a given function across this either's right projection. + * + * @param the type of the right value + * @param the type of the left value + * @param the type of the function output * @return A function that maps another function across an either's right projection. */ public static F, F, Either>> rightMap_() { @@ -796,6 +864,7 @@ public static Either joinRight(final Either> e) { * * @param a The list of values to sequence with the either monad. * @return A sequenced value. + * @see fj.data.List#sequenceEitherLeft */ public static Either, X> sequenceLeft(final List> a) { return a.isEmpty() ? @@ -808,6 +877,8 @@ public static Either, X> sequenceLeft(final List> a) * * @param a The list of values to sequence with the either monad. * @return A sequenced value. + * @see fj.data.List#sequenceEither + * @see fj.data.List#sequenceEitherRight */ public static Either> sequenceRight(final List> a) { return a.isEmpty() ? @@ -898,7 +969,7 @@ public static A reduce(final Either e) { } /** - * If the condition satisfies, return the given A in left, otherwise, return the given B in right. + * If the condition satisfies, return the given B in right, otherwise, return the given A in left. * * @param c The condition to test. * @param right The right value to use if the condition satisfies. diff --git a/core/src/main/java/fj/data/Either3.java b/core/src/main/java/fj/data/Either3.java new file mode 100644 index 00000000..9b545f7e --- /dev/null +++ b/core/src/main/java/fj/data/Either3.java @@ -0,0 +1,580 @@ +package fj.data; + +import fj.*; +import fj.function.Effect1; + +import java.util.Collection; +import java.util.Iterator; + +import static fj.Function.identity; +import static fj.P.p; +import static fj.Unit.unit; +import static fj.data.Array.mkArray; +import static fj.data.List.*; +import static fj.data.Option.none; +import static fj.data.Option.some; + +public abstract class Either3 { + + private Either3() {} + + private static final class Left extends Either3 { + private final A a; + + Left(A a) { + this.a = a; + } + + @Override + public D either(F fa, F fb, F fc) { + return fa.f(a); + } + + } + + private static final class Middle extends Either3 { + private final B b; + + Middle(B b) { + this.b = b; + } + + @Override + public D either(F fa, F fb, F fc) { + return fb.f(b); + } + } + + private static final class Right extends Either3 { + private final C c; + Right(C c) { + this.c = c; + } + + @Override + public D either(F fa, F fb, F fc) { + return fc.f(c); + } + + + + } + + public static final class LeftProjection { + private final Either3 e; + + private LeftProjection(final Either3 e) { + this.e = e; + } + + public Either3 apply(final Either3, B, C> e) { + return e.left().bind(this::map); + } + + public Either3 bind(F> f) { + return e.either(a -> f.f(a), b -> middle(b), c -> right(c)); + } + + public Either3 either() { + return e; + } + + public boolean exists(final F f) { + return e.either(a -> f.f(a), b -> false, c -> false); + } + + public Option> filter(final F f) { + return e.either(a -> f.f(a) ? some(left(a)) : none(), b -> none(), c -> none()); + } + + public boolean forall(final F f) { + return e.either(a -> f.f(a), b -> true, c -> true); + } + + public Unit foreach(final F f) { + return e.either(a -> f.f(a), b -> unit(), c -> unit()); + } + + public void foreachDoEffect(final Effect1 f) { + e.either(a -> f.toF().f(a), b -> unit(), c -> unit()); + } + + public Iterator iterator() { + return toList().iterator(); + } + + public Either3 map(final F f) { + return e.either(a -> left(f.f(a)), b -> middle(b), c -> right(c)); + } + + public A orValue(final A value) { + return orValue(() -> value); + } + + public A orValue(final F0 f) { + return e.either(a -> a, b -> f.f(), c -> f.f()); + } + + public Either3 sequence(final Either3 e) { + return bind(Function.constant(e)); + } + + public Array toArray() { + return e.either( + a -> Array.single(a), + b -> Array.empty(), + c -> Array.empty() + ); + } + + public Collection toCollection() { + return toList().toCollection(); + } + + public List toList() { + return e.either(a -> single(a), b -> nil(), c -> nil()); + } + + public Option toOption() { + return e.either(a -> some(a), b -> none(), c -> none()); + } + + public Stream toStream() { + return e.either(a -> Stream.single(a), b -> Stream.nil(), c -> Stream.nil()); + } + + public IO> traverseIO(final F> f) { + return e.either( + a -> f.f(a).map(Either3::left), + b -> IOFunctions.unit(middle(b)), + c -> IOFunctions.unit(right(c)) + ); + } + + public List> traverseList1(final F> f) { + return e.either( + a -> f.f(a).map(Either3::left), + b -> single(middle(b)), + c -> single(right(c)) + ); + } + + public Option> traverseOption(final F> f) { + return e.either( + a -> f.f(a).map(Either3::left), + b -> some(middle(b)), + c -> some(right(c)) + ); + + } + + public P1> traverseP1(final F> f) { + return e.either( + a -> f.f(a).map(Either3::left), + b -> p(middle(b)), + c -> p(right(c)) + ); + + } + + public Stream> traverseStream(final F> f) { + return e.either( + a -> f.f(a).map(Either3::left), + b -> Stream.single(middle(b)), + c -> Stream.single(right(c)) + ); + + } + + } + + public static final class MiddleProjection { + private final Either3 e; + + private MiddleProjection(final Either3 e) { + this.e = e; + } + + public Either3 apply(final Either3, C> e) { + return e.middle().bind(this::map); + } + + public Either3 bind(F> f) { + return e.either(a -> left(a), b -> f.f(b), c -> right(c)); + } + + public Either3 either() { + return e; + } + + public boolean exists(final F f) { + return e.either(a -> false, b -> f.f(b), c -> false); + } + + public Option> filter(final F f) { + return e.either(a -> none(), b -> f.f(b) ? some(middle(b)) : none(), c -> none()); + } + + public boolean forall(final F f) { + return e.either(a -> true, b -> f.f(b), c -> true); + } + + public Unit foreach(final F f) { + return e.either(a -> unit(), b -> f.f(b), c -> unit()); + } + + public void foreachDoEffect(final Effect1 f) { + e.either(a -> unit(), b -> f.toF().f(b), c -> unit()); + } + + public Iterator iterator() { + return toList().iterator(); + } + + public Either3 map(final F f) { + return e.either(a -> left(a), b -> middle(f.f(b)), c -> right(c)); + } + + public B orValue(final B value) { + return orValue(() -> value); + } + + public B orValue(final F0 f) { + return e.either(a -> f.f(), b -> b, c -> f.f()); + } + + public Either3 sequence(final Either3 e) { + return bind(Function.constant(e)); + } + + public Array toArray() { + return e.either( + a -> Array.empty(), + b -> Array.single(b), + c -> Array.empty() + ); + } + + public Collection toCollection() { + return toList().toCollection(); + } + + public List toList() { + return e.either(a -> nil(), b -> single(b), c -> nil()); + } + + public Option toOption() { + return e.either(a -> none(), b -> some(b), c -> none()); + } + + public Stream toStream() { + return e.either(a -> Stream.nil(), b -> Stream.single(b), c -> Stream.nil()); + } + + public IO> traverseIO(final F> f) { + return e.either( + a -> IOFunctions.unit(left(a)), + b -> f.f(b).map(Either3::middle), + c -> IOFunctions.unit(right(c)) + ); + } + + public List> traverseList1(final F> f) { + return e.either( + a -> single(left(a)), + b -> f.f(b).map(Either3::middle), + c -> single(right(c)) + ); + } + + public Option> traverseOption(final F> f) { + return e.either( + a -> some(left(a)), + b -> f.f(b).map(Either3::middle), + c -> some(right(c)) + ); + + } + + public P1> traverseP1(final F> f) { + return e.either( + a -> p(left(a)), + b -> f.f(b).map(Either3::middle), + c -> p(right(c)) + ); + + } + + public Stream> traverseStream(final F> f) { + return e.either( + a -> Stream.single(left(a)), + b -> f.f(b).map(Either3::middle), + c -> Stream.single(right(c)) + ); + + } + + + } + + public final Either3 leftMap(F f) { + return left().map(f); + } + + public final F, Either3> leftMap_() { + return this::leftMap; + } + + public final Either3 middleMap(F f) { + return middle().map(f); + } + + public final F, Either3> middleMap_() { + return this::middleMap; + } + + public final Either3 rightMap(F f) { + return right().map(f); + } + + public final F, Either3> rightMap_() { + return this::rightMap; + } + + public static final class RightProjection { + private final Either3 e; + + private RightProjection(final Either3 e) { + this.e = e; + } + public Either3 apply(final Either3> e) { + return e.right().bind(this::map); + } + + public Either3 bind(F> f) { + return e.either(a -> left(a), b -> middle(b), c -> f.f(c)); + } + + public Either3 either() { + return e; + } + + public boolean exists(final F f) { + return e.either(a -> false, b -> false, c -> f.f(c)); + } + + public Option> filter(final F f) { + return e.either(a -> none(), b -> none(), c -> f.f(c) ? some(right(c)) : none()); + } + + public boolean forall(final F f) { + return e.either(a -> true, b -> true, c -> f.f(c)); + } + + public Unit foreach(final F f) { + return e.either(a -> unit(), b -> unit(), c -> f.f(c)); + } + + public void foreachDoEffect(final Effect1 f) { + e.either(a -> unit(), b -> unit(), c -> f.toF().f(c)); + } + + public Iterator iterator() { + return toList().iterator(); + } + + public Either3 map(final F f) { + return e.either(a -> left(a), b -> middle(b), c -> right(f.f(c))); + } + + public C orValue(final C value) { + return orValue(() -> value); + } + + public C orValue(final F0 f) { + return e.either(a -> f.f(), b -> f.f(), c -> c); + } + + public Either3 sequence(final Either3 e) { + return bind(Function.constant(e)); + } + + public Array toArray() { + return e.either( + a -> Array.empty(), + b -> Array.empty(), + c -> Array.single(c) + ); + } + + public Collection toCollection() { + return toList().toCollection(); + } + + public List toList() { + return e.either(a -> nil(), b -> nil(), c -> single(c)); + } + + public Option toOption() { + return e.either(a -> none(), b -> none(), c -> some(c)); + } + + public Stream toStream() { + return e.either(a -> Stream.nil(), b -> Stream.nil(), c -> Stream.single(c)); + } + + public IO> traverseIO(final F> f) { + return e.either( + a -> IOFunctions.unit(left(a)), + b -> IOFunctions.unit(middle(b)), + c -> f.f(c).map(Either3::right) + ); + } + + public List> traverseList1(final F> f) { + return e.either( + a -> single(left(a)), + b -> single(middle(b)), + c -> f.f(c).map(Either3::right) + ); + } + + public Option> traverseOption(final F> f) { + return e.either( + a -> some(left(a)), + b -> some(middle(b)), + c -> f.f(c).map(Either3::right) + ); + + } + + public P1> traverseP1(final F> f) { + return e.either( + a -> p(left(a)), + b -> p(middle(b)), + c -> f.f(c).map(Either3::right) + ); + + } + + public Stream> traverseStream(final F> f) { + return e.either( + a -> Stream.single(left(a)), + b -> Stream.single(middle(b)), + c -> f.f(c).map(Either3::right) + ); + + } + + + } + + public static Either3 left(A a) { + return new Left<>(a); + } + + public static F> left_() { + return Either3::left; + } + + public static Either3 middle(B b) { + return new Middle<>(b); + } + + public static Either3 right(C c) { + return new Right<>(c); + } + + public boolean isLeft() { + return either(a -> true, b -> false, c -> false); + } + + public boolean isMiddle() { + return either(a -> false, b -> true, c -> false); + } + + public boolean isRight() { + return either(a -> false, b -> false, c -> true); + } + + public Either3 map3(F fl, F fm, F fr) { + return either( + a -> left(fl.f(a)), + b -> middle(fm.f(b)), + c -> right(fr.f(c)) + ); + } + + public abstract D either(F fa, F fb, F fc); + + public static F, D> either_(F fa, F fb, F fc) { + return e -> e.either(fa, fb, fc); + } + + public static Either3 joinLeft(final Either3, B, C> e) { + return e.left().bind(identity()); + } + + public static Either3 joinMiddle(final Either3, C> e) { + return e.middle().bind(identity()); + } + + public static Either3 joinRight(final Either3> e) { + return e.right().bind(identity()); + } + + public Either3 moveLeft() { + return either(a -> right(a), b -> left(b), c -> middle(c)); + } + + public Either3 moveRight() { + return either(a -> middle(a), b -> right(b), c -> left(c)); + } + + public Either3 swap() { + return either(a -> right(a), b -> middle(b), c -> left(c)); + } + + public Either3 swapLefts() { + return either(a -> middle(a), b -> left(b), c -> right(c)); + } + + public Either3 swapRights() { + return either(a -> left(a), b -> right(b), c -> middle(c)); + } + + public Option leftOption() { + return either(a -> some(a), b -> none(), c -> none()); + } + + public Option middleOption() { + return either(a -> none(), b -> some(b), c -> none()); + } + + public Option rightOption() { + return either(a -> none(), b -> none(), c -> some(c)); + } + + public final LeftProjection left() { + return new LeftProjection<>(this); + } + + public final MiddleProjection middle() { + return new MiddleProjection<>(this); + } + + public final RightProjection right() { + return new RightProjection<>(this); + } + + @Override + public final boolean equals(Object other) { + return Equal.equals0(Either3.class, this, other, () -> Equal.either3Equal(Equal.anyEqual(), Equal.anyEqual(), Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.either3Hash(Hash.anyHash(), Hash.anyHash(), Hash.anyHash()).hash(this); + } + +} diff --git a/core/src/main/java/fj/data/Enumerator.java b/core/src/main/java/fj/data/Enumerator.java index 94f1b2cd..2fa96a6f 100644 --- a/core/src/main/java/fj/data/Enumerator.java +++ b/core/src/main/java/fj/data/Enumerator.java @@ -1,16 +1,12 @@ package fj.data; -import fj.F; +import fj.*; import static fj.Function.*; import static fj.data.Option.none; import static fj.data.Option.some; -import fj.Function; -import fj.Ord; - import static fj.Ord.*; -import fj.Ordering; import static fj.Ordering.*; import java.math.BigDecimal; @@ -185,6 +181,18 @@ public Stream toStream(final A a) { return Stream.fromFunction(this, id, a); } + /** + * Returns a stream of the values from this enumerator, + * starting at the min of given Bounded, ending at the max, counting up. + * + * @param bounded A value at which to begin the stream. + * @return a stream of the values from this enumerator, cut by bounded, counting up. + */ + public Stream toStream(final Bounded bounded) { + final F id = identity(); + return Stream.fromFunction(this, id, bounded.min()).takeWhile(item -> order.isLessThanOrEqualTo(item, bounded.max())); + } + /** * Create a new enumerator with the given minimum value. * diff --git a/core/src/main/java/fj/data/HashMap.java b/core/src/main/java/fj/data/HashMap.java index 400cf415..455c529c 100755 --- a/core/src/main/java/fj/data/HashMap.java +++ b/core/src/main/java/fj/data/HashMap.java @@ -325,16 +325,6 @@ public java.util.Map toMap() { return result; } - /** - * Converts the Iterable to a HashMap - * - * @deprecated As of release 4.5, use {@link #iterableHashMap(Iterable)} - */ - @Deprecated - public static HashMap from(final Iterable> entries) { - return iterableHashMap(entries); - } - public static HashMap fromMap(java.util.Map map) { return fromMap(Equal.anyEqual(), Hash.anyHash(), map); } @@ -347,16 +337,6 @@ public static HashMap fromMap(Equal eq, Hash h, java.util.Map return m; } - /** - * Converts the Iterable to a HashMap - * - * @deprecated As of release 4.5, use {@link #iterableHashMap} - */ - @Deprecated - public static HashMap from(final Iterable> entries, final Equal equal, final Hash hash) { - return iterableHashMap(equal, hash, entries); - } - /** * Converts the Iterable to a HashMap */ diff --git a/core/src/main/java/fj/data/IO.java b/core/src/main/java/fj/data/IO.java index ee27bdb7..69434728 100644 --- a/core/src/main/java/fj/data/IO.java +++ b/core/src/main/java/fj/data/IO.java @@ -1,8 +1,8 @@ package fj.data; -/** - * Created by MarkPerry on 19/06/2014. - */ +import fj.F; +import fj.Unit; +import fj.function.Try0; import java.io.IOException; @@ -13,8 +13,37 @@ * * @param the type of the result produced by the IO */ -public interface IO { +@FunctionalInterface +public interface IO extends Try0 { A run() throws IOException; + default A f() throws IOException { + return run(); + } + + default SafeIO> safe() { + return IOFunctions.toSafeValidation(this); + } + + default IO map(F f) { + return IOFunctions.map(this, f); + } + + default IO bind(F> f) { + return IOFunctions.bind(this, f); + } + + default IO append(IO iob) { + return IOFunctions.append(this, iob); + } + + public static IO getContents() { + return () -> IOFunctions.getContents().run(); + } + + public static IO interact(F f) { + return () -> IOFunctions.interact(f).run(); + } + } diff --git a/core/src/main/java/fj/data/IOFunctions.java b/core/src/main/java/fj/data/IOFunctions.java index 2ea15dfb..9573deea 100644 --- a/core/src/main/java/fj/data/IOFunctions.java +++ b/core/src/main/java/fj/data/IOFunctions.java @@ -2,7 +2,6 @@ import fj.F; import fj.F0; -import fj.F1Functions; import fj.F2; import fj.Function; import fj.P; @@ -204,7 +203,7 @@ public IO> f(IterV it) { return i; } final Input input = Input.el(s); - final F, IterV>, P1>> cont = F1Functions.lazy(Function.apply(input)); + final F, IterV>, P1>> cont = Function., IterV>apply(input).lazy(); i = i.fold(done, cont)._1(); } return i; @@ -248,7 +247,7 @@ public IO> f(IterV it) { } final Input input = Input.el(buffer); final F, IterV>, P1>> cont = - F1Functions.lazy(Function.apply(input)); + Function., IterV>apply(input).lazy(); i = i.fold(done, cont)._1(); } return i; diff --git a/core/src/main/java/fj/data/IOW.java b/core/src/main/java/fj/data/IOW.java deleted file mode 100644 index 859c9cdb..00000000 --- a/core/src/main/java/fj/data/IOW.java +++ /dev/null @@ -1,45 +0,0 @@ -package fj.data; - -import fj.F; -import fj.Unit; - -import java.io.IOException; - -/** - * Created by MarkPerry on 9/06/2015. - */ -public final class IOW implements IO { - - private final IO io; - - private IOW(IO in) { - io = in; - } - - public static IOW lift(IO io) { - return new IOW<>(io); - } - - @Override - public A run() throws IOException { - return io.run(); - } - - public SafeIO> safe() { - return IOFunctions.toSafeValidation(io); - } - - public IOW map(F f) { return lift(IOFunctions.map(io, f)); } - - public IOW bind(F> f) { return lift(IOFunctions.bind(io, f)); } - - public IOW append(IO iob) { return lift(IOFunctions.append(io, iob)); } - - public static IOW getContents() { - return lift(() -> IOFunctions.getContents().run()); - } - - public static IOW interact(F f) { - return lift(() -> IOFunctions.interact(f).run()); - } -} diff --git a/core/src/main/java/fj/data/Iteratee.java b/core/src/main/java/fj/data/Iteratee.java index bcb03cc0..e6177068 100644 --- a/core/src/main/java/fj/data/Iteratee.java +++ b/core/src/main/java/fj/data/Iteratee.java @@ -2,15 +2,11 @@ import fj.F; import fj.F0; -import fj.F1Functions; import fj.Function; import fj.P; import fj.P2; import fj.Unit; -/** - * - */ public final class Iteratee { /** The input to an iteratee. */ @@ -82,7 +78,7 @@ public Z fold(final F>, Z> done, final F, IterV, Option> runCont = new F, Option>() { - final F>, Option> done = F1Functions.andThen(P2.__1(), Option.some_()); + final F>, Option> done = P2.>__1().andThen(Option.some_()); final F, IterV>, Option> cont = Function.constant(Option.none()); @Override diff --git a/core/src/main/java/fj/data/Java.java b/core/src/main/java/fj/data/Java.java index ae7fcf6a..b3cb7e7f 100644 --- a/core/src/main/java/fj/data/Java.java +++ b/core/src/main/java/fj/data/Java.java @@ -35,8 +35,6 @@ /** * Functions that convert between types from the core Java API. - * - * @version %build.number% */ public final class Java { private Java() { @@ -1414,17 +1412,6 @@ public static F, List> ArrayList_List() { // END ArrayList -> - /** - * A function that converts Java lists to lists. - * @deprecated As of 4.3, use {@link #JavaList_List} - * - * @return A function that converts Java lists to lists. - */ - @Deprecated - public static F, List> JUList_List() { - return Java::JavaList_List; - } - public static F, List> JavaList_List() { return Java::JavaList_List; } diff --git a/java8/src/main/java/fj/data/Java8.java b/core/src/main/java/fj/data/Java8.java similarity index 96% rename from java8/src/main/java/fj/data/Java8.java rename to core/src/main/java/fj/data/Java8.java index e41f0958..184c01c5 100644 --- a/java8/src/main/java/fj/data/Java8.java +++ b/core/src/main/java/fj/data/Java8.java @@ -1,14 +1,5 @@ package fj.data; -import java.util.Iterator; -import java.util.Optional; -import java.util.Spliterators; -import java.util.function.BiFunction; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.StreamSupport; - import fj.F; import fj.F2; import fj.P; @@ -19,9 +10,15 @@ import fj.function.Try1; import fj.function.Try2; -/** - * Created by mperry on 3/06/2014. - */ +import java.util.Iterator; +import java.util.Optional; +import java.util.Spliterators; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.StreamSupport; + public final class Java8 { private Java8() { @@ -134,7 +131,7 @@ public static F Consumer_F(final Consumer c) { }; } - public static java.util.stream.Stream Stream_JavaStream(final fj.data.Stream s) { + public static java.util.stream.Stream Stream_JavaStream(final Stream s) { return Iterable_JavaStream(s); } @@ -146,7 +143,7 @@ public static java.util.stream.Stream Iterator_JavaStream(final Iterator< return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, 0), false); } - public static F, java.util.stream.Stream> Stream_JavaStream() { + public static F, java.util.stream.Stream> Stream_JavaStream() { return Java8::Stream_JavaStream; } diff --git a/core/src/main/java/fj/data/List.java b/core/src/main/java/fj/data/List.java index e0e12877..721ee9f1 100644 --- a/core/src/main/java/fj/data/List.java +++ b/core/src/main/java/fj/data/List.java @@ -1,59 +1,33 @@ package fj.data; -import static fj.Bottom.error; +import fj.*; +import fj.control.Trampoline; +import fj.control.parallel.*; +import fj.data.optic.Optional; +import fj.data.optic.*; +import fj.data.vector.V2; +import fj.function.Effect1; -import fj.Equal; -import fj.F2Functions; -import fj.Hash; -import fj.Monoid; -import fj.Ord; -import fj.Ordering; -import fj.P; -import fj.P1; -import fj.Semigroup; -import fj.Show; -import fj.Unit; -import fj.P2; -import fj.F0; -import fj.F; -import fj.F2; -import fj.Function; +import java.lang.Class; +import java.util.*; +import static fj.Bottom.error; import static fj.Function.*; -import static fj.P.p; -import static fj.P.p2; +import static fj.Ord.intOrd; +import static fj.Ordering.GT; +import static fj.P.*; import static fj.Unit.unit; import static fj.data.Array.mkArray; +import static fj.data.Either.*; import static fj.data.List.Buffer.*; -import static fj.data.Option.none; -import static fj.data.Option.some; +import static fj.data.Option.*; import static fj.data.optic.Optional.optional; import static fj.data.optic.Prism.prism; import static fj.data.vector.V.v; import static fj.function.Booleans.not; -import static fj.Ordering.GT; -import static fj.Ord.intOrd; - - -import fj.control.Trampoline; -import fj.control.parallel.Promise; -import fj.control.parallel.Strategy; -import fj.data.optic.Optional; -import fj.data.optic.PTraversal; -import fj.data.optic.Prism; -import fj.data.optic.Traversal; -import fj.data.vector.V2; -import fj.function.Effect1; - -import java.util.AbstractCollection; -import java.util.Collection; -import java.util.Iterator; -import java.util.NoSuchElementException; /** * Provides an in-memory, immutable, singly linked list. - * - * @version %build.number% */ public abstract class List implements Iterable { private List() { @@ -110,19 +84,6 @@ public final boolean isNotEmpty() { return this instanceof Cons; } - /** - * Performs a reduction on this list using the given arguments. - * @deprecated As of release 4.5, use {@link #uncons} - * - * @param nil The value to return if this list is empty. - * @param cons The function to apply to the head and tail of this list if it is not empty. - * @return A reduction on this list. - */ - @Deprecated - public final B list(final B nil, final F, B>> cons) { - return uncons(uncurryF2(cons), nil); - } - public final B uncons(final F2, B> cons, final B nil) { return isEmpty() ? nil : cons.f(head(), tail()); } @@ -148,19 +109,7 @@ public final List orTail(final F0> as) { } /** - * Returns an option projection of this list; None if empty, or the first element in - * Some. Equivalent to {@link #headOption()}. - * @deprecated As of release 4.5, use {@link #headOption()} - * @return An option projection of this list. - */ - @Deprecated - public final Option toOption() { - return headOption(); - - } - - /** - * Returns the head of the list, if any. Equivalent to {@link #toOption()} . + * Returns the head of the list, if any. * * @return The optional head of the list. */ @@ -209,19 +158,6 @@ public final Object[] toArrayObject() { return a; } - /** - * To be removed in future release: - * affectation of the result of this method to a non generic array - * will result in runtime error (ClassCastException). - * - * @deprecated As of release 4.6, use {@link #array(Class)}. - */ - @SuppressWarnings("unchecked") - @Deprecated - public final A[] toJavaArray() { - return (A[]) toArrayObject(); - } - /** * Returns a array projection of this list. * @@ -604,61 +540,315 @@ public final List sequence(final List bs) { return bind(c); } - /** - * Traverses through the List with the given function - * - * @param f The function that produces Option value - * @return none if applying f returns none to any element of the list or f mapped list in some . - */ - public final Option> traverseOption(final F> f) { - return foldRight( - (a, obs) -> f.f(a).bind(o -> obs.map(os -> os.cons(o))), - some(List.nil()) - ); - } + /** + * Sequence the given list and collect the output on the right side of an either. + * + * @param list the given list + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either> sequenceEither(final List> list) { + return list.traverseEither(identity()); + } - /** - * Traverse through the List with given function. - * - * @param f The function that produces Either value. - * @return error in left or f mapped list in right. - */ - public final Either> traverseEither(final F> f) { - return foldRight( - (a, acc) -> f.f(a).right().bind(e -> acc.right().map(es -> es.cons(e))), - Either.right(List.nil()) - ); - } + /** + * Sequence the given list and collect the output on the left side of an either. + * + * @param list the given list + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either, R> sequenceEitherLeft(final List> list) { + return list.traverseEitherLeft(identity()); + } - public final Stream> traverseStream(final F> f) { - return foldRight( - (a, acc) -> f.f(a).bind(s -> acc.map(ss -> ss.cons(s))), - Stream.nil() - ); - } + /** + * Sequence the given list and collect the output on the right side of an either. + * + * @param list the given list + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either> sequenceEitherRight(final List> list) { + return list.traverseEitherRight(identity()); + } - public final P1> traverseP1(final F> f){ - return foldRight( - (a, acc) -> f.f(a).bind(b -> acc.map(bs -> bs.cons(b))), - p(List.nil()) - ); - } + /** + * Sequence the given list and collect the output as a function. + * + * @param list the given list + * @param the type of the input value + * @param the type of the output value + * @return the either + */ + public static final F> sequenceF(final List> list) { + return list.traverseF(identity()); + } - public final IO> traverseIO(F> f) { - return this.foldRight( - (a, acc) -> IOFunctions.bind(f.f(a), b -> IOFunctions.map(acc, bs -> bs.cons(b))), - IOFunctions.unit(List.nil()) - ); - } + /** + * Sequence the given list and collect the output as an IO. + * + * @param list the given list + * @param the type of the IO value + * @return the IO + */ + public static final IO> sequenceIO(final List> list) { + return list.traverseIO(identity()); + } + + /** + * Sequence the given list and collect the output as an list. + * + * @param list the given list + * @param the type of the list value + * @return the list + */ + public static final List> sequenceList(final List> list) { + return list.traverseList(identity()); + } + + /** + * Sequence the given list and collect the output as an list. + * + * @param list the given list + * @param the type of the list value + * @return the list + */ + public static final Option> sequenceOption(final List> list) { + return list.traverseOption(identity()); + } + + /** + * Sequence the given list and collect the output as a P1. + * + * @param list the given list + * @param the type of the P1 value + * @return the P1 + */ + public static final P1> sequenceP1(final List> list) { + return list.traverseP1(identity()); + } + + /** + * Sequence the given list and collect the output as a seq. + * + * @param list the given list + * @param the type of the seq value + * @return the seq + */ + public static final Seq> sequenceSeq(final List> list) { + return list.traverseSeq(identity()); + } + + /** + * Sequence the given list and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param list the given list + * @param the type of the set value + * @return the either + */ + public static final Set> sequenceSet(final Ord ord, final List> list) { + return list.traverseSet(ord, identity()); + } + + /** + * Sequence the given list and collect the output as a stream. + * + * @param list the given list + * @param the type of the stream value + * @return the stream + */ + public static final Stream> sequenceStream(final List> list) { + return list.traverseStream(identity()); + } + + /** + * Sequence the given list and collect the output as a trampoline. + * + * @param list the given trampoline + * @param the type of the stream value + * @return the stream + */ + public static final Trampoline> sequenceTrampoline(final List> list) { + return list.traverseTrampoline(identity()); + } + + /** + * Sequence the given list and collect the output as a validation; use the given semigroup to reduce the errors. + * + * @param semigroup the given semigroup + * @param list the given list + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public static final Validation> sequenceValidation(final Semigroup semigroup, final List> list) { + return list.traverseValidation(semigroup, identity()); + } + + /** + * Traverse through the List with given function. + * + * @param f The function that produces Either value. + * @return error in left or f mapped list in right. + */ + public final Either> traverseEither(final F> f) { + return foldRight( + (a, acc) -> f.f(a).right().bind(e -> acc.right().map(es -> es.cons(e))), + Either.right(List.nil()) + ); + } + /** + * Traverse this list with the given function and collect the output on the left side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public final Either, R> traverseEitherLeft(final F> f) { + return foldRight( + (element, either) -> f.f(element).left().bind(elementInner -> either.left().map(list -> list.cons(elementInner))), + left(nil())); + } + + /** + * Traverse this list with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public final Either> traverseEitherRight(final F> f) { + return foldRight( + (element, either) -> f.f(element).right().bind(elementInner -> either.right().map(list -> list.cons(elementInner))), + right(nil())); + } + + /** + * Traverse this list with the given function and collect the output as a function. + * + * @param f the given function + * @param the type of the input value + * @param the type of the output value + * @return the function + */ public final F> traverseF(F> f) { return this.foldRight( (a, acc) -> Function.bind(acc, (bs) -> Function.compose(bs::cons, f.f(a))), constant(List.nil()) - ); + ); + } + + /** + * Traverse this list with the given function and collect the output as an IO. + * + * @param f the given function + * @param the type of the IO value + * @return the IO + */ + public final IO> traverseIO(F> f) { + return this.foldRight( + (a, acc) -> IOFunctions.bind(f.f(a), b -> IOFunctions.map(acc, bs -> bs.cons(b))), + IOFunctions.unit(List.nil()) + ); + } + + /** + * Traverse this list with the given function and collect the output as a list. + * + * @param f the given function + * @param the type of the list value + * @return the list + */ + public final List> traverseList(final F> f) { + return foldRight( + (a, acc) -> f.f(a).bind(b -> acc.map(bs -> bs.cons(b))), + single(List.nil())); + } + + /** + * Traverses through the List with the given function + * + * @param f The function that produces Option value + * @return none if applying f returns none to any element of the list or f mapped list in some . + */ + public final Option> traverseOption(final F> f) { + return foldRight( + (a, obs) -> f.f(a).bind(o -> obs.map(os -> os.cons(o))), + some(List.nil()) + ); + } + + /** + * Traverse this list with the given function and collect the output as a p1. + * + * @param f the given function + * @param the type of the p1 value + * @return the p1 + */ + public final P1> traverseP1(final F> f) { + return foldRight( + (a, acc) -> f.f(a).bind(b -> acc.map(bs -> bs.cons(b))), + p(List.nil()) + ); + } + + /** + * Traverse this list with the given function and collect the output as a seq. + * + * @param f the given function + * @param the type of the seq value + * @return the seq + */ + public final Seq> traverseSeq(final F> f) { + return foldRight( + (element, seq) -> f.f(element).bind(elementInner -> seq.map(list -> list.cons(elementInner))), + Seq.single(nil())); + } + + /** + * Traverse this list with the given function and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param f the given function + * @param the type of the set value + * @return the set + */ + public final Set> traverseSet(final Ord ord, final F> f) { + final Ord> listOption = Ord.listOrd(ord); + return foldRight( + (element, set) -> f.f(element).bind(listOption, elementInner -> set.map(listOption, list -> list.cons(elementInner))), + Set.single(listOption, nil())); + } + + /** + * Traverse this list with the given function and collect the output as a stream. + * + * @param f the given function + * @param the type of the stream value + * @return the stream + */ + public final Stream> traverseStream(final F> f) { + return foldRight( + (a, acc) -> f.f(a).bind(s -> acc.map(ss -> ss.cons(s))), + Stream.single(nil())); } + /** + * Traverse this list with the given function and collect the output as a trampoline. + * + * @param f the given function + * @param the type of the trampoline value + * @return the trampoline + */ public final Trampoline> traverseTrampoline(final F> f) { return foldRight( (a, acc) -> f.f(a).bind(b -> acc.map(bs -> bs.cons(b))), @@ -671,12 +861,15 @@ public final Promise> traversePromise(final F> f) { Promise.promise(Strategy.idStrategy(), p(List.nil()))); } - public final List> traverseList(final F> f) { - return foldRight( - (a, acc) -> f.f(a).bind(b -> acc.map(bs -> bs.cons(b))), - single(List.nil())); - } - + /** + * Traverse this list with the given function and collect the output as a validation; use the given semigroup to reduce the errors. + * + * @param s the given semigroup + * @param f the given function + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ public final Validation> traverseValidation(Semigroup s, final F> f) { return Validation.sequence(s, map(f)); } @@ -736,7 +929,7 @@ public final B foldRight(final F2 f, final B b) { * @return A Trampoline containing the final result after the right-fold reduction. */ public final Trampoline foldRightC(final F2 f, final B b) { - return Trampoline.suspend(() -> isEmpty() ? Trampoline.pure(b) : tail().foldRightC(f, b).map(F2Functions.f(f, head()))); + return Trampoline.suspend(() -> isEmpty() ? Trampoline.pure(b) : tail().foldRightC(f, b).map(f.f(head()))); } /** @@ -1296,21 +1489,6 @@ public final A mode(final Ord o) { return sort(o).group(o.equal()).maximum(intOrd.contramap(List.length_())).head(); } - /** - * Groups the elements of this list by a given keyFunction into a {@link TreeMap}. - * The ordering of the keys is determined by {@link fj.Ord#hashOrd()} (ie. Object#hasCode). - * This is not safe and therefore this method is deprecated. - * - * @param keyFunction The function to select the keys for the map. - * @return A TreeMap containing the keys with the accumulated list of matched elements. - * - * @deprecated As of release 4.7, use {@link #groupBy(F, Ord)} - */ - @Deprecated - public final TreeMap> groupBy(final F keyFunction) { - return groupBy(keyFunction, Ord.hashOrd()); - } - /** * Groups the elements of this list by a given keyFunction into a {@link TreeMap}. * @@ -1322,25 +1500,6 @@ public final TreeMap> groupBy(final F keyFunction, final Or return groupBy(keyFunction, identity(), keyOrd); } - /** - * Groups the elements of this list by a given keyFunction into a {@link TreeMap} and transforms - * the matching elements with the given valueFunction. The ordering of the keys is determined by - * {@link fj.Ord#hashOrd()} (ie. Object#hasCode). - * This is not safe and therefore this method is deprecated. - * - * @param keyFunction The function to select the keys for the map. - * @param valueFunction The function to apply on each matching value. - * @return A TreeMap containing the keys with the accumulated list of matched and mapped elements. - * - * @deprecated As of release 4.7, use {@link #groupBy(F, F, Ord)} - */ - @Deprecated - public final TreeMap> groupBy( - final F keyFunction, - final F valueFunction) { - return this.groupBy(keyFunction, valueFunction, Ord.hashOrd()); - } - /** * Groups the elements of this list by a given keyFunction into a {@link TreeMap} and transforms * the matching elements with the given valueFunction. The ordering of the keys is determined by @@ -1474,7 +1633,7 @@ public final A maximum(final Ord o) { public final Option maximumOption(final Ord o) { return NonEmptyList.fromList(this).map(nel -> nel.maximum(o)); } - + /** * Returns the minimum element in this list according to the given ordering. * @@ -1484,7 +1643,7 @@ public final Option maximumOption(final Ord o) { public final A minimum(final Ord o) { return foldLeft1(o::min); } - + /** * Returns the minimum element in this list according to the given ordering. * @@ -1588,24 +1747,6 @@ public static List arrayList(final A... as) { return Array.array(as).toList(); } - /** - * Constructs a list from the given Iterable. - * @deprecated As of release 4.5, use {@link #iterableList(Iterable)} - */ - @Deprecated - public static List list(final Iterable i) { - return iterableList(i); - } - - /** - * Constructs a list from the given Iterator. - * @deprecated As of release 4.5, use {@link #iteratorList(Iterator)} - */ - @Deprecated - public static List list(final Iterator it) { - return iteratorList(it); - } - /** * Constructs a list from the given Iterator. */ diff --git a/core/src/main/java/fj/data/NonEmptyList.java b/core/src/main/java/fj/data/NonEmptyList.java index 4073476d..96bf444a 100644 --- a/core/src/main/java/fj/data/NonEmptyList.java +++ b/core/src/main/java/fj/data/NonEmptyList.java @@ -14,8 +14,6 @@ /** * Provides an in-memory, immutable, singly linked list with total head and tail. - * - * @version %build.number% */ public final class NonEmptyList implements Iterable { /** @@ -169,9 +167,10 @@ public NonEmptyList bind(final F> f) { * @return a NonEmptyList of the sublists of this list. */ public NonEmptyList> sublists() { + F, Option>> f = s -> NonEmptyList.fromList(Conversions.Stream_List().f(s)); return fromList( somes(toList().toStream().substreams() - .map(F1Functions.o(NonEmptyList::fromList, Conversions.Stream_List())).toList())).some(); + .map(f).toList())).some(); } /** diff --git a/core/src/main/java/fj/data/Option.java b/core/src/main/java/fj/data/Option.java index 54b4bb01..9831c48c 100644 --- a/core/src/main/java/fj/data/Option.java +++ b/core/src/main/java/fj/data/Option.java @@ -1,49 +1,28 @@ package fj.data; -import static fj.Bottom.error; -import fj.F; -import fj.F0; -import fj.F2; -import fj.P; -import fj.P1; -import fj.P2; -import fj.P3; -import fj.P4; -import fj.P5; -import fj.P6; -import fj.P7; -import fj.P8; -import fj.Unit; -import fj.Show; +import fj.*; +import fj.control.Trampoline; +import fj.data.optic.*; import fj.function.Effect1; -import fj.Equal; -import fj.Ord; -import fj.Hash; -import fj.data.optic.Prism; -import fj.data.optic.PPrism; + +import java.lang.Class; +import java.util.*; + +import static fj.Bottom.error; import static fj.Function.*; import static fj.P.p; +import static fj.Show.optionShow; import static fj.Unit.unit; -import static fj.data.List.cons; -import static fj.data.List.cons_; -import static fj.data.Validation.parseByte; -import static fj.data.Validation.parseDouble; -import static fj.data.Validation.parseFloat; -import static fj.data.Validation.parseInt; -import static fj.data.Validation.parseLong; -import static fj.data.Validation.parseShort; -import static fj.data.optic.Prism.prism; +import static fj.control.Trampoline.pure; +import static fj.data.Either.*; +import static fj.data.List.*; +import static fj.data.Validation.*; import static fj.data.optic.PPrism.pPrism; -import static fj.Show.optionShow; - -import java.util.Collection; -import java.util.Iterator; +import static fj.data.optic.Prism.prism; /** * An optional value that may be none (no value) or some (a value). This type is a replacement for * the use of null with better type checks. - * - * @version %build.number% */ public abstract class Option implements Iterable { private Option() { @@ -372,11 +351,11 @@ public final Option> bindProduct(final Option ob) { public final Option> bindProduct(final Option ob, final Option oc) { return bind(ob, oc, P.p3()); } - + public final Option> bindProduct(final Option ob, final Option oc, final Option od) { return bind(ob, oc, od, P.p4()); } - + public final Option> bindProduct(final Option ob, final Option oc, final Option od, final Option oe) { return bind(ob, oc, od, oe, P.p5()); @@ -411,45 +390,331 @@ public final Option sequence(final Option o) { return bind(c); } - public final Either> traverseEither(F> f) { - return map(a -> f.f(a).right().map(Option::some)).orSome(Either.right(none())); + /** + * Sequence the given option and collect the output on the right side of an either. + * + * @param option the given option + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either> sequenceEither(final Option> option) { + return option.traverseEitherRight(identity()); + } + + /** + * Sequence the given option and collect the output on the left side of an either. + * + * @param option the given option + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either, R> sequenceEitherLeft(final Option> option) { + return option.traverseEitherLeft(identity()); + } + + /** + * Sequence the given option and collect the output on the right side of an either. + * + * @param option the given option + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either> sequenceEitherRight(final Option> option) { + return option.traverseEitherRight(identity()); + } + + /** + * Sequence the given option and collect the output as a function. + * + * @param option the given option + * @param the type of the input value + * @param the type of the output value + * @return the either + */ + public static final F> sequenceF(final Option> option) { + return option.traverseF(identity()); + } + + /** + * Sequence the given option and collect the output as an IO. + * + * @param option the given option + * @param the type of the IO value + * @return the IO + */ + public static final IO> sequenceIO(final Option> option) { + return option.traverseIO(identity()); + } + + /** + * Sequence the given option and collect the output as an list. + * + * @param option the given option + * @param the type of the list value + * @return the list + */ + public static final List> sequenceList(final Option> option) { + return option.traverseList(identity()); } - public final IO> traverseIO(F> f) { - return map(a -> IOFunctions.map(f.f(a), Option::some)).orSome(IOFunctions.lazy(Option::none)); + /** + * Sequence the given option and collect the output as an option. + * + * @param option the given option + * @param the type of the option value + * @return the option + */ + public static final Option> sequenceOption(final Option> option) { + return option.traverseOption(identity()); } - public final List> traverseList(F> f) { - return map(a -> f.f(a).map(Option::some)).orSome(List.list()); + /** + * Sequence the given option and collect the output as a P1. + * + * @param option the given option + * @param the type of the P1 value + * @return the P1 + */ + public static final P1> sequenceP1(final Option> option) { + return option.traverseP1(identity()); } - public final Option> traverseOption(F> f) { - return map(f); + /** + * Sequence the given option and collect the output as a seq. + * + * @param option the given option + * @param the type of the seq value + * @return the seq + */ + public static final Seq> sequenceSeq(final Option> option) { + return option.traverseSeq(identity()); } - public final Stream> traverseStream(F> f) { - return map(a -> f.f(a).map(Option::some)).orSome(Stream.nil()); + /** + * Sequence the given option and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param option the given option + * @param the type of the set value + * @return the either + */ + public static final Set> sequenceSet(final Ord ord, final Option> option) { + return option.traverseSet(ord, identity()); } - public final P1> traverseP1(F> f) { - return map(a -> f.f(a).map(Option::some)).orSome(p(none())); + /** + * Sequence the given option and collect the output as a stream. + * + * @param option the given option + * @param the type of the stream value + * @return the stream + */ + public static final Stream> sequenceStream(final Option> option) { + return option.traverseStream(identity()); } - public final Seq> traverseSeq(F> f) { - return map(a -> f.f(a).map(Option::some)).orSome(Seq.empty()); + /** + * Sequence the given option and collect the output as a trampoline. + * + * @param option the given trampoline + * @param the type of the stream value + * @return the stream + */ + public static final Trampoline> sequenceTrampoline(final Option> option) { + return option.traverseTrampoline(identity()); } - public final Set> traverseSet(Ord ord, F> f) { - Ord> optOrd = Ord.optionOrd(ord); - return map(a -> f.f(a).map(optOrd, Option::some)).orSome(Set.empty(optOrd)); + /** + * Sequence the given option and collect the output as a validation. + * + * @param option the given option + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public static final Validation> sequenceValidation(final Option> option) { + return option.traverseValidation(identity()); } - public final F2, F>, Set>> traverseSet() { - return this::traverseSet; + /** + * Traverse this option with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public final Either> traverseEither(final F> f) { + return traverseEitherRight(f); + } + + /** + * Traverse this option with the given function and collect the output on the left side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public final Either, R> traverseEitherLeft(final F> f) { + return option( + left(none()), + a -> f.f(a).left().map(Option::some)); + } + + /** + * Traverse this option with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public final Either> traverseEitherRight(final F> f) { + return option( + right(none()), + a -> f.f(a).right().map(Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as a function. + * + * @param f the given function + * @param the type of the input value + * @param the type of the output value + * @return the function + */ + public final F> traverseF(final F> f) { + return option( + constant(none()), + a -> andThen(f.f(a), Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as an IO. + * + * @param f the given function + * @param the type of the IO value + * @return the IO + */ + public final IO> traverseIO(final F> f) { + return option( + IOFunctions.lazy(Option::none), + a -> IOFunctions.map(f.f(a), Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as a list. + * + * @param f the given function + * @param the type of the list value + * @return the list + */ + public final List> traverseList(final F> f) { + return option( + List.single(none()), + a -> f.f(a).map(Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as an option. + * + * @param f the given function + * @param the type of the option value + * @return the option + */ + public final Option> traverseOption(final F> f) { + return option( + some(none()), + a -> f.f(a).map(Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as a P1. + * + * @param f the given function + * @param the type of the P1 value + * @return the P1 + */ + public final P1> traverseP1(final F> f) { + return option( + p(none()), + (F>>) a -> f.f(a).map(Option::some)); } - public final Validation> traverseValidation(F> f) { - return map(a -> f.f(a).map(Option::some)).orSome(Validation.success(none())); + /** + * Traverse this option with the given function and collect the output a seq. + * + * @param f the given function + * @param the type of the seq value + * @return the seq + */ + public final Seq> traverseSeq(final F> f) { + return option( + Seq.single(none()), + a -> f.f(a).map(Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param f the given function + * @param the type of the set value + * @return the set + */ + public final Set> traverseSet(final Ord ord, final F> f) { + final Ord> ordOption = Ord.optionOrd(ord); + return option( + Set.single(ordOption, none()), + a -> f.f(a).map(ordOption, Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as a stream. + * + * @param f the given function + * @param the type of the stream value + * @return the stream + */ + public final Stream> traverseStream(final F> f) { + return option( + Stream.single(none()), + a -> f.f(a).map(Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as a trampoline. + * + * @param f the given function + * @param the type of the trampoline value + * @return the trampoline + */ + public final Trampoline> traverseTrampoline(final F> f) { + return option( + pure(none()), + a -> f.f(a).map(Option::some)); + } + + /** + * Traverse this option with the given function and collect the output as a validation. + * + * @param f the given function + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public final Validation> traverseValidation(final F> f) { + return option( + success(none()), + a -> f.f(a).map(Option::some)); + } + + public final F2, F>, Set>> traverseSet() { + return this::traverseSet; } /** @@ -712,7 +977,7 @@ public static Option join(final Option> o) { } /** - * Sequence through the option monad. + * Sequence a list through the option monad. * * @param a The list of option to sequence. * @return The option of list after sequencing. @@ -723,6 +988,16 @@ public static Option> sequence(final List> a) { a.head().bind(aa -> sequence(a.tail()).map(cons_(aa))); } + /** + * Sequence a validation through the option monad. + * + * @param a The validation of option to sequence. + * @return The option of validation after sequencing. + */ + public static Option> sequence(final Validation> a) { + return a.traverseOption(identity()); + } + /** * Returns an optional value that has a value of the given argument, if the given predicate holds * on that argument, otherwise, returns no value. diff --git a/core/src/main/java/fj/data/PriorityQueue.java b/core/src/main/java/fj/data/PriorityQueue.java index 21f6ed6f..f685c36d 100644 --- a/core/src/main/java/fj/data/PriorityQueue.java +++ b/core/src/main/java/fj/data/PriorityQueue.java @@ -20,8 +20,6 @@ * annotated with type K, are combined using a monoid of K and both the * key and value are stored in the leaf. Priorities of the same value * are returned FIFO (first in, first out). - * - * Created by MarkPerry on 31 May 16. */ public final class PriorityQueue { diff --git a/core/src/main/java/fj/data/Reader.java b/core/src/main/java/fj/data/Reader.java index af66c87e..49010e31 100644 --- a/core/src/main/java/fj/data/Reader.java +++ b/core/src/main/java/fj/data/Reader.java @@ -1,11 +1,9 @@ package fj.data; import fj.F; -import fj.F1Functions; /** * The Reader monad (also called the function monad, so equivalent to the idea of F). - * Created by MarkPerry on 7/07/2014. */ public class Reader { @@ -32,7 +30,7 @@ public final B f(A a) { } public final Reader map(F f) { - return unit(F1Functions.andThen(function, f)); + return unit(function.andThen(f)); } public final Reader andThen(F f) { diff --git a/core/src/main/java/fj/data/SafeIO.java b/core/src/main/java/fj/data/SafeIO.java index ef4c608c..bcc16bf6 100644 --- a/core/src/main/java/fj/data/SafeIO.java +++ b/core/src/main/java/fj/data/SafeIO.java @@ -1,12 +1,15 @@ package fj.data; -/** - * Created by MarkPerry on 3/07/2014. - */ +@FunctionalInterface public interface SafeIO extends IO { @Override A run(); + @Override + default A f() { + return run(); + } + } diff --git a/core/src/main/java/fj/data/Seq.java b/core/src/main/java/fj/data/Seq.java index abd20162..59a46df7 100644 --- a/core/src/main/java/fj/data/Seq.java +++ b/core/src/main/java/fj/data/Seq.java @@ -1,20 +1,21 @@ package fj.data; import fj.*; +import fj.control.Trampoline; +import fj.data.List.Buffer; +import fj.data.fingertrees.*; + +import java.util.*; import static fj.Bottom.error; +import static fj.Function.*; import static fj.Monoid.intAdditionMonoid; +import static fj.P.p; +import static fj.data.Either.*; +import static fj.data.Option.some; +import static fj.data.Validation.success; import static fj.data.fingertrees.FingerTree.measured; -import fj.data.List.Buffer; -import fj.data.fingertrees.FingerTree; -import fj.data.fingertrees.MakeTree; -import fj.data.fingertrees.Measured; - -import java.util.AbstractList; -import java.util.Iterator; -import java.util.NoSuchElementException; - /** * Provides an immutable finite sequence, implemented as a finger tree. This structure gives O(1) access to * the head and tail, as well as O(log n) random access and concatenation of sequences. @@ -77,25 +78,9 @@ public static Seq single(final A a) { /** * Constructs a sequence from the given list. * - * @deprecated As of release 4.5, use {@link #listSeq(List)} - * - * @param list The list to create the sequence from. - * @return A sequence with the given elements in the list. - */ - @Deprecated - public static Seq seq(final List list) { - return iterableSeq(list); - } - - /** - * Constructs a sequence from the given list. - * - * @deprecated As of release 4.5, use {@link #iterableSeq} - * * @param list The list to create the sequence from. * @return A sequence with the elements of the list. */ - @Deprecated public static Seq listSeq(final List list) { return iterableSeq(list); } @@ -396,4 +381,371 @@ public Seq map(F f) { return new Seq<>(ftree.map(f, Seq.elemMeasured())); } + /** + * Bind the given function across this seq. + * + * @param f the given function + * @param the type of the seq value + * @return the seq + */ + public Seq bind(final F> f) { + return foldRight( + (element, accumulator) -> f.f(element).append(accumulator), + empty()); + } + + /** + * Sequence the given seq and collect the output on the right side of an either. + * + * @param seq the given seq + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static Either> sequenceEither(final Seq> seq) { + return seq.traverseEither(identity()); + } + + /** + * Sequence the given seq and collect the output on the left side of an either. + * + * @param seq the given seq + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static Either, R> sequenceEitherLeft(final Seq> seq) { + return seq.traverseEitherLeft(identity()); + } + + /** + * Sequence the given seq and collect the output on the right side of an either. + * + * @param seq the given seq + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static Either> sequenceEitherRight(final Seq> seq) { + return seq.traverseEitherRight(identity()); + } + + /** + * Sequence the given seq and collect the output as a function. + * + * @param seq the given seq + * @param the type of the input value + * @param the type of the output value + * @return the either + */ + public static F> sequenceF(final Seq> seq) { + return seq.traverseF(identity()); + } + + /** + * Sequence the given seq and collect the output as an IO. + * + * @param seq the given seq + * @param the type of the IO value + * @return the IO + */ + public static IO> sequenceIO(final Seq> seq) { + return seq.traverseIO(identity()); + } + + /** + * Sequence the given seq and collect the output as a list. + * + * @param seq the given seq + * @param the type of the seq value + * @return the list + */ + public static List> sequenceList(final Seq> seq) { + return seq.traverseList(identity()); + } + + /** + * Sequence the given seq and collect the output as an seq. + * + * @param seq the given seq + * @param the type of the seq value + * @return the seq + */ + public static Option> sequenceOption(final Seq> seq) { + return seq.traverseOption(identity()); + } + + /** + * Sequence the given seq and collect the output as a P1. + * + * @param seq the given seq + * @param the type of the P1 value + * @return the P1 + */ + public static P1> sequenceP1(final Seq> seq) { + return seq.traverseP1(identity()); + } + + /** + * Sequence the given seq and collect the output as a seq. + * + * @param seq the given seq + * @param the type of the seq value + * @return the seq + */ + public static Seq> sequenceSeq(final Seq> seq) { + return seq.traverseSeq(identity()); + } + + /** + * Sequence the given seq and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param seq the given seq + * @param the type of the set value + * @return the either + */ + public static Set> sequenceSet(final Ord ord, final Seq> seq) { + return seq.traverseSet(ord, identity()); + } + + /** + * Sequence the given seq and collect the output as a stream. + * + * @param seq the given seq + * @param the type of the stream value + * @return the stream + */ + public static Stream> sequenceStream(final Seq> seq) { + return seq.traverseStream(identity()); + } + + /** + * Sequence the given seq and collect the output as a trampoline. + * + * @param seq the given trampoline + * @param the type of the stream value + * @return the stream + */ + public static Trampoline> sequenceTrampoline(final Seq> seq) { + return seq.traverseTrampoline(identity()); + } + + /** + * Sequence the given seq and collect the output as a validation. + * + * @param seq the given seq + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public static Validation> sequenceValidation(final Seq> seq) { + return seq.traverseValidation(identity()); + } + + /** + * Sequence the given seq and collect the output as a validation; use the given semigroup to reduce the errors. + * + * @param semigroup the given semigroup + * @param seq the given seq + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public static Validation> sequenceValidation(final Semigroup semigroup, final Seq> seq) { + return seq.traverseValidation(semigroup, identity()); + } + + /** + * Traverse this seq with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public Either> traverseEither(final F> f) { + return traverseEitherRight(f); + } + + /** + * Traverse this seq with the given function and collect the output on the left side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public Either, R> traverseEitherLeft(final F> f) { + return foldRight( + (element, either) -> f.f(element).left().bind(elementInner -> either.left().map(seq -> seq.cons(elementInner))), + left(empty())); + } + + /** + * Traverse this seq with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public Either> traverseEitherRight(final F> f) { + return foldRight( + (element, either) -> f.f(element).right().bind(elementInner -> either.right().map(seq -> seq.cons(elementInner))), + right(empty())); + } + + /** + * Traverse this seq with the given function and collect the output as a function. + * + * @param f the given function + * @param the type of the input value + * @param the type of the output value + * @return the function + */ + public F> traverseF(final F> f) { + return foldRight( + (element, fInner) -> Function.bind(f.f(element), elementInner -> andThen(fInner, seq -> seq.cons(elementInner))), + constant(empty())); + } + + /** + * Traverse this seq with the given function and collect the output as an IO. + * + * @param f the given function + * @param the type of the IO value + * @return the IO + */ + public IO> traverseIO(final F> f) { + return foldRight( + (element, io) -> IOFunctions.bind(f.f(element), elementInner -> IOFunctions.map(io, seq -> seq.cons(elementInner))), + IOFunctions.unit(empty()) + ); + } + + /** + * Traverse this seq with the given function and collect the output as a list. + * + * @param f the given function + * @param the type of the list value + * @return the list + */ + public List> traverseList(final F> f) { + return foldRight( + (element, list) -> f.f(element).bind(elementInner -> list.map(seq -> seq.cons(elementInner))), + List.single(empty())); + } + + /** + * Traverses through the Seq with the given function + * + * @param f The function that produces Option value + * @return none if applying f returns none to any element of the seq or f mapped seq in some . + */ + public Option> traverseOption(final F> f) { + return foldRight( + (element, option) -> f.f(element).bind(elementInner -> option.map(seq -> seq.cons(elementInner))), + some(empty()) + ); + } + + /** + * Traverse this seq with the given function and collect the output as a p1. + * + * @param f the given function + * @param the type of the p1 value + * @return the p1 + */ + public P1> traverseP1(final F> f) { + return foldRight( + (element, p1) -> f.f(element).bind(elementInner -> p1.map(seq -> seq.cons(elementInner))), + p(empty()) + ); + } + + /** + * Traverse this seq with the given function and collect the output as a seq. + * + * @param f the given function + * @param the type of the seq value + * @return the seq + */ + public Seq> traverseSeq(final F> f) { + return foldRight( + (element, seq) -> f.f(element).bind(elementInner -> seq.map(seqInner -> seqInner.cons(elementInner))), + single(empty())); + } + + /** + * Traverse this seq with the given function and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param f the given function + * @param the type of the set value + * @return the set + */ + public Set> traverseSet(final Ord ord, final F> f) { + final Ord> seqOrd = Ord.seqOrd(ord); + return foldRight( + (element, set) -> f.f(element).bind(seqOrd, elementInner -> set.map(seqOrd, seq -> seq.cons(elementInner))), + Set.single(seqOrd, empty())); + } + + /** + * Traverse this seq with the given function and collect the output as a stream. + * + * @param f the given function + * @param the type of the stream value + * @return the stream + */ + public Stream> traverseStream(final F> f) { + return foldRight( + (element, stream) -> f.f(element).bind(elementInner -> stream.map(seq -> seq.cons(elementInner))), + Stream.single(empty())); + } + + /** + * Traverse this seq with the given function and collect the output as a trampoline. + * + * @param f the given function + * @param the type of the trampoline value + * @return the trampoline + */ + public Trampoline> traverseTrampoline(final F> f) { + return foldRight( + (element, trampoline) -> f.f(element).bind(elementInner -> trampoline.map(seq -> seq.cons(elementInner))), + Trampoline.pure(empty())); + } + + /** + * Traverse this seq with the given function and collect the output as a validation. + * + * @param f the given function + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public Validation> traverseValidation(final F> f) { + return foldRight( + (element, validation) -> f.f(element).bind(elementInner -> validation.map(seq -> seq.cons(elementInner))), + success(empty()) + ); + } + + /** + * Traverse this seq with the given function and collect the output as a validation; use the given semigroup to reduce the errors. + * + * @param semigroup the given semigroup + * @param f the given function + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public Validation> traverseValidation(final Semigroup semigroup, final F> f) { + return foldRight( + (element, validation) -> f.f(element).map(Seq::single).accumulate(semigroup, validation, Seq::append), + success(empty()) + ); + } } diff --git a/core/src/main/java/fj/data/Set.java b/core/src/main/java/fj/data/Set.java index 15207d83..4c0aeaa0 100644 --- a/core/src/main/java/fj/data/Set.java +++ b/core/src/main/java/fj/data/Set.java @@ -713,28 +713,4 @@ public static Set arraySet(final Ord o, final A...as) { return arraySet(o, as); } - /** - * Constructs a set from the list. - * - * @deprecated As of release 4.5, use {@link #iterableSet} - * - * @param o An order for the elements of the new set. - * @param list The elements to add to a set. - * @return A new set containing the elements of the given list. - */ - @Deprecated - public static Set set(final Ord o, List list) { - return iterableSet(o, list); - } - - /** - * Constructs a set from the list. - * - * @deprecated As of release 4.5, use {@link #iterableSet} - */ - @Deprecated - public static Set fromList(final Ord o, List list) { - return iterableSet(o, list); - } - } diff --git a/core/src/main/java/fj/data/State.java b/core/src/main/java/fj/data/State.java index f29d55d7..c5b638a3 100644 --- a/core/src/main/java/fj/data/State.java +++ b/core/src/main/java/fj/data/State.java @@ -9,9 +9,6 @@ import static fj.control.Trampoline.suspend; import static fj.data.List.cons; -/** - * Created by MarkPerry on 7/07/2014. - */ public final class State { public static State unit(F> runF) { @@ -111,6 +108,24 @@ public State withs(F f) { return suspended(s -> runF.f(f.f(s))); } + /** + * Bind the given function across this state. + * + * @param f the given function + * @param the type of the output value + * @return the state + */ + public State bind(F> f) { + return flatMap(f); + } + + /** + * Bind the given function across this state. + * + * @param f the given function + * @param the type of the output value + * @return the state + */ public State flatMap(F> f) { return suspended(s -> runF.f(s).bind(result -> Trampoline.pure(f.f(result._2()).run(result._1())))); } diff --git a/core/src/main/java/fj/data/Stream.java b/core/src/main/java/fj/data/Stream.java index a523515b..8afa36ee 100644 --- a/core/src/main/java/fj/data/Stream.java +++ b/core/src/main/java/fj/data/Stream.java @@ -4,6 +4,7 @@ import fj.F0; import fj.F3; import fj.Hash; +import fj.Semigroup; import fj.Show; import fj.F; import fj.F2; @@ -14,6 +15,7 @@ import fj.P1; import fj.P2; import fj.Unit; +import fj.control.Trampoline; import fj.control.parallel.Promise; import fj.control.parallel.Strategy; import fj.Ordering; @@ -38,8 +40,6 @@ /** * A lazy (not yet evaluated), immutable, singly linked list. - * - * @version %build.number% */ public abstract class Stream implements Iterable { private Stream() { @@ -87,20 +87,6 @@ public final boolean isNotEmpty() { return this instanceof Cons; } - /** - * Performs a reduction on this stream using the given arguments. Equivalent to {@link #uncons}. - * - * @deprecated As of release 4.5, use {@link #uncons} - * - * @param nil The value to return if this stream is empty. - * @param cons The function to apply to the head and tail of this stream if it is not empty. - * @return A reduction on this stream. - */ - @Deprecated - public final B stream(final B nil, final F>, B>> cons) { - return uncons(nil, cons); - } - /** * Performs a reduction on this stream using the given arguments. * @@ -710,26 +696,6 @@ public static Stream range(final int from, final long to) { return arrayStream(as); } - /** - * Constructs a stream with the given elements in the Iterable. Equivalent to {@link #iterableStream(Iterable)} . - * - * @deprecated As of release 4.5, use {@link #iterableStream(Iterable)} - */ - @Deprecated - public static Stream stream(Iterable it) { - return iterableStream(it); - } - - /** - * Constructs a stream with the given elements in the Iterator. Equivalent to {@link #iteratorStream(Iterator)} . - * - * @deprecated As of release 4.5, use {@link #iteratorStream(Iterator)} - */ - @Deprecated - public static Stream stream(final Iterator it) { - return iteratorStream(it); - } - /** * Constructs a stream with the given elements in the Iterator. */ @@ -914,25 +880,6 @@ public final Option toOption() { return isEmpty() ? Option.none() : some(head()); } - /** - * To be removed in future release: - * affectation of the result of this method to a non generic array - * will result in runtime error (ClassCastException). - * - * @deprecated As of release 4.6, use {@link #array(Class)}. - */ - @Deprecated - public final A[] toJavaArray() { - @SuppressWarnings("unchecked") - final A[] array = (A[]) new Object[length()]; - int i = 0; - for (A a: this) { - array[i] = a; - i++; - } - return array; - } - /** * Returns a list projection of this stream. * @@ -1094,27 +1041,6 @@ public final Stream takeWhile(final F f) { Stream.nil(); } - /** - * Traversable instance of Stream for IO. - * - * @return traversed value - */ - public final IO> traverseIO(F> f) { - return this.foldRight1((a, acc) -> - IOFunctions.bind(acc, (Stream bs) -> - IOFunctions.map(f.f(a), bs::cons)), IOFunctions.unit(Stream.nil())); - - } - - /** - * Traversable instance of Stream for Option. - * - * @return traversed value - */ - public final Option> traverseOption(F> f) { - return this.foldRight1((a, acc) -> acc.bind(bs -> f.f(a).map(bs::cons)), some(Stream.nil())); - } - /** * Removes elements from the head of this stream that do not match the given predicate function * until an element is found that does match or the stream is exhausted. @@ -1669,4 +1595,413 @@ public static F>, F, Stream>> bind_() { public static F, B>>, F, B>>> foldRight() { return curry((f, b, as) -> as.foldRight(f, b)); } + + /** + * Sequence the given stream and collect the output on the right side of an either. + * + * @param stream the given stream + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static Either> sequenceEither( + final Stream> stream) { + return stream.traverseEither(identity()); + } + + /** + * Sequence the given stream and collect the output on the left side of an either. + * + * @param stream the given stream + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static Either, R> sequenceEitherLeft( + final Stream> stream) { + return stream.traverseEitherLeft(identity()); + } + + /** + * Sequence the given stream and collect the output on the right side of an either. + * + * @param stream the given stream + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static Either> sequenceEitherRight( + final Stream> stream) { + return stream.traverseEitherRight(identity()); + } + + /** + * Sequence the given stream and collect the output as a function. + * + * @param stream the given stream + * @param the type of the input value + * @param the type of the output value + * @return the either + */ + public static F> sequenceF( + final Stream> stream) { + return stream.traverseF(identity()); + } + + /** + * Sequence the given stream and collect the output as an IO. + * + * @param stream the given stream + * @param the type of the IO value + * @return the IO + */ + public static IO> sequenceIO( + final Stream> stream) { + return stream.traverseIO(identity()); + } + + /** + * Sequence the given stream and collect the output as a list. + * + * @param stream the given stream + * @param the type of the list value + * @return the list + */ + public static List> sequenceList( + final Stream> stream) { + return stream.traverseList(identity()); + } + + /** + * Sequence the given stream and collect the output as an stream. + * + * @param stream the given stream + * @param the type of the option value + * @return the stream + */ + public static Option> sequenceOption( + final Stream> stream) { + return stream.traverseOption(identity()); + } + + /** + * Sequence the given stream and collect the output as a P1. + * + * @param stream the given stream + * @param the type of the P1 value + * @return the P1 + */ + public static P1> sequenceP1( + final Stream> stream) { + return stream.traverseP1(identity()); + } + + /** + * Sequence the given stream and collect the output as a seq. + * + * @param stream the given stream + * @param the type of the stream value + * @return the seq + */ + public static Seq> sequenceSeq( + final Stream> stream) { + return stream.traverseSeq(identity()); + } + + /** + * Sequence the given stream and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param stream the given stream + * @param the type of the set value + * @return the either + */ + public static Set> sequenceSet( + final Ord ord, + final Stream> stream) { + return stream.traverseSet(ord, identity()); + } + + /** + * Sequence the given stream and collect the output as a stream. + * + * @param stream the given stream + * @param the type of the stream value + * @return the stream + */ + public static Stream> sequenceStream( + final Stream> stream) { + return stream.traverseStream(identity()); + } + + /** + * Sequence the given stream and collect the output as a trampoline. + * + * @param stream the given trampoline + * @param the type of the stream value + * @return the stream + */ + public static Trampoline> sequenceTrampoline( + final Stream> stream) { + return stream.traverseTrampoline(identity()); + } + + /** + * Sequence the given stream and collect the output as a validation. + * + * @param stream the given stream + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public static Validation> sequenceValidation( + final Stream> stream) { + return stream.traverseValidation(identity()); + } + + /** + * Sequence the given stream and collect the output as a validation; use the given semigroup to reduce the errors. + * + * @param semigroup the given semigroup + * @param stream the given stream + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public static Validation> sequenceValidation( + final Semigroup semigroup, + final Stream> stream) { + return stream.traverseValidation(semigroup, identity()); + } + + /** + * Traverse this stream with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public Either> traverseEither( + final F> f) { + return traverseEitherRight(f); + } + + /** + * Traverse this stream with the given function and collect the output on the left side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public Either, R> traverseEitherLeft( + final F> f) { + return foldRight1( + ( + element, + either) -> f.f(element).left().bind(elementInner -> either.left().map(stream -> stream.cons(elementInner))), + Either.left(nil())); + } + + /** + * Traverse this stream with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the either + */ + public Either> traverseEitherRight( + final F> f) { + return foldRight1( + ( + element, + either) -> f.f(element).right().bind(elementInner -> either.right().map(stream -> stream.cons(elementInner))), + Either.right(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a function. + * + * @param f the given function + * @param the type of the input value + * @param the type of the output value + * @return the function + */ + public F> traverseF( + final F> f) { + return foldRight1( + ( + element, + fInner) -> Function.bind(f.f(element), elementInner -> andThen(fInner, stream -> stream.cons(elementInner))), + Function.constant(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as an IO. + * + * @param f the given function + * @param the type of the IO value + * @return the IO + */ + public IO> traverseIO( + final F> f) { + return foldRight1( + ( + element, + io) -> IOFunctions.bind(f.f(element), elementInner -> IOFunctions.map(io, stream -> stream.cons(elementInner))), + IOFunctions.unit(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a list. + * + * @param f the given function + * @param the type of the list value + * @return the list + */ + public List> traverseList( + final F> f) { + return foldRight1( + ( + element, + list) -> f.f(element).bind(elementInner -> list.map(stream -> stream.cons(elementInner))), + List.single(nil())); + } + + /** + * Traverses through the Seq with the given function + * + * @param f The function that produces Option value + * @return none if applying f returns none to any element of the seq or f mapped seq in some . + */ + public Option> traverseOption( + final F> f) { + return foldRight1( + ( + element, + option) -> f.f(element).bind(elementInner -> option.map(stream -> stream.cons(elementInner))), + Option.some(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a p1. + * + * @param f the given function + * @param the type of the p1 value + * @return the p1 + */ + public P1> traverseP1( + final F> f) { + return foldRight1( + ( + element, + p1) -> f.f(element).bind(elementInner -> p1.map(stream -> stream.cons(elementInner))), + P.p(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a seq. + * + * @param f the given function + * @param the type of the seq value + * @return the seq + */ + public Seq> traverseSeq( + final F> f) { + return foldRight1( + ( + element, + seq) -> f.f(element).bind(elementInner -> seq.map(stream -> stream.cons(elementInner))), + Seq.single(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a set; use the given ord to order the set. + * + * @param ord the given ord + * @param f the given function + * @param the type of the set value + * @return the set + */ + public Set> traverseSet( + final Ord ord, + final F> f) { + final Ord> seqOrd = Ord.streamOrd(ord); + return foldRight1( + ( + element, + set) -> f.f(element).bind(seqOrd, elementInner -> set.map(seqOrd, seq -> seq.cons(elementInner))), + Set.single(seqOrd, nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a stream. + * + * @param f the given function + * @param the type of the stream value + * @return the stream + */ + public Stream> traverseStream( + final F> f) { + return foldRight1( + ( + element, + stream) -> f.f(element).bind(elementInner -> stream.map(seq -> seq.cons(elementInner))), + Stream.single(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a trampoline. + * + * @param f the given function + * @param the type of the trampoline value + * @return the trampoline + */ + public Trampoline> traverseTrampoline( + final F> f) { + return foldRight1( + ( + element, + trampoline) -> f.f(element).bind(elementInner -> trampoline.map(seq -> seq.cons(elementInner))), + Trampoline.pure(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a validation. + * + * @param f the given function + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public final Validation> traverseValidation( + final F> f) { + return foldRight1( + ( + element, + validation) -> f.f(element).bind(elementInner -> validation.map(stream -> stream.cons(elementInner))), + Validation.success(nil())); + } + + /** + * Traverse this stream with the given function and collect the output as a validation; use the given semigroup to + * reduce the errors. + * + * @param semigroup the given semigroup + * @param f the given function + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public final Validation> traverseValidation( + final Semigroup semigroup, + final F> f) { + return foldRight1( + ( + element, + validation) -> f.f(element).map(Stream::single).accumulate(semigroup, validation, (stream1, stream2) -> stream1.append(stream2)), + Validation.success(nil())); + } } diff --git a/core/src/main/java/fj/data/Tree.java b/core/src/main/java/fj/data/Tree.java index 9eebb932..3b12d506 100644 --- a/core/src/main/java/fj/data/Tree.java +++ b/core/src/main/java/fj/data/Tree.java @@ -10,8 +10,6 @@ /** * Provides a lazy, immutable, non-empty, multi-way tree (a rose tree). - * - * @version %build.number% */ public final class Tree implements Iterable { /** @@ -127,7 +125,7 @@ public static F, P1>>> subForest_() { public Stream flatten() { final F2, P1>, Stream> squish = new F2, P1>, Stream>() { public Stream f(final Tree t, final P1> xs) { - return cons(t.root(), t.subForest().map(Stream., Stream>foldRight().f(F2Functions.curry(this)).f(xs._1()))); + return cons(t.root(), t.subForest().map(Stream., Stream>foldRight().f(this.curry()).f(xs._1()))); } }; return squish.f(this, P.p(Stream.nil())); @@ -305,7 +303,7 @@ public static Show> show2D(final Show s) { * @return A new tree of the results of applying the given function over this tree and the given tree, position-wise. */ public Tree zipWith(final Tree bs, final F2 f) { - return F2Functions.zipTreeM(f).f(this, bs); + return f.zipTreeM().f(this, bs); } /** diff --git a/core/src/main/java/fj/data/TreeMap.java b/core/src/main/java/fj/data/TreeMap.java index cd111d06..e1af4d91 100644 --- a/core/src/main/java/fj/data/TreeMap.java +++ b/core/src/main/java/fj/data/TreeMap.java @@ -2,7 +2,6 @@ import fj.Equal; import fj.F; -import fj.F1Functions; import fj.Hash; import fj.Ord; import fj.P; @@ -70,20 +69,6 @@ public String toString() { return arrayTreeMap(keyOrd, p2s); } - /** - * Constructs a tree map from the given elements. - * - * @deprecated As of release 4.5, use {@link #iterableTreeMap(Ord, Iterable)} - * - * @param keyOrd An order for the keys of the tree map. - * @param list The elements to construct the tree map with. - * @return a TreeMap with the given elements. - */ - @Deprecated - public static TreeMap treeMap(final Ord keyOrd, final List> list) { - return iterableTreeMap(keyOrd, list); - } - /** * Constructs a tree map from the given elements. * @@ -308,18 +293,18 @@ public TreeMap update(final K k, final F f, final V v) { * and the optional value is the value associated with the given key if present, otherwise None. */ public P3, Option, Set> split(Ord ord, final K k) { - final F>>, Set> getSome = F1Functions.mapSet(F1Functions.o(Option.fromSome(), P2.__2()), ord); + final F>>, Set> getSome = Option.fromSome().o(P2.>__2()).mapSet(ord); return tree.split(p(k, Option.none())).map1(getSome).map3(getSome) - .map2(F1Functions.o(Option.join(), F1Functions.mapOption(P2.__2()))); + .map2(Option.join().o(P2.>__2().mapOption())); } /** - * Internal construction of a TreeMap from the given set. + * Constructs a TreeMap from the given set. * @param ord An order for the keys of the tree map. * @param s The elements to construct the tree map with. * @return a TreeMap with the given elements. */ - private static TreeMap treeMap(Ord ord, Set>> s) { + public static TreeMap setTreeMap(Ord ord, Set>> s) { TreeMap empty = TreeMap.empty(ord); TreeMap tree = s.toList().foldLeft((tm, p2) -> { Option opt = p2._2(); @@ -348,7 +333,7 @@ private static TreeMap treeMap(Ord ord, Set>> s) public P3, Option, TreeMap> splitLookup(final K k) { P3>>, Option>>, Set>>> p3 = tree.split(p(k, get(k))); Ord o = tree.ord().contramap(k2 -> p(k2, Option.none())); - return p(treeMap(o, p3._1()), get(k), treeMap(o, p3._3())); + return p(setTreeMap(o, p3._1()), get(k), setTreeMap(o, p3._3())); } /** @@ -359,7 +344,7 @@ public P3, Option, TreeMap> splitLookup(final K k) { */ @SuppressWarnings("unchecked") public TreeMap map(final F f) { - final F>, P2>> g = compose(p2 -> p(p2._1(), p2._2()), P2.map2_(F1Functions.mapOption(f))); + final F>, P2>> g = compose(p2 -> p(p2._1(), p2._2()), P2.map2_(f.mapOption())); final F>> coord = flip(P.>p2()).f(Option.none()); final Ord o = tree.ord().contramap(coord); return new TreeMap<>(tree.map(TreeMap.ord(o), g)); diff --git a/core/src/main/java/fj/data/TreeZipper.java b/core/src/main/java/fj/data/TreeZipper.java index 9ffad4f4..545238f3 100644 --- a/core/src/main/java/fj/data/TreeZipper.java +++ b/core/src/main/java/fj/data/TreeZipper.java @@ -1,678 +1,678 @@ -package fj.data; - -import fj.*; -import fj.function.Booleans; - -import java.util.Iterator; - -import static fj.Equal.p3Equal; -import static fj.Equal.p4Equal; -import static fj.Equal.streamEqual; -import static fj.Equal.treeEqual; -import static fj.Function.compose; -import static fj.Function.curry; -import static fj.Function.flip; -import static fj.Function.uncurryF2; -import static fj.Show.p3Show; -import static fj.Show.p4Show; -import static fj.Show.streamShow; -import static fj.Show.treeShow; -import static fj.data.Option.none; -import static fj.data.Option.some; -import static fj.data.Stream.nil; -import static fj.data.Stream.unfold; -import static fj.data.Tree.node; -import static fj.data.Tree.unfoldTree; - -/** - * Provides a zipper structure for rose trees, which is a Tree supplied with a location within that tree. - * Provides navigation, insertion, deletion, and memorization of visited locations within a tree. - */ -public final class TreeZipper implements Iterable> { - - /** - * Returns an iterator of all the positions of this TreeZipper. Exists for use with the foreach syntax. - * - * @return An iterator of all the positions of this TreeZipper. - */ - public Iterator> iterator() { - return positions().toTree().iterator(); - } - - private final Tree tree; - private final Stream> lefts; - private final Stream> rights; - private final Stream>, A, Stream>>> parents; - - private TreeZipper(final Tree tree, - final Stream> lefts, - final Stream> rights, - final Stream>, A, Stream>>> parents) { - this.tree = tree; - this.lefts = lefts; - this.rights = rights; - this.parents = parents; - } - - @Override - public final boolean equals(Object other) { - return Equal.equals0(TreeZipper.class, this, other, () -> Equal.treeZipperEqual(Equal.anyEqual())); - } - - @Override - public final int hashCode() { - return Hash.treeZipperHash(Hash.anyHash()).hash(this); - } - - /** - * Creates a new tree zipper given a currently selected tree, a forest on the left, a forest on the right, - * and a stream of parent contexts. - * - * @param tree The currently selected tree. - * @param lefts The selected tree's left siblings, closest first. - * @param rights The selected tree's right siblings, closest first. - * @param parents The parent of the selected tree, and the parent's siblings. - * @return A new zipper with the given tree selected, and the given forests on the left and right. - */ - public static TreeZipper treeZipper(final Tree tree, - final Stream> lefts, - final Stream> rights, - final Stream>, A, Stream>>> parents) { - return new TreeZipper<>(tree, lefts, rights, parents); - } - - /** - * First-class constructor for tree zippers. - * - * @return A function that returns a new tree zipper, given a selected tree, left and right siblings, - * and a parent context. - */ - public static - F, F>, F>, F>, A, Stream>>>, TreeZipper>>>> - treeZipper() { - return curry( - TreeZipper::treeZipper); - } - - /** - * Returns the product-4 representation of this zipper. - * - * @return the product-4 representation of this zipper. - */ - public P4, Stream>, Stream>, Stream>, A, Stream>>>> p() { - return P.p(tree, lefts, rights, parents); - } - - /** - * A first-class function that returns the product-4 representation of a given zipper. - * - * @return a function that converts a given zipper to its product-4 representation. - */ - public static - F, P4, Stream>, Stream>, Stream>, A, Stream>>>>> p_() { - return TreeZipper::p; - } - - /** - * An Equal instance for tree zippers. - * - * @param e An Equal instance for tree elements. - * @return An Equal instance for tree zippers. - */ - public static Equal> eq(final Equal e) { - return p4Equal( - treeEqual(e), - streamEqual(treeEqual(e)), - streamEqual(treeEqual(e)), - streamEqual(p3Equal(streamEqual(treeEqual(e)), e, streamEqual(treeEqual(e))))).contramap(TreeZipper.p_()); - } - - /** - * A Show instance for tree zippers. - * - * @param s A Show instance for tree elements. - * @return A Show instance for tree zippers. - */ - public static Show> show(final Show s) { - return p4Show( - treeShow(s), - streamShow(treeShow(s)), - streamShow(treeShow(s)), - streamShow(p3Show(streamShow(treeShow(s)), s, streamShow(treeShow(s))))).contramap(TreeZipper.p_()); - } - - private static Stream> combChildren(final Stream> ls, - final Tree t, - final Stream> rs) { - return ls.foldLeft(compose(flip(Stream.cons()), P.p1()), Stream.cons(t, P.p(rs))); - } - - /** - * Navigates to the parent of the current location. - * - * @return A new tree zipper focused on the parent node of the current node, - * or none if the current node is the root node. - */ - public Option> parent() { - if (parents.isEmpty()) - return none(); - else { - final P3>, A, Stream>> p = parents.head(); - return some(treeZipper(node(p._2(), combChildren(lefts, tree, rights)), p._1(), p._3(), parents.tail()._1())); - } - } - - /** - * Navigates to the top-most parent of the current location. - * - * @return A new tree zipper focused on the top-most parent of the current node. - */ - public TreeZipper root() { - return parent().option(this, TreeZipper.root_()); - } - - /** - * A first-class version of the root function. - * - * @return A function that returns a new tree-zipper focused on the root of the given tree zipper's tree. - */ - public static F, TreeZipper> root_() { - return TreeZipper::root; - } - - /** - * Navigates to the left sibling of the current location. - * - * @return A new tree zipper focused on the left sibling of the current node, - * or none if there are no siblings on the left. - */ - public Option> left() { - return lefts.isEmpty() ? Option.none() - : some(treeZipper(lefts.head(), lefts.tail()._1(), rights.cons(tree), parents)); - } - - /** - * Navigates to the right sibling of the current location. - * - * @return A new tree zipper focused on the right sibling of the current node, - * or none if there are no siblings on the right. - */ - public Option> right() { - return rights.isEmpty() ? Option.none() - : some(treeZipper(rights.head(), lefts.cons(tree), rights.tail()._1(), parents)); - } - - /** - * Navigtes to the first child of the current location. - * - * @return A new tree zipper focused on the first child of the current node, or none if the node has no children. - */ - public Option> firstChild() { - final Stream> ts = tree.subForest()._1(); - return ts.isEmpty() ? Option.none() - : some(treeZipper(ts.head(), Stream.nil(), ts.tail()._1(), downParents())); - } - - /** - * Navigtes to the last child of the current location. - * - * @return A new tree zipper focused on the last child of the current node, or none if the node has no children. - */ - public Option> lastChild() { - final Stream> ts = tree.subForest()._1().reverse(); - return ts.isEmpty() ? Option.none() - : some(treeZipper(ts.head(), ts.tail()._1(), Stream.nil(), downParents())); - } - - /** - * Navigates to the given child of the current location, starting at index 0. - * - * @param n The index of the child to which to navigate. - * @return An optional tree zipper focused on the child node at the given index, or none if there is no such child. - */ - public Option> getChild(final int n) { - Option> r = none(); - for (final P2>, Stream>> lr - : splitChildren(Stream.nil(), tree.subForest()._1(), n)) { - r = some(treeZipper(lr._1().head(), lr._1().tail()._1(), lr._2(), downParents())); - } - return r; - } - - /** - * Navigates to the first child of the current location, that satisfies the given predicate. - * - * @param p A predicate to be satisfied by the child node. - * @return An optional tree zipper focused on the first child node that satisfies the given predicate, - * or none if there is no such child. - */ - public Option> findChild(final F, Boolean> p) { - Option> r = none(); - - final F2>, Stream>, Option>, Tree, Stream>>>> split = - new F2>, Stream>, Option>, Tree, Stream>>>>() { - public Option>, Tree, Stream>>> f(final Stream> acc, - final Stream> xs) { - return xs.isNotEmpty() - ? p.f(xs.head()) ? some(P.p(acc, xs.head(), xs.tail()._1())) - : f(acc.cons(xs.head()), xs.tail()._1()) - : Option.none(); - } - }; - - Stream> subforest = tree.subForest()._1(); - if (subforest.isNotEmpty()) { - for (final P3>, Tree, Stream>> ltr - : split.f(Stream.nil(), subforest)) { - r = some(treeZipper(ltr._2(), ltr._1(), ltr._3(), downParents())); - } - } - return r; - } - - private Stream>, A, Stream>>> downParents() { - return parents.cons(P.p(lefts, tree.root(), rights)); - } - - private static Option, Stream>> splitChildren(final Stream acc, - final Stream xs, - final int n) { - return n == 0 ? some(P.p(acc, xs)) - : xs.isNotEmpty() ? splitChildren(acc.cons(xs.head()), xs.tail()._1(), n - 1) - : Option.none(); - } - - private static Stream>, A, Stream>>> lp3nil() { - return nil(); - } - - /** - * Creates a new tree zipper focused on the root of the given tree. - * - * @param t A tree over which to create a new zipper. - * @return a new tree zipper focused on the root of the given tree. - */ - public static TreeZipper fromTree(final Tree t) { - return treeZipper(t, Stream.nil(), Stream.nil(), TreeZipper.lp3nil()); - } - - /** - * Creates a new tree zipper focused on the first element of the given forest. - * - * @param ts A forest over which to create a new zipper. - * @return a new tree zipper focused on the first element of the given forest. - */ - public static Option> fromForest(final Stream> ts) { - return ts.isNotEmpty() - ? some(treeZipper(ts.head(), Stream.nil(), ts.tail()._1(), TreeZipper.lp3nil())) - : Option.none(); - } - - /** - * Returns the tree containing this location. - * - * @return the tree containing this location. - */ - public Tree toTree() { - return root().tree; - } - - /** - * Returns the forest containing this location. - * - * @return the forest containing this location. - */ - public Stream> toForest() { - final TreeZipper r = root(); - return combChildren(r.lefts, r.tree, r.rights); - } - - /** - * Returns the tree at the currently focused node. - * - * @return the tree at the currently focused node. - */ - public Tree focus() { - return tree; - } - - /** - * Returns the left siblings of the currently focused node. - * - * @return the left siblings of the currently focused node. - */ - public Stream> lefts() { - return lefts; - } - - /** - * Returns the right siblings of the currently focused node. - * - * @return the right siblings of the currently focused node. - */ - public Stream> rights() { - return rights; - } - - /** - * Returns the parents of the currently focused node. - * - * @return the parents of the currently focused node. - */ - public Stream>, A, Stream>>> parents() { - return parents; - } - - /** - * Indicates whether the current node is at the top of the tree. - * - * @return true if the current node is the root of the tree, otherwise false. - */ - public boolean isRoot() { - return parents.isEmpty(); - } - - /** - * Indicates whether the current node is the leftmost tree in the current forest. - * - * @return true if the current node has no left siblings, otherwise false. - */ - public boolean isFirst() { - return lefts.isEmpty(); - } - - /** - * Indicates whether the current node is the rightmost tree in the current forest. - * - * @return true if the current node has no siblings on its right, otherwise false. - */ - public boolean isLast() { - return rights.isEmpty(); - } - - /** - * Indicates whether the current node is at the bottom of the tree. - * - * @return true if the current node has no child nodes, otherwise false. - */ - public boolean isLeaf() { - return tree.subForest()._1().isEmpty(); - } - - /** - * Indicates whether the current node is a child node of another node. - * - * @return true if the current node has a parent node, otherwise false. - */ - public boolean isChild() { - return !isRoot(); - } - - /** - * Indicates whether the current node has any child nodes. - * - * @return true if the current node has child nodes, otherwise false. - */ - public boolean hasChildren() { - return !isLeaf(); - } - - /** - * Replaces the current node with the given tree. - * - * @param t A tree with which to replace the current node. - * @return A new tree zipper in which the focused node is replaced with the given tree. - */ - public TreeZipper setTree(final Tree t) { - return treeZipper(t, lefts, rights, parents); - } - - /** - * Modifies the current node with the given function. - * - * @param f A function with which to modify the current tree. - * @return A new tree zipper in which the focused node has been transformed by the given function. - */ - public TreeZipper modifyTree(final F, Tree> f) { - return setTree(f.f(tree)); - } - - /** - * Modifies the label at the current node with the given function. - * - * @param f A function with which to transform the current node's label. - * @return A new tree zipper with the focused node's label transformed by the given function. - */ - public TreeZipper modifyLabel(final F f) { - return setLabel(f.f(getLabel())); - } - - /** - * Replaces the label of the current node with the given value. - * - * @param v The new value for the node's label. - * @return A new tree zipper with the focused node's label replaced by the given value. - */ - public TreeZipper setLabel(final A v) { - return modifyTree(t -> Tree.node(v, t.subForest())); - } - - /** - * Returns the label at the current node. - * - * @return the label at the current node. - */ - public A getLabel() { - return tree.root(); - } - - /** - * Inserts a tree to the left of the current position. The inserted tree becomes the current tree. - * - * @param t A tree to insert to the left of the current position. - * @return A new tree zipper with the given tree in focus and the current tree on the right. - */ - public TreeZipper insertLeft(final Tree t) { - return treeZipper(t, lefts, rights.cons(tree), parents); - } - - /** - * Inserts a tree to the right of the current position. The inserted tree becomes the current tree. - * - * @param t A tree to insert to the right of the current position. - * @return A new tree zipper with the given tree in focus and the current tree on the left. - */ - public TreeZipper insertRight(final Tree t) { - return treeZipper(t, lefts.cons(tree), rights, parents); - } - - /** - * Inserts a tree as the first child of the current node. The inserted tree becomes the current tree. - * - * @param t A tree to insert. - * @return A new tree zipper with the given tree in focus, as the first child of the current node. - */ - public TreeZipper insertDownFirst(final Tree t) { - return treeZipper(t, Stream.nil(), tree.subForest()._1(), downParents()); - } - - /** - * Inserts a tree as the last child of the current node. The inserted tree becomes the current tree. - * - * @param t A tree to insert. - * @return A new tree zipper with the given tree in focus, as the last child of the current node. - */ - public TreeZipper insertDownLast(final Tree t) { - return treeZipper(t, tree.subForest()._1().reverse(), Stream.nil(), downParents()); - } - - /** - * Inserts a tree at the specified location in the current node's stream of children. The inserted tree - * becomes the current node. - * - * @param n The index at which to insert the given tree, starting at 0. - * @param t A tree to insert. - * @return A new tree zipper with the given tree in focus, at the specified index in the current node's stream - * of children, or None if the current node has fewer than n children. - */ - public Option> insertDownAt(final int n, final Tree t) { - Option> r = none(); - for (final P2>, Stream>> lr - : splitChildren(Stream.nil(), tree.subForest()._1(), n)) { - r = some(treeZipper(t, lr._1(), lr._2(), downParents())); - } - return r; - } - - /** - * Removes the current node from the tree. The new position becomes the right sibling, or the left sibling - * if the current node has no right siblings, or the parent node if the current node has no siblings. - * - * @return A new tree zipper with the current node removed. - */ - public Option> delete() { - Option> r = none(); - if (rights.isNotEmpty()) - r = some(treeZipper(rights.head(), lefts, rights.tail()._1(), parents)); - else if (lefts.isNotEmpty()) - r = some(treeZipper(lefts.head(), lefts.tail()._1(), rights, parents)); - else for (final TreeZipper loc : parent()) - r = some(loc.modifyTree(t -> node(t.root(), Stream.nil()))); - return r; - } - - /** - * Zips the nodes in this zipper with a boolean that indicates whether that node has focus. - * All of the booleans will be false, except for the focused node. - * - * @return A new zipper of pairs, with each node of this zipper paired with a boolean that is true if that - * node has focus, and false otherwise. - */ - public TreeZipper> zipWithFocus() { - final F> f = flip(P.p2()).f(false); - return map(f).modifyLabel(P2.map2_(Booleans.not)); - } - - /** - * Maps the given function across this zipper (covariant functor pattern). - * - * @param f A function to map across this zipper. - * @return A new zipper with the given function applied to the label of every node. - */ - public TreeZipper map(final F f) { - final F, Tree> g = Tree.fmap_().f(f); - final F>, Stream>> h = Stream., Tree>map_().f(g); - return treeZipper(tree.fmap(f), lefts.map(g), rights.map(g), parents.map( - p -> p.map1(h).map2(f).map3(h))); - } - - /** - * First-class conversion of a Tree to the corresponding tree zipper. - * - * @return A function that takes a tree to its tree zipper representation. - */ - public static F, TreeZipper> fromTree() { - return TreeZipper::fromTree; - } - - /** - * A first-class version of the left() function. - * - * @return A function that focuses the given tree zipper on its left sibling. - */ - public static F, Option>> left_() { - return TreeZipper::left; - } - - /** - * A first-class version of the right() function. - * - * @return A function that focuses the given tree zipper on its right sibling. - */ - public static F, Option>> right_() { - return TreeZipper::right; - } - - /** - * Returns a zipper over the tree of all possible permutations of this tree zipper (comonad pattern). - * This tree zipper becomes the focused node of the new zipper. - * - * @return A tree zipper over the tree of all possible permutations of this tree zipper. - */ - public TreeZipper> positions() { - final Tree> t = unfoldTree(TreeZipper.dwn()).f(this); - final Stream>> l = uf(TreeZipper.left_()); - final Stream>> r = uf(TreeZipper.right_()); - final Stream>>, TreeZipper, Stream>>>> p = unfold( - o -> { - Option>>, TreeZipper, Stream>>>, - Option>>> r1 = none(); - for (final TreeZipper z : o) { - r1 = some(P.p(P.p(z.uf(TreeZipper.left_()), z, z.uf(TreeZipper.right_())), z.parent())); - } - return r1; - }, parent()); - return treeZipper(t, l, r, p); - } - - private Stream>> uf(final F, Option>> f) { - return unfold( - o -> { - Option>, Option>>> r = none(); - for (final TreeZipper c : o) { - r = some(P.p(unfoldTree(TreeZipper.dwn()).f(c), f.f(c))); - } - return r; - }, f.f(this)); - } - - private static F, P2, P1>>>> dwn() { - F>, Option, Option>>>> fwd = o -> o.map(c -> P.p(c, c.right())); - return tz -> P.p(tz, P.lazy(() -> unfold(fwd, tz.firstChild()))); - } - - /** - * Maps the given function over the tree of all positions for this zipper (comonad pattern). Returns a zipper - * over the tree of results of the function application. - * - * @param f A function to map over the tree of all positions for this zipper. - * @return A zipper over the tree of results of the function application. - */ - public TreeZipper cobind(final F, B> f) { - return positions().map(f); - } - - /** - * A first-class version of the findChild function. - * - * @return a function that finds the first child, of a given tree zipper, that matches a given predicate. - */ - public static F2, Boolean>, TreeZipper, Option>> findChild() { - return (f, az) -> az.findChild(f); - } - - /** - * Zips this TreeZipper with another, applying the given function lock-step over both zippers in all directions. - * The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers. - * - * @param bs A TreeZipper to zip this one with. - * @param f A function with which to zip together the two TreeZippers. - * @return The result of applying the given function over this TreeZipper and the given TreeZipper, location-wise. - */ - public TreeZipper zipWith(final TreeZipper bs, final F2 f) { - return F2Functions.zipTreeZipperM(f).f(this, bs); - } - - /** - * Zips this TreeZipper with another, applying the given function lock-step over both zippers in all directions. - * The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers. - * - * @param bs A TreeZipper to zip this one with. - * @param f A function with which to zip together the two TreeZippers. - * @return The result of applying the given function over this TreeZipper and the given TreeZipper, location-wise. - */ - public TreeZipper zipWith(final TreeZipper bs, final F> f) { - return zipWith(bs, uncurryF2(f)); - } -} +package fj.data; + +import fj.*; +import fj.function.Booleans; + +import java.util.Iterator; + +import static fj.Equal.p3Equal; +import static fj.Equal.p4Equal; +import static fj.Equal.streamEqual; +import static fj.Equal.treeEqual; +import static fj.Function.compose; +import static fj.Function.curry; +import static fj.Function.flip; +import static fj.Function.uncurryF2; +import static fj.Show.p3Show; +import static fj.Show.p4Show; +import static fj.Show.streamShow; +import static fj.Show.treeShow; +import static fj.data.Option.none; +import static fj.data.Option.some; +import static fj.data.Stream.nil; +import static fj.data.Stream.unfold; +import static fj.data.Tree.node; +import static fj.data.Tree.unfoldTree; + +/** + * Provides a zipper structure for rose trees, which is a Tree supplied with a location within that tree. + * Provides navigation, insertion, deletion, and memorization of visited locations within a tree. + */ +public final class TreeZipper implements Iterable> { + + /** + * Returns an iterator of all the positions of this TreeZipper. Exists for use with the foreach syntax. + * + * @return An iterator of all the positions of this TreeZipper. + */ + public Iterator> iterator() { + return positions().toTree().iterator(); + } + + private final Tree tree; + private final Stream> lefts; + private final Stream> rights; + private final Stream>, A, Stream>>> parents; + + private TreeZipper(final Tree tree, + final Stream> lefts, + final Stream> rights, + final Stream>, A, Stream>>> parents) { + this.tree = tree; + this.lefts = lefts; + this.rights = rights; + this.parents = parents; + } + + @Override + public final boolean equals(Object other) { + return Equal.equals0(TreeZipper.class, this, other, () -> Equal.treeZipperEqual(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.treeZipperHash(Hash.anyHash()).hash(this); + } + + /** + * Creates a new tree zipper given a currently selected tree, a forest on the left, a forest on the right, + * and a stream of parent contexts. + * + * @param tree The currently selected tree. + * @param lefts The selected tree's left siblings, closest first. + * @param rights The selected tree's right siblings, closest first. + * @param parents The parent of the selected tree, and the parent's siblings. + * @return A new zipper with the given tree selected, and the given forests on the left and right. + */ + public static TreeZipper treeZipper(final Tree tree, + final Stream> lefts, + final Stream> rights, + final Stream>, A, Stream>>> parents) { + return new TreeZipper<>(tree, lefts, rights, parents); + } + + /** + * First-class constructor for tree zippers. + * + * @return A function that returns a new tree zipper, given a selected tree, left and right siblings, + * and a parent context. + */ + public static + F, F>, F>, F>, A, Stream>>>, TreeZipper>>>> + treeZipper() { + return curry( + TreeZipper::treeZipper); + } + + /** + * Returns the product-4 representation of this zipper. + * + * @return the product-4 representation of this zipper. + */ + public P4, Stream>, Stream>, Stream>, A, Stream>>>> p() { + return P.p(tree, lefts, rights, parents); + } + + /** + * A first-class function that returns the product-4 representation of a given zipper. + * + * @return a function that converts a given zipper to its product-4 representation. + */ + public static + F, P4, Stream>, Stream>, Stream>, A, Stream>>>>> p_() { + return TreeZipper::p; + } + + /** + * An Equal instance for tree zippers. + * + * @param e An Equal instance for tree elements. + * @return An Equal instance for tree zippers. + */ + public static Equal> eq(final Equal e) { + return p4Equal( + treeEqual(e), + streamEqual(treeEqual(e)), + streamEqual(treeEqual(e)), + streamEqual(p3Equal(streamEqual(treeEqual(e)), e, streamEqual(treeEqual(e))))).contramap(TreeZipper.p_()); + } + + /** + * A Show instance for tree zippers. + * + * @param s A Show instance for tree elements. + * @return A Show instance for tree zippers. + */ + public static Show> show(final Show s) { + return p4Show( + treeShow(s), + streamShow(treeShow(s)), + streamShow(treeShow(s)), + streamShow(p3Show(streamShow(treeShow(s)), s, streamShow(treeShow(s))))).contramap(TreeZipper.p_()); + } + + private static Stream> combChildren(final Stream> ls, + final Tree t, + final Stream> rs) { + return ls.foldLeft(compose(flip(Stream.cons()), P.p1()), Stream.cons(t, P.p(rs))); + } + + /** + * Navigates to the parent of the current location. + * + * @return A new tree zipper focused on the parent node of the current node, + * or none if the current node is the root node. + */ + public Option> parent() { + if (parents.isEmpty()) + return none(); + else { + final P3>, A, Stream>> p = parents.head(); + return some(treeZipper(node(p._2(), combChildren(lefts, tree, rights)), p._1(), p._3(), parents.tail()._1())); + } + } + + /** + * Navigates to the top-most parent of the current location. + * + * @return A new tree zipper focused on the top-most parent of the current node. + */ + public TreeZipper root() { + return parent().option(this, TreeZipper.root_()); + } + + /** + * A first-class version of the root function. + * + * @return A function that returns a new tree-zipper focused on the root of the given tree zipper's tree. + */ + public static F, TreeZipper> root_() { + return TreeZipper::root; + } + + /** + * Navigates to the left sibling of the current location. + * + * @return A new tree zipper focused on the left sibling of the current node, + * or none if there are no siblings on the left. + */ + public Option> left() { + return lefts.isEmpty() ? Option.none() + : some(treeZipper(lefts.head(), lefts.tail()._1(), rights.cons(tree), parents)); + } + + /** + * Navigates to the right sibling of the current location. + * + * @return A new tree zipper focused on the right sibling of the current node, + * or none if there are no siblings on the right. + */ + public Option> right() { + return rights.isEmpty() ? Option.none() + : some(treeZipper(rights.head(), lefts.cons(tree), rights.tail()._1(), parents)); + } + + /** + * Navigtes to the first child of the current location. + * + * @return A new tree zipper focused on the first child of the current node, or none if the node has no children. + */ + public Option> firstChild() { + final Stream> ts = tree.subForest()._1(); + return ts.isEmpty() ? Option.none() + : some(treeZipper(ts.head(), Stream.nil(), ts.tail()._1(), downParents())); + } + + /** + * Navigtes to the last child of the current location. + * + * @return A new tree zipper focused on the last child of the current node, or none if the node has no children. + */ + public Option> lastChild() { + final Stream> ts = tree.subForest()._1().reverse(); + return ts.isEmpty() ? Option.none() + : some(treeZipper(ts.head(), ts.tail()._1(), Stream.nil(), downParents())); + } + + /** + * Navigates to the given child of the current location, starting at index 0. + * + * @param n The index of the child to which to navigate. + * @return An optional tree zipper focused on the child node at the given index, or none if there is no such child. + */ + public Option> getChild(final int n) { + Option> r = none(); + for (final P2>, Stream>> lr + : splitChildren(Stream.nil(), tree.subForest()._1(), n)) { + r = some(treeZipper(lr._1().head(), lr._1().tail()._1(), lr._2(), downParents())); + } + return r; + } + + /** + * Navigates to the first child of the current location, that satisfies the given predicate. + * + * @param p A predicate to be satisfied by the child node. + * @return An optional tree zipper focused on the first child node that satisfies the given predicate, + * or none if there is no such child. + */ + public Option> findChild(final F, Boolean> p) { + Option> r = none(); + + final F2>, Stream>, Option>, Tree, Stream>>>> split = + new F2>, Stream>, Option>, Tree, Stream>>>>() { + public Option>, Tree, Stream>>> f(final Stream> acc, + final Stream> xs) { + return xs.isNotEmpty() + ? p.f(xs.head()) ? some(P.p(acc, xs.head(), xs.tail()._1())) + : f(acc.cons(xs.head()), xs.tail()._1()) + : Option.none(); + } + }; + + Stream> subforest = tree.subForest()._1(); + if (subforest.isNotEmpty()) { + for (final P3>, Tree, Stream>> ltr + : split.f(Stream.nil(), subforest)) { + r = some(treeZipper(ltr._2(), ltr._1(), ltr._3(), downParents())); + } + } + return r; + } + + private Stream>, A, Stream>>> downParents() { + return parents.cons(P.p(lefts, tree.root(), rights)); + } + + private static Option, Stream>> splitChildren(final Stream acc, + final Stream xs, + final int n) { + return n == 0 ? some(P.p(acc, xs)) + : xs.isNotEmpty() ? splitChildren(acc.cons(xs.head()), xs.tail()._1(), n - 1) + : Option.none(); + } + + private static Stream>, A, Stream>>> lp3nil() { + return nil(); + } + + /** + * Creates a new tree zipper focused on the root of the given tree. + * + * @param t A tree over which to create a new zipper. + * @return a new tree zipper focused on the root of the given tree. + */ + public static TreeZipper fromTree(final Tree t) { + return treeZipper(t, Stream.nil(), Stream.nil(), TreeZipper.lp3nil()); + } + + /** + * Creates a new tree zipper focused on the first element of the given forest. + * + * @param ts A forest over which to create a new zipper. + * @return a new tree zipper focused on the first element of the given forest. + */ + public static Option> fromForest(final Stream> ts) { + return ts.isNotEmpty() + ? some(treeZipper(ts.head(), Stream.nil(), ts.tail()._1(), TreeZipper.lp3nil())) + : Option.none(); + } + + /** + * Returns the tree containing this location. + * + * @return the tree containing this location. + */ + public Tree toTree() { + return root().tree; + } + + /** + * Returns the forest containing this location. + * + * @return the forest containing this location. + */ + public Stream> toForest() { + final TreeZipper r = root(); + return combChildren(r.lefts, r.tree, r.rights); + } + + /** + * Returns the tree at the currently focused node. + * + * @return the tree at the currently focused node. + */ + public Tree focus() { + return tree; + } + + /** + * Returns the left siblings of the currently focused node. + * + * @return the left siblings of the currently focused node. + */ + public Stream> lefts() { + return lefts; + } + + /** + * Returns the right siblings of the currently focused node. + * + * @return the right siblings of the currently focused node. + */ + public Stream> rights() { + return rights; + } + + /** + * Returns the parents of the currently focused node. + * + * @return the parents of the currently focused node. + */ + public Stream>, A, Stream>>> parents() { + return parents; + } + + /** + * Indicates whether the current node is at the top of the tree. + * + * @return true if the current node is the root of the tree, otherwise false. + */ + public boolean isRoot() { + return parents.isEmpty(); + } + + /** + * Indicates whether the current node is the leftmost tree in the current forest. + * + * @return true if the current node has no left siblings, otherwise false. + */ + public boolean isFirst() { + return lefts.isEmpty(); + } + + /** + * Indicates whether the current node is the rightmost tree in the current forest. + * + * @return true if the current node has no siblings on its right, otherwise false. + */ + public boolean isLast() { + return rights.isEmpty(); + } + + /** + * Indicates whether the current node is at the bottom of the tree. + * + * @return true if the current node has no child nodes, otherwise false. + */ + public boolean isLeaf() { + return tree.subForest()._1().isEmpty(); + } + + /** + * Indicates whether the current node is a child node of another node. + * + * @return true if the current node has a parent node, otherwise false. + */ + public boolean isChild() { + return !isRoot(); + } + + /** + * Indicates whether the current node has any child nodes. + * + * @return true if the current node has child nodes, otherwise false. + */ + public boolean hasChildren() { + return !isLeaf(); + } + + /** + * Replaces the current node with the given tree. + * + * @param t A tree with which to replace the current node. + * @return A new tree zipper in which the focused node is replaced with the given tree. + */ + public TreeZipper setTree(final Tree t) { + return treeZipper(t, lefts, rights, parents); + } + + /** + * Modifies the current node with the given function. + * + * @param f A function with which to modify the current tree. + * @return A new tree zipper in which the focused node has been transformed by the given function. + */ + public TreeZipper modifyTree(final F, Tree> f) { + return setTree(f.f(tree)); + } + + /** + * Modifies the label at the current node with the given function. + * + * @param f A function with which to transform the current node's label. + * @return A new tree zipper with the focused node's label transformed by the given function. + */ + public TreeZipper modifyLabel(final F f) { + return setLabel(f.f(getLabel())); + } + + /** + * Replaces the label of the current node with the given value. + * + * @param v The new value for the node's label. + * @return A new tree zipper with the focused node's label replaced by the given value. + */ + public TreeZipper setLabel(final A v) { + return modifyTree(t -> Tree.node(v, t.subForest())); + } + + /** + * Returns the label at the current node. + * + * @return the label at the current node. + */ + public A getLabel() { + return tree.root(); + } + + /** + * Inserts a tree to the left of the current position. The inserted tree becomes the current tree. + * + * @param t A tree to insert to the left of the current position. + * @return A new tree zipper with the given tree in focus and the current tree on the right. + */ + public TreeZipper insertLeft(final Tree t) { + return treeZipper(t, lefts, rights.cons(tree), parents); + } + + /** + * Inserts a tree to the right of the current position. The inserted tree becomes the current tree. + * + * @param t A tree to insert to the right of the current position. + * @return A new tree zipper with the given tree in focus and the current tree on the left. + */ + public TreeZipper insertRight(final Tree t) { + return treeZipper(t, lefts.cons(tree), rights, parents); + } + + /** + * Inserts a tree as the first child of the current node. The inserted tree becomes the current tree. + * + * @param t A tree to insert. + * @return A new tree zipper with the given tree in focus, as the first child of the current node. + */ + public TreeZipper insertDownFirst(final Tree t) { + return treeZipper(t, Stream.nil(), tree.subForest()._1(), downParents()); + } + + /** + * Inserts a tree as the last child of the current node. The inserted tree becomes the current tree. + * + * @param t A tree to insert. + * @return A new tree zipper with the given tree in focus, as the last child of the current node. + */ + public TreeZipper insertDownLast(final Tree t) { + return treeZipper(t, tree.subForest()._1().reverse(), Stream.nil(), downParents()); + } + + /** + * Inserts a tree at the specified location in the current node's stream of children. The inserted tree + * becomes the current node. + * + * @param n The index at which to insert the given tree, starting at 0. + * @param t A tree to insert. + * @return A new tree zipper with the given tree in focus, at the specified index in the current node's stream + * of children, or None if the current node has fewer than n children. + */ + public Option> insertDownAt(final int n, final Tree t) { + Option> r = none(); + for (final P2>, Stream>> lr + : splitChildren(Stream.nil(), tree.subForest()._1(), n)) { + r = some(treeZipper(t, lr._1(), lr._2(), downParents())); + } + return r; + } + + /** + * Removes the current node from the tree. The new position becomes the right sibling, or the left sibling + * if the current node has no right siblings, or the parent node if the current node has no siblings. + * + * @return A new tree zipper with the current node removed. + */ + public Option> delete() { + Option> r = none(); + if (rights.isNotEmpty()) + r = some(treeZipper(rights.head(), lefts, rights.tail()._1(), parents)); + else if (lefts.isNotEmpty()) + r = some(treeZipper(lefts.head(), lefts.tail()._1(), rights, parents)); + else for (final TreeZipper loc : parent()) + r = some(loc.modifyTree(t -> node(t.root(), Stream.nil()))); + return r; + } + + /** + * Zips the nodes in this zipper with a boolean that indicates whether that node has focus. + * All of the booleans will be false, except for the focused node. + * + * @return A new zipper of pairs, with each node of this zipper paired with a boolean that is true if that + * node has focus, and false otherwise. + */ + public TreeZipper> zipWithFocus() { + final F> f = flip(P.p2()).f(false); + return map(f).modifyLabel(P2.map2_(Booleans.not)); + } + + /** + * Maps the given function across this zipper (covariant functor pattern). + * + * @param f A function to map across this zipper. + * @return A new zipper with the given function applied to the label of every node. + */ + public TreeZipper map(final F f) { + final F, Tree> g = Tree.fmap_().f(f); + final F>, Stream>> h = Stream., Tree>map_().f(g); + return treeZipper(tree.fmap(f), lefts.map(g), rights.map(g), parents.map( + p -> p.map1(h).map2(f).map3(h))); + } + + /** + * First-class conversion of a Tree to the corresponding tree zipper. + * + * @return A function that takes a tree to its tree zipper representation. + */ + public static F, TreeZipper> fromTree() { + return TreeZipper::fromTree; + } + + /** + * A first-class version of the left() function. + * + * @return A function that focuses the given tree zipper on its left sibling. + */ + public static F, Option>> left_() { + return TreeZipper::left; + } + + /** + * A first-class version of the right() function. + * + * @return A function that focuses the given tree zipper on its right sibling. + */ + public static F, Option>> right_() { + return TreeZipper::right; + } + + /** + * Returns a zipper over the tree of all possible permutations of this tree zipper (comonad pattern). + * This tree zipper becomes the focused node of the new zipper. + * + * @return A tree zipper over the tree of all possible permutations of this tree zipper. + */ + public TreeZipper> positions() { + final Tree> t = unfoldTree(TreeZipper.dwn()).f(this); + final Stream>> l = uf(TreeZipper.left_()); + final Stream>> r = uf(TreeZipper.right_()); + final Stream>>, TreeZipper, Stream>>>> p = unfold( + o -> { + Option>>, TreeZipper, Stream>>>, + Option>>> r1 = none(); + for (final TreeZipper z : o) { + r1 = some(P.p(P.p(z.uf(TreeZipper.left_()), z, z.uf(TreeZipper.right_())), z.parent())); + } + return r1; + }, parent()); + return treeZipper(t, l, r, p); + } + + private Stream>> uf(final F, Option>> f) { + return unfold( + o -> { + Option>, Option>>> r = none(); + for (final TreeZipper c : o) { + r = some(P.p(unfoldTree(TreeZipper.dwn()).f(c), f.f(c))); + } + return r; + }, f.f(this)); + } + + private static F, P2, P1>>>> dwn() { + F>, Option, Option>>>> fwd = o -> o.map(c -> P.p(c, c.right())); + return tz -> P.p(tz, P.lazy(() -> unfold(fwd, tz.firstChild()))); + } + + /** + * Maps the given function over the tree of all positions for this zipper (comonad pattern). Returns a zipper + * over the tree of results of the function application. + * + * @param f A function to map over the tree of all positions for this zipper. + * @return A zipper over the tree of results of the function application. + */ + public TreeZipper cobind(final F, B> f) { + return positions().map(f); + } + + /** + * A first-class version of the findChild function. + * + * @return a function that finds the first child, of a given tree zipper, that matches a given predicate. + */ + public static F2, Boolean>, TreeZipper, Option>> findChild() { + return (f, az) -> az.findChild(f); + } + + /** + * Zips this TreeZipper with another, applying the given function lock-step over both zippers in all directions. + * The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers. + * + * @param bs A TreeZipper to zip this one with. + * @param f A function with which to zip together the two TreeZippers. + * @return The result of applying the given function over this TreeZipper and the given TreeZipper, location-wise. + */ + public TreeZipper zipWith(final TreeZipper bs, final F2 f) { + return f.zipTreeZipperM().f(this, bs); + } + + /** + * Zips this TreeZipper with another, applying the given function lock-step over both zippers in all directions. + * The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers. + * + * @param bs A TreeZipper to zip this one with. + * @param f A function with which to zip together the two TreeZippers. + * @return The result of applying the given function over this TreeZipper and the given TreeZipper, location-wise. + */ + public TreeZipper zipWith(final TreeZipper bs, final F> f) { + return zipWith(bs, uncurryF2(f)); + } +} diff --git a/core/src/main/java/fj/data/Validation.java b/core/src/main/java/fj/data/Validation.java index 58aa5e9c..5c1985a4 100644 --- a/core/src/main/java/fj/data/Validation.java +++ b/core/src/main/java/fj/data/Validation.java @@ -1,23 +1,22 @@ package fj.data; import fj.*; +import fj.control.Trampoline; import fj.function.Effect1; -import static fj.Function.curry; -import static fj.P.p; +import java.util.Iterator; -import static fj.Unit.unit; import static fj.Bottom.error; +import static fj.Function.*; +import static fj.P.p; +import static fj.Unit.unit; +import static fj.data.Either.*; import static fj.data.List.list; -import java.util.Iterator; - /** * Isomorphic to {@link Either} but has renamed functions and represents failure on the left and success on the right. * This type also has accumulating functions that accept a {@link Semigroup} for binding computation while keeping error * values - * - * @version %build.number% */ public class Validation implements Iterable { private final Either e; @@ -737,20 +736,14 @@ public final Validation, B> accumulate(F f) { } - public final Validation, C> accumulate(Validation v2, F2 f) { - List list = List.nil(); - if (isFail()) { - list = list.cons(fail()); - } - if (v2.isFail()) { - list = list.cons(v2.fail()); - } - if (!list.isEmpty()) { - return fail(list); - } else { - return success(f.f(success(), v2.success())); - } + public final Validation, C> accumulate(Validation v2, F2 f) { + List list = fails(list(this, v2)); + if (!list.isEmpty()) { + return fail(list); + } else { + return success(f.f(success(), v2.success())); } + } @@ -783,7 +776,7 @@ public final Validation, D> accumulate(Validation v2, Va public final Validation, G> accumulate(Validation v2, Validation v3, Validation v4, Validation v5, Validation v6, F6 f) { - List list = fails(list(this, v2, v3, v4, v5)); + List list = fails(list(this, v2, v3, v4, v5, v6)); if (!list.isEmpty()) { return fail(list); } else { @@ -792,7 +785,7 @@ public final Validation, D> accumulate(Validation v2, Va } public final Validation, H> accumulate(Validation v2, Validation v3, Validation v4, Validation v5, Validation v6, Validation v7, F7 f) { - List list = fails(list(this, v2, v3, v4, v5)); + List list = fails(list(this, v2, v3, v4, v5, v6, v7)); if (!list.isEmpty()) { return fail(list); } else { @@ -801,7 +794,7 @@ public final Validation, D> accumulate(Validation v2, Va } public final Validation, I> accumulate(Validation v2, Validation v3, Validation v4, Validation v5, Validation v6, Validation v7, Validation v8, F8 f) { - List list = fails(list(this, v2, v3, v4, v5)); + List list = fails(list(this, v2, v3, v4, v5, v6, v7, v8)); if (!list.isEmpty()) { return fail(list); } else { @@ -825,44 +818,328 @@ public static Validation, List> sequenceNonCumulative(List List> traverseList(F> f){ - return isSuccess() ? - f.f(success()).map(Validation::success) : - List.iterableList(fail(e.left().value())); - } + /** + * Sequence the given validation and collect the output on the left side of an either. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either, R> sequenceEitherLeft(final Validation> validation) { + return validation.traverseEitherLeft(identity()); + } - public final Stream> traverseStream(F> f){ - return isSuccess() ? - f.f(success()).map(Validation::success) : - Stream.iterableStream(fail(e.left().value())); - } + /** + * Sequence the given validation and collect the output on the right side of an either. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the right value + * @param the type of the left value + * @return the either + */ + public static final Either> sequenceEitherRight(final Validation> validation) { + return validation.traverseEitherRight(identity()); + } - public final Option> traverseOption(F> f){ - return isSuccess() ? - f.f(success()).map(Validation::success) : - Option.some(fail(e.left().value())); - } + /** + * Sequence the given validation and collect the output as a function. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of input value + * @param the type of output value + * @return the function + */ + public static final F> sequenceF(final Validation> validation) { + return validation.traverseF(identity()); + } - public final IO> traverseIO(F> f){ - return isSuccess() ? - IOFunctions.map(f.f(success()), Validation::success) : - IOFunctions.unit(fail(e.left().value())); - } + /** + * Sequence the given validation and collect the output as an IO. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the IO value + * @return the IO + */ + public static final IO> sequenceIO(final Validation> validation) { + return validation.traverseIO(identity()); + } - public final P1> traverseP1(F> f){ - return isSuccess() ? - f.f(success()).map(Validation::success) : - p(fail(e.left().value())); - } + /** + * Sequence the given validation and collect the output as a list. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the list value + * @return the list + */ + public static final List> sequenceList(final Validation> validation) { + return validation.traverseList(identity()); + } + /** + * Sequence the given validation and collect the output as an option. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the option value + * @return the option + */ + public static final Option> sequenceOption(final Validation> validation) { + return validation.traverseOption(identity()); + } - public static List fails(List> list) { - return list.filter(Validation::isFail).map(v -> v.fail()); - } + /** + * Sequence the given validation and collect the output as a P1. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the P1 value + * @return the P1 + */ + public static final P1> sequenceP1(final Validation> validation) { + return validation.traverseP1(identity()); + } - public static List successes(List> list) { - return list.filter(Validation::isSuccess).map(v -> v.success()); - } + + /** + * Sequence the given validation and collect the output as a seq. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the seq value + * @return the seq + */ + public static final Seq> sequenceSeq(final Validation> validation) { + return validation.traverseSeq(identity()); + } + + /** + * Sequence the given validation and collect the output as a set. + * + * @param ordE the given failure value ord + * @param ordC the given success value ord + * @param validation the given validation + * @param the type of the failure value + * @param the type of the set value + * @return the set + */ + public static final Set> sequenceSet(final Ord ordE, final Ord ordC, final Validation> validation) { + return validation.traverseSet(ordE, ordC, identity()); + } + + /** + * Sequence the given validation and collect the output as a stream. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the stream value + * @return the stream + */ + public static final Stream> sequenceStream(final Validation> validation) { + return validation.traverseStream(identity()); + } + + /** + * Sequence the given validation and collect the output as a trampoline. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the trampoline value + * @return the trampoline + */ + public static final Trampoline> sequenceTrampoline(final Validation> validation) { + return validation.traverseTrampoline(identity()); + } + + /** + * Sequence the given validation and collect the output as a validation. + * + * @param validation the given validation + * @param the type of the failure value + * @param the type of the failure value + * @param the type of the success value + * @return the validation + */ + public static final Validation> sequenceValidation(final Validation> validation) { + return validation.traverseValidation(identity()); + } + + /** + * Traverse this validation with the given function and collect the output on the left side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the list + */ + public final Either, R> traverseEitherLeft(final F> f) { + return validation( + failure -> left(fail(failure)), + success -> f.f(success).left().map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output on the right side of an either. + * + * @param f the given function + * @param the type of the left value + * @param the type of the right value + * @return the list + */ + public final Either> traverseEitherRight(final F> f) { + return validation( + failure -> right(fail(failure)), + success -> f.f(success).right().map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a function. + * + * @param f the given function + * @param the type of the input value + * @param the type of the output value + * @return the function + */ + public final F> traverseF(final F> f) { + return validation( + failure -> constant(fail(failure)), + success -> andThen(f.f(success), Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as an IO. + * + * @param f the given function + * @param the type of the IO value + * @return the IO + */ + public final IO> traverseIO(final F> f) { + return validation( + failure -> IOFunctions.unit(fail(failure)), + success -> IOFunctions.map(f.f(success), Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a list. + * + * @param f the given function + * @param the type of the list value + * @return the list + */ + public final List> traverseList(final F> f) { + return validation( + failure -> List.single(fail(failure)), + success -> f.f(success).map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as an option. + * + * @param f the given function + * @param the type of the option value + * @return the option + */ + public final Option> traverseOption(F> f) { + return validation( + failure -> Option.some(fail(failure)), + success -> f.f(success).map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a P1. + * + * @param f the given function + * @param the type of the P1 value + * @return the P1 + */ + public final P1> traverseP1(final F> f) { + return validation( + failure -> p(fail(failure)), + success -> f.f(success).map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a seq. + * + * @param f the given function + * @param the type of the seq value + * @return the seq + */ + public final Seq> traverseSeq(final F> f) { + return validation( + failure -> Seq.single(fail(failure)), + success -> f.f(success).map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a set; use the given success and failure value ords to order the set. + * + * @param ordE the given failure value ord + * @param ordC the given success value ord + * @param f the given function + * @param the type of the set value + * @return the set + */ + public final Set> traverseSet(final Ord ordE, final Ord ordC, final F> f) { + final Ord> ord = Ord.validationOrd(ordE, ordC); + return validation( + failure -> Set.single(ord, fail(failure)), + success -> f.f(success).map(ord, Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a stream. + * + * @param f the given function + * @param the type of the stream value + * @return the stream + */ + public final Stream> traverseStream(final F> f) { + return validation( + failure -> Stream.single(fail(failure)), + success -> f.f(success).map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a trampoline. + * + * @param f the given function + * @param the type of the trampoline value + * @return the trampoline + */ + public final Trampoline> traverseTrampoline(final F> f) { + return validation( + failure -> Trampoline.pure(fail(failure)), + success -> f.f(success).map(Validation::success)); + } + + /** + * Traverse this validation with the given function and collect the output as a validation. + * + * @param f the given function + * @param the type of the failure value + * @param the type of the seq value + * @return the validation + */ + public final Validation> traverseValidation(final F> f) { + return validation( + failure -> success(fail(failure)), + success -> f.f(success).map(Validation::success)); + } + + + public static List fails(List> list) { + return list.filter(Validation::isFail).map(v -> v.fail()); + } + + public static List successes(List> list) { + return list.filter(Validation::isSuccess).map(v -> v.success()); + } /** * A failing projection of a validation. @@ -1268,7 +1545,7 @@ public static Validation parseShort(final String s } /** - * A function that parses a string into a short. + * A function that parses a string into a short. */ public static final F> parseShort = Validation::parseShort; diff --git a/core/src/main/java/fj/data/Writer.java b/core/src/main/java/fj/data/Writer.java index b6b4579b..ba2cded5 100644 --- a/core/src/main/java/fj/data/Writer.java +++ b/core/src/main/java/fj/data/Writer.java @@ -2,9 +2,6 @@ import fj.*; -/** - * Created by MarkPerry on 7/07/2014. - */ public final class Writer { private final A val; diff --git a/core/src/main/java/fj/data/Zipper.java b/core/src/main/java/fj/data/Zipper.java index 2514d8e2..4b96377f 100644 --- a/core/src/main/java/fj/data/Zipper.java +++ b/core/src/main/java/fj/data/Zipper.java @@ -1,585 +1,585 @@ -package fj.data; - -import fj.*; -import fj.function.Integers; - -import java.util.Iterator; - -import static fj.Function.compose; -import static fj.Function.curry; -import static fj.Function.flip; -import static fj.Function.join; -import static fj.Function.uncurryF2; -import static fj.data.Option.none; -import static fj.data.Option.some; -import static fj.data.Stream.nil; -import static fj.data.Stream.repeat; - -/** - * Provides a pointed stream, which is a non-empty zipper-like stream structure that tracks an index (focus) - * position in a stream. Focus can be moved forward and backwards through the stream, elements can be inserted - * before or after the focused position, and the focused item can be deleted. - *

    - * Based on the pointedlist library by Jeff Wheeler. - */ -public final class Zipper implements Iterable> { - private final Stream left; - private final A focus; - private final Stream right; - - private Zipper(final Stream left, final A focus, final Stream right) { - this.left = left; - this.focus = focus; - this.right = right; - } - - - /** - * Creates a new Zipper with the given streams before and after the focus, and the given focused item. - * - * @param left The stream of elements before the focus. - * @param focus The element under focus. - * @param right The stream of elements after the focus. - * @return a new Zipper with the given streams before and after the focus, and the given focused item. - */ - public static Zipper zipper(final Stream left, final A focus, final Stream right) { - return new Zipper<>(left, focus, right); - } - - /** - * Creates a new Zipper from the given triple. - * - * @param p A triple of the elements before the focus, the focus element, and the elements after the focus, - * respectively. - * @return a new Zipper created from the given triple. - */ - public static Zipper zipper(final P3, A, Stream> p) { - return new Zipper<>(p._1(), p._2(), p._3()); - } - - /** - * First-class constructor of zippers. - * - * @return A function that yields a new zipper given streams on the left and right and a focus element. - */ - public static F3, A, Stream, Zipper> zipper() { - return Zipper::zipper; - } - - /** - * Returns the product-3 representation of this Zipper. - * - * @return the product-3 representation of this Zipper. - */ - public P3, A, Stream> p() { - return P.p(left, focus, right); - } - - /** - * A first-class function that yields the product-3 representation of a given Zipper. - * - * @return A first-class function that yields the product-3 representation of a given Zipper. - */ - public static F, P3, A, Stream>> p_() { - return Zipper::p; - } - - /** - * An Ord instance for Zippers. - * - * @param o An Ord instance for the element type. - * @return An Ord instance for Zippers. - */ - public static Ord> ord(final Ord o) { - final Ord> so = Ord.streamOrd(o); - return Ord.p3Ord(so, o, so).contramap(Zipper.p_()); - } - - @Override - public final boolean equals(Object other) { - return Equal.equals0(Zipper.class, this, other, () -> Equal.zipperEqual(Equal.anyEqual())); - } - - @Override - public final int hashCode() { - return Hash.zipperHash(Hash.anyHash()).hash(this); - } - - /** - * An Equal instance for Zippers. - * - * @param e An Equal instance for the element type. - * @return An Equal instance for Zippers. - */ - public static Equal> eq(final Equal e) { - final Equal> se = Equal.streamEqual(e); - return Equal.p3Equal(se, e, se).contramap(Zipper.p_()); - } - - /** - * A Show instance for Zippers. - * - * @param s A Show instance for the element type. - * @return A Show instance for Zippers. - */ - public static Show> show(final Show s) { - final Show> ss = Show.streamShow(s); - return Show.p3Show(ss, s, ss).contramap(Zipper.p_()); - } - - /** - * Maps the given function across the elements of this zipper (covariant functor pattern). - * - * @param f A function to map across this zipper. - * @return A new zipper with the given function applied to all elements. - */ - public Zipper map(final F f) { - return zipper(left.map(f), f.f(focus), right.map(f)); - } - - /** - * Performs a right-fold reduction across this zipper. - * - * @param f The function to apply on each element of this zipper. - * @param z The beginning value to start the application from. - * @return the final result after the right-fold reduction. - */ - public B foldRight(final F> f, final B z) { - return left.foldLeft(flip(f), - right.cons(focus).foldRight(compose( - Function., B, B>andThen().f(P1.__1()), f), z)); - } - - /** - * Creates a new zipper with a single element. - * - * @param a The focus element of the new zipper. - * @return a new zipper with a single element which is in focus. - */ - public static Zipper single(final A a) { - return zipper(Stream.nil(), a, Stream.nil()); - } - - /** - * Possibly create a zipper if the provided stream has at least one element, otherwise None. - * The provided stream's head will be the focus of the zipper, and the rest of the stream will follow - * on the right side. - * - * @param a The stream from which to create a zipper. - * @return a new zipper if the provided stream has at least one element, otherwise None. - */ - @SuppressWarnings("IfMayBeConditional") - public static Option> fromStream(final Stream a) { - if (a.isEmpty()) - return none(); - else - return some(zipper(Stream.nil(), a.head(), a.tail()._1())); - } - - /** - * Possibly create a zipper if the provided stream has at least one element, otherwise None. - * The provided stream's last element will be the focus of the zipper, following the rest of the stream in order, - * to the left. - * - * @param a The stream from which to create a zipper. - * @return a new zipper if the provided stream has at least one element, otherwise None. - */ - public static Option> fromStreamEnd(final Stream a) { - if (a.isEmpty()) - return none(); - else { - final Stream xs = a.reverse(); - return some(zipper(xs.tail()._1(), xs.head(), Stream.nil())); - } - } - - /** - * Returns the focus element of this zipper. - * - * @return the focus element of this zipper. - */ - public A focus() { - return focus; - } - - /** - * Possibly moves the focus to the next element in the list. - * - * @return An optional zipper with the focus moved one element to the right, if there are elements to the right of - * focus, otherwise None. - */ - public Option> next() { - return right.isEmpty() ? Option.none() : some(tryNext()); - } - - /** - * Attempts to move the focus to the next element, or throws an error if there are no more elements. - * - * @return A zipper with the focus moved one element to the right, if there are elements to the right of - * focus, otherwise throws an error. - */ - public Zipper tryNext() { - if (right.isEmpty()) - throw new Error("Tried next at the end of a zipper."); - else - return zipper(left.cons(focus), right.head(), right.tail()._1()); - } - - /** - * Possibly moves the focus to the previous element in the list. - * - * @return An optional zipper with the focus moved one element to the left, if there are elements to the left of - * focus, otherwise None. - */ - public Option> previous() { - return left.isEmpty() ? Option.none() : some(tryPrevious()); - } - - /** - * Attempts to move the focus to the previous element, or throws an error if there are no more elements. - * - * @return A zipper with the focus moved one element to the left, if there are elements to the left of - * focus, otherwise throws an error. - */ - public Zipper tryPrevious() { - if (left.isEmpty()) - throw new Error("Tried previous at the beginning of a zipper."); - else - return zipper(left.tail()._1(), left.head(), right.cons(focus)); - } - - /** - * First-class version of the next() function. - * - * @return A function that moves the given zipper's focus to the next element. - */ - public static F, Option>> next_() { - return Zipper::next; - } - - /** - * First-class version of the previous() function. - * - * @return A function that moves the given zipper's focus to the previous element. - */ - public static F, Option>> previous_() { - return Zipper::previous; - } - - /** - * Inserts an element to the left of the focus, then moves the focus to the new element. - * - * @param a A new element to insert into this zipper. - * @return A new zipper with the given element in focus, and the current focus element on its right. - */ - public Zipper insertLeft(final A a) { - return zipper(left, a, right.cons(focus)); - } - - /** - * Inserts an element to the right of the focus, then moves the focus to the new element. - * - * @param a A new element to insert into this zipper. - * @return A new zipper with the given element in focus, and the current focus element on its left. - */ - public Zipper insertRight(final A a) { - return zipper(left.cons(focus), a, right); - } - - /** - * Possibly deletes the element at the focus, then moves the element on the left into focus. - * If no element is on the left, focus on the element to the right. - * Returns None if the focus element is the only element in this zipper. - * - * @return A new zipper with this zipper's focus element removed, or None if deleting the focus element - * would cause the zipper to be empty. - */ - public Option> deleteLeft() { - return left.isEmpty() && right.isEmpty() - ? Option.none() - : some(zipper(left.isEmpty() ? left : left.tail()._1(), - left.isEmpty() ? right.head() : left.head(), - left.isEmpty() ? right.tail()._1() : right)); - } - - /** - * Possibly deletes the element at the focus, then moves the element on the right into focus. - * If no element is on the right, focus on the element to the left. - * Returns None if the focus element is the only element in this zipper. - * - * @return A new zipper with this zipper's focus element removed, or None if deleting the focus element - * would cause the zipper to be empty. - */ - public Option> deleteRight() { - return left.isEmpty() && right.isEmpty() - ? Option.none() - : some(zipper(right.isEmpty() ? left.tail()._1() : left, - right.isEmpty() ? left.head() : right.head(), - right.isEmpty() ? right : right.tail()._1())); - } - - /** - * Deletes all elements in the zipper except the focus. - * - * @return A new zipper with the focus element as the only element. - */ - public Zipper deleteOthers() { - final Stream nil = nil(); - return zipper(nil, focus, nil); - } - - /** - * Returns the length of this zipper. - * - * @return the length of this zipper. - */ - public int length() { - return foldRight(Function.constant(Integers.add.f(1)), 0); - } - - /** - * Returns whether the focus is on the first element. - * - * @return true if the focus is on the first element, otherwise false. - */ - public boolean atStart() { - return left.isEmpty(); - } - - /** - * Returns whether the focus is on the last element. - * - * @return true if the focus is on the last element, otherwise false. - */ - public boolean atEnd() { - return right.isEmpty(); - } - - /** - * Creates a zipper of variations of this zipper, in which each element is focused, - * with this zipper as the focus of the zipper of zippers (comonad pattern). - * - * @return a zipper of variations of the provided zipper, in which each element is focused, - * with this zipper as the focus of the zipper of zippers. - */ - public Zipper> positions() { - final Stream> left = Stream.unfold( - p -> p.previous().map(join(P.p2())), this); - final Stream> right = Stream.unfold( - p -> p.next().map(join(P.p2())), this); - - return zipper(left, this, right); - } - - /** - * Maps over variations of this zipper, such that the given function is applied to each variation (comonad pattern). - * - * @param f The comonadic function to apply for each variation of this zipper. - * @return A new zipper, with the given function applied for each variation of this zipper. - */ - public Zipper cobind(final F, B> f) { - return positions().map(f); - } - - /** - * Zips the elements of this zipper with a boolean that indicates whether that element has focus. - * All of the booleans will be false, except the focused element. - * - * @return A new zipper of pairs, with each element of this zipper paired with a boolean that is true if that - * element has focus, and false otherwise. - */ - public Zipper> zipWithFocus() { - return zipper(left.zip(repeat(false)), P.p(focus, true), right.zip(repeat(false))); - } - - /** - * Move the focus to the specified index. - * - * @param n The index to which to move the focus. - * @return A new zipper with the focus moved to the specified index, or none if there is no such index. - */ - public Option> move(final int n) { - final int ll = left.length(); - final int rl = right.length(); - Option> p = some(this); - if (n < 0 || n >= length()) - return none(); - else if (ll >= n) - for (int i = ll - n; i > 0; i--) - p = p.bind(Zipper.previous_()); - else if (rl >= n) - for (int i = rl - n; i > 0; i--) - p = p.bind(Zipper.next_()); - return p; - } - - /** - * A first-class version of the move function. - * - * @return A function that moves the focus of the given zipper to the given index. - */ - public static F, Option>>> move() { - return curry((i, a) -> a.move(i)); - } - - /** - * Moves the focus to the element matching the given predicate, if present. - * - * @param p A predicate to match. - * @return A new zipper with the nearest matching element focused if it is present in this zipper. - */ - public Option> find(final F p) { - if (p.f(focus())) - return some(this); - else { - final Zipper> ps = positions(); - return ps.lefts().interleave(ps.rights()).find(zipper -> p.f(zipper.focus())); - } - } - - /** - * Returns the index of the focus. - * - * @return the index of the focus. - */ - public int index() { - return left.length(); - } - - /** - * Move the focus to the next element. If the last element is focused, loop to the first element. - * - * @return A new zipper with the next element focused, unless the last element is currently focused, in which case - * the first element becomes focused. - */ - public Zipper cycleNext() { - if (left.isEmpty() && right.isEmpty()) - return this; - else if (right.isEmpty()) { - final Stream xs = left.reverse(); - return zipper(Stream.nil(), xs.head(), xs.tail()._1().snoc(P.p(focus))); - } else - return tryNext(); - } - - /** - * Move the focus to the previous element. If the first element is focused, loop to the last element. - * - * @return A new zipper with the previous element focused, unless the first element is currently focused, - * in which case the last element becomes focused. - */ - public Zipper cyclePrevious() { - if (left.isEmpty() && right.isEmpty()) - return this; - else if (left.isEmpty()) { - final Stream xs = right.reverse(); - return zipper(xs.tail()._1().snoc(P.p(focus)), xs.head(), Stream.nil()); - } else - return tryPrevious(); - } - - /** - * Possibly deletes the element at the focus, then move the element on the left into focus. If no element is on the - * left, focus on the last element. If the deletion will cause the list to be empty, return None. - * - * @return A new zipper with the focused element removed, and focus on the previous element to the left, or the last - * element if there is no element to the left. - */ - public Option> deleteLeftCycle() { - if (left.isEmpty() && right.isEmpty()) - return none(); - else if (left.isNotEmpty()) - return some(zipper(left.tail()._1(), left.head(), right)); - else { - final Stream xs = right.reverse(); - return some(zipper(xs.tail()._1(), xs.head(), Stream.nil())); - } - } - - /** - * Possibly deletes the element at the focus, then move the element on the right into focus. If no element is on the - * right, focus on the first element. If the deletion will cause the list to be empty, return None. - * - * @return A new zipper with the focused element removed, and focus on the next element to the right, or the first - * element if there is no element to the right. - */ - public Option> deleteRightCycle() { - if (left.isEmpty() && right.isEmpty()) - return none(); - else if (right.isNotEmpty()) - return some(zipper(left, right.head(), right.tail()._1())); - else { - final Stream xs = left.reverse(); - return some(zipper(Stream.nil(), xs.head(), xs.tail()._1())); - } - } - - /** - * Replaces the element in focus with the given element. - * - * @param a An element to replace the focused element with. - * @return A new zipper with the given element in focus. - */ - public Zipper replace(final A a) { - return zipper(left, a, right); - } - - /** - * Returns the Stream representation of this zipper. - * - * @return A stream that contains all the elements of this zipper. - */ - public Stream toStream() { - return left.reverse().snoc(P.p(focus)).append(right); - } - - /** - * Returns a Stream of the elements to the left of focus. - * - * @return a Stream of the elements to the left of focus. - */ - public Stream lefts() { - return left; - } - - /** - * Returns a Stream of the elements to the right of focus. - * - * @return a Stream of the elements to the right of focus. - */ - public Stream rights() { - return right; - } - - /** - * Zips this Zipper with another, applying the given function lock-step over both zippers in both directions. - * The structure of the resulting Zipper is the structural intersection of the two Zippers. - * - * @param bs A Zipper to zip this one with. - * @param f A function with which to zip together the two Zippers. - * @return The result of applying the given function over this Zipper and the given Zipper, location-wise. - */ - public Zipper zipWith(final Zipper bs, final F2 f) { - return F2Functions.zipZipperM(f).f(this, bs); - } - - - /** - * Zips this Zipper with another, applying the given function lock-step over both zippers in both directions. - * The structure of the resulting Zipper is the structural intersection of the two Zippers. - * - * @param bs A Zipper to zip this one with. - * @param f A function with which to zip together the two Zippers. - * @return The result of applying the given function over this Zipper and the given Zipper, location-wise. - */ - public Zipper zipWith(final Zipper bs, final F> f) { - return zipWith(bs, uncurryF2(f)); - } - - /** - * Returns an iterator of all the positions of this Zipper, starting from the leftmost position. - * - * @return An iterator of all the positions of this Zipper, starting from the leftmost position. - */ - public Iterator> iterator() { return positions().toStream().iterator(); } -} +package fj.data; + +import fj.*; +import fj.function.Integers; + +import java.util.Iterator; + +import static fj.Function.compose; +import static fj.Function.curry; +import static fj.Function.flip; +import static fj.Function.join; +import static fj.Function.uncurryF2; +import static fj.data.Option.none; +import static fj.data.Option.some; +import static fj.data.Stream.nil; +import static fj.data.Stream.repeat; + +/** + * Provides a pointed stream, which is a non-empty zipper-like stream structure that tracks an index (focus) + * position in a stream. Focus can be moved forward and backwards through the stream, elements can be inserted + * before or after the focused position, and the focused item can be deleted. + *

    + * Based on the pointedlist library by Jeff Wheeler. + */ +public final class Zipper implements Iterable> { + private final Stream left; + private final A focus; + private final Stream right; + + private Zipper(final Stream left, final A focus, final Stream right) { + this.left = left; + this.focus = focus; + this.right = right; + } + + + /** + * Creates a new Zipper with the given streams before and after the focus, and the given focused item. + * + * @param left The stream of elements before the focus. + * @param focus The element under focus. + * @param right The stream of elements after the focus. + * @return a new Zipper with the given streams before and after the focus, and the given focused item. + */ + public static Zipper zipper(final Stream left, final A focus, final Stream right) { + return new Zipper<>(left, focus, right); + } + + /** + * Creates a new Zipper from the given triple. + * + * @param p A triple of the elements before the focus, the focus element, and the elements after the focus, + * respectively. + * @return a new Zipper created from the given triple. + */ + public static Zipper zipper(final P3, A, Stream> p) { + return new Zipper<>(p._1(), p._2(), p._3()); + } + + /** + * First-class constructor of zippers. + * + * @return A function that yields a new zipper given streams on the left and right and a focus element. + */ + public static F3, A, Stream, Zipper> zipper() { + return Zipper::zipper; + } + + /** + * Returns the product-3 representation of this Zipper. + * + * @return the product-3 representation of this Zipper. + */ + public P3, A, Stream> p() { + return P.p(left, focus, right); + } + + /** + * A first-class function that yields the product-3 representation of a given Zipper. + * + * @return A first-class function that yields the product-3 representation of a given Zipper. + */ + public static F, P3, A, Stream>> p_() { + return Zipper::p; + } + + /** + * An Ord instance for Zippers. + * + * @param o An Ord instance for the element type. + * @return An Ord instance for Zippers. + */ + public static Ord> ord(final Ord o) { + final Ord> so = Ord.streamOrd(o); + return Ord.p3Ord(so, o, so).contramap(Zipper.p_()); + } + + @Override + public final boolean equals(Object other) { + return Equal.equals0(Zipper.class, this, other, () -> Equal.zipperEqual(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.zipperHash(Hash.anyHash()).hash(this); + } + + /** + * An Equal instance for Zippers. + * + * @param e An Equal instance for the element type. + * @return An Equal instance for Zippers. + */ + public static Equal> eq(final Equal e) { + final Equal> se = Equal.streamEqual(e); + return Equal.p3Equal(se, e, se).contramap(Zipper.p_()); + } + + /** + * A Show instance for Zippers. + * + * @param s A Show instance for the element type. + * @return A Show instance for Zippers. + */ + public static Show> show(final Show s) { + final Show> ss = Show.streamShow(s); + return Show.p3Show(ss, s, ss).contramap(Zipper.p_()); + } + + /** + * Maps the given function across the elements of this zipper (covariant functor pattern). + * + * @param f A function to map across this zipper. + * @return A new zipper with the given function applied to all elements. + */ + public Zipper map(final F f) { + return zipper(left.map(f), f.f(focus), right.map(f)); + } + + /** + * Performs a right-fold reduction across this zipper. + * + * @param f The function to apply on each element of this zipper. + * @param z The beginning value to start the application from. + * @return the final result after the right-fold reduction. + */ + public B foldRight(final F> f, final B z) { + return left.foldLeft(flip(f), + right.cons(focus).foldRight(compose( + Function., B, B>andThen().f(P1.__1()), f), z)); + } + + /** + * Creates a new zipper with a single element. + * + * @param a The focus element of the new zipper. + * @return a new zipper with a single element which is in focus. + */ + public static Zipper single(final A a) { + return zipper(Stream.nil(), a, Stream.nil()); + } + + /** + * Possibly create a zipper if the provided stream has at least one element, otherwise None. + * The provided stream's head will be the focus of the zipper, and the rest of the stream will follow + * on the right side. + * + * @param a The stream from which to create a zipper. + * @return a new zipper if the provided stream has at least one element, otherwise None. + */ + @SuppressWarnings("IfMayBeConditional") + public static Option> fromStream(final Stream a) { + if (a.isEmpty()) + return none(); + else + return some(zipper(Stream.nil(), a.head(), a.tail()._1())); + } + + /** + * Possibly create a zipper if the provided stream has at least one element, otherwise None. + * The provided stream's last element will be the focus of the zipper, following the rest of the stream in order, + * to the left. + * + * @param a The stream from which to create a zipper. + * @return a new zipper if the provided stream has at least one element, otherwise None. + */ + public static Option> fromStreamEnd(final Stream a) { + if (a.isEmpty()) + return none(); + else { + final Stream xs = a.reverse(); + return some(zipper(xs.tail()._1(), xs.head(), Stream.nil())); + } + } + + /** + * Returns the focus element of this zipper. + * + * @return the focus element of this zipper. + */ + public A focus() { + return focus; + } + + /** + * Possibly moves the focus to the next element in the list. + * + * @return An optional zipper with the focus moved one element to the right, if there are elements to the right of + * focus, otherwise None. + */ + public Option> next() { + return right.isEmpty() ? Option.none() : some(tryNext()); + } + + /** + * Attempts to move the focus to the next element, or throws an error if there are no more elements. + * + * @return A zipper with the focus moved one element to the right, if there are elements to the right of + * focus, otherwise throws an error. + */ + public Zipper tryNext() { + if (right.isEmpty()) + throw new Error("Tried next at the end of a zipper."); + else + return zipper(left.cons(focus), right.head(), right.tail()._1()); + } + + /** + * Possibly moves the focus to the previous element in the list. + * + * @return An optional zipper with the focus moved one element to the left, if there are elements to the left of + * focus, otherwise None. + */ + public Option> previous() { + return left.isEmpty() ? Option.none() : some(tryPrevious()); + } + + /** + * Attempts to move the focus to the previous element, or throws an error if there are no more elements. + * + * @return A zipper with the focus moved one element to the left, if there are elements to the left of + * focus, otherwise throws an error. + */ + public Zipper tryPrevious() { + if (left.isEmpty()) + throw new Error("Tried previous at the beginning of a zipper."); + else + return zipper(left.tail()._1(), left.head(), right.cons(focus)); + } + + /** + * First-class version of the next() function. + * + * @return A function that moves the given zipper's focus to the next element. + */ + public static F, Option>> next_() { + return Zipper::next; + } + + /** + * First-class version of the previous() function. + * + * @return A function that moves the given zipper's focus to the previous element. + */ + public static F, Option>> previous_() { + return Zipper::previous; + } + + /** + * Inserts an element to the left of the focus, then moves the focus to the new element. + * + * @param a A new element to insert into this zipper. + * @return A new zipper with the given element in focus, and the current focus element on its right. + */ + public Zipper insertLeft(final A a) { + return zipper(left, a, right.cons(focus)); + } + + /** + * Inserts an element to the right of the focus, then moves the focus to the new element. + * + * @param a A new element to insert into this zipper. + * @return A new zipper with the given element in focus, and the current focus element on its left. + */ + public Zipper insertRight(final A a) { + return zipper(left.cons(focus), a, right); + } + + /** + * Possibly deletes the element at the focus, then moves the element on the left into focus. + * If no element is on the left, focus on the element to the right. + * Returns None if the focus element is the only element in this zipper. + * + * @return A new zipper with this zipper's focus element removed, or None if deleting the focus element + * would cause the zipper to be empty. + */ + public Option> deleteLeft() { + return left.isEmpty() && right.isEmpty() + ? Option.none() + : some(zipper(left.isEmpty() ? left : left.tail()._1(), + left.isEmpty() ? right.head() : left.head(), + left.isEmpty() ? right.tail()._1() : right)); + } + + /** + * Possibly deletes the element at the focus, then moves the element on the right into focus. + * If no element is on the right, focus on the element to the left. + * Returns None if the focus element is the only element in this zipper. + * + * @return A new zipper with this zipper's focus element removed, or None if deleting the focus element + * would cause the zipper to be empty. + */ + public Option> deleteRight() { + return left.isEmpty() && right.isEmpty() + ? Option.none() + : some(zipper(right.isEmpty() ? left.tail()._1() : left, + right.isEmpty() ? left.head() : right.head(), + right.isEmpty() ? right : right.tail()._1())); + } + + /** + * Deletes all elements in the zipper except the focus. + * + * @return A new zipper with the focus element as the only element. + */ + public Zipper deleteOthers() { + final Stream nil = nil(); + return zipper(nil, focus, nil); + } + + /** + * Returns the length of this zipper. + * + * @return the length of this zipper. + */ + public int length() { + return foldRight(Function.constant(Integers.add.f(1)), 0); + } + + /** + * Returns whether the focus is on the first element. + * + * @return true if the focus is on the first element, otherwise false. + */ + public boolean atStart() { + return left.isEmpty(); + } + + /** + * Returns whether the focus is on the last element. + * + * @return true if the focus is on the last element, otherwise false. + */ + public boolean atEnd() { + return right.isEmpty(); + } + + /** + * Creates a zipper of variations of this zipper, in which each element is focused, + * with this zipper as the focus of the zipper of zippers (comonad pattern). + * + * @return a zipper of variations of the provided zipper, in which each element is focused, + * with this zipper as the focus of the zipper of zippers. + */ + public Zipper> positions() { + final Stream> left = Stream.unfold( + p -> p.previous().map(join(P.p2())), this); + final Stream> right = Stream.unfold( + p -> p.next().map(join(P.p2())), this); + + return zipper(left, this, right); + } + + /** + * Maps over variations of this zipper, such that the given function is applied to each variation (comonad pattern). + * + * @param f The comonadic function to apply for each variation of this zipper. + * @return A new zipper, with the given function applied for each variation of this zipper. + */ + public Zipper cobind(final F, B> f) { + return positions().map(f); + } + + /** + * Zips the elements of this zipper with a boolean that indicates whether that element has focus. + * All of the booleans will be false, except the focused element. + * + * @return A new zipper of pairs, with each element of this zipper paired with a boolean that is true if that + * element has focus, and false otherwise. + */ + public Zipper> zipWithFocus() { + return zipper(left.zip(repeat(false)), P.p(focus, true), right.zip(repeat(false))); + } + + /** + * Move the focus to the specified index. + * + * @param n The index to which to move the focus. + * @return A new zipper with the focus moved to the specified index, or none if there is no such index. + */ + public Option> move(final int n) { + final int ll = left.length(); + final int rl = right.length(); + Option> p = some(this); + if (n < 0 || n >= length()) + return none(); + else if (ll >= n) + for (int i = ll - n; i > 0; i--) + p = p.bind(Zipper.previous_()); + else if (rl >= n) + for (int i = rl - n; i > 0; i--) + p = p.bind(Zipper.next_()); + return p; + } + + /** + * A first-class version of the move function. + * + * @return A function that moves the focus of the given zipper to the given index. + */ + public static F, Option>>> move() { + return curry((i, a) -> a.move(i)); + } + + /** + * Moves the focus to the element matching the given predicate, if present. + * + * @param p A predicate to match. + * @return A new zipper with the nearest matching element focused if it is present in this zipper. + */ + public Option> find(final F p) { + if (p.f(focus())) + return some(this); + else { + final Zipper> ps = positions(); + return ps.lefts().interleave(ps.rights()).find(zipper -> p.f(zipper.focus())); + } + } + + /** + * Returns the index of the focus. + * + * @return the index of the focus. + */ + public int index() { + return left.length(); + } + + /** + * Move the focus to the next element. If the last element is focused, loop to the first element. + * + * @return A new zipper with the next element focused, unless the last element is currently focused, in which case + * the first element becomes focused. + */ + public Zipper cycleNext() { + if (left.isEmpty() && right.isEmpty()) + return this; + else if (right.isEmpty()) { + final Stream xs = left.reverse(); + return zipper(Stream.nil(), xs.head(), xs.tail()._1().snoc(P.p(focus))); + } else + return tryNext(); + } + + /** + * Move the focus to the previous element. If the first element is focused, loop to the last element. + * + * @return A new zipper with the previous element focused, unless the first element is currently focused, + * in which case the last element becomes focused. + */ + public Zipper cyclePrevious() { + if (left.isEmpty() && right.isEmpty()) + return this; + else if (left.isEmpty()) { + final Stream xs = right.reverse(); + return zipper(xs.tail()._1().snoc(P.p(focus)), xs.head(), Stream.nil()); + } else + return tryPrevious(); + } + + /** + * Possibly deletes the element at the focus, then move the element on the left into focus. If no element is on the + * left, focus on the last element. If the deletion will cause the list to be empty, return None. + * + * @return A new zipper with the focused element removed, and focus on the previous element to the left, or the last + * element if there is no element to the left. + */ + public Option> deleteLeftCycle() { + if (left.isEmpty() && right.isEmpty()) + return none(); + else if (left.isNotEmpty()) + return some(zipper(left.tail()._1(), left.head(), right)); + else { + final Stream xs = right.reverse(); + return some(zipper(xs.tail()._1(), xs.head(), Stream.nil())); + } + } + + /** + * Possibly deletes the element at the focus, then move the element on the right into focus. If no element is on the + * right, focus on the first element. If the deletion will cause the list to be empty, return None. + * + * @return A new zipper with the focused element removed, and focus on the next element to the right, or the first + * element if there is no element to the right. + */ + public Option> deleteRightCycle() { + if (left.isEmpty() && right.isEmpty()) + return none(); + else if (right.isNotEmpty()) + return some(zipper(left, right.head(), right.tail()._1())); + else { + final Stream xs = left.reverse(); + return some(zipper(Stream.nil(), xs.head(), xs.tail()._1())); + } + } + + /** + * Replaces the element in focus with the given element. + * + * @param a An element to replace the focused element with. + * @return A new zipper with the given element in focus. + */ + public Zipper replace(final A a) { + return zipper(left, a, right); + } + + /** + * Returns the Stream representation of this zipper. + * + * @return A stream that contains all the elements of this zipper. + */ + public Stream toStream() { + return left.reverse().snoc(P.p(focus)).append(right); + } + + /** + * Returns a Stream of the elements to the left of focus. + * + * @return a Stream of the elements to the left of focus. + */ + public Stream lefts() { + return left; + } + + /** + * Returns a Stream of the elements to the right of focus. + * + * @return a Stream of the elements to the right of focus. + */ + public Stream rights() { + return right; + } + + /** + * Zips this Zipper with another, applying the given function lock-step over both zippers in both directions. + * The structure of the resulting Zipper is the structural intersection of the two Zippers. + * + * @param bs A Zipper to zip this one with. + * @param f A function with which to zip together the two Zippers. + * @return The result of applying the given function over this Zipper and the given Zipper, location-wise. + */ + public Zipper zipWith(final Zipper bs, final F2 f) { + return f.zipZipperM().f(this, bs); + } + + + /** + * Zips this Zipper with another, applying the given function lock-step over both zippers in both directions. + * The structure of the resulting Zipper is the structural intersection of the two Zippers. + * + * @param bs A Zipper to zip this one with. + * @param f A function with which to zip together the two Zippers. + * @return The result of applying the given function over this Zipper and the given Zipper, location-wise. + */ + public Zipper zipWith(final Zipper bs, final F> f) { + return zipWith(bs, uncurryF2(f)); + } + + /** + * Returns an iterator of all the positions of this Zipper, starting from the leftmost position. + * + * @return An iterator of all the positions of this Zipper, starting from the leftmost position. + */ + public Iterator> iterator() { return positions().toStream().iterator(); } +} diff --git a/core/src/main/java/fj/data/fingertrees/FingerTree.java b/core/src/main/java/fj/data/fingertrees/FingerTree.java index 160b5508..ab0effb0 100644 --- a/core/src/main/java/fj/data/fingertrees/FingerTree.java +++ b/core/src/main/java/fj/data/fingertrees/FingerTree.java @@ -1,268 +1,268 @@ -package fj.data.fingertrees; - -import fj.*; -import fj.data.Option; -import fj.data.Seq; -import fj.data.Stream; - -import static fj.Monoid.intAdditionMonoid; -import static fj.Monoid.intMaxMonoid; -import static fj.data.Stream.nil; - -/** - * Provides 2-3 finger trees, a functional representation of persistent sequences supporting access to the ends in - * amortized O(1) time. Concatenation and splitting time is O(log n) in the size of the smaller piece. - * A general purpose data structure that can serve as a sequence, priority queue, search tree, priority search queue - * and more. - *

    - * This class serves as a datastructure construction kit, rather than a datastructure in its own right. By supplying - * a monoid, a measurement function, insertion, deletion, and so forth, any purely functional datastructure can be - * emulated. See {@link Seq} for an example. - *

    - * Based on "Finger trees: a simple general-purpose data structure", by Ralf Hinze and Ross Paterson. - * - * @param The monoidal type with which to annotate nodes. - * @param The type of the tree's elements. - */ -public abstract class FingerTree { - private final Measured m; - - /** - * Folds the tree to the right with the given function and the given initial element. - * - * @param f A function with which to fold the tree. - * @param z An initial element to apply to the fold. - * @return A reduction of this tree by applying the given function, associating to the right. - */ - public abstract B foldRight(final F> f, final B z); - - public final B foldRight(final F2 f, final B z) { - return foldRight(F2Functions.curry(f), z); - } - - /** - * Folds the tree to the right with the given function. - * - * @param f A function with which to fold the tree. - * @return A reduction of this tree by applying the given function, associating to the right. - */ - public abstract A reduceRight(final F> f); - - /** - * Folds the tree to the left with the given function and the given initial element. - * - * @param f A function with which to fold the tree. - * @param z An initial element to apply to the fold. - * @return A reduction of this tree by applying the given function, associating to the left. - */ - public abstract B foldLeft(final F> f, final B z); - - public final B foldLeft(final F2 f, final B z) { - return foldLeft(F2Functions.curry(f), z); - } - - /** - * Folds the tree to the left with the given function. - * - * @param f A function with which to fold the tree. - * @return A reduction of this tree by applying the given function, associating to the right. - */ - public abstract A reduceLeft(final F> f); - - /** - * Maps the given function across this tree, measuring with the given Measured instance. - * - * @param f A function to map across the values of this tree. - * @param m A measuring with which to annotate the tree. - * @return A new tree with the same structure as this tree, with each element transformed by the given function, - * and nodes annotated according to the given measuring. - */ - public abstract FingerTree map(final F f, final Measured m); - - public final FingerTree filter(final F f) { - FingerTree tree = new Empty<>(m); - return foldLeft((acc, a) -> f.f(a) ? acc.snoc(a) : acc, tree); - } - - /** - * Returns the sum of this tree's annotations. - * - * @return the sum of this tree's annotations. - */ - public abstract V measure(); - - /** - * Indicates whether this tree is empty. - * - * @return true if this tree is the empty tree, otherwise false. - */ - public final boolean isEmpty() { - return this instanceof Empty; - } - - public final Measured measured() { - return m; - } - - /** - * Provides pattern matching on trees. This is the Church encoding of the FingerTree datatype. - * - * @param empty The function to apply to this empty tree. - * @param single A function to apply if this tree contains a single element. - * @param deep A function to apply if this tree contains more than one element. - * @return The result of the function that matches this tree structurally, applied to this tree. - */ - public abstract B match(final F, B> empty, final F, B> single, - final F, B> deep); - - FingerTree(final Measured m) { - this.m = m; - } - - /** - * Constructs a Measured instance for the element type, given a monoid and a measuring function. - * - * @param monoid A monoid for the measures. - * @param measure A function with which to measure element values. - * @return A Measured instance for the given element type, that uses the given monoid and measuring function. - */ - public static Measured measured(final Monoid monoid, final F measure) { - return Measured.measured(monoid, measure); - } - - /** - * Returns a builder of trees and tree components that annotates them using the given Measured instance. - * - * @param m A Measured instance with which to annotate trees, digits, and nodes. - * @return A builder of trees and tree components that annotates them using the given Measured instance. - */ - public static MakeTree mkTree(final Measured m) { - return new MakeTree<>(m); - } - - /** - * Adds the given element to this tree as the first element. - * - * @param a The element to add to the front of this tree. - * @return A new tree with the given element at the front. - */ - public abstract FingerTree cons(final A a); - - /** - * Adds the given element to this tree as the last element. - * - * @param a The element to add to the end of this tree. - * @return A new tree with the given element at the end. - */ - public abstract FingerTree snoc(final A a); - - /** - * The first element of this tree. This is an O(1) operation. - * - * @return The first element if this tree is nonempty, otherwise throws an error. - */ - public abstract A head(); - - public final Option headOption() { - return isEmpty() ? Option.none() : Option.some(head()); - } - - /** - * Performs a reduction on this finger tree using the given arguments. - * - * @param nil The value to return if this finger tree is empty. - * @param cons The function to apply to the head and tail of this finger tree if it is not empty. - * @return A reduction on this finger tree. - */ - public final B uncons(B nil, F2, B> cons) { - return isEmpty() ? nil : cons.f(head(), tail()); - } - - - /** - * The last element of this tree. This is an O(1) operation. - * - * @return The last element if this tree is nonempty, otherwise throws an error. - */ - public abstract A last(); - - /** - * The tree without the first element. This is an O(1) operation. - * - * @return The tree without the first element if this tree is nonempty, otherwise throws an error. - */ - public abstract FingerTree tail(); - - /** - * The tree without the last element. This is an O(1) operation. - * - * @return The tree without the last element if this tree is nonempty, otherwise throws an error. - */ - public abstract FingerTree init(); - - /** - * Appends one finger tree to another. - * - * @param t A finger tree to append to this one. - * @return A new finger tree which is a concatenation of this tree and the given tree. - */ - public abstract FingerTree append(final FingerTree t); - - /** - * Splits this tree into a pair of subtrees at the point where the given predicate, based on the measure, - * changes from false to true. This is a O(log(n)) operation. - * - * @return Pair: the subtree containing elements before the point where pred first holds and the subtree - * containing element at and after the point where pred first holds. Empty if pred never holds. - */ - public final P2, FingerTree> split(final F predicate) { - if (!isEmpty() && predicate.f(measure())) { - final P3, A, FingerTree> lxr = split1(predicate); - return P.p(lxr._1(), lxr._3().cons(lxr._2())); - } else { - return P.p(this, mkTree(m).empty()); - } - } - - /** - * Like split, but returns the element where pred first holds separately. - * - * Throws an error if the tree is empty. - */ - public final P3, A, FingerTree> split1(final F predicate) { - return split1(predicate, measured().zero()); - } - - abstract P3, A, FingerTree> split1(final F predicate, final V acc); - - public abstract P2 lookup(final F o, final int i); - - public abstract int length(); - - public static FingerTree emptyIntAddition() { - return empty(intAdditionMonoid, Function.constant(1)); - } - - /** - * Creates an empty finger tree with elements of type A and node annotations - * of type V. - * - * @param m A monoid to combine node annotations - * @param f Function to convert node element to annotation. - * @return An empty finger tree. - */ - public static FingerTree empty(Monoid m, F f) { - return FingerTree.mkTree(measured(m, f)).empty(); - } - - /** - * Returns a finger tree which combines the integer node annotations with the - * maximum function. A priority queue with integer priorities. - */ - public static FingerTree> emptyIntMax() { - return empty(intMaxMonoid, (P2 p) -> p._1()); - } - - public abstract Stream toStream(); - -} +package fj.data.fingertrees; + +import fj.*; +import fj.data.Option; +import fj.data.Seq; +import fj.data.Stream; + +import static fj.Monoid.intAdditionMonoid; +import static fj.Monoid.intMaxMonoid; +import static fj.data.Stream.nil; + +/** + * Provides 2-3 finger trees, a functional representation of persistent sequences supporting access to the ends in + * amortized O(1) time. Concatenation and splitting time is O(log n) in the size of the smaller piece. + * A general purpose data structure that can serve as a sequence, priority queue, search tree, priority search queue + * and more. + *

    + * This class serves as a datastructure construction kit, rather than a datastructure in its own right. By supplying + * a monoid, a measurement function, insertion, deletion, and so forth, any purely functional datastructure can be + * emulated. See {@link Seq} for an example. + *

    + * Based on "Finger trees: a simple general-purpose data structure", by Ralf Hinze and Ross Paterson. + * + * @param The monoidal type with which to annotate nodes. + * @param The type of the tree's elements. + */ +public abstract class FingerTree { + private final Measured m; + + /** + * Folds the tree to the right with the given function and the given initial element. + * + * @param f A function with which to fold the tree. + * @param z An initial element to apply to the fold. + * @return A reduction of this tree by applying the given function, associating to the right. + */ + public abstract B foldRight(final F> f, final B z); + + public final B foldRight(final F2 f, final B z) { + return foldRight(f.curry(), z); + } + + /** + * Folds the tree to the right with the given function. + * + * @param f A function with which to fold the tree. + * @return A reduction of this tree by applying the given function, associating to the right. + */ + public abstract A reduceRight(final F> f); + + /** + * Folds the tree to the left with the given function and the given initial element. + * + * @param f A function with which to fold the tree. + * @param z An initial element to apply to the fold. + * @return A reduction of this tree by applying the given function, associating to the left. + */ + public abstract B foldLeft(final F> f, final B z); + + public final B foldLeft(final F2 f, final B z) { + return foldLeft(f.curry(), z); + } + + /** + * Folds the tree to the left with the given function. + * + * @param f A function with which to fold the tree. + * @return A reduction of this tree by applying the given function, associating to the right. + */ + public abstract A reduceLeft(final F> f); + + /** + * Maps the given function across this tree, measuring with the given Measured instance. + * + * @param f A function to map across the values of this tree. + * @param m A measuring with which to annotate the tree. + * @return A new tree with the same structure as this tree, with each element transformed by the given function, + * and nodes annotated according to the given measuring. + */ + public abstract FingerTree map(final F f, final Measured m); + + public final FingerTree filter(final F f) { + FingerTree tree = new Empty<>(m); + return foldLeft((acc, a) -> f.f(a) ? acc.snoc(a) : acc, tree); + } + + /** + * Returns the sum of this tree's annotations. + * + * @return the sum of this tree's annotations. + */ + public abstract V measure(); + + /** + * Indicates whether this tree is empty. + * + * @return true if this tree is the empty tree, otherwise false. + */ + public final boolean isEmpty() { + return this instanceof Empty; + } + + public final Measured measured() { + return m; + } + + /** + * Provides pattern matching on trees. This is the Church encoding of the FingerTree datatype. + * + * @param empty The function to apply to this empty tree. + * @param single A function to apply if this tree contains a single element. + * @param deep A function to apply if this tree contains more than one element. + * @return The result of the function that matches this tree structurally, applied to this tree. + */ + public abstract B match(final F, B> empty, final F, B> single, + final F, B> deep); + + FingerTree(final Measured m) { + this.m = m; + } + + /** + * Constructs a Measured instance for the element type, given a monoid and a measuring function. + * + * @param monoid A monoid for the measures. + * @param measure A function with which to measure element values. + * @return A Measured instance for the given element type, that uses the given monoid and measuring function. + */ + public static Measured measured(final Monoid monoid, final F measure) { + return Measured.measured(monoid, measure); + } + + /** + * Returns a builder of trees and tree components that annotates them using the given Measured instance. + * + * @param m A Measured instance with which to annotate trees, digits, and nodes. + * @return A builder of trees and tree components that annotates them using the given Measured instance. + */ + public static MakeTree mkTree(final Measured m) { + return new MakeTree<>(m); + } + + /** + * Adds the given element to this tree as the first element. + * + * @param a The element to add to the front of this tree. + * @return A new tree with the given element at the front. + */ + public abstract FingerTree cons(final A a); + + /** + * Adds the given element to this tree as the last element. + * + * @param a The element to add to the end of this tree. + * @return A new tree with the given element at the end. + */ + public abstract FingerTree snoc(final A a); + + /** + * The first element of this tree. This is an O(1) operation. + * + * @return The first element if this tree is nonempty, otherwise throws an error. + */ + public abstract A head(); + + public final Option headOption() { + return isEmpty() ? Option.none() : Option.some(head()); + } + + /** + * Performs a reduction on this finger tree using the given arguments. + * + * @param nil The value to return if this finger tree is empty. + * @param cons The function to apply to the head and tail of this finger tree if it is not empty. + * @return A reduction on this finger tree. + */ + public final B uncons(B nil, F2, B> cons) { + return isEmpty() ? nil : cons.f(head(), tail()); + } + + + /** + * The last element of this tree. This is an O(1) operation. + * + * @return The last element if this tree is nonempty, otherwise throws an error. + */ + public abstract A last(); + + /** + * The tree without the first element. This is an O(1) operation. + * + * @return The tree without the first element if this tree is nonempty, otherwise throws an error. + */ + public abstract FingerTree tail(); + + /** + * The tree without the last element. This is an O(1) operation. + * + * @return The tree without the last element if this tree is nonempty, otherwise throws an error. + */ + public abstract FingerTree init(); + + /** + * Appends one finger tree to another. + * + * @param t A finger tree to append to this one. + * @return A new finger tree which is a concatenation of this tree and the given tree. + */ + public abstract FingerTree append(final FingerTree t); + + /** + * Splits this tree into a pair of subtrees at the point where the given predicate, based on the measure, + * changes from false to true. This is a O(log(n)) operation. + * + * @return Pair: the subtree containing elements before the point where pred first holds and the subtree + * containing element at and after the point where pred first holds. Empty if pred never holds. + */ + public final P2, FingerTree> split(final F predicate) { + if (!isEmpty() && predicate.f(measure())) { + final P3, A, FingerTree> lxr = split1(predicate); + return P.p(lxr._1(), lxr._3().cons(lxr._2())); + } else { + return P.p(this, mkTree(m).empty()); + } + } + + /** + * Like split, but returns the element where pred first holds separately. + * + * Throws an error if the tree is empty. + */ + public final P3, A, FingerTree> split1(final F predicate) { + return split1(predicate, measured().zero()); + } + + abstract P3, A, FingerTree> split1(final F predicate, final V acc); + + public abstract P2 lookup(final F o, final int i); + + public abstract int length(); + + public static FingerTree emptyIntAddition() { + return empty(intAdditionMonoid, Function.constant(1)); + } + + /** + * Creates an empty finger tree with elements of type A and node annotations + * of type V. + * + * @param m A monoid to combine node annotations + * @param f Function to convert node element to annotation. + * @return An empty finger tree. + */ + public static FingerTree empty(Monoid m, F f) { + return FingerTree.mkTree(measured(m, f)).empty(); + } + + /** + * Returns a finger tree which combines the integer node annotations with the + * maximum function. A priority queue with integer priorities. + */ + public static FingerTree> emptyIntMax() { + return empty(intMaxMonoid, (P2 p) -> p._1()); + } + + public abstract Stream toStream(); + +} diff --git a/core/src/main/java/fj/data/hamt/BitSet.java b/core/src/main/java/fj/data/hamt/BitSet.java index d164642d..9efbab34 100644 --- a/core/src/main/java/fj/data/hamt/BitSet.java +++ b/core/src/main/java/fj/data/hamt/BitSet.java @@ -13,8 +13,6 @@ * For example, the BitSet("1011") represents the decimal number 11 and has * indices [3, 0] inclusive where the bit with the lowest value has the lowest * index and is the rightmost bit. - * - * Created by maperr on 31/05/2016. */ public final class BitSet { diff --git a/core/src/main/java/fj/data/hamt/HashArrayMappedTrie.java b/core/src/main/java/fj/data/hamt/HashArrayMappedTrie.java index f13ae90f..371f5435 100644 --- a/core/src/main/java/fj/data/hamt/HashArrayMappedTrie.java +++ b/core/src/main/java/fj/data/hamt/HashArrayMappedTrie.java @@ -22,8 +22,6 @@ * mapped trie. It is a refined version of the more general notion of * a hash tree. * - * @author Mark Perry - * * Based on "Ideal Hash Trees" by Phil Bagwell, available from * http://lampwww.epfl.ch/papers/idealhashtrees.pdf */ diff --git a/core/src/main/java/fj/data/hamt/Node.java b/core/src/main/java/fj/data/hamt/Node.java index f8a1ff40..af453925 100644 --- a/core/src/main/java/fj/data/hamt/Node.java +++ b/core/src/main/java/fj/data/hamt/Node.java @@ -10,8 +10,6 @@ /** * A Hash Array Mapped Trie node that is either a key-value pair or a * Hash Array Mapped Trie. - * - * Created by maperr on 31/05/2016. */ public final class Node { diff --git a/core/src/main/java/fj/data/package-info.java b/core/src/main/java/fj/data/package-info.java index 911ef492..ae9577a7 100644 --- a/core/src/main/java/fj/data/package-info.java +++ b/core/src/main/java/fj/data/package-info.java @@ -1,6 +1,4 @@ /** * Common algebraic data types. - * - * @version %build.number% */ package fj.data; diff --git a/core/src/main/java/fj/function/BigIntegers.java b/core/src/main/java/fj/function/BigIntegers.java index 30d07a10..ac6584fc 100644 --- a/core/src/main/java/fj/function/BigIntegers.java +++ b/core/src/main/java/fj/function/BigIntegers.java @@ -10,8 +10,6 @@ /** * Curried functions over Integers. - * - * @version %build.number% */ public final class BigIntegers { private BigIntegers() { diff --git a/core/src/main/java/fj/function/Booleans.java b/core/src/main/java/fj/function/Booleans.java index 6211e870..bef1b347 100644 --- a/core/src/main/java/fj/function/Booleans.java +++ b/core/src/main/java/fj/function/Booleans.java @@ -15,8 +15,6 @@ /** * Curried logical functions. - * - * @version %build.number% */ public final class Booleans { private Booleans() { diff --git a/core/src/main/java/fj/function/Doubles.java b/core/src/main/java/fj/function/Doubles.java index 6fe1b033..aefcbfdc 100644 --- a/core/src/main/java/fj/function/Doubles.java +++ b/core/src/main/java/fj/function/Doubles.java @@ -11,8 +11,6 @@ /** * Curried functions over Doubles. - * - * @version %build.number% */ public final class Doubles { private Doubles() { diff --git a/core/src/main/java/fj/function/Effect0.java b/core/src/main/java/fj/function/Effect0.java index c5d4fe50..7644d394 100644 --- a/core/src/main/java/fj/function/Effect0.java +++ b/core/src/main/java/fj/function/Effect0.java @@ -1,10 +1,36 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ +import fj.*; + +import static fj.Unit.unit; + public interface Effect0 { void f(); + default F0 toF0() { + return () -> { + f(); + return unit(); + }; + } + + default TryEffect0 toTryEffect0() { + return () -> f(); + } + + default Try0 toTry0() { + return () -> { + f(); + return unit(); + }; + } + + default P1 toP1() { + return P.lazy(() -> { + f(); + return unit(); + }); + } + } diff --git a/core/src/main/java/fj/function/Effect1.java b/core/src/main/java/fj/function/Effect1.java index 8c89b715..1f3be909 100644 --- a/core/src/main/java/fj/function/Effect1.java +++ b/core/src/main/java/fj/function/Effect1.java @@ -1,10 +1,76 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ -public interface Effect1 { +import fj.F; +import fj.P; +import fj.P1; +import fj.Unit; + +import java.util.function.Consumer; + +import static fj.Unit.unit; + +public interface Effect1 extends Consumer { void f(A a); + default void accept(A a) { + f(a); + } + + default F bind(final F> g) { + return a -> { + return g.f(toF().f(a)).f(a); + }; + } + + default void apply(A a) { + f(a); + } + + default Effect1 contramap(F f) { + return o(f); + } + + default F map(F f) { + return a -> f.f(toF().f(a)); + } + + default F andThen(final F f) { + return map(f); + } + + default Effect1 o(final F f) { + return c -> f(f.f(c)); + } + + default F toF() { + return a -> { + f(a); + return unit(); + }; + } + + default TryEffect1 toTryEffect1() { + return a -> f(a); + } + + + default Try1 toTry1() { + return a -> toF().f(a); + } + + static Effect1 fromF(F f) { + return a -> f.f(a); + } + + default F dimap(F f, F g) { + return c -> g.f(toF().f(f.f(c))); + } + + default P1 partial(final A a) { + return P.lazy(() -> toF().f(a)); + } + + + } diff --git a/core/src/main/java/fj/function/Effect2.java b/core/src/main/java/fj/function/Effect2.java index c0735043..b9a5ad5a 100644 --- a/core/src/main/java/fj/function/Effect2.java +++ b/core/src/main/java/fj/function/Effect2.java @@ -1,10 +1,25 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ -public interface Effect2 { +import fj.F2; +import fj.Unit; + +import java.util.function.BiConsumer; + +import static fj.Unit.unit; + +public interface Effect2 extends BiConsumer { void f(A a, B b); + default void accept(A a, B b) { + f(a, b); + } + + default F2 toF2() { + return (a, b) -> { + f(a, b); + return unit(); + }; + } + } diff --git a/core/src/main/java/fj/function/Effect3.java b/core/src/main/java/fj/function/Effect3.java index 0609584b..675d2301 100644 --- a/core/src/main/java/fj/function/Effect3.java +++ b/core/src/main/java/fj/function/Effect3.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface Effect3 { void f(A a, B b, C c); diff --git a/core/src/main/java/fj/function/Effect4.java b/core/src/main/java/fj/function/Effect4.java index b24a7624..fdd20af8 100644 --- a/core/src/main/java/fj/function/Effect4.java +++ b/core/src/main/java/fj/function/Effect4.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface Effect4 { void f(A a, B b, C c, D d); diff --git a/core/src/main/java/fj/function/Effect5.java b/core/src/main/java/fj/function/Effect5.java index 142be280..48634ac8 100644 --- a/core/src/main/java/fj/function/Effect5.java +++ b/core/src/main/java/fj/function/Effect5.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface Effect5 { void f(A a, B b, C c, D d, E e); diff --git a/core/src/main/java/fj/function/Effect6.java b/core/src/main/java/fj/function/Effect6.java index fe72314b..f81cdf25 100644 --- a/core/src/main/java/fj/function/Effect6.java +++ b/core/src/main/java/fj/function/Effect6.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface Effect6 { void f(A a, B b, C c, D d, E e, F f); diff --git a/core/src/main/java/fj/function/Effect7.java b/core/src/main/java/fj/function/Effect7.java index 944273af..4c77e4f4 100644 --- a/core/src/main/java/fj/function/Effect7.java +++ b/core/src/main/java/fj/function/Effect7.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface Effect7 { void f(A a, B b, C c, D d, E e, F f, G g); diff --git a/core/src/main/java/fj/function/Effect8.java b/core/src/main/java/fj/function/Effect8.java index a8cfd3bb..d8b970ec 100644 --- a/core/src/main/java/fj/function/Effect8.java +++ b/core/src/main/java/fj/function/Effect8.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface Effect8 { void f(A a, B b, C c, D d, E e, F f, G g, H h); diff --git a/core/src/main/java/fj/function/Integers.java b/core/src/main/java/fj/function/Integers.java index 4b1b14b5..7f8fd0e8 100644 --- a/core/src/main/java/fj/function/Integers.java +++ b/core/src/main/java/fj/function/Integers.java @@ -16,8 +16,6 @@ /** * Curried functions over Integers. - * - * @version %build.number% */ public final class Integers { private Integers() { diff --git a/core/src/main/java/fj/function/Longs.java b/core/src/main/java/fj/function/Longs.java index 18667699..2762f3e2 100644 --- a/core/src/main/java/fj/function/Longs.java +++ b/core/src/main/java/fj/function/Longs.java @@ -1,17 +1,20 @@ package fj.function; import fj.F; +import fj.Monoid; +import fj.data.List; +import fj.data.Option; import static fj.Function.curry; import static fj.Semigroup.longAdditionSemigroup; import static fj.Semigroup.longMultiplicationSemigroup; +import static fj.data.Option.none; +import static fj.data.Option.some; import static java.lang.Math.abs; /** * Curried functions over Longs. - * - * @version %build.number% */ public final class Longs { private Longs() { @@ -48,4 +51,38 @@ private Longs() { */ public static final F> remainder = curry((a, b) -> a % b); + /** + * Sums a list of longs. + * + * @param longs A list of longs to sum. + * @return The sum of the longs in the list. + */ + public static long sum(final List longs) { + return Monoid.longAdditionMonoid.sumLeft(longs); + } + + /** + * Returns the product of a list of integers. + * + * @param longs A list of longs to multiply together. + * @return The product of the longs in the list. + */ + public static long product(final List longs) { + return Monoid.longMultiplicationMonoid.sumLeft(longs); + } + + /** + * A function that converts strings to integers. + * + * @return A function that converts strings to integers. + */ + public static F> fromString() { + return s -> { + try { return some(Long.valueOf(s)); } + catch (final NumberFormatException ignored) { + return none(); + } + }; + } + } diff --git a/core/src/main/java/fj/function/Strings.java b/core/src/main/java/fj/function/Strings.java index dadbfd22..d9815248 100644 --- a/core/src/main/java/fj/function/Strings.java +++ b/core/src/main/java/fj/function/Strings.java @@ -12,8 +12,6 @@ /** * Curried string functions. - * - * @version %build.number% */ public final class Strings { private Strings() { @@ -98,4 +96,8 @@ public static F, String> unlines() { return Strings::unlines; } + public static F reverse() { + return s -> new StringBuilder(s).reverse().toString(); + } + } diff --git a/core/src/main/java/fj/function/Try0.java b/core/src/main/java/fj/function/Try0.java index e218f0a7..7cdacfc1 100644 --- a/core/src/main/java/fj/function/Try0.java +++ b/core/src/main/java/fj/function/Try0.java @@ -1,16 +1,52 @@ package fj.function; +import fj.F0; +import fj.P; +import fj.P1; +import fj.data.Option; +import fj.data.Validation; + +import static fj.data.Validation.fail; +import static fj.data.Validation.success; + /** * A product of A which may throw an Exception. * * Used to instantiate a lambda that may throw an Exception before converting to a P1. * * @see fj.Try#f(Try0) - * @version %build.number% */ public interface Try0 { A f() throws Z; + @SuppressWarnings("unchecked") + default F0> toF0() { + return () -> { + try { + return success(f()); + } catch (Exception e) { + return fail((Z) e); + } + }; + } + + default TryEffect0 toTryEffect0() { + return () -> f(); + } + + default Effect0 toEffect0() { + return () -> { + try { + f(); + } catch (Exception e) { + } + }; + } + + default P1> toP1() { + return P.lazy(() -> toF0().f()); + } + } diff --git a/core/src/main/java/fj/function/Try1.java b/core/src/main/java/fj/function/Try1.java index b626890b..53f5d048 100644 --- a/core/src/main/java/fj/function/Try1.java +++ b/core/src/main/java/fj/function/Try1.java @@ -6,7 +6,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F. * * @see fj.Try#f(Try1) - * @version %build.number% */ public interface Try1 { diff --git a/core/src/main/java/fj/function/Try2.java b/core/src/main/java/fj/function/Try2.java index 94438ccb..24e6a099 100644 --- a/core/src/main/java/fj/function/Try2.java +++ b/core/src/main/java/fj/function/Try2.java @@ -6,7 +6,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F2. * * @see fj.Try#f(Try2) - * @version %build.number% */ public interface Try2 { diff --git a/core/src/main/java/fj/function/Try3.java b/core/src/main/java/fj/function/Try3.java index 7464c013..7c0de0eb 100644 --- a/core/src/main/java/fj/function/Try3.java +++ b/core/src/main/java/fj/function/Try3.java @@ -6,7 +6,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F3. * * @see fj.Try#f(Try3) - * @version %build.number% */ public interface Try3 { diff --git a/core/src/main/java/fj/function/Try4.java b/core/src/main/java/fj/function/Try4.java index a7d969ed..d2b79d2c 100644 --- a/core/src/main/java/fj/function/Try4.java +++ b/core/src/main/java/fj/function/Try4.java @@ -6,7 +6,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F4. * * @see fj.Try#f(Try4) - * @version %build.number% */ public interface Try4 { diff --git a/core/src/main/java/fj/function/Try5.java b/core/src/main/java/fj/function/Try5.java index b9855fd7..cbc1a93d 100644 --- a/core/src/main/java/fj/function/Try5.java +++ b/core/src/main/java/fj/function/Try5.java @@ -6,7 +6,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F5. * * @see fj.Try#f(Try5) - * @version %build.number% */ public interface Try5 { diff --git a/core/src/main/java/fj/function/Try6.java b/core/src/main/java/fj/function/Try6.java index b66775c6..39bffbcb 100644 --- a/core/src/main/java/fj/function/Try6.java +++ b/core/src/main/java/fj/function/Try6.java @@ -6,7 +6,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F6. * * @see fj.Try#f(Try6) - * @version %build.number% */ public interface Try6 { diff --git a/core/src/main/java/fj/function/Try7.java b/core/src/main/java/fj/function/Try7.java index 3bc09cd9..ab56ffcd 100644 --- a/core/src/main/java/fj/function/Try7.java +++ b/core/src/main/java/fj/function/Try7.java @@ -7,7 +7,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F7. * * @see fj.Try#f(Try7) - * @version %build.number% */ public interface Try7 { diff --git a/core/src/main/java/fj/function/Try8.java b/core/src/main/java/fj/function/Try8.java index 5b2f1b55..1486de3c 100644 --- a/core/src/main/java/fj/function/Try8.java +++ b/core/src/main/java/fj/function/Try8.java @@ -6,7 +6,6 @@ * Used to instantiate a lambda that may throw an Exception before converting to an F8. * * @see fj.Try#f(Try8) - * @version %build.number% */ public interface Try8 { diff --git a/core/src/main/java/fj/function/TryEffect0.java b/core/src/main/java/fj/function/TryEffect0.java index a6c9cbee..1ccaa9af 100644 --- a/core/src/main/java/fj/function/TryEffect0.java +++ b/core/src/main/java/fj/function/TryEffect0.java @@ -1,10 +1,57 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ +import fj.F0; +import fj.P; +import fj.P1; +import fj.Unit; +import fj.data.Option; + +import static fj.Unit.unit; +import static fj.data.Option.none; +import static fj.data.Option.some; + public interface TryEffect0 { void f() throws Z; + @SuppressWarnings("unchecked") + default F0> toF0() { + return () -> { + try { + f(); + return none(); + } catch (Exception e) { + return some((Z) e); + } + }; + } + + @SuppressWarnings("unchecked") + default Try0 toTry0() { + return () -> { + try { + f(); + return unit(); + } catch (Exception e) { + throw ((Z) e); + } + }; + } + + default Effect0 toEffect0() { + return () -> { + try { + f(); + } catch (Exception e) { + } + }; + } + + default P1 toP1() { + return P.lazy(() -> { + toEffect0().f(); + return Unit.unit(); + }); + } + } diff --git a/core/src/main/java/fj/function/TryEffect1.java b/core/src/main/java/fj/function/TryEffect1.java index 40db423d..1ddaca08 100644 --- a/core/src/main/java/fj/function/TryEffect1.java +++ b/core/src/main/java/fj/function/TryEffect1.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect1 { void f(A a) throws Z; diff --git a/core/src/main/java/fj/function/TryEffect2.java b/core/src/main/java/fj/function/TryEffect2.java index c9c13da9..c54f3fe7 100644 --- a/core/src/main/java/fj/function/TryEffect2.java +++ b/core/src/main/java/fj/function/TryEffect2.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect2 { void f(A a, B b) throws Z; diff --git a/core/src/main/java/fj/function/TryEffect3.java b/core/src/main/java/fj/function/TryEffect3.java index c581221e..4c3f19d0 100644 --- a/core/src/main/java/fj/function/TryEffect3.java +++ b/core/src/main/java/fj/function/TryEffect3.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect3 { void f(A a, B b, C c) throws Z; diff --git a/core/src/main/java/fj/function/TryEffect4.java b/core/src/main/java/fj/function/TryEffect4.java index f99900ca..8b04ed5f 100644 --- a/core/src/main/java/fj/function/TryEffect4.java +++ b/core/src/main/java/fj/function/TryEffect4.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect4 { void f(A a, B b, C c, D d) throws Z; diff --git a/core/src/main/java/fj/function/TryEffect5.java b/core/src/main/java/fj/function/TryEffect5.java index d0445e3e..0bca874a 100644 --- a/core/src/main/java/fj/function/TryEffect5.java +++ b/core/src/main/java/fj/function/TryEffect5.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect5 { void f(A a, B b, C c, D d, E e) throws Z; diff --git a/core/src/main/java/fj/function/TryEffect6.java b/core/src/main/java/fj/function/TryEffect6.java index b4d81a41..d8f1f468 100644 --- a/core/src/main/java/fj/function/TryEffect6.java +++ b/core/src/main/java/fj/function/TryEffect6.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect6 { void f(A a, B b, C c, D d, E e, F f) throws Z; diff --git a/core/src/main/java/fj/function/TryEffect7.java b/core/src/main/java/fj/function/TryEffect7.java index 2e6004e9..133212dc 100644 --- a/core/src/main/java/fj/function/TryEffect7.java +++ b/core/src/main/java/fj/function/TryEffect7.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect7 { void f(A a, B b, C c, D d, E e, F f, G g) throws Z; diff --git a/core/src/main/java/fj/function/TryEffect8.java b/core/src/main/java/fj/function/TryEffect8.java index 4424bf00..d9c8c384 100644 --- a/core/src/main/java/fj/function/TryEffect8.java +++ b/core/src/main/java/fj/function/TryEffect8.java @@ -1,8 +1,5 @@ package fj.function; -/** - * Created by mperry on 28/08/2014. - */ public interface TryEffect8 { void f(A a, B b, C c, D d, E e, F f, G g, H h) throws Z; diff --git a/core/src/main/java/fj/function/Visitor.java b/core/src/main/java/fj/function/Visitor.java index bdca3b5e..ed0e7b53 100644 --- a/core/src/main/java/fj/function/Visitor.java +++ b/core/src/main/java/fj/function/Visitor.java @@ -15,8 +15,6 @@ /** * The essence of the visitor design pattern expressed polymorphically. - * - * @version %build.number% */ public final class Visitor { private Visitor() { diff --git a/core/src/main/java/fj/function/package-info.java b/core/src/main/java/fj/function/package-info.java index 6698bd5d..851bb534 100644 --- a/core/src/main/java/fj/function/package-info.java +++ b/core/src/main/java/fj/function/package-info.java @@ -1,6 +1,4 @@ /** * A prelude of commonly used first-class functions - * - * @version %build.number% */ package fj.function; diff --git a/core/src/main/java/fj/package-info.java b/core/src/main/java/fj/package-info.java index abeffe0f..c457d1e9 100644 --- a/core/src/main/java/fj/package-info.java +++ b/core/src/main/java/fj/package-info.java @@ -1,6 +1,4 @@ /** * Types that set the premise for the existence of Functional Java. - * - * @version %build.number% */ package fj; diff --git a/core/src/main/java/fj/parser/Parser.java b/core/src/main/java/fj/parser/Parser.java index 3ea6dd15..b5753f23 100644 --- a/core/src/main/java/fj/parser/Parser.java +++ b/core/src/main/java/fj/parser/Parser.java @@ -14,8 +14,6 @@ /** * A parser is a function that takes some input (I) and produces either an error (E) or a parse result (A) and the * remainder of the input. - * - * @version %build.number% */ public final class Parser { private final F>> f; diff --git a/core/src/main/java/fj/parser/Result.java b/core/src/main/java/fj/parser/Result.java index b8acc6c4..843dd558 100644 --- a/core/src/main/java/fj/parser/Result.java +++ b/core/src/main/java/fj/parser/Result.java @@ -12,8 +12,6 @@ /** * A parse result made up of a value (A) and the remainder of the parse input (I). - * - * @version %build.number% */ public final class Result implements Iterable { private final I i; diff --git a/core/src/main/java/fj/parser/package-info.java b/core/src/main/java/fj/parser/package-info.java index 08140f0b..8f31c204 100644 --- a/core/src/main/java/fj/parser/package-info.java +++ b/core/src/main/java/fj/parser/package-info.java @@ -1,6 +1,4 @@ /** * Parser combinators. - * - * @version %build.number% */ package fj.parser; diff --git a/core/src/test/java/fj/ClassTest.java b/core/src/test/java/fj/ClassTest.java index 2959e351..dfbc7f00 100644 --- a/core/src/test/java/fj/ClassTest.java +++ b/core/src/test/java/fj/ClassTest.java @@ -11,7 +11,7 @@ import java.util.Iterator; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class ClassTest { @Test diff --git a/core/src/test/java/fj/DigitTest.java b/core/src/test/java/fj/DigitTest.java index 9fc98d4c..13169aca 100644 --- a/core/src/test/java/fj/DigitTest.java +++ b/core/src/test/java/fj/DigitTest.java @@ -5,7 +5,7 @@ import static fj.data.Array.range; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class DigitTest { @Test diff --git a/core/src/test/java/fj/FFunctionsTest.java b/core/src/test/java/fj/FFunctionsTest.java index e13b1f2b..4f01a0db 100644 --- a/core/src/test/java/fj/FFunctionsTest.java +++ b/core/src/test/java/fj/FFunctionsTest.java @@ -1,43 +1,30 @@ -package fj; - -import fj.data.Tree; -import fj.data.TreeZipper; -import org.junit.Test; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; - -public class FFunctionsTest { - @Test - public void testApply() { - F8 f8 = - (i1, i2, i3, i4, i5, i6, i7, i8) -> - i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8; - F7 f7 = F8Functions.f(f8, 8); - F6 f6 = - F7Functions.f(f7, 7); - F5 f5 = F6Functions.f(f6, 6); - F4 f4 = F5Functions.f(f5, 5); - F3 f3 = F4Functions.f(f4, 4); - F2 f2 = F3Functions.f(f3, 3); - F f1 = F2Functions.f(f2, 2); - assertThat(F1Functions.f(f1, 1).f(), is(36)); - } - - @Test - public void testTreeK() { - final Tree t1 = F1Functions.treeK(Function.identity()).f(1); - final Tree t2 = F1Functions.treeK(Function.identity()).f(2); - assertThat(F1Functions.mapTree(i -> i + 1).f(t1), - is(F1Functions.mapTree(i -> i * 1).f(t2))); - } - - @Test - public void testTreeZipperK() { - final TreeZipper tz1 = F1Functions.treeZipperK(Function.identity()).f(1); - final TreeZipper tz2 = F1Functions.treeZipperK(Function.identity()).f(2); - assertThat(F1Functions.mapTreeZipper(i -> i + 1).f(tz1), - is(F1Functions.mapTreeZipper(i -> i * 1).f(tz2))); - } -} +package fj; + +import fj.data.Tree; +import fj.data.TreeZipper; +import org.junit.Test; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class FFunctionsTest { + + @Test + public void testTreeK() { + final Tree t1 = Function.identity().treeK().f(1); + final Tree t2 = Function.identity().treeK().f(2); + F f = i -> i + 1; + F g = i -> i * 1; + assertThat(f.mapTree().f(t1), + is(g.mapTree().f(t2))); + } + + @Test + public void testTreeZipperK() { + final TreeZipper tz1 = Function.identity().treeZipperK().f(1); + final TreeZipper tz2 = Function.identity().treeZipperK().f(2); + F f = i -> i + 1; + F g = i -> i * 1; + assertThat(f.mapTreeZipper().f(tz1), + is(g.mapTreeZipper().f(tz2))); + } +} diff --git a/core/src/test/java/fj/FWFunctionsTest.java b/core/src/test/java/fj/FWFunctionsTest.java deleted file mode 100644 index 1c034ab9..00000000 --- a/core/src/test/java/fj/FWFunctionsTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package fj; - -import org.junit.Test; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -public class FWFunctionsTest { - @Test - public void testLift1() { - F f = i -> i + 1; - F1W f1w = F1W.lift(f); - assertThat(f1w.f(1), is(2)); - } - - @Test - public void testLift2() { - F2 f2 = (i, j) -> i + j; - F2W f2w = F2W.lift(f2); - assertThat(f2w.f(1, 2), is(3)); - } - -} diff --git a/core/src/test/java/fj/MonoidTest.java b/core/src/test/java/fj/MonoidTest.java index 8bab5315..68457f09 100644 --- a/core/src/test/java/fj/MonoidTest.java +++ b/core/src/test/java/fj/MonoidTest.java @@ -1,12 +1,14 @@ package fj; +import fj.data.Enumerator; import fj.data.Option; +import fj.data.Set; import fj.data.Stream; import org.junit.Test; import static fj.data.Option.some; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class MonoidTest { @@ -16,4 +18,41 @@ public void lifted_sum_of_two_numbers() { assertThat(optionMonoid.sum(some(3), some(5)), is(some(8))); assertThat(optionMonoid.sumLeft(Stream.arrayStream(some(3), some(5))), is(some(8))); } + + @Test + public void intersection_monoid_test() { + Bounded integersBounded = Bounded.bounded(0, 10); + Monoid> intersectionMonoid = Monoid.setIntersectionMonoid(integersBounded, Enumerator.intEnumerator); + Set first = Set.set(Ord.intOrd, 1, 2, 3, 4); + Set second = Set.set(Ord.intOrd, 3, 4, 5, 6); + Set actual = intersectionMonoid.sum(first, second); + assertThat(actual, is(Set.set(Ord.intOrd, 3, 4))); + } + + @Test + public void union_monoid_test() { + Monoid> unionMonoid = Monoid.setMonoid(Ord.intOrd); + Set first = Set.set(Ord.intOrd, 1, 2, 3, 4); + Set second = Set.set(Ord.intOrd, 3, 4, 5, 6); + Set actual = unionMonoid.sum(first, second); + assertThat(actual, is(Set.set(Ord.intOrd, 1, 2, 3, 4, 5, 6))); + } + + @Test + public void intersection_monoid_zero_test() { + Bounded integersBounded = Bounded.bounded(0, 10); + Monoid> monoid = Monoid.setIntersectionMonoid(integersBounded, Enumerator.intEnumerator); + Set set = Set.set(Ord.intOrd, 7, 8, 9, 10); + Set zero = monoid.zero(); + assertThat(monoid.sum(zero, set), is(set)); + } + + @Test + public void union_monoid_zero_test() { + Monoid> monoid = Monoid.setMonoid(Ord.intOrd); + Set set = Set.set(Ord.intOrd, 1, 2, 3, 4); + Set zero = monoid.zero(); + assertThat(monoid.sum(zero, set), is(set)); + } + } diff --git a/core/src/test/java/fj/OrderingTest.java b/core/src/test/java/fj/OrderingTest.java index 9b980e67..640bc1d3 100644 --- a/core/src/test/java/fj/OrderingTest.java +++ b/core/src/test/java/fj/OrderingTest.java @@ -8,7 +8,7 @@ import static fj.Ordering.GT; import static fj.Ordering.LT; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; public class OrderingTest { diff --git a/core/src/test/java/fj/P2Test.java b/core/src/test/java/fj/P2Test.java index dc25a3e0..d14b6199 100644 --- a/core/src/test/java/fj/P2Test.java +++ b/core/src/test/java/fj/P2Test.java @@ -3,9 +3,6 @@ import org.junit.Assert; import org.junit.Test; -/** - * Created by MarkPerry on 22/07/2014. - */ public class P2Test { @Test diff --git a/core/src/test/java/fj/PTest.java b/core/src/test/java/fj/PTest.java index 2a97303f..00220f9d 100644 --- a/core/src/test/java/fj/PTest.java +++ b/core/src/test/java/fj/PTest.java @@ -4,7 +4,7 @@ import static fj.Function.identity; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class PTest { @Test diff --git a/core/src/test/java/fj/SemigroupTest.java b/core/src/test/java/fj/SemigroupTest.java new file mode 100644 index 00000000..20ecb2cb --- /dev/null +++ b/core/src/test/java/fj/SemigroupTest.java @@ -0,0 +1,26 @@ +package fj; + +import fj.data.Set; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class SemigroupTest { + + @Test + public void intersection_semigroup_test() { + Semigroup> intersectionSemigroup = Semigroup.setIntersectionSemigroup(); + Set first = Set.set(Ord.intOrd, 1, 2, 3, 4); + Set second = Set.set(Ord.intOrd, 3, 4, 5, 6); + assertThat(intersectionSemigroup.sum(first, second), is(Set.set(Ord.intOrd, 3, 4))); + } + + @Test + public void union_semigroup_test() { + Semigroup> unionSemigroup = Semigroup.setSemigroup(); + Set first = Set.set(Ord.intOrd, 1, 2, 3, 4); + Set second = Set.set(Ord.intOrd, 3, 4, 5, 6); + assertThat(unionSemigroup.sum(first, second), is(Set.set(Ord.intOrd, 1, 2, 3, 4, 5, 6))); + } +} diff --git a/core/src/test/java/fj/ShowTest.java b/core/src/test/java/fj/ShowTest.java index c516c0df..3d6d8354 100644 --- a/core/src/test/java/fj/ShowTest.java +++ b/core/src/test/java/fj/ShowTest.java @@ -6,9 +6,6 @@ import static fj.data.Array.array; import static org.junit.Assert.assertTrue; -/** - * Created by MarkPerry on 4/06/2015. - */ public class ShowTest { @Test public void arrayShow() { diff --git a/core/src/test/java/fj/TryEffectTest.java b/core/src/test/java/fj/TryEffectTest.java index ea92b8f2..6fd46fd1 100644 --- a/core/src/test/java/fj/TryEffectTest.java +++ b/core/src/test/java/fj/TryEffectTest.java @@ -5,7 +5,7 @@ import fj.function.TryEffect1; import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; public class TryEffectTest { diff --git a/core/src/test/java/fj/TryTest.java b/core/src/test/java/fj/TryTest.java index bd94e986..46fb2dd3 100644 --- a/core/src/test/java/fj/TryTest.java +++ b/core/src/test/java/fj/TryTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class TryTest { diff --git a/core/src/test/java/fj/control/db/TestDbState.java b/core/src/test/java/fj/control/db/TestDbState.java index 188eae3d..2a942e1a 100644 --- a/core/src/test/java/fj/control/db/TestDbState.java +++ b/core/src/test/java/fj/control/db/TestDbState.java @@ -9,7 +9,7 @@ import java.sql.*; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class TestDbState { @Test diff --git a/core/src/test/java/fj/control/parallel/StrategyTest.java b/core/src/test/java/fj/control/parallel/StrategyTest.java index 8475b35a..1f5e7bba 100644 --- a/core/src/test/java/fj/control/parallel/StrategyTest.java +++ b/core/src/test/java/fj/control/parallel/StrategyTest.java @@ -5,6 +5,7 @@ import fj.P1; import fj.Unit; import fj.data.Enumerator; +import fj.data.Java; import fj.data.List; import fj.data.Stream; import org.junit.Test; @@ -15,7 +16,7 @@ import static fj.control.parallel.Strategy.*; import static fj.data.Stream.range; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class StrategyTest { @@ -49,10 +50,16 @@ public void testStrategyCompletion() { @Test public void testStrategyMergeAll() { final List l = List.range(0, 100); - final List> p1s = mergeAll(l.map(x -> CompletableFuture.supplyAsync(() -> x))); + final List> p1s = mergeAll(l.map(x -> future(x))); assertThat(P1.sequence(p1s)._1(), is(l)); } + public static Future future(A a) { + FutureTask ft = new FutureTask<>(() -> a); + new Thread(ft).start(); + return ft; + } + @Test public void testStrategyCallables() throws Exception { final Strategy> s = strategy(c -> c); diff --git a/core/src/test/java/fj/data/ArrayTest.java b/core/src/test/java/fj/data/ArrayTest.java index 886031b3..11683fbf 100644 --- a/core/src/test/java/fj/data/ArrayTest.java +++ b/core/src/test/java/fj/data/ArrayTest.java @@ -1,14 +1,13 @@ package fj.data; +import org.hamcrest.CoreMatchers; import org.junit.Test; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + -/** - * Created by MarkPerry on 14 Feb 16. - */ public class ArrayTest { @Test diff --git a/core/src/test/java/fj/data/BooleansTest.java b/core/src/test/java/fj/data/BooleansTest.java index b0d3f969..e5dbeb12 100644 --- a/core/src/test/java/fj/data/BooleansTest.java +++ b/core/src/test/java/fj/data/BooleansTest.java @@ -9,10 +9,9 @@ import static fj.data.List.list; import static fj.function.Booleans.isnot; import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + -/** - * Created by amar on 28/01/15. - */ public class BooleansTest { @Test @@ -73,7 +72,7 @@ public void testIsNot(){ F f1 = a -> a == 4; List result = list("some", "come", "done!").filter(isnot(String::length, f1)); - Assert.assertThat(result.length(), is(1)); + assertThat(result.length(), is(1)); Assert.assertEquals(result, list("done!")); } diff --git a/core/src/test/java/fj/data/DListTest.java b/core/src/test/java/fj/data/DListTest.java index 6846ca2d..77f4db6c 100644 --- a/core/src/test/java/fj/data/DListTest.java +++ b/core/src/test/java/fj/data/DListTest.java @@ -4,7 +4,8 @@ import static fj.data.DList.*; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + public class DListTest { @Test diff --git a/core/src/test/java/fj/data/Either3Test.java b/core/src/test/java/fj/data/Either3Test.java new file mode 100644 index 00000000..1507bf41 --- /dev/null +++ b/core/src/test/java/fj/data/Either3Test.java @@ -0,0 +1,6 @@ +package fj.data; + +public final class Either3Test { + + +} diff --git a/core/src/test/java/fj/data/EitherTest.java b/core/src/test/java/fj/data/EitherTest.java new file mode 100644 index 00000000..31a83347 --- /dev/null +++ b/core/src/test/java/fj/data/EitherTest.java @@ -0,0 +1,653 @@ +package fj.data; + +import org.junit.Test; + +import java.io.IOException; + +import static fj.Function.constant; +import static fj.P.p; +import static fj.Unit.unit; +import static fj.data.Either.*; +import static org.junit.Assert.*; + +public final class EitherTest { + + public static final class LeftProjectionTest { + @Test + public void testIterator() { + assertEquals(0L, (long) left(0L).left().iterator().next()); + assertFalse(right(0).left().iterator().hasNext()); + } + + @Test + public void testEither() { + assertEquals(left(0), left(0).left().either()); + assertEquals(right(0), right(0).left().either()); + } + + @Test + public void testValueEString() { + assertEquals(0L, (long) left(0L).left().valueE("zero")); + + try { + right(0L).left().valueE("zero"); + fail(); + } catch (final Error error) { + assertEquals("zero", error.getMessage()); + } + } + + @Test + public void testValueEF0() { + assertEquals(0L, (long) left(0L).left().valueE(() -> "zero")); + + try { + right(0L).left().valueE(() -> "zero"); + fail(); + } catch (final Error error) { + assertEquals("zero", error.getMessage()); + } + } + + @Test + public void testValue() { + assertEquals(0L, (long) left(0L).left().value()); + + try { + right(0L).left().value(); + fail(); + } catch (final Error error) { + // pass + } + } + + @Test + public void testOrValue() { + assertEquals(0L, (long) left(0L).left().orValue(1L)); + assertEquals(1L, (long) right(0L).left().orValue(1L)); + } + + @Test + public void testOrValueF0() { + assertEquals(0L, (long) left(0L).left().orValue(() -> 1L)); + assertEquals(1L, (long) right(0L).left().orValue(() -> 1L)); + } + + @Test + public void testOn() { + assertEquals(0L, (long) Either.left(0L).left().on(constant(1L))); + assertEquals(1L, (long) Either.right(0L).left().on(constant(1L))); + } + + @Test + public void testForeach() { + left(0).left().foreach(constant(unit())); + right(0).left().foreach(ignore -> { + fail(); + return unit(); + }); + } + + @Test + public void testForeachDoEffect() { + left(0).left().foreachDoEffect(ignore -> { + }); + right(0).left().foreachDoEffect(ignore -> fail()); + } + + @Test + public void testMap() { + assertEquals(left(0), left("zero").left().map(constant(0))); + assertEquals(right("zero"), right("zero").left().map(constant(0))); + } + + @Test + public void testBind() { + assertEquals(left(0), left("zero").left().bind(constant(left(0)))); + assertEquals(right("zero"), right("zero").left().bind(constant(left(0)))); + } + + @Test + public void testSequence() { + assertEquals(left(0), left("zero").left().sequence(left(0))); + assertEquals(right(0), left("zero").left().sequence(right(0))); + assertEquals(right("zero"), right("zero").left().sequence(left(0))); + assertEquals(right("zero"), right("zero").left().sequence(right("one"))); + } + + @Test + public void testTraverseList() { + assertEquals(List.nil(), left("zero").left().traverseList(constant(List.nil()))); + assertEquals(List.single(left(0)), left("zero").left().traverseList(constant(List.single(0)))); + assertEquals(List.arrayList(left(0), left(1)), left("zero").left().traverseList(constant(List.arrayList(0, 1)))); + assertEquals(List.single(right("zero")), right("zero").left().traverseList(constant(List.nil()))); + assertEquals(List.single(right("zero")), right("zero").left().traverseList(constant(List.single(0)))); + assertEquals(List.single(right("zero")), right("zero").left().traverseList(constant(List.arrayList(0, 1)))); + } + + @Test + public void testTraverseIO() throws IOException { + assertEquals(left(0), left("zero").left().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(right("zero"), right("zero").left().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseP1() { + assertEquals(p(left(0)), left("zero").left().traverseP1(constant(p(0)))); + assertEquals(p(right("zero")), right("zero").left().traverseP1(constant(p(0)))); + } + + @Test + public void testFilter() { + assertEquals(Option.none(), left(0).left().filter(constant(false))); + assertEquals(Option.none(), right(0).left().filter(constant(false))); + assertEquals(Option.some(left(0)), left(0).left().filter(constant(true))); + assertEquals(Option.none(), right(0).left().filter(constant(true))); + } + + @Test + public void testApply() { + assertEquals(left(1), left("zero").left().apply(left(constant(1)))); + assertEquals(right("zero"), right("zero").left().apply(left(constant(1)))); + } + + @Test + public void testForAll() { + assertFalse(left(0).left().forall(constant(false))); + assertTrue(right(0).left().forall(constant(false))); + assertTrue(left(0).left().forall(constant(true))); + assertTrue(right(0).left().forall(constant(true))); + } + + @Test + public void testExists() { + assertFalse(left(0).left().exists(constant(false))); + assertFalse(right(0).left().exists(constant(false))); + assertTrue(left(0).left().exists(constant(true))); + assertFalse(right(0).left().exists(constant(true))); + } + + @Test + public void testToList() { + assertEquals(List.single(0), left(0).left().toList()); + assertEquals(List.nil(), right(0).left().toList()); + } + + @Test + public void testToOption() { + assertEquals(Option.some(0), left(0).left().toOption()); + assertEquals(Option.none(), right(0).left().toOption()); + } + + @Test + public void testToArray() { + assertEquals(Array.single(0), left(0).left().toArray()); + assertEquals(Array.empty(), right(0).left().toArray()); + } + + @Test + public void testToStream() { + assertEquals(Stream.single(0), left(0).left().toStream()); + assertEquals(Stream.nil(), right(0).left().toStream()); + } + + @Test + public void testToCollection() { + assertEquals(1L, left(0L).left().toCollection().size()); + assertEquals(0L, (long) left(0L).left().toCollection().iterator().next()); + assertTrue(right(0).left().toCollection().isEmpty()); + } + + @Test + public void testTraverseOption() { + assertEquals(Option.none(), left("zero").left().traverseOption(constant(Option.none()))); + assertEquals(Option.some(left(0)), left("zero").left().traverseOption(constant(Option.some(0)))); + assertEquals(Option.some(right("zero")), right("zero").left().traverseOption(constant(Option.none()))); + assertEquals(Option.some(right("zero")), right("zero").left().traverseOption(constant(Option.some(0)))); + } + + @Test + public void testTraverseStream() { + assertEquals(Stream.nil(), left("zero").left().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(left(0)), left("zero").left().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.arrayStream(left(0), left(1)), left("zero").left().traverseStream(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.single(right("zero")), right("zero").left().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(right("zero")), right("zero").left().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(right("zero")), right("zero").left().traverseStream(constant(Stream.arrayStream(0, 1)))); + } + } + + public static final class RightProjectionTest { + @Test + public void testIterator() { + assertEquals(0L, (long) right(0L).right().iterator().next()); + assertFalse(left(0).right().iterator().hasNext()); + } + + @Test + public void testEither() { + assertEquals(right(0), right(0).right().either()); + assertEquals(left(0), left(0).right().either()); + } + + @Test + public void testValueEString() { + assertEquals(0L, (long) right(0L).right().valueE("zero")); + + try { + left(0L).right().valueE("zero"); + fail(); + } catch (final Error error) { + assertEquals("zero", error.getMessage()); + } + } + + @Test + public void testValueEF0() { + assertEquals(0L, (long) right(0L).right().valueE(() -> "zero")); + + try { + left(0L).right().valueE(() -> "zero"); + fail(); + } catch (final Error error) { + assertEquals("zero", error.getMessage()); + } + } + + @Test + public void testValue() { + assertEquals(0L, (long) right(0L).right().value()); + + try { + left(0L).right().value(); + fail(); + } catch (final Error error) { + // pass + } + } + + @Test + public void testOrValue() { + assertEquals(0L, (long) right(0L).right().orValue(1L)); + assertEquals(1L, (long) left(0L).right().orValue(1L)); + } + + @Test + public void testOrValueF0() { + assertEquals(0L, (long) right(0L).right().orValue(() -> 1L)); + assertEquals(1L, (long) left(0L).right().orValue(() -> 1L)); + } + + @Test + public void testOn() { + assertEquals(0L, (long) Either.right(0L).right().on(constant(1L))); + assertEquals(1L, (long) Either.left(0L).right().on(constant(1L))); + } + + @Test + public void testForeach() { + right(0).right().foreach(constant(unit())); + left(0).right().foreach(ignore -> { + fail(); + return unit(); + }); + } + + @Test + public void testForeachDoEffect() { + right(0).right().foreachDoEffect(ignore -> { + }); + left(0).right().foreachDoEffect(ignore -> fail()); + } + + @Test + public void testMap() { + assertEquals(right(0), right("zero").right().map(constant(0))); + assertEquals(left("zero"), left("zero").right().map(constant(0))); + } + + @Test + public void testBind() { + assertEquals(right(0), right("zero").right().bind(constant(right(0)))); + assertEquals(left("zero"), left("zero").right().bind(constant(right(0)))); + } + + @Test + public void testSequence() { + assertEquals(right(0), right("zero").right().sequence(right(0))); + assertEquals(left(0), right("zero").right().sequence(left(0))); + assertEquals(left("zero"), left("zero").right().sequence(right(0))); + assertEquals(left("zero"), left("zero").right().sequence(left("one"))); + } + + @Test + public void testTraverseList() { + assertEquals(List.nil(), right("zero").right().traverseList(constant(List.nil()))); + assertEquals(List.single(right(0)), right("zero").right().traverseList(constant(List.single(0)))); + assertEquals(List.arrayList(right(0), right(1)), right("zero").right().traverseList(constant(List.arrayList(0, 1)))); + assertEquals(List.single(left("zero")), left("zero").right().traverseList(constant(List.nil()))); + assertEquals(List.single(left("zero")), left("zero").right().traverseList(constant(List.single(0)))); + assertEquals(List.single(left("zero")), left("zero").right().traverseList(constant(List.arrayList(0, 1)))); + } + + @Test + public void testTraverseIO() throws IOException { + assertEquals(right(0), right("zero").right().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(left("zero"), left("zero").right().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseP1() { + assertEquals(p(right(0)), right("zero").right().traverseP1(constant(p(0)))); + assertEquals(p(left("zero")), left("zero").right().traverseP1(constant(p(0)))); + } + + @Test + public void testFilter() { + assertEquals(Option.none(), right(0).right().filter(constant(false))); + assertEquals(Option.none(), left(0).right().filter(constant(false))); + assertEquals(Option.some(right(0)), right(0).right().filter(constant(true))); + assertEquals(Option.none(), left(0).right().filter(constant(true))); + } + + @Test + public void testApply() { + assertEquals(right(1), right("zero").right().apply(right(constant(1)))); + assertEquals(left("zero"), left("zero").right().apply(right(constant(1)))); + } + + @Test + public void testForAll() { + assertFalse(right(0).right().forall(constant(false))); + assertTrue(left(0).right().forall(constant(false))); + assertTrue(right(0).right().forall(constant(true))); + assertTrue(left(0).right().forall(constant(true))); + } + + @Test + public void testExists() { + assertFalse(right(0).right().exists(constant(false))); + assertFalse(left(0).right().exists(constant(false))); + assertTrue(right(0).right().exists(constant(true))); + assertFalse(left(0).right().exists(constant(true))); + } + + @Test + public void testToList() { + assertEquals(List.single(0), right(0).right().toList()); + assertEquals(List.nil(), left(0).right().toList()); + } + + @Test + public void testToOption() { + assertEquals(Option.some(0), right(0).right().toOption()); + assertEquals(Option.none(), left(0).right().toOption()); + } + + @Test + public void testToArray() { + assertEquals(Array.single(0), right(0).right().toArray()); + assertEquals(Array.empty(), left(0).right().toArray()); + } + + @Test + public void testToStream() { + assertEquals(Stream.single(0), right(0).right().toStream()); + assertEquals(Stream.nil(), left(0).right().toStream()); + } + + @Test + public void testToCollection() { + assertEquals(1L, right(0L).right().toCollection().size()); + assertEquals(0L, (long) right(0L).right().toCollection().iterator().next()); + assertTrue(left(0).right().toCollection().isEmpty()); + } + + @Test + public void testTraverseOption() { + assertEquals(Option.none(), right("zero").right().traverseOption(constant(Option.none()))); + assertEquals(Option.some(right(0)), right("zero").right().traverseOption(constant(Option.some(0)))); + assertEquals(Option.some(left("zero")), left("zero").right().traverseOption(constant(Option.none()))); + assertEquals(Option.some(left("zero")), left("zero").right().traverseOption(constant(Option.some(0)))); + } + + @Test + public void testTraverseStream() { + assertEquals(Stream.nil(), right("zero").right().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(right(0)), right("zero").right().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.arrayStream(right(0), right(1)), right("zero").right().traverseStream(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.single(left("zero")), left("zero").right().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(left("zero")), left("zero").right().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(left("zero")), left("zero").right().traverseStream(constant(Stream.arrayStream(0, 1)))); + } + } + + @Test + public void testIsLeft() { + assertTrue(left(0).isLeft()); + assertFalse(right(0).isLeft()); + } + + @Test + public void testIsRight() { + assertFalse(left(0).isRight()); + assertTrue(right(0).isRight()); + } + + @Test + public void testEither() { + assertEquals(-1L, (long) left("zero").either(constant(-1L), constant(1L))); + assertEquals(1L, (long) right("zero").either(constant(-1L), constant(1L))); + } + + @Test + public void testBimap() { + assertEquals(left(-1), left("zero").bimap(constant(-1), constant(1))); + assertEquals(right(1), right("zero").bimap(constant(-1), constant(1))); + } + + @Test + public void testTestEquals() { + assertNotEquals(null, left(0)); + assertNotEquals(new Object(), left(0)); + assertNotEquals(left(0), right(0)); + assertEquals(left(0), left(0)); + + assertNotEquals(null, right(0)); + assertNotEquals(new Object(), right(0)); + assertEquals(right(0), right(0)); + assertNotEquals(right(0), left(0)); + } + + @Test + public void testTestHashCode() { + assertEquals(left(0).hashCode(), left(0).hashCode()); + assertEquals(left(0).hashCode(), right(0).hashCode()); + assertEquals(right(0).hashCode(), left(0).hashCode()); + assertEquals(right(0).hashCode(), left(0).hashCode()); + } + + @Test + public void testSwap() { + assertEquals(right(0), left(0).swap()); + assertEquals(left(0), right(0).swap()); + } + + @Test + public void testLeft_() { + assertEquals(left(0), left_().f(0)); + } + + @Test + public void testRight_() { + assertEquals(right(0), right_().f(0)); + } + + @Test + public void testEither_() { + assertEquals(-1L, (long) either_(constant(-1L), constant(1L)).f(left("zero"))); + assertEquals(1L, (long) either_(constant(-1L), constant(1L)).f(right("zero"))); + } + + @Test + public void testLeftMap() { + assertEquals(left(0), left("zero").leftMap(constant(0))); + assertEquals(right("zero"), right("zero").leftMap(constant(0))); + } + + @Test + public void testLeftMap_() { + assertEquals(left(0), leftMap_().f(constant(0)).f(left("zero"))); + assertEquals(right("zero"), leftMap_().f(constant(0)).f(right("zero"))); + } + + @Test + public void testRightMap() { + assertEquals(left("zero"), left("zero").rightMap(constant(0))); + assertEquals(right(0), right("zero").rightMap(constant(0))); + } + + @Test + public void testRightMap_() { + assertEquals(left("zero"), rightMap_().f(constant(0)).f(left("zero"))); + assertEquals(right(0), rightMap_().f(constant(0)).f(right("zero"))); + } + + @Test + public void testJoinLeft() { + assertEquals(left(0), joinLeft(left(left(0)))); + assertEquals(right(0), joinLeft(left(right(0)))); + assertEquals(right(left(0)), joinLeft(right(left(0)))); + assertEquals(right(right(0)), joinLeft(right(right(0)))); + } + + @Test + public void testJoinRight() { + assertEquals(left(left(0)), joinRight(left(left(0)))); + assertEquals(left(right(0)), joinRight(left(right(0)))); + assertEquals(left(0), joinRight(right(left(0)))); + assertEquals(right(0), joinRight(right(right(0)))); + } + + @Test + public void testSequenceLeft() { + assertEquals(left(List.nil()), sequenceLeft(List.nil())); + assertEquals(left(List.single("zero")), sequenceLeft(List.single(left("zero")))); + assertEquals(right("zero"), sequenceLeft(List.single(right("zero")))); + } + + @Test + public void testSequenceRight() { + assertEquals(right(List.nil()), sequenceRight(List.nil())); + assertEquals(right(List.single("zero")), sequenceRight(List.single(right("zero")))); + assertEquals(left("zero"), sequenceRight(List.single(left("zero")))); + } + + @Test + public void testTraverseListRight() { + assertEquals(List.single(left("zero")), left("zero").traverseListRight(constant(List.nil()))); + assertEquals(List.single(left("zero")), left("zero").traverseListRight(constant(List.single(0)))); + assertEquals(List.single(left("zero")), left("zero").traverseListRight(constant(List.arrayList(0, 1)))); + assertEquals(List.nil(), right("zero").traverseListRight(constant(List.nil()))); + assertEquals(List.single(right(0)), right("zero").traverseListRight(constant(List.single(0)))); + assertEquals(List.arrayList(right(0), right(1)), right("zero").traverseListRight(constant(List.arrayList(0, 1)))); + } + + @Test + public void testTraverseListLeft() { + assertEquals(List.nil(), left("zero").traverseListLeft(constant(List.nil()))); + assertEquals(List.single(left(0)), left("zero").traverseListLeft(constant(List.single(0)))); + assertEquals(List.arrayList(left(0), left(1)), left("zero").traverseListLeft(constant(List.arrayList(0, 1)))); + assertEquals(List.single(right("zero")), right("zero").traverseListLeft(constant(List.nil()))); + assertEquals(List.single(right("zero")), right("zero").traverseListLeft(constant(List.single(0)))); + assertEquals(List.single(right("zero")), right("zero").traverseListLeft(constant(List.arrayList(0, 1)))); + } + + @Test + public void testTraverseIORight() throws IOException { + assertEquals(left("zero"), left("zero").traverseIORight(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(right(0), right("zero").traverseIORight(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseIOLeft() throws IOException { + assertEquals(left(0), left("zero").traverseIOLeft(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(right("zero"), right("zero").traverseIOLeft(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseOptionRight() { + assertEquals(Option.some(left("zero")), left("zero").traverseOptionRight(constant(Option.none()))); + assertEquals(Option.some(left("zero")), left("zero").traverseOptionRight(constant(Option.some(0)))); + assertEquals(Option.none(), right("zero").traverseOptionRight(constant(Option.none()))); + assertEquals(Option.some(right(0)), right("zero").traverseOptionRight(constant(Option.some(0)))); + } + + @Test + public void testTraverseOptionLeft() { + assertEquals(Option.none(), left("zero").traverseOptionLeft(constant(Option.none()))); + assertEquals(Option.some(left(0)), left("zero").traverseOptionLeft(constant(Option.some(0)))); + assertEquals(Option.some(right("zero")), right("zero").traverseOptionLeft(constant(Option.none()))); + assertEquals(Option.some(right("zero")), right("zero").traverseOptionLeft(constant(Option.some(0)))); + } + + @Test + public void testTraverseStreamRight() { + assertEquals(Stream.single(left("zero")), left("zero").traverseStreamRight(constant(Stream.nil()))); + assertEquals(Stream.single(left("zero")), left("zero").traverseStreamRight(constant(Stream.single(0)))); + assertEquals(Stream.single(left("zero")), left("zero").traverseStreamRight(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.nil(), right("zero").traverseStreamRight(constant(Stream.nil()))); + assertEquals(Stream.single(right(0)), right("zero").traverseStreamRight(constant(Stream.single(0)))); + assertEquals(Stream.arrayStream(right(0), right(1)), right("zero").traverseStreamRight(constant(Stream.arrayStream(0, 1)))); + } + + @Test + public void testTraverseStreamLeft() { + assertEquals(Stream.nil(), left("zero").traverseStreamLeft(constant(Stream.nil()))); + assertEquals(Stream.single(left(0)), left("zero").traverseStreamLeft(constant(Stream.single(0)))); + assertEquals(Stream.arrayStream(left(0), left(1)), left("zero").traverseStreamLeft(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.single(right("zero")), right("zero").traverseStreamLeft(constant(Stream.nil()))); + assertEquals(Stream.single(right("zero")), right("zero").traverseStreamLeft(constant(Stream.single(0)))); + assertEquals(Stream.single(right("zero")), right("zero").traverseStreamLeft(constant(Stream.arrayStream(0, 1)))); + } + + @Test + public void testReduce() { + assertEquals(0L, (long) reduce(left(0L))); + assertEquals(0L, (long) reduce(right(0L))); + } + + @Test + public void testIif() { + assertEquals(right(-1), iif(true, () -> -1, () -> 1)); + assertEquals(left(1), iif(false, () -> -1, () -> 1)); + } + + @Test + public void testLefts() { + assertEquals(List.nil(), lefts(List.nil())); + assertEquals(List.single(0), lefts(List.single(left(0)))); + assertEquals(List.nil(), lefts(List.single(right(0)))); + assertEquals(List.arrayList(0, 1), lefts(List.arrayList(left(0), left(1)))); + assertEquals(List.single(0), lefts(List.arrayList(left(0), right(1)))); + assertEquals(List.single(1), lefts(List.arrayList(right(0), left(1)))); + assertEquals(List.nil(), lefts(List.arrayList(right(0), right(1)))); + } + + @Test + public void testRights() { + assertEquals(List.nil(), rights(List.nil())); + assertEquals(List.single(0), rights(List.single(right(0)))); + assertEquals(List.single(0), lefts(List.single(left(0)))); + assertEquals(List.arrayList(0, 1), rights(List.arrayList(right(0), right(1)))); + assertEquals(List.single(0), rights(List.arrayList(right(0), left(1)))); + assertEquals(List.single(1), rights(List.arrayList(left(0), right(1)))); + assertEquals(List.nil(), rights(List.arrayList(left(0), left(1)))); + } + + @Test + public void testTestToString() { + assertNotNull(left(0).toString()); + assertNotNull(right(0).toString()); + } +} \ No newline at end of file diff --git a/core/src/test/java/fj/data/IOFunctionsTest.java b/core/src/test/java/fj/data/IOFunctionsTest.java index b943db36..8ae18b1a 100644 --- a/core/src/test/java/fj/data/IOFunctionsTest.java +++ b/core/src/test/java/fj/data/IOFunctionsTest.java @@ -12,7 +12,8 @@ import static fj.data.Stream.cons; import static fj.data.Stream.nil_; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; public class IOFunctionsTest { @@ -33,8 +34,8 @@ public void close() { r -> () -> new BufferedReader(r).readLine() ); - Assert.assertThat(bracketed.run(), is("Read OK")); - Assert.assertThat(closed.get(), is(true)); + assertThat(bracketed.run(), is("Read OK")); + assertThat(closed.get(), is(true)); } @Test @@ -59,9 +60,9 @@ public void close() { bracketed.run(); fail("Exception expected"); } catch (IllegalArgumentException e) { - Assert.assertThat(e.getMessage(), is("OoO")); + assertThat(e.getMessage(), is("OoO")); } - Assert.assertThat(closed.get(), is(true)); + assertThat(closed.get(), is(true)); } @Test @@ -73,7 +74,7 @@ public void testTraverseIO() throws IOException { System.setOut(new PrintStream(outContent)); stream.traverseIO(IOFunctions::stdoutPrint).run(); System.setOut(originalOut); - assertThat(outContent.toString(), is("foobar3bar2foo1")); + assertThat(outContent.toString(), is("foo1bar2foobar3")); } @Test @@ -107,7 +108,8 @@ public void testReplicateM() throws IOException { @Test public void testLift() throws IOException { final IO readName = () -> new BufferedReader(new StringReader("foo")).readLine(); - final F> upperCaseAndPrint = F1Functions., String>o(this::println).f(String::toUpperCase); + F> f = this::println; + final F> upperCaseAndPrint = f.o().f(String::toUpperCase); final IO readAndPrintUpperCasedName = IOFunctions.bind(readName, upperCaseAndPrint); assertThat(readAndPrintUpperCasedName.run(), is("FOO")); } diff --git a/core/src/test/java/fj/data/JavaTest.java b/core/src/test/java/fj/data/JavaTest.java index 2a9b6bfd..b8d99510 100644 --- a/core/src/test/java/fj/data/JavaTest.java +++ b/core/src/test/java/fj/data/JavaTest.java @@ -9,9 +9,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -/** - * Created by MarkPerry on 14/07/2014. - */ public class JavaTest { @Test diff --git a/core/src/test/java/fj/data/LazyStringTest.java b/core/src/test/java/fj/data/LazyStringTest.java index 4f6325af..060c5394 100644 --- a/core/src/test/java/fj/data/LazyStringTest.java +++ b/core/src/test/java/fj/data/LazyStringTest.java @@ -4,11 +4,9 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + -/** - * Created by MarkPerry on 11/06/2015. - */ public class LazyStringTest { @Test diff --git a/core/src/test/java/fj/data/ListBufferTest.java b/core/src/test/java/fj/data/ListBufferTest.java index 1dfbbc39..784b9510 100644 --- a/core/src/test/java/fj/data/ListBufferTest.java +++ b/core/src/test/java/fj/data/ListBufferTest.java @@ -3,11 +3,9 @@ import org.junit.Test; import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + -/** - * Created by MarkPerry on 17/08/2015. - */ public class ListBufferTest { @Test diff --git a/core/src/test/java/fj/data/ListTest.java b/core/src/test/java/fj/data/ListTest.java index 3b720fe6..720157e8 100644 --- a/core/src/test/java/fj/data/ListTest.java +++ b/core/src/test/java/fj/data/ListTest.java @@ -1,20 +1,40 @@ package fj.data; -import fj.Equal; -import fj.P2; +import fj.*; +import fj.control.Trampoline; import org.junit.Test; +import java.io.IOException; import java.util.Arrays; +import static fj.Function.constant; +import static fj.Ord.*; +import static fj.P.*; +import static fj.Semigroup.listSemigroup; +import static fj.data.Either.*; +import static fj.data.List.sequenceEither; +import static fj.data.List.sequenceEitherLeft; +import static fj.data.List.sequenceEitherRight; +import static fj.data.List.sequenceF; +import static fj.data.List.sequenceIO; +import static fj.data.List.sequenceList; +import static fj.data.List.sequenceOption; +import static fj.data.List.sequenceP1; +import static fj.data.List.sequenceSeq; +import static fj.data.List.sequenceSet; +import static fj.data.List.sequenceStream; +import static fj.data.List.sequenceTrampoline; +import static fj.data.List.sequenceValidation; +import static fj.data.List.*; +import static fj.data.Option.*; +import static fj.data.Validation.fail; +import static fj.data.Validation.*; import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; -/** - * Created by MarkPerry on 16/01/2015. - */ public class ListTest { @Test @@ -91,4 +111,190 @@ public void array() { assertThat(List.range(1, max + 1).array(Integer[].class), equalTo(ints)); } + @Test + public void testSequenceEither() { + assertEquals(right(nil()), sequenceEither(nil())); + assertEquals(right(single("zero")), sequenceEither(single(right("zero")))); + assertEquals(left("zero"), sequenceEither(single(left("zero")))); + } + + @Test + public void testSequenceEitherLeft() { + assertEquals(left(nil()), sequenceEitherLeft(nil())); + assertEquals(left(single("zero")), sequenceEitherLeft(single(left("zero")))); + assertEquals(right("zero"), sequenceEitherLeft(single(right("zero")))); + } + + @Test + public void testSequenceEitherRight() { + assertEquals(right(nil()), sequenceEitherRight(nil())); + assertEquals(right(single("zero")), sequenceEitherRight(single(right("zero")))); + assertEquals(left("zero"), sequenceEitherRight(single(left("zero")))); + } + + @Test + public void testSequenceF() { + assertEquals(constant(nil()).f(1), sequenceF(nil()).f(1)); + assertEquals(constant(single("zero")).f(1), sequenceF(single(constant("zero"))).f(1)); + } + + @Test + public void testSequenceIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(nil())).run(), sequenceIO(nil()).run()); + assertEquals(IOFunctions.lazy(constant(single("zero"))).run(), sequenceIO(single(IOFunctions.lazy(constant("zero")))).run()); + } + + @Test + public void testSequenceList() { + assertEquals(single(nil()), sequenceList(nil())); + assertEquals(List.nil(), sequenceList(single(nil()))); + assertEquals(single(single("zero")), sequenceList(single(single("zero")))); + assertEquals(List.arrayList(single("zero"), single("one")), sequenceList(single(arrayList("zero", "one")))); + } + + @Test + public void testSequenceOption() { + assertEquals(some(nil()), sequenceOption(nil())); + assertEquals(none(), sequenceOption(single(none()))); + assertEquals(some(single("zero")), sequenceOption(single(some("zero")))); + } + + @Test + public void testSequenceP1() { + assertEquals(p(nil()), sequenceP1(nil())); + assertEquals(p(single("zero")), sequenceP1(single(p("zero")))); + } + + @Test + public void testSequenceSeq() { + assertEquals(Seq.single(nil()), sequenceSeq(nil())); + assertEquals(Seq.empty(), sequenceSeq(single(Seq.empty()))); + assertEquals(Seq.single(single("zero")), sequenceSeq(single(Seq.single("zero")))); + assertEquals(Seq.arraySeq(single("zero"), single("one")), sequenceSeq(single(Seq.arraySeq("zero", "one")))); + } + + @Test + public void testSequenceSet() { + assertEquals(Set.arraySet(listOrd(stringOrd), nil()), sequenceSet(stringOrd, nil())); + assertEquals(Set.empty(listOrd(stringOrd)), sequenceSet(stringOrd, single(Set.empty(stringOrd)))); + assertEquals(Set.arraySet(listOrd(stringOrd), single("zero")), sequenceSet(stringOrd, single(Set.single(stringOrd, "zero")))); + assertEquals(Set.arraySet(listOrd(stringOrd), single("zero"), single("one")), sequenceSet(stringOrd, single(Set.arraySet(stringOrd, "zero", "one")))); + } + + @Test + public void testSequenceStream() { + assertEquals(Stream.single(nil()), sequenceStream(nil())); + assertEquals(Stream.nil(), sequenceStream(single(Stream.nil()))); + assertEquals(Stream.single(single("zero")), sequenceStream(single(Stream.single("zero")))); + assertEquals(Stream.arrayStream(single("zero"), single("one")), sequenceStream(single(Stream.arrayStream("zero", "one")))); + } + + @Test + public void testSequenceTrampoline() { + assertEquals(Trampoline.pure(nil()).run(), sequenceTrampoline(nil()).run()); + assertEquals(Trampoline.pure(single(0)).run(), sequenceTrampoline(single(Trampoline.pure(0))).run()); + } + + @Test + public void testSequenceValidation() { + assertEquals(Validation.success(nil()), sequenceValidation(listSemigroup(), nil())); + assertEquals(Validation.fail(single(0)), sequenceValidation(listSemigroup(), single(Validation.fail(single(0))))); + assertEquals(Validation.success(single(0)), sequenceValidation(listSemigroup(), single(Validation.success(0)))); + } + + @Test + public void testTraverseEitherLeft() { + assertEquals(left(nil()), nil().traverseEitherLeft(constant(left(0)))); + assertEquals(left(single(0)), single("zero").traverseEitherLeft(constant(left(0)))); + assertEquals(left(nil()), nil().traverseEitherLeft(constant(right(0)))); + assertEquals(right(0), single("zero").traverseEitherLeft(constant(right(0)))); + } + + @Test + public void testTraverseEitherRight() { + assertEquals(right(nil()), nil().traverseEitherRight(constant(right(0)))); + assertEquals(right(single(0)), single("zero").traverseEitherRight(constant(right(0)))); + assertEquals(right(nil()), nil().traverseEitherRight(constant(left(0)))); + assertEquals(left(0), single("zero").traverseEitherRight(constant(left(0)))); + } + + @Test + public void testTraverseF() { + assertEquals(constant(nil()).f(1), nil().traverseF(constant(constant(0))).f(1)); + assertEquals(constant(single(0)).f(1), single("zero").traverseF(constant(constant(0))).f(1)); + } + + @Test + public void testTraverseIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(nil())).run(), nil().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(IOFunctions.lazy(constant(single(0))).run(), single("zero").traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseList() { + assertEquals(single(nil()), nil().traverseList(constant(List.nil()))); + assertEquals(List.nil(), single("zero").traverseList(constant(List.nil()))); + assertEquals(single(nil()), nil().traverseList(constant(single(0)))); + assertEquals(single(single(0)), single("zero").traverseList(constant(single(0)))); + assertEquals(single(nil()), nil().traverseList(constant(List.arrayList(0, 1)))); + assertEquals(List.arrayList(single(0), single(1)), single("zero").traverseList(constant(List.arrayList(0, 1)))); + } + + @Test + public void testTraverseOption() { + assertEquals(some(nil()), nil().traverseOption(constant(none()))); + assertEquals(none(), single("zero").traverseOption(constant(none()))); + assertEquals(some(nil()), nil().traverseOption(constant(some(0)))); + assertEquals(some(single(0)), single("zero").traverseOption(constant(some(0)))); + } + + @Test + public void testTraverseP1() { + assertEquals(p(nil()), nil().traverseP1(constant(p(0)))); + assertEquals(p(single(0)), single("zero").traverseP1(constant(p(0)))); + } + + @Test + public void testTraverseSeq() { + assertEquals(Seq.single(nil()), nil().traverseSeq(constant(Seq.empty()))); + assertEquals(Seq.empty(), single("zero").traverseSeq(constant(Seq.empty()))); + assertEquals(Seq.single(nil()), nil().traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(single(0)), single("zero").traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(nil()), nil().traverseSeq(constant(Seq.arraySeq(0, 1)))); + assertEquals(Seq.arraySeq(single(0), single(1)), single("zero").traverseSeq(constant(Seq.arraySeq(0, 1)))); + } + + @Test + public void testTraverseSet() { + assertEquals(Set.arraySet(listOrd(intOrd), nil()), nil().traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.empty(listOrd(intOrd)), single("zero").traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.single(listOrd(intOrd), nil()), nil().traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(listOrd(intOrd), single(0)), single("zero").traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(listOrd(intOrd), nil()), nil().traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + assertEquals(Set.arraySet(listOrd(intOrd), single(0), single(1)), single("zero").traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + } + + @Test + public void testTraverseStream() { + assertEquals(Stream.single(nil()), nil().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.nil(), single("zero").traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(nil()), nil().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(single(0)), single("zero").traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(nil()), nil().traverseStream(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.arrayStream(single(0), single(1)), single("zero").traverseStream(constant(Stream.arrayStream(0, 1)))); + } + + @Test + public void testTraverseTrampoline() { + assertEquals(Trampoline.pure(nil()).run(), nil().traverseTrampoline(constant(Trampoline.pure(0))).run()); + assertEquals(Trampoline.pure(single(0)).run(), single("zero").traverseTrampoline(constant(Trampoline.pure(0))).run()); + } + + @Test + public void testTraverseValidation() { + assertEquals(success(nil()), nil().traverseValidation(listSemigroup(), constant(Validation.fail(single(0))))); + assertEquals(fail(single(0)), single("zero").traverseValidation(listSemigroup(), constant(Validation.fail(single(0))))); + assertEquals(success(nil()), nil().traverseValidation(listSemigroup(), constant(Validation.success(0)))); + assertEquals(success(single(0)), single("zero").traverseValidation(listSemigroup(), constant(Validation.success(0)))); + } } diff --git a/core/src/test/java/fj/data/List_Traverse_Tests.java b/core/src/test/java/fj/data/List_Traverse_Tests.java index 3d964f45..eee5752d 100644 --- a/core/src/test/java/fj/data/List_Traverse_Tests.java +++ b/core/src/test/java/fj/data/List_Traverse_Tests.java @@ -7,11 +7,9 @@ import static fj.data.List.list; import static fj.data.Option.some; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + -/** - * Created by amar on 28/12/14. - */ public class List_Traverse_Tests { @Test diff --git a/core/src/test/java/fj/data/OptionTest.java b/core/src/test/java/fj/data/OptionTest.java index 3eeed489..e03d7aae 100644 --- a/core/src/test/java/fj/data/OptionTest.java +++ b/core/src/test/java/fj/data/OptionTest.java @@ -1,15 +1,37 @@ package fj.data; -import org.junit.Assert; +import fj.P; +import fj.control.Trampoline; import org.junit.Test; -import static fj.data.Option.some; -import static org.junit.Assert.assertTrue; +import java.io.IOException; -/** - * Created by MarkPerry on 15/01/2015. - */ -public class OptionTest { +import static fj.Function.constant; +import static fj.Ord.*; +import static fj.P.*; +import static fj.data.Either.*; +import static fj.data.List.*; +import static fj.data.Option.iif; +import static fj.data.Option.sequence; +import static fj.data.Option.sequenceEither; +import static fj.data.Option.sequenceEitherLeft; +import static fj.data.Option.sequenceEitherRight; +import static fj.data.Option.sequenceF; +import static fj.data.Option.sequenceIO; +import static fj.data.Option.sequenceList; +import static fj.data.Option.sequenceOption; +import static fj.data.Option.sequenceP1; +import static fj.data.Option.sequenceSeq; +import static fj.data.Option.sequenceSet; +import static fj.data.Option.sequenceStream; +import static fj.data.Option.sequenceTrampoline; +import static fj.data.Option.sequenceValidation; +import static fj.data.Option.*; +import static fj.data.Validation.fail; +import static fj.data.Validation.*; +import static org.junit.Assert.*; + +public final class OptionTest { @Test public void equals() { @@ -26,4 +48,386 @@ public void traverseList() { assertTrue(actual.equals(expected)); } + @Test + public void sequenceListTest() { + assertEquals(some(nil()), sequence(nil())); + assertEquals(none(), sequence(arrayList(none()))); + assertEquals(some(arrayList(1)), sequence(arrayList(some(1)))); + assertEquals(none(), sequence(arrayList(none(), none()))); + assertEquals(none(), sequence(arrayList(some(1), none()))); + assertEquals(none(), sequence(arrayList(none(), some(2)))); + assertEquals(some(arrayList(1, 2)), sequence(arrayList(some(1), some(2)))); + } + + @Test + public void sequenceValidationTest() { + assertEquals(some(fail(1)), sequence(Validation.>fail(1))); + assertEquals(none(), sequence(Validation.>success(none()))); + assertEquals(some(success("string")), sequence(Validation.>success(some("string")))); + } + + @Test + public void testBind1() { + range(0, 1).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), 0), list.index(0).bind(Option::some)); + }); + + } + + @Test + public void testBind2() { + range(0, 2).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1)), list.index(0).bind(list.index(1), p2())); + }); + } + + @Test + public void testBind3() { + range(0, 3).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2)), list.index(0).bind(list.index(1), list.index(2), p3())); + }); + + } + + @Test + public void testBind4() { + range(0, 4).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3)), list.index(0).bind(list.index(1), list.index(2), list.index(3), p4())); + }); + + } + + @Test + public void testBind5() { + range(0, 5).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), p5())); + }); + } + + @Test + public void testBind6() { + range(0, 6).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), p6())); + }); + } + + @Test + public void testBind7() { + range(0, 7).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), p7())); + }); + } + + @Test + public void testBind8() { + range(0, 8).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6, 7)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7), P.p8())); + }); + } + + @Test + public void testBindProduct2() { + range(0, 2).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1)), list.index(0).bindProduct(list.index(1))); + }); + } + + @Test + public void testBindProduct3() { + range(0, 3).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2)), list.index(0).bindProduct(list.index(1), list.index(2))); + }); + + } + + @Test + public void testBindProduct4() { + range(0, 4).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3))); + }); + + } + + @Test + public void testBindProduct5() { + range(0, 5).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4))); + }); + } + + @Test + public void testBindProduct6() { + range(0, 6).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5))); + }); + } + + @Test + public void testBindProduct7() { + range(0, 7).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6))); + }); + } + + @Test + public void testBindProduct8() { + range(0, 8).map(i -> arrayList(Option.none(), some(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6, 7)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7))); + }); + } + + @Test + public void testSequenceEither() { + assertEquals(right(none()), sequenceEither(none())); + assertEquals(right(some("zero")), sequenceEither(some(right("zero")))); + assertEquals(left("zero"), sequenceEither(some(left("zero")))); + } + + @Test + public void testSequenceEitherLeft() { + assertEquals(left(none()), sequenceEitherLeft(none())); + assertEquals(left(some("zero")), sequenceEitherLeft(some(left("zero")))); + assertEquals(right("zero"), sequenceEitherLeft(some(right("zero")))); + } + + @Test + public void testSequenceEitherRight() { + assertEquals(right(none()), sequenceEitherRight(none())); + assertEquals(right(some("zero")), sequenceEitherRight(some(right("zero")))); + assertEquals(left("zero"), sequenceEitherRight(some(left("zero")))); + } + + @Test + public void testSequenceF() { + assertEquals(constant(none()).f(1), sequenceF(none()).f(1)); + assertEquals(constant(some("zero")).f(1), sequenceF(some(constant("zero"))).f(1)); + } + + @Test + public void testSequenceIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(none())).run(), sequenceIO(none()).run()); + assertEquals(IOFunctions.lazy(constant(some("zero"))).run(), sequenceIO(some(IOFunctions.lazy(constant("zero")))).run()); + } + + @Test + public void testSequenceList() { + assertEquals(List.single(none()), sequenceList(none())); + assertEquals(List.nil(), sequenceList(some(List.nil()))); + assertEquals(List.single(some("zero")), sequenceList(some(List.single("zero")))); + assertEquals(List.arrayList(some("zero"), some("one")), sequenceList(some(List.arrayList("zero", "one")))); + } + + @Test + public void testSequenceOption() { + assertEquals(some(none()), sequenceOption(none())); + assertEquals(none(), sequenceOption(some(none()))); + assertEquals(some(some("zero")), sequenceOption(some(some("zero")))); + } + + @Test + public void testSequenceP1() { + assertEquals(p(none()), sequenceP1(none())); + assertEquals(p(some("zero")), sequenceP1(some(p("zero")))); + } + + @Test + public void testSequenceSeq() { + assertEquals(Seq.single(none()), sequenceSeq(none())); + assertEquals(Seq.empty(), sequenceSeq(some(Seq.empty()))); + assertEquals(Seq.single(some("zero")), sequenceSeq(some(Seq.single("zero")))); + assertEquals(Seq.arraySeq(some("zero"), some("one")), sequenceSeq(some(Seq.arraySeq("zero", "one")))); + } + + @Test + public void testSequenceSet() { + assertEquals(Set.arraySet(optionOrd(stringOrd), none()), sequenceSet(stringOrd, none())); + assertEquals(Set.empty(optionOrd(stringOrd)), sequenceSet(stringOrd, some(Set.empty(stringOrd)))); + assertEquals(Set.arraySet(optionOrd(stringOrd), some("zero")), sequenceSet(stringOrd, some(Set.single(stringOrd, "zero")))); + assertEquals(Set.arraySet(optionOrd(stringOrd), some("zero"), some("one")), sequenceSet(stringOrd, some(Set.arraySet(stringOrd, "zero", "one")))); + } + + @Test + public void testSequenceStream() { + assertEquals(Stream.single(none()), sequenceStream(none())); + assertEquals(Stream.nil(), sequenceStream(some(Stream.nil()))); + assertEquals(Stream.single(some("zero")), sequenceStream(some(Stream.single("zero")))); + assertEquals(Stream.arrayStream(some("zero"), some("one")), sequenceStream(some(Stream.arrayStream("zero", "one")))); + } + + @Test + public void testSequenceTrampoline() { + assertEquals(Trampoline.pure(none()).run(), sequenceTrampoline(none()).run()); + assertEquals(Trampoline.pure(some(0)).run(), sequenceTrampoline(some(Trampoline.pure(0))).run()); + } + + @Test + public void testSequenceValidation() { + assertEquals(Validation.success(none()), sequenceValidation(none())); + assertEquals(Validation.fail(0), sequenceValidation(some(Validation.fail(0)))); + assertEquals(Validation.success(some(0)), sequenceValidation(some(Validation.success(0)))); + } + + @Test + public void testTraverseEither() { + assertEquals(right(none()), none().traverseEither(constant(right(0)))); + assertEquals(right(some(0)), some("zero").traverseEither(constant(right(0)))); + assertEquals(right(none()), none().traverseEither(constant(left(0)))); + assertEquals(left(0), some("zero").traverseEither(constant(left(0)))); + } + + @Test + public void testTraverseEitherLeft() { + assertEquals(left(none()), none().traverseEitherLeft(constant(left(0)))); + assertEquals(left(some(0)), some("zero").traverseEitherLeft(constant(left(0)))); + assertEquals(left(none()), none().traverseEitherLeft(constant(right(0)))); + assertEquals(right(0), some("zero").traverseEitherLeft(constant(right(0)))); + } + + @Test + public void testTraverseEitherRight() { + assertEquals(right(none()), none().traverseEitherRight(constant(right(0)))); + assertEquals(right(some(0)), some("zero").traverseEitherRight(constant(right(0)))); + assertEquals(right(none()), none().traverseEitherRight(constant(left(0)))); + assertEquals(left(0), some("zero").traverseEitherRight(constant(left(0)))); + } + + @Test + public void testTraverseF() { + assertEquals(constant(none()).f(1), none().traverseF(constant(constant(0))).f(1)); + assertEquals(constant(some(0)).f(1), some("zero").traverseF(constant(constant(0))).f(1)); + } + + @Test + public void testTraverseIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(none())).run(), none().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(IOFunctions.lazy(constant(some(0))).run(), some("zero").traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseList() { + assertEquals(List.single(none()), none().traverseList(constant(List.nil()))); + assertEquals(List.nil(), some("zero").traverseList(constant(List.nil()))); + assertEquals(List.single(none()), none().traverseList(constant(List.single(0)))); + assertEquals(List.single(some(0)), some("zero").traverseList(constant(List.single(0)))); + assertEquals(List.single(none()), none().traverseList(constant(List.arrayList(0, 1)))); + assertEquals(List.arrayList(some(0), some(1)), some("zero").traverseList(constant(List.arrayList(0, 1)))); + } + + @Test + public void testTraverseOption() { + assertEquals(some(none()), none().traverseOption(constant(none()))); + assertEquals(none(), some("zero").traverseOption(constant(none()))); + assertEquals(some(none()), none().traverseOption(constant(some(0)))); + assertEquals(some(some(0)), some("zero").traverseOption(constant(some(0)))); + } + + @Test + public void testTraverseP1() { + assertEquals(p(none()), none().traverseP1(constant(p(0)))); + assertEquals(p(some(0)), some("zero").traverseP1(constant(p(0)))); + } + + @Test + public void testTraverseSeq() { + assertEquals(Seq.single(none()), none().traverseSeq(constant(Seq.empty()))); + assertEquals(Seq.empty(), some("zero").traverseSeq(constant(Seq.empty()))); + assertEquals(Seq.single(none()), none().traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(some(0)), some("zero").traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(none()), none().traverseSeq(constant(Seq.arraySeq(0, 1)))); + assertEquals(Seq.arraySeq(some(0), some(1)), some("zero").traverseSeq(constant(Seq.arraySeq(0, 1)))); + } + + @Test + public void testTraverseSet() { + assertEquals(Set.arraySet(optionOrd(intOrd), none()), none().traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.empty(optionOrd(intOrd)), some("zero").traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.arraySet(optionOrd(intOrd), none()), none().traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.arraySet(optionOrd(intOrd), some(0)), some("zero").traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.arraySet(optionOrd(intOrd), none()), none().traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + assertEquals(Set.arraySet(optionOrd(intOrd), some(0), some(1)), some("zero").traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + } + + @Test + public void testTraverseStream() { + assertEquals(Stream.single(none()), none().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.nil(), some("zero").traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(none()), none().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(some(0)), some("zero").traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(none()), none().traverseStream(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.arrayStream(some(0), some(1)), some("zero").traverseStream(constant(Stream.arrayStream(0, 1)))); + } + + @Test + public void testTraverseTrampoline() { + assertEquals(Trampoline.pure(none()).run(), none().traverseTrampoline(constant(Trampoline.pure(0))).run()); + assertEquals(Trampoline.pure(some(0)).run(), some("zero").traverseTrampoline(constant(Trampoline.pure(0))).run()); + } + + @Test + public void testTraverseValidation() { + assertEquals(success(none()), none().traverseValidation(constant(Validation.fail(0)))); + assertEquals(fail(0), some("zero").traverseValidation(constant(Validation.fail(0)))); + assertEquals(success(none()), none().traverseValidation(constant(Validation.success(0)))); + assertEquals(success(some(0)), some("zero").traverseValidation(constant(Validation.success(0)))); + } } diff --git a/core/src/test/java/fj/data/SeqTest.java b/core/src/test/java/fj/data/SeqTest.java index a7c85175..8a78ab31 100644 --- a/core/src/test/java/fj/data/SeqTest.java +++ b/core/src/test/java/fj/data/SeqTest.java @@ -1,17 +1,38 @@ package fj.data; import fj.P2; +import fj.control.Trampoline; import org.junit.Test; +import java.io.IOException; + +import static fj.Function.constant; +import static fj.Ord.*; +import static fj.P.p; +import static fj.Semigroup.listSemigroup; +import static fj.data.Either.*; +import static fj.data.List.arrayList; +import static fj.data.Option.*; +import static fj.data.Seq.sequenceEither; +import static fj.data.Seq.sequenceEitherLeft; +import static fj.data.Seq.sequenceEitherRight; +import static fj.data.Seq.sequenceF; +import static fj.data.Seq.sequenceIO; +import static fj.data.Seq.sequenceList; +import static fj.data.Seq.sequenceOption; +import static fj.data.Seq.sequenceP1; +import static fj.data.Seq.sequenceSeq; +import static fj.data.Seq.sequenceSet; +import static fj.data.Seq.sequenceStream; +import static fj.data.Seq.sequenceTrampoline; +import static fj.data.Seq.sequenceValidation; +import static fj.data.Seq.*; +import static fj.data.Validation.fail; +import static fj.data.Validation.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -/** - * Created by MarkPerry on 16/01/2015. - */ public class SeqTest { @Test @@ -46,4 +67,224 @@ public void test() { assertThat(p2._2(), is(Seq.empty())); } + @Test + public void testBind() { + assertEquals(empty(), empty().bind(constant(empty()))); + assertEquals(empty(), empty().bind(constant(single(0)))); + assertEquals(empty(), empty().bind(constant(arraySeq(0, 1)))); + assertEquals(empty(), single("zero").bind(constant(empty()))); + assertEquals(single(0), single("zero").bind(constant(single(0)))); + assertEquals(arraySeq(0, 1), single("zero").bind(constant(arraySeq(0, 1)))); + assertEquals(empty(), arraySeq("zero", "one").bind(constant(empty()))); + assertEquals(arraySeq(0, 0), arraySeq("zero", "one").bind(constant(single(0)))); + assertEquals(arraySeq(0, 1, 0, 1), arraySeq("zero", "one").bind(constant(arraySeq(0, 1)))); + } + + @Test + public void testSequenceEither() { + assertEquals(right(empty()), sequenceEither(empty())); + assertEquals(right(single("zero")), sequenceEither(single(right("zero")))); + assertEquals(left("zero"), sequenceEither(single(left("zero")))); + } + + @Test + public void testSequenceEitherLeft() { + assertEquals(left(empty()), sequenceEitherLeft(empty())); + assertEquals(left(single("zero")), sequenceEitherLeft(single(left("zero")))); + assertEquals(right("zero"), sequenceEitherLeft(single(right("zero")))); + } + + @Test + public void testSequenceEitherRight() { + assertEquals(right(empty()), sequenceEitherRight(empty())); + assertEquals(right(single("zero")), sequenceEitherRight(single(right("zero")))); + assertEquals(left("zero"), sequenceEitherRight(single(left("zero")))); + } + + @Test + public void testSequenceF() { + assertEquals(constant(empty()).f(1), sequenceF(empty()).f(1)); + assertEquals(constant(single("zero")).f(1), sequenceF(single(constant("zero"))).f(1)); + } + + @Test + public void testSequenceIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(empty())).run(), sequenceIO(empty()).run()); + assertEquals(IOFunctions.lazy(constant(single("zero"))).run(), sequenceIO(single(IOFunctions.lazy(constant("zero")))).run()); + } + + @Test + public void testSequenceList() { + assertEquals(List.single(empty()), sequenceList(empty())); + assertEquals(List.nil(), sequenceList(single(List.nil()))); + assertEquals(List.single(single("zero")), sequenceList(single(List.single("zero")))); + assertEquals(arrayList(single("zero"), single("one")), sequenceList(single(arrayList("zero", "one")))); + } + + @Test + public void testSequenceOption() { + assertEquals(some(empty()), sequenceOption(empty())); + assertEquals(none(), sequenceOption(single(none()))); + assertEquals(some(single("zero")), sequenceOption(single(some("zero")))); + } + + @Test + public void testSequenceP1() { + assertEquals(p(empty()), sequenceP1(empty())); + assertEquals(p(single("zero")), sequenceP1(single(p("zero")))); + } + + @Test + public void testSequenceSeq() { + assertEquals(single(empty()), sequenceSeq(empty())); + assertEquals(empty(), sequenceSeq(single(empty()))); + assertEquals(single(single("zero")), sequenceSeq(single(single("zero")))); + assertEquals(arraySeq(single("zero"), single("one")), sequenceSeq(single(arraySeq("zero", "one")))); + } + + @Test + public void testSequenceSet() { + assertEquals(Set.arraySet(seqOrd(stringOrd), empty()), sequenceSet(stringOrd, empty())); + assertEquals(Set.empty(seqOrd(stringOrd)), sequenceSet(stringOrd, single(Set.empty(stringOrd)))); + assertEquals(Set.arraySet(seqOrd(stringOrd), single("zero")), sequenceSet(stringOrd, single(Set.single(stringOrd, "zero")))); + assertEquals(Set.arraySet(seqOrd(stringOrd), single("zero"), single("one")), sequenceSet(stringOrd, single(Set.arraySet(stringOrd, "zero", "one")))); + } + + @Test + public void testSequenceStream() { + assertEquals(Stream.single(empty()), sequenceStream(empty())); + assertEquals(Stream.nil(), sequenceStream(single(Stream.nil()))); + assertEquals(Stream.single(single("zero")), sequenceStream(single(Stream.single("zero")))); + assertEquals(Stream.arrayStream(single("zero"), single("one")), sequenceStream(single(Stream.arrayStream("zero", "one")))); + } + + @Test + public void testSequenceTrampoline() { + assertEquals(Trampoline.pure(empty()).run(), sequenceTrampoline(empty()).run()); + assertEquals(Trampoline.pure(single(0)).run(), sequenceTrampoline(single(Trampoline.pure(0))).run()); + } + + @Test + public void testSequenceValidation() { + assertEquals(success(empty()), sequenceValidation(empty())); + assertEquals(fail(single(0)), sequenceValidation(single(fail(single(0))))); + assertEquals(success(single(0)), sequenceValidation(single(success(0)))); + } + + @Test + public void testSequenceValidationSemigroup() { + assertEquals(success(empty()), sequenceValidation(listSemigroup(), empty())); + assertEquals(fail(List.single(0)), sequenceValidation(listSemigroup(), single(fail(List.single(0))))); + assertEquals(success(single(0)), sequenceValidation(listSemigroup(), single(success(0)))); + } + + @Test + public void testTraverseEitherLeft() { + assertEquals(left(empty()), empty().traverseEitherLeft(constant(left(0)))); + assertEquals(left(single(0)), single("zero").traverseEitherLeft(constant(left(0)))); + assertEquals(left(empty()), empty().traverseEitherLeft(constant(right(0)))); + assertEquals(right(0), single("zero").traverseEitherLeft(constant(right(0)))); + } + + @Test + public void testTraverseEitherRight() { + assertEquals(right(empty()), empty().traverseEitherRight(constant(right(0)))); + assertEquals(right(single(0)), single("zero").traverseEitherRight(constant(right(0)))); + assertEquals(right(empty()), empty().traverseEitherRight(constant(left(0)))); + assertEquals(left(0), single("zero").traverseEitherRight(constant(left(0)))); + } + + @Test + public void testTraverseF() { + assertEquals(constant(empty()).f(1), empty().traverseF(constant(constant(0))).f(1)); + assertEquals(constant(single(0)).f(1), single("zero").traverseF(constant(constant(0))).f(1)); + } + + @Test + public void testTraverseIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(empty())).run(), empty().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(IOFunctions.lazy(constant(single(0))).run(), single("zero").traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseList() { + assertEquals(List.single(empty()), empty().traverseList(constant(List.nil()))); + assertEquals(List.nil(), single("zero").traverseList(constant(List.nil()))); + assertEquals(List.single(empty()), empty().traverseList(constant(List.single(0)))); + assertEquals(List.single(single(0)), single("zero").traverseList(constant(List.single(0)))); + assertEquals(List.single(empty()), empty().traverseList(constant(arrayList(0, 1)))); + assertEquals(arrayList(single(0), single(1)), single("zero").traverseList(constant(arrayList(0, 1)))); + } + + @Test + public void testTraverseOption() { + assertEquals(some(empty()), empty().traverseOption(constant(none()))); + assertEquals(none(), single("zero").traverseOption(constant(none()))); + assertEquals(some(empty()), empty().traverseOption(constant(some(0)))); + assertEquals(some(single(0)), single("zero").traverseOption(constant(some(0)))); + } + + @Test + public void testTraverseP1() { + assertEquals(p(empty()), empty().traverseP1(constant(p(0)))); + assertEquals(p(single(0)), single("zero").traverseP1(constant(p(0)))); + } + + @Test + public void testTraverseSeq() { + assertEquals(single(empty()), empty().traverseSeq(constant(empty()))); + assertEquals(empty(), single("zero").traverseSeq(constant(empty()))); + assertEquals(single(empty()), empty().traverseSeq(constant(single(0)))); + assertEquals(single(single(0)), single("zero").traverseSeq(constant(single(0)))); + assertEquals(single(empty()), empty().traverseSeq(constant(arraySeq(0, 1)))); + assertEquals(arraySeq(single(0), single(1)), single("zero").traverseSeq(constant(arraySeq(0, 1)))); + } + + @Test + public void testTraverseSet() { + assertEquals(Set.arraySet(seqOrd(intOrd), empty()), empty().traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.empty(seqOrd(intOrd)), single("zero").traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.single(seqOrd(intOrd), empty()), empty().traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(seqOrd(intOrd), single(0)), single("zero").traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(seqOrd(intOrd), empty()), empty().traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + assertEquals(Set.arraySet(seqOrd(intOrd), single(0), single(1)), single("zero").traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + } + + @Test + public void testTraverseStream() { + assertEquals(Stream.single(empty()), empty().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.nil(), single("zero").traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(empty()), empty().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(single(0)), single("zero").traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(empty()), empty().traverseStream(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.arrayStream(single(0), single(1)), single("zero").traverseStream(constant(Stream.arrayStream(0, 1)))); + } + + @Test + public void testTraverseTrampoline() { + assertEquals(Trampoline.pure(empty()).run(), empty().traverseTrampoline(constant(Trampoline.pure(0))).run()); + assertEquals(Trampoline.pure(single(0)).run(), single("zero").traverseTrampoline(constant(Trampoline.pure(0))).run()); + } + + @Test + public void testTraverseValidation() { + assertEquals(success(empty()), empty().traverseValidation(constant(fail(single(0))))); + assertEquals(fail(single(0)), single("zero").traverseValidation(constant(fail(single(0))))); + assertEquals(success(empty()), empty().traverseValidation(constant(success(0)))); + assertEquals(success(single(0)), single("zero").traverseValidation(constant(success(0)))); + + assertEquals(success(arraySeq(0, 2, 4, 6, 8)), arraySeq(0, 2, 4, 6, 8).traverseValidation(i -> condition(i% 2 == 0, List.single(i), i))); + assertEquals(fail(List.single(1)), arraySeq(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).traverseValidation(i -> condition(i% 2 == 0, List.single(i), i))); + } + + @Test + public void testTraverseValidationSemigroup() { + assertEquals(success(empty()), empty().traverseValidation(listSemigroup(), constant(fail(List.single(0))))); + assertEquals(fail(List.single(0)), single("zero").traverseValidation(listSemigroup(), constant(fail(List.single(0))))); + assertEquals(success(empty()), empty().traverseValidation(listSemigroup(), constant(success(0)))); + assertEquals(success(single(0)), single("zero").traverseValidation(listSemigroup(), constant(success(0)))); + + assertEquals(success(arraySeq(0, 2, 4, 6, 8)), arraySeq(0, 2, 4, 6, 8).traverseValidation(listSemigroup(),i -> condition(i% 2 == 0, List.single(i), i))); + assertEquals(fail(arrayList(1, 3, 5, 7, 9)), arraySeq(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).traverseValidation(listSemigroup(),i -> condition(i% 2 == 0, List.single(i), i))); + } } diff --git a/core/src/test/java/fj/data/SetTest.java b/core/src/test/java/fj/data/SetTest.java index cdf2519c..e30d166a 100644 --- a/core/src/test/java/fj/data/SetTest.java +++ b/core/src/test/java/fj/data/SetTest.java @@ -6,11 +6,8 @@ import static fj.data.Option.some; import static fj.Ord.intOrd; import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; -/** - * Created by MarkPerry on 18/08/2015. - */ public class SetTest { @Test diff --git a/core/src/test/java/fj/data/StateTest.java b/core/src/test/java/fj/data/StateTest.java index 9fc8a9fb..479efc38 100644 --- a/core/src/test/java/fj/data/StateTest.java +++ b/core/src/test/java/fj/data/StateTest.java @@ -2,14 +2,44 @@ import org.junit.Test; -/** - * Created by MarkPerry on 18/12/2014. - */ +import static fj.P.p; +import static org.junit.Assert.assertEquals; + public class StateTest { - @Test - public void map() { + @Test + public void testBind() { + assertEquals(p(2, "one"), state().run(1)); + assertEquals(p(3, "two"), state().run(2)); + assertEquals(p(4, "three"), state().run(3)); + assertEquals(p(2, "?"), state().bind(state -> State.constant("?")).run(1)); + assertEquals(p(3, "?"), state().bind(state -> State.constant("?")).run(2)); + assertEquals(p(4, "?"), state().bind(state -> State.constant("?")).run(3)); + } + + @Test + public void testFlatMap() { + assertEquals(p(2, "one"), state().run(1)); + assertEquals(p(3, "two"), state().run(2)); + assertEquals(p(4, "three"), state().run(3)); + assertEquals(p(2, "?"), state().flatMap(state -> State.constant("?")).run(1)); + assertEquals(p(3, "?"), state().flatMap(state -> State.constant("?")).run(2)); + assertEquals(p(4, "?"), state().flatMap(state -> State.constant("?")).run(3)); + } - } + private static final State state() { + return State.unit(i -> p(i + 1, toLapine(i))); + } + private static String toLapine( + final int i) { + return i == 1 ? + "one" : + i == 2 ? + "two" : + i == 3 ? + "three" : + i == 4 ? + "four" : "hrair"; + } } diff --git a/core/src/test/java/fj/data/StreamTest.java b/core/src/test/java/fj/data/StreamTest.java index 999a3a3e..febd4567 100644 --- a/core/src/test/java/fj/data/StreamTest.java +++ b/core/src/test/java/fj/data/StreamTest.java @@ -2,18 +2,42 @@ import fj.Equal; import fj.P2; +import fj.control.Trampoline; import org.junit.Test; +import java.io.IOException; import java.util.ConcurrentModificationException; +import static fj.Function.constant; +import static fj.Ord.*; +import static fj.P.p; +import static fj.Semigroup.listSemigroup; +import static fj.data.Either.left; +import static fj.data.Either.right; +import static fj.data.List.arrayList; +import static fj.data.Option.none; +import static fj.data.Option.some; +import static fj.data.Seq.arraySeq; +import static fj.data.Seq.empty; +import static fj.data.Stream.sequenceEitherLeft; +import static fj.data.Stream.sequenceEitherRight; +import static fj.data.Stream.sequenceF; +import static fj.data.Stream.sequenceIO; +import static fj.data.Stream.sequenceList; +import static fj.data.Stream.sequenceOption; +import static fj.data.Stream.sequenceP1; +import static fj.data.Stream.sequenceSeq; +import static fj.data.Stream.sequenceSet; +import static fj.data.Stream.sequenceStream; +import static fj.data.Stream.sequenceTrampoline; +import static fj.data.Stream.sequenceValidation; import static fj.data.Stream.*; +import static fj.data.Validation.*; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; -/** - * Created by Zheka Kozlov on 27.05.2015. - */ public class StreamTest { @Test @@ -103,4 +127,214 @@ public void testMinus() { assertThat(s1.minus(Equal.charEqual, s2), is(stream(new Character[]{'a', 'c', 'd'}))); } + + @Test + public void testSequenceEither() { + assertEquals(right(nil()), sequenceEither(nil())); + assertEquals(right(single("zero")), sequenceEither(single(right("zero")))); + assertEquals(left("zero"), sequenceEither(single(left("zero")))); + } + + @Test + public void testSequenceEitherLeft() { + assertEquals(left(nil()), sequenceEitherLeft(nil())); + assertEquals(left(single("zero")), sequenceEitherLeft(single(left("zero")))); + assertEquals(right("zero"), sequenceEitherLeft(single(right("zero")))); + } + + @Test + public void testSequenceEitherRight() { + assertEquals(right(nil()), sequenceEitherRight(nil())); + assertEquals(right(single("zero")), sequenceEitherRight(single(right("zero")))); + assertEquals(left("zero"), sequenceEitherRight(single(left("zero")))); + } + + @Test + public void testSequenceF() { + assertEquals(constant(nil()).f(1), sequenceF(nil()).f(1)); + assertEquals(constant(single("zero")).f(1), sequenceF(single(constant("zero"))).f(1)); + } + + @Test + public void testSequenceIO() + throws IOException { + assertEquals(IOFunctions.lazy(constant(nil())).run(), sequenceIO(nil()).run()); + assertEquals(IOFunctions.lazy(constant(single("zero"))).run(), sequenceIO(single(IOFunctions.lazy(constant("zero")))).run()); + } + + @Test + public void testSequenceList() { + assertEquals(List.single(nil()), sequenceList(nil())); + assertEquals(List.nil(), sequenceList(single(List.nil()))); + assertEquals(List.single(single("zero")), sequenceList(single(List.single("zero")))); + assertEquals(arrayList(single("zero"), single("one")), sequenceList(single(arrayList("zero", "one")))); + } + + @Test + public void testSequenceOption() { + assertEquals(some(nil()), sequenceOption(nil())); + assertEquals(none(), sequenceOption(single(none()))); + assertEquals(some(single("zero")), sequenceOption(single(some("zero")))); + } + + @Test + public void testSequenceP1() { + assertEquals(p(nil()), sequenceP1(nil())); + assertEquals(p(single("zero")), sequenceP1(single(p("zero")))); + } + + @Test + public void testSequenceSeq() { + assertEquals(Seq.single(nil()), sequenceSeq(nil())); + assertEquals(Seq.empty(), sequenceSeq(single(Seq.empty()))); + assertEquals(Seq.single(single("zero")), sequenceSeq(single(Seq.single("zero")))); + assertEquals(arraySeq(single("zero"), single("one")), sequenceSeq(single(arraySeq("zero", "one")))); + } + + @Test + public void testSequenceSet() { + assertEquals(Set.arraySet(streamOrd(stringOrd), nil()), sequenceSet(stringOrd, nil())); + assertEquals(Set.empty(streamOrd(stringOrd)), sequenceSet(stringOrd, single(Set.empty(stringOrd)))); + assertEquals(Set.arraySet(streamOrd(stringOrd), single("zero")), sequenceSet(stringOrd, single(Set.single(stringOrd, "zero")))); + assertEquals(Set.arraySet(streamOrd(stringOrd), single("zero"), single("one")), sequenceSet(stringOrd, single(Set.arraySet(stringOrd, "zero", "one")))); + } + + @Test + public void testSequenceStream() { + assertEquals(single(nil()), sequenceStream(nil())); + assertEquals(nil(), sequenceStream(single(nil()))); + assertEquals(single(single("zero")), sequenceStream(single(single("zero")))); + assertEquals(arrayStream(single("zero"), single("one")), sequenceStream(single(arrayStream("zero", "one")))); + } + + @Test + public void testSequenceTrampoline() { + assertEquals(Trampoline.pure(nil()).run(), sequenceTrampoline(nil()).run()); + assertEquals(Trampoline.pure(single(0)).run(), sequenceTrampoline(single(Trampoline.pure(0))).run()); + } + + @Test + public void testSequenceValidation() { + assertEquals(success(nil()), sequenceValidation(nil())); + assertEquals(fail(single(0)), sequenceValidation(single(fail(single(0))))); + assertEquals(success(single(0)), sequenceValidation(single(success(0)))); + } + + @Test + public void testSequenceValidationSemigroup() { + assertEquals(success(nil()), sequenceValidation(listSemigroup(), nil())); + assertEquals(fail(List.single(0)), sequenceValidation(listSemigroup(), single(fail(List.single(0))))); + assertEquals(success(single(0)), sequenceValidation(listSemigroup(), single(success(0)))); + } + + @Test + public void testTraverseEitherLeft() { + assertEquals(left(nil()), nil().traverseEitherLeft(constant(left(0)))); + assertEquals(left(single(0)), single("zero").traverseEitherLeft(constant(left(0)))); + assertEquals(left(nil()), nil().traverseEitherLeft(constant(right(0)))); + assertEquals(right(0), single("zero").traverseEitherLeft(constant(right(0)))); + } + + @Test + public void testTraverseEitherRight() { + assertEquals(right(nil()), nil().traverseEitherRight(constant(right(0)))); + assertEquals(right(single(0)), single("zero").traverseEitherRight(constant(right(0)))); + assertEquals(right(nil()), nil().traverseEitherRight(constant(left(0)))); + assertEquals(left(0), single("zero").traverseEitherRight(constant(left(0)))); + } + + @Test + public void testTraverseF() { + assertEquals(constant(nil()).f(1), nil().traverseF(constant(constant(0))).f(1)); + assertEquals(constant(single(0)).f(1), single("zero").traverseF(constant(constant(0))).f(1)); + } + + @Test + public void testTraverseIO() + throws IOException { + assertEquals(IOFunctions.lazy(constant(nil())).run(), nil().traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(IOFunctions.lazy(constant(single(0))).run(), single("zero").traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseList() { + assertEquals(List.single(nil()), nil().traverseList(constant(List.nil()))); + assertEquals(List.nil(), single("zero").traverseList(constant(List.nil()))); + assertEquals(List.single(nil()), nil().traverseList(constant(List.single(0)))); + assertEquals(List.single(single(0)), single("zero").traverseList(constant(List.single(0)))); + assertEquals(List.single(nil()), nil().traverseList(constant(arrayList(0, 1)))); + assertEquals(arrayList(single(0), single(1)), single("zero").traverseList(constant(arrayList(0, 1)))); + } + + @Test + public void testTraverseOption() { + assertEquals(some(nil()), nil().traverseOption(constant(none()))); + assertEquals(none(), single("zero").traverseOption(constant(none()))); + assertEquals(some(nil()), nil().traverseOption(constant(some(0)))); + assertEquals(some(single(0)), single("zero").traverseOption(constant(some(0)))); + } + + @Test + public void testTraverseP1() { + assertEquals(p(nil()), nil().traverseP1(constant(p(0)))); + assertEquals(p(single(0)), single("zero").traverseP1(constant(p(0)))); + } + + @Test + public void testTraverseSeq() { + assertEquals(Seq.single(nil()), nil().traverseSeq(constant(empty()))); + assertEquals(Seq.empty(), single("zero").traverseSeq(constant(empty()))); + assertEquals(Seq.single(nil()), nil().traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(single(0)), single("zero").traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(nil()), nil().traverseSeq(constant(arraySeq(0, 1)))); + assertEquals(arraySeq(single(0), single(1)), single("zero").traverseSeq(constant(arraySeq(0, 1)))); + } + + @Test + public void testTraverseSet() { + assertEquals(Set.arraySet(streamOrd(intOrd), nil()), nil().traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.empty(streamOrd(intOrd)), single("zero").traverseSet(intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.single(streamOrd(intOrd), nil()), nil().traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(streamOrd(intOrd), single(0)), single("zero").traverseSet(intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(streamOrd(intOrd), nil()), nil().traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + assertEquals(Set.arraySet(streamOrd(intOrd), single(0), single(1)), single("zero").traverseSet(intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + } + + @Test + public void testTraverseStream() { + assertEquals(Stream.single(nil()), nil().traverseStream(constant(Stream.nil()))); + assertEquals(Stream.nil(), single("zero").traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(nil()), nil().traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(single(0)), single("zero").traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(nil()), nil().traverseStream(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.arrayStream(single(0), single(1)), single("zero").traverseStream(constant(Stream.arrayStream(0, 1)))); + } + + @Test + public void testTraverseTrampoline() { + assertEquals(Trampoline.pure(nil()).run(), nil().traverseTrampoline(constant(Trampoline.pure(0))).run()); + assertEquals(Trampoline.pure(single(0)).run(), single("zero").traverseTrampoline(constant(Trampoline.pure(0))).run()); + } + + @Test + public void testTraverseValidation() { + assertEquals(success(nil()), nil().traverseValidation(constant(fail(single(0))))); + assertEquals(fail(single(0)), single("zero").traverseValidation(constant(fail(single(0))))); + assertEquals(success(nil()), nil().traverseValidation(constant(success(0)))); + assertEquals(success(single(0)), single("zero").traverseValidation(constant(success(0)))); + + assertEquals(success(arraySeq(0, 2, 4, 6, 8)), arraySeq(0, 2, 4, 6, 8).traverseValidation(i -> condition(i % 2 == 0, List.single(i), i))); + assertEquals(fail(List.single(1)), arraySeq(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).traverseValidation(i -> condition(i % 2 == 0, List.single(i), i))); + } + + @Test + public void testTraverseValidationSemigroup() { + assertEquals(success(nil()), nil().traverseValidation(listSemigroup(), constant(fail(List.single(0))))); + assertEquals(fail(List.single(0)), single("zero").traverseValidation(listSemigroup(), constant(fail(List.single(0))))); + assertEquals(success(nil()), nil().traverseValidation(listSemigroup(), constant(success(0)))); + assertEquals(success(single(0)), single("zero").traverseValidation(listSemigroup(), constant(success(0)))); + + assertEquals(success(arraySeq(0, 2, 4, 6, 8)), arraySeq(0, 2, 4, 6, 8).traverseValidation(listSemigroup(), i -> condition(i % 2 == 0, List.single(i), i))); + assertEquals(fail(arrayList(1, 3, 5, 7, 9)), arraySeq(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).traverseValidation(listSemigroup(), i -> condition(i % 2 == 0, List.single(i), i))); + } } diff --git a/core/src/test/java/fj/data/TreeMapTest.java b/core/src/test/java/fj/data/TreeMapTest.java index 96cb95b2..051f0773 100644 --- a/core/src/test/java/fj/data/TreeMapTest.java +++ b/core/src/test/java/fj/data/TreeMapTest.java @@ -16,12 +16,9 @@ import static fj.data.TreeMap.iterableTreeMap; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertTrue; -/** - * Created by MarkPerry on 11/01/2015. - */ public class TreeMapTest { @Test diff --git a/core/src/test/java/fj/data/TreeTest.java b/core/src/test/java/fj/data/TreeTest.java index 811826fb..a6028f37 100644 --- a/core/src/test/java/fj/data/TreeTest.java +++ b/core/src/test/java/fj/data/TreeTest.java @@ -1,15 +1,11 @@ package fj.data; -import org.junit.Assert; import org.junit.Test; import static fj.data.Tree.leaf; import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; -/** - * Created by MarkPerry on 29/08/2015. - */ public class TreeTest { @Test diff --git a/core/src/test/java/fj/data/TreeZipperTest.java b/core/src/test/java/fj/data/TreeZipperTest.java index c52f4111..c2549963 100644 --- a/core/src/test/java/fj/data/TreeZipperTest.java +++ b/core/src/test/java/fj/data/TreeZipperTest.java @@ -4,7 +4,7 @@ import static fj.data.Option.none; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class TreeZipperTest { @Test diff --git a/core/src/test/java/fj/data/UnitTest.java b/core/src/test/java/fj/data/UnitTest.java index eef15887..1fab0a7b 100644 --- a/core/src/test/java/fj/data/UnitTest.java +++ b/core/src/test/java/fj/data/UnitTest.java @@ -4,9 +4,6 @@ import org.junit.Assert; import org.junit.Test; -/** - * Created by MarkPerry on 17/01/2015. - */ public class UnitTest { @Test diff --git a/core/src/test/java/fj/data/ValidationTest.java b/core/src/test/java/fj/data/ValidationTest.java new file mode 100644 index 00000000..4505499c --- /dev/null +++ b/core/src/test/java/fj/data/ValidationTest.java @@ -0,0 +1,748 @@ +package fj.data; + +import fj.P; +import fj.P2; +import fj.Semigroup; +import fj.control.Trampoline; +import org.junit.Test; + +import java.io.IOException; + +import static fj.Function.*; +import static fj.Ord.*; +import static fj.P.*; +import static fj.Semigroup.firstSemigroup; +import static fj.data.Either.left; +import static fj.data.Either.right; +import static fj.data.List.*; +import static fj.data.Option.*; +import static fj.data.Validation.parseByte; +import static fj.data.Validation.parseDouble; +import static fj.data.Validation.parseFloat; +import static fj.data.Validation.parseInt; +import static fj.data.Validation.parseLong; +import static fj.data.Validation.parseShort; +import static fj.data.Validation.sequenceEitherLeft; +import static fj.data.Validation.sequenceEitherRight; +import static fj.data.Validation.sequenceF; +import static fj.data.Validation.sequenceIO; +import static fj.data.Validation.sequenceList; +import static fj.data.Validation.sequenceOption; +import static fj.data.Validation.sequenceP1; +import static fj.data.Validation.sequenceSeq; +import static fj.data.Validation.sequenceSet; +import static fj.data.Validation.sequenceStream; +import static fj.data.Validation.sequenceTrampoline; +import static fj.data.Validation.sequenceValidation; +import static fj.data.Validation.*; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; + + +public class ValidationTest { + @Test + public void testParseShort() { + final List> l = + List.list(parseShort("10"), parseShort("x"), parseShort("20")); + assertThat(successes(l).foldLeft1((s, a) -> (short)(s + a)), is((short)30)); + } + + @Test + public void testParseLong() { + final List> l = + List.list(parseLong("10"), parseLong("x"), parseLong("20")); + P2, List> p2 = partition(l); + assertThat(p2._1().length(), is(1)); + assertThat(p2._2().length(), is(2)); + } + + @Test + public void testParseInt() { + final List> l = + List.list(parseInt("10"), parseInt("x"), parseInt("20")); + assertThat(l.map(v -> v.validation(e -> 0, i -> 1)).foldLeft1((s, a) -> s + a), + is(2)); + } + + @Test + public void testParseFloat() { + final List> l = + List.list(parseFloat("2.0"), parseFloat("x"), parseFloat("3.0")); + assertThat(l.map(v -> v.validation(e -> 0, i -> 1)).foldLeft1((s, a) -> s + a), + is(2)); + } + + @Test + public void testParseByte() { + final List> l = + List.list(parseByte("10"), parseByte("x"), parseByte("-10")); + assertThat(l.map(v -> v.validation(e -> 0, i -> 1)).foldLeft1((s, a) -> s + a), + is(2)); + } + + @Test + public void testAccumulate1() { + final Validation, Double> v = + parseDouble("10.0").accumulate( + f1 -> f1); + assertThat(v.success(), is(10.0)); + } + + @Test + public void testAccumulate1Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + f1 -> f1); + assertThat(v.fail().length(), is(1)); + } + + @Test + public void testAccumulate2() { + final Validation, Double> v = + parseDouble("1.0").accumulate( + parseDouble("2.0"), + (f1, f2) -> f1 + f2); + assertThat(v.success(), is(3.0)); + } + + @Test + public void testAccumulate2Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + parseDouble("y"), + (f1, f2) -> f1 + f2); + assertThat(v.fail().length(), is(2)); + } + + @Test + public void testAccumulate3() { + final Validation, Double> v = + parseDouble("1.0").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + (f1, f2, f3) -> f1 + f2 + f3); + assertThat(v.success(), is(6.0)); + } + + @Test + public void testAccumulate3Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + parseDouble("2.0"), + parseDouble("y"), + (f1, f2, f3) -> f1 + f2 + f3); + assertThat(v.fail().length(), is(2)); + } + + @Test + public void testAccumulate4() { + final Validation, Double> v = + parseDouble("1.0").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + (f1, f2, f3, f4) -> f1 + f2 + f3 + f4); + assertThat(v.success(), is(10.0)); + } + + @Test + public void testAccumulate4Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("y"), + (f1, f2, f3, f4) -> f1 + f2 + f3 + f4); + assertThat(v.fail().length(), is(2)); + } + + @Test + public void testAccumulate5() { + final Validation, Double> v = + parseDouble("1.0").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("5.0"), + (f1, f2, f3, f4, f5) -> f1 + f2 + f3 + f4 + f5); + assertThat(v.success(), is(15.0)); + } + + @Test + public void testAccumulate5Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("y"), + (f1, f2, f3, f4, f5) -> f1 + f2 + f3 + f4 + f5); + assertThat(v.fail().length(), is(2)); + } + + @Test + public void testAccumulate6() { + final Validation, Double> v = + parseDouble("1.0").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("5.0"), + parseDouble("6.0"), + (f1, f2, f3, f4, f5, f6) -> f1 + f2 + f3 + f4 + f5 + f6); + assertThat(v.success(), is(21.0)); + } + + @Test + public void testAccumulate6Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("5.0"), + parseDouble("y"), + (f1, f2, f3, f4, f5, f6) -> f1 + f2 + f3 + f4 + f5 + f6); + assertThat(v.fail().length(), is(2)); + } + + @Test + public void testAccumulate7() { + final Validation, Double> v = + parseDouble("1.0").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("5.0"), + parseDouble("6.0"), + parseDouble("7.0"), + (f1, f2, f3, f4, f5, f6, f7) -> f1 + f2 + f3 + f4 + f5 + f6 + f7); + assertThat(v.success(), is(28.0)); + } + + @Test + public void testAccumulate7Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("5.0"), + parseDouble("6.0"), + parseDouble("y"), + (f1, f2, f3, f4, f5, f6, f7) -> f1 + f2 + f3 + f4 + f5 + f6 + f7); + assertThat(v.fail().length(), is(2)); + } + + @Test + public void testAccumulate8() { + final Validation, Double> v = + parseDouble("1.0").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("5.0"), + parseDouble("6.0"), + parseDouble("7.0"), + parseDouble("8.0"), + (f1, f2, f3, f4, f5, f6, f7, f8) -> f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8); + assertThat(v.success(), is(36.0)); + } + + @Test + public void testAccumulate8Fail() { + final Validation, Double> v = + parseDouble("x").accumulate( + parseDouble("2.0"), + parseDouble("3.0"), + parseDouble("4.0"), + parseDouble("5.0"), + parseDouble("6.0"), + parseDouble("7.0"), + parseDouble("y"), + (f1, f2, f3, f4, f5, f6, f7, f8) -> f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8); + assertThat(v.fail().length(), is(2)); + } + + @Test + public void testAccumulate8s() { + final Validation v1 = parseInt("1"); + final Validation v2 = parseInt("2"); + final Validation v3 = parseInt("3"); + final Validation v4 = parseInt("4"); + final Validation v5 = parseInt("5"); + final Validation v6 = parseInt("6"); + final Validation v7 = parseInt("7"); + final Validation v8 = parseInt("8"); + final Option on2 = v1.accumulate(firstSemigroup(), v2); + assertThat(on2, is(Option.none())); + final Option on3 = v1.accumulate(firstSemigroup(), v2, v3); + assertThat(on3, is(Option.none())); + final Option on4 = v1.accumulate(firstSemigroup(), v2, v3, v4); + assertThat(on4, is(Option.none())); + final Option on5 = v1.accumulate(firstSemigroup(), v2, v3, v4, v5); + assertThat(on5, is(Option.none())); + final Option on6 = v1.accumulate(firstSemigroup(), v2, v3, v4, v5, v6); + assertThat(on6, is(Option.none())); + final Option on7 = v1.accumulate(firstSemigroup(), v2, v3, v4, v5, v6, v7); + assertThat(on7, is(Option.none())); + final Option on8 = v1.accumulate(firstSemigroup(), v2, v3, v4, v5, v6, v7, v8); + assertThat(on8, is(Option.none())); + } + + @Test + public void testAccumulate8sFail() { + final Option on = + parseInt("x").accumulate( + firstSemigroup(), + parseInt("2"), + parseInt("3"), + parseInt("4"), + parseInt("5"), + parseInt("6"), + parseInt("7"), + parseInt("y")); + assertThat(on.some().getMessage(), is("For input string: \"x\"")); + } + + @Test(expected = Error.class) + public void testSuccess() { + parseShort("x").success(); + } + + @Test(expected = Error.class) + public void testFail() { + parseShort("12").fail(); + } + + @Test + public void testCondition() { + final Validation one = condition(true, "not 1", "one"); + assertThat(one.success(), is("one")); + final Validation fail = condition(false, "not 1", "one"); + assertThat(fail.fail(), is("not 1")); + } + + @Test + public void testNel() { + assertThat(Validation.success("success").nel().success(), is("success")); + assertThat(Validation.fail("fail").nel().fail().head(), is("fail")); + } + + @Test + public void testFailNEL() { + Validation, Integer> v = failNEL(new Exception("failed")); + assertThat(v.isFail(), is(true)); + } + + @Test + public void testEither() { + assertThat(either().f(Validation.success("success")).right().value(), is("success")); + assertThat(either().f(Validation.fail("fail")).left().value(), is("fail")); + } + + @Test + public void testValidation() { + assertThat(validation().f(Either.right("success")).success(), is("success")); + assertThat(validation().f(Either.left("fail")).fail(), is("fail")); + } + + @Test + public void testAccumulateSemigroup2() { + range(0, 2).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.exists(Validation::isFail), list.filter(Validation::isFail).bind(validation -> validation.fail())), list.index(0).accumulate(Semigroup.listSemigroup(), list.index(1))); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1)), list.index(0).accumulate(Semigroup.listSemigroup(), list.index(1), p2())); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1)), list.index(0).accumulate(Semigroup.listSemigroup(), list.index(1), uncurryF2(p2()))); + }); + } + + @Test + public void testAccumulateSemigroup3() { + range(0, 3).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> + accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.exists(Validation::isFail), list.filter(Validation::isFail).bind(validation -> validation.fail())), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2))); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), p3())); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), uncurryF3(p3()))); + }); + + } + + @Test + public void testAccumulateSemigroup4() { + range(0, 4).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> + accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.exists(Validation::isFail), list.filter(Validation::isFail).bind(validation -> validation.fail())), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3))); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), p4())); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), uncurryF4(p4()))); + }); + + } + + @Test + public void testAccumulateSemigroup5() { + range(0, 5).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> + accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.exists(Validation::isFail), list.filter(Validation::isFail).bind(validation -> validation.fail())), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4))); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), p5())); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), uncurryF5(p5()))); + }); + } + + @Test + public void testAccumulateSemigroup6() { + range(0, 6).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> + accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.exists(Validation::isFail), list.filter(Validation::isFail).bind(validation -> validation.fail())), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5))); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), p6())); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), uncurryF6(p6()))); + }); + } + + @Test + public void testAccumulateSemigroup7() { + range(0, 7).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> + accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.exists(Validation::isFail), list.filter(Validation::isFail).bind(validation -> validation.fail())), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6))); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5, 6)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), p7())); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5, 6)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), uncurryF7(p7()))); + }); + } + + @Test + public void testAccumulateSemigroup8() { + range(0, 8).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> + accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(iif(list.exists(Validation::isFail), list.filter(Validation::isFail).bind(validation -> validation.fail())), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7))); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5, 6, 7)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7), P.p8())); + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).bind(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5, 6, 7)), list.index(0).accumulate(Semigroup.listSemigroup(),list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7), uncurryF8(P.p8()))); + }); + } + + @Test + public void testAccumulate0() { + range(0, 1).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), 0), list.index(0).accumulate()); + }); + } + + @Test + public void testAccumulate1Complex() { + range(0, 1).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), 0), list.index(0).accumulate(identity())); + }); + + } + + @Test + public void testAccumulate2Complex() { + range(0, 2).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), p(0, 1)), list.index(0).accumulate(list.index(1), P::p)); + }); + } + + @Test + public void testAccumulate3Complex() { + range(0, 3).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), p(0, 1, 2)), list.index(0).accumulate(list.index(1), list.index(2), P::p)); + }); + + } + + @Test + public void testAccumulate4Complex() { + range(0, 4).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), p(0, 1, 2, 3)), list.index(0).accumulate(list.index(1), list.index(2), list.index(3), P::p)); + }); + + } + + @Test + public void testAccumulate5Complex() { + range(0, 5).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), p(0, 1, 2, 3, 4)), list.index(0).accumulate(list.index(1), list.index(2), list.index(3), list.index(4), P::p)); + }); + } + + @Test + public void testAccumulate6Complex() { + range(0, 6).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5)), list.index(0).accumulate(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), P::p)); + }); + } + + @Test + public void testAccumulate7Complex() { + range(0, 7).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5, 6)), list.index(0).accumulate(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), P::p)); + }); + } + + @Test + public void testAccumulate8Complex() { + range(0, 8).map(i -> List., Integer>>arrayList(fail(arrayList(String.valueOf(i))), success(i))) + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? + list.map(List::single) : + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List., Integer>>>nil()) + .foreachDoEffect(list -> { + assertEquals(condition(list.forall(Validation::isSuccess), list.filter(Validation::isFail).map(validation -> validation.fail()), p(0, 1, 2, 3, 4, 5, 6, 7)), list.index(0).accumulate(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7), P::p)); + }); + } + + @Test + public void testMap() { + assertEquals(Validation.fail("zero"), Validation.fail("zero").map(constant(0))); + assertEquals(Validation.success(0), Validation.success("zero").map(constant(0))); + assertEquals(Validation.fail("zero"), Validation.fail("zero").map(constant(0))); + assertEquals(Validation.success(0), Validation.success("zero").map(constant(0))); + } + + @Test + public void testBind() { + assertEquals(Validation.fail("zero"), Validation.fail("zero").bind(constant(Validation.fail("zero")))); + assertEquals(Validation.fail("zero"), Validation.success("zero").bind(constant(Validation.fail("zero")))); + assertEquals(Validation.fail("zero"), Validation.fail("zero").bind(constant(Validation.success(0)))); + assertEquals(Validation.success(0), Validation.success("zero").bind(constant(Validation.success(0)))); + } + + @Test + public void testSequenceEitherLeft() { + assertEquals(left(fail("zero")), sequenceEitherLeft(fail("zero"))); + assertEquals(left(success("zero")), sequenceEitherLeft(success(left("zero")))); + assertEquals(right("zero"), sequenceEitherLeft(success(right("zero")))); + } + + @Test + public void testSequenceEitherRight() { + assertEquals(right(fail("zero")), sequenceEitherRight(fail("zero"))); + assertEquals(right(success("zero")), sequenceEitherRight(success(right("zero")))); + assertEquals(left("zero"), sequenceEitherRight(success(left("zero")))); + } + + @Test + public void testSequenceF() { + assertEquals(constant(fail("zero")).f(1), sequenceF(fail("zero")).f(1)); + assertEquals(constant(success("zero")).f(1), sequenceF(success(constant("zero"))).f(1)); + } + + @Test + public void testSequenceIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(fail("zero"))).run(), sequenceIO(fail("zero")).run()); + assertEquals(IOFunctions.lazy(constant(success("zero"))).run(), sequenceIO(success(IOFunctions.lazy(constant("zero")))).run()); + } + + @Test + public void testSequenceList() { + assertEquals(single(fail("zero")), sequenceList(fail("zero"))); + assertEquals(nil(), sequenceList(success(nil()))); + assertEquals(single(success("zero")), sequenceList(success(single("zero")))); + assertEquals(arrayList(success("zero"), success("one")), sequenceList(success(arrayList("zero", "one")))); + } + + @Test + public void testSequenceOption() { + assertEquals(some(fail("zero")), sequenceOption(fail("zero"))); + assertEquals(none(), sequenceOption(success(none()))); + assertEquals(some(success("zero")), sequenceOption(success(some("zero")))); + } + + @Test + public void testSequenceP1() { + assertEquals(p(fail("zero")), sequenceP1(fail("zero"))); + assertEquals(p(success("zero")), sequenceP1(success(p("zero")))); + } + + @Test + public void testSequenceSeq() { + assertEquals(Seq.single(fail("zero")), sequenceSeq(fail("zero"))); + assertEquals(Seq.empty(), sequenceSeq(success(Seq.empty()))); + assertEquals(Seq.single(success("zero")), sequenceSeq(success(Seq.single("zero")))); + assertEquals(Seq.arraySeq(success("zero"), success("one")), sequenceSeq(success(Seq.arraySeq("zero", "one")))); + } + + @Test + public void testSequenceSet() { + assertEquals(Set.single(validationOrd(stringOrd, intOrd), fail("zero")), sequenceSet(stringOrd, intOrd, fail("zero"))); + assertEquals(Set.empty(validationOrd(stringOrd, intOrd)), sequenceSet(stringOrd, intOrd, success(Set.empty(intOrd)))); + assertEquals(Set.single(validationOrd(intOrd, stringOrd), success("zero")), sequenceSet(intOrd, stringOrd, success(Set.single(stringOrd, "zero")))); + assertEquals(Set.arraySet(validationOrd(intOrd, stringOrd), success("zero"), success("one")), sequenceSet(intOrd, stringOrd, success(Set.arraySet(stringOrd, "zero", "one")))); + } + + @Test + public void testSequenceStream() { + assertEquals(Stream.single(fail("zero")), sequenceStream(fail("zero"))); + assertEquals(Stream.nil(), sequenceStream(success(Stream.nil()))); + assertEquals(Stream.single(success("zero")), sequenceStream(success(Stream.single("zero")))); + assertEquals(Stream.arrayStream(success("zero"), success("one")), sequenceStream(success(Stream.arrayStream("zero", "one")))); + } + + @Test + public void testSequenceTrampoline() { + assertEquals(Trampoline.pure(fail("zero")).run(), sequenceTrampoline(fail("zero")).run()); + assertEquals(Trampoline.pure(success(0)).run(), sequenceTrampoline(success(Trampoline.pure(0))).run()); + } + + @Test + public void testSequenceValidation() { + assertEquals(success(fail("zero")), sequenceValidation(fail("zero"))); + assertEquals(fail("zero"), sequenceValidation(success(fail("zero")))); + assertEquals(success(success(0)), sequenceValidation(success(success(0)))); + } + + @Test + public void testTraverseEitherLeft() { + assertEquals(left(fail("zero")), fail("zero").traverseEitherLeft(constant(left(0)))); + assertEquals(left(success(0)), success("zero").traverseEitherLeft(constant(left(0)))); + assertEquals(left(fail("zero")), fail("zero").traverseEitherLeft(constant(right(0)))); + assertEquals(right(0), success("zero").traverseEitherLeft(constant(right(0)))); + } + + @Test + public void testTraverseEitherRight() { + assertEquals(right(fail("zero")), fail("zero").traverseEitherRight(constant(right(0)))); + assertEquals(right(success(0)), success("zero").traverseEitherRight(constant(right(0)))); + assertEquals(right(fail("zero")), fail("zero").traverseEitherRight(constant(left(0)))); + assertEquals(left(0), success("zero").traverseEitherRight(constant(left(0)))); + } + + @Test + public void testTraverseF() { + assertEquals(constant(fail("zero")).f(1), fail("zero").traverseF(constant(constant(0))).f(1)); + assertEquals(constant(success(0)).f(1), success("zero").traverseF(constant(constant(0))).f(1)); + } + + @Test + public void testTraverseIO() throws IOException { + assertEquals(IOFunctions.lazy(constant(fail("zero"))).run(), fail("zero").traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + assertEquals(IOFunctions.lazy(constant(success(0))).run(), success("zero").traverseIO(constant(IOFunctions.lazy(constant(0)))).run()); + } + + @Test + public void testTraverseList() { + assertEquals(single(fail("zero")), fail("zero").traverseList(constant(nil()))); + assertEquals(nil(), success("zero").traverseList(constant(nil()))); + assertEquals(single(fail("zero")), fail("zero").traverseList(constant(single(0)))); + assertEquals(single(success(0)), success("zero").traverseList(constant(single(0)))); + assertEquals(single(fail("zero")), fail("zero").traverseList(constant(arrayList(0, 1)))); + assertEquals(arrayList(success(0), success(1)), success("zero").traverseList(constant(arrayList(0, 1)))); + } + + @Test + public void testTraverseOption() { + assertEquals(some(fail("zero")), fail("zero").traverseOption(constant(none()))); + assertEquals(none(), success("zero").traverseOption(constant(none()))); + assertEquals(some(fail("zero")), fail("zero").traverseOption(constant(some(0)))); + assertEquals(some(success(0)), success("zero").traverseOption(constant(some(0)))); + } + + @Test + public void testTraverseP1() { + assertEquals(p(fail("zero")), fail("zero").traverseP1(constant(p(0)))); + assertEquals(p(success(0)), success("zero").traverseP1(constant(p(0)))); + } + + @Test + public void testTraverseSeq() { + assertEquals(Seq.single(fail("zero")), fail("zero").traverseSeq(constant(Seq.empty()))); + assertEquals(Seq.empty(), success("zero").traverseSeq(constant(Seq.empty()))); + assertEquals(Seq.single(fail("zero")), fail("zero").traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(success(0)), success("zero").traverseSeq(constant(Seq.single(0)))); + assertEquals(Seq.single(fail("zero")), fail("zero").traverseSeq(constant(Seq.arraySeq(0, 1)))); + assertEquals(Seq.arraySeq(success(0), success(1)), success("zero").traverseSeq(constant(Seq.arraySeq(0, 1)))); + } + + @Test + public void testTraverseSet() { + assertEquals(Set.single(validationOrd(stringOrd, intOrd), fail("zero")), fail("zero").traverseSet(stringOrd, intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.empty(validationOrd(stringOrd, intOrd)), Validation.success("zero").traverseSet(stringOrd, intOrd, constant(Set.empty(intOrd)))); + assertEquals(Set.single(validationOrd(stringOrd, intOrd), fail("zero")), fail("zero").traverseSet(stringOrd, intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(validationOrd(stringOrd, intOrd), success(0)), Validation.success("zero").traverseSet(stringOrd, intOrd, constant(Set.single(intOrd, 0)))); + assertEquals(Set.single(validationOrd(stringOrd, intOrd), fail("zero")), fail("zero").traverseSet(stringOrd, intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + assertEquals(Set.arraySet(validationOrd(stringOrd, intOrd), success(0), success(1)), Validation.success("zero").traverseSet(stringOrd, intOrd, constant(Set.arraySet(intOrd, 0, 1)))); + } + + @Test + public void testTraverseStream() { + assertEquals(Stream.single(fail("zero")), fail("zero").traverseStream(constant(Stream.nil()))); + assertEquals(Stream.nil(), success("zero").traverseStream(constant(Stream.nil()))); + assertEquals(Stream.single(fail("zero")), fail("zero").traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(success(0)), success("zero").traverseStream(constant(Stream.single(0)))); + assertEquals(Stream.single(fail("zero")), fail("zero").traverseStream(constant(Stream.arrayStream(0, 1)))); + assertEquals(Stream.arrayStream(success(0), success(1)), success("zero").traverseStream(constant(Stream.arrayStream(0, 1)))); + } + + @Test + public void testTraverseTrampoline() { + assertEquals(Trampoline.pure(fail("zero")).run(), fail("zero").traverseTrampoline(constant(Trampoline.pure(0))).run()); + assertEquals(Trampoline.pure(success(0)).run(), success("zero").traverseTrampoline(constant(Trampoline.pure(0))).run()); + } + + @Test + public void testTraverseValidation() { + assertEquals(Validation.>success(fail("zero")), Validation.fail("zero").traverseValidation(constant(Validation.fail(0)))); + assertEquals(Validation.>fail(0), Validation.success("zero").traverseValidation(constant(Validation.fail(0)))); + assertEquals(Validation.>success(fail("zero")), Validation.fail("zero").traverseValidation(constant(Validation.success(0)))); + assertEquals(Validation.>success(success(0)), Validation.success("zero").traverseValidation(constant(Validation.success(0)))); + } + +} diff --git a/core/src/test/java/fj/data/ZipperTest.java b/core/src/test/java/fj/data/ZipperTest.java index 51c7b4ea..78d837aa 100644 --- a/core/src/test/java/fj/data/ZipperTest.java +++ b/core/src/test/java/fj/data/ZipperTest.java @@ -3,7 +3,7 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class ZipperTest { @Test diff --git a/core/src/test/java/fj/data/hamt/HamtTest.java b/core/src/test/java/fj/data/hamt/HamtTest.java index 1e601f24..ce901e6e 100644 --- a/core/src/test/java/fj/data/hamt/HamtTest.java +++ b/core/src/test/java/fj/data/hamt/HamtTest.java @@ -12,7 +12,8 @@ import static fj.P.p; import static fj.data.List.list; import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + /** * @author Mark Perry diff --git a/core/src/test/java/fj/data/optic/IsoTest.java b/core/src/test/java/fj/data/optic/IsoTest.java index 6cace533..f9559d3f 100644 --- a/core/src/test/java/fj/data/optic/IsoTest.java +++ b/core/src/test/java/fj/data/optic/IsoTest.java @@ -5,7 +5,8 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + public class IsoTest { @Test diff --git a/core/src/test/java/fj/data/optic/LensTest.java b/core/src/test/java/fj/data/optic/LensTest.java index d78fa0d0..98be7fca 100644 --- a/core/src/test/java/fj/data/optic/LensTest.java +++ b/core/src/test/java/fj/data/optic/LensTest.java @@ -4,7 +4,8 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + public class LensTest { @Test diff --git a/core/src/test/java/fj/data/optic/OptionalTest.java b/core/src/test/java/fj/data/optic/OptionalTest.java index 07c3f79a..7fddb31b 100644 --- a/core/src/test/java/fj/data/optic/OptionalTest.java +++ b/core/src/test/java/fj/data/optic/OptionalTest.java @@ -4,7 +4,8 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + public class OptionalTest { @Test diff --git a/core/src/test/java/fj/data/optic/PrismTest.java b/core/src/test/java/fj/data/optic/PrismTest.java index da128d72..ce6dddb4 100644 --- a/core/src/test/java/fj/data/optic/PrismTest.java +++ b/core/src/test/java/fj/data/optic/PrismTest.java @@ -4,7 +4,7 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class PrismTest { @Test diff --git a/core/src/test/java/fj/data/optic/TraversalTest.java b/core/src/test/java/fj/data/optic/TraversalTest.java index cadd6ccf..9cfc25cc 100644 --- a/core/src/test/java/fj/data/optic/TraversalTest.java +++ b/core/src/test/java/fj/data/optic/TraversalTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class TraversalTest { @Test diff --git a/core/src/test/java/fj/data/vector/VTest.java b/core/src/test/java/fj/data/vector/VTest.java index 9a3709ac..34f8686c 100644 --- a/core/src/test/java/fj/data/vector/VTest.java +++ b/core/src/test/java/fj/data/vector/VTest.java @@ -5,7 +5,8 @@ import org.junit.Test; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + public class VTest { @Test diff --git a/core/src/test/java/fj/function/DoublesTest.java b/core/src/test/java/fj/function/DoublesTest.java index 6ba98fa2..53b74e52 100644 --- a/core/src/test/java/fj/function/DoublesTest.java +++ b/core/src/test/java/fj/function/DoublesTest.java @@ -1,6 +1,8 @@ package fj.function; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; + import fj.F; import org.junit.Test; @@ -9,6 +11,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import static org.junit.Assert.assertTrue; import static org.hamcrest.core.Is.is; import static fj.data.List.list; diff --git a/core/src/test/java/fj/function/IntegersTest.java b/core/src/test/java/fj/function/IntegersTest.java new file mode 100644 index 00000000..c07197af --- /dev/null +++ b/core/src/test/java/fj/function/IntegersTest.java @@ -0,0 +1,65 @@ +package fj.function; + +import org.junit.Test; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import static fj.data.List.list; +import static fj.data.Option.none; +import static org.hamcrest.core.Is.is; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.Assert.assertTrue; + +public class IntegersTest { + + @Test + public void testSum() { + assertThat(Integers.sum(list(3, 4, 5)), is(12)); + } + + @Test + public void testProduct() { + assertThat(Integers.product(list(3, 4, 5)), is(60)); + } + + @Test + public void testAdd() { + assertThat(Integers.add.f(10).f(20), is(30)); + } + + @Test + public void testMultiply() { + assertThat(Integers.multiply.f(3).f(5), is(15)); + } + + @Test + public void testAbs() { + assertThat(Integers.abs.f(-5), is(5)); + } + + @Test + public void testFromString() { + assertThat(Integers.fromString().f("-123").some(), is(-123)); + } + + @Test + public void testFromStringFail() { + assertThat(Integers.fromString().f("w"), is(none())); + } + + @Test + public void testCannotInstantiate() throws NoSuchMethodException, IllegalAccessException, InstantiationException { + Constructor constructor = Integers.class.getDeclaredConstructor(); + constructor.setAccessible(true); + try { + constructor.newInstance(); + fail("expected InvocationTargetException"); + } catch (InvocationTargetException ite) { + assertTrue(ite.getCause() instanceof UnsupportedOperationException); + } + } + +} diff --git a/core/src/test/java/fj/function/LongsTest.java b/core/src/test/java/fj/function/LongsTest.java new file mode 100644 index 00000000..196da20f --- /dev/null +++ b/core/src/test/java/fj/function/LongsTest.java @@ -0,0 +1,65 @@ +package fj.function; + +import org.junit.Test; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import static fj.data.List.list; +import static fj.data.Option.none; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + + +public class LongsTest { + + @Test + public void testSum() { + assertThat(Longs.sum(list(3L, 4L, 5L)), is(12L)); + } + + @Test + public void testProduct() { + assertThat(Longs.product(list(3L, 4L, 5L)), is(60L)); + } + + @Test + public void testAdd() { + assertThat(Longs.add.f(10L).f(20L), is(30L)); + } + + @Test + public void testMultiply() { + assertThat(Longs.multiply.f(3L).f(5L), is(15L)); + } + + @Test + public void testAbs() { + assertThat(Longs.abs.f(-5L), is(5L)); + } + + @Test + public void testFromString() { + assertThat(Longs.fromString().f("-123").some(), is(-123L)); + } + + @Test + public void testFromStringFail() { + assertThat(Longs.fromString().f("w"), is(none())); + } + + @Test + public void testCannotInstantiate() throws NoSuchMethodException, IllegalAccessException, InstantiationException { + Constructor constructor = Longs.class.getDeclaredConstructor(); + constructor.setAccessible(true); + try { + constructor.newInstance(); + fail("expected InvocationTargetException"); + } catch (InvocationTargetException ite) { + assertTrue(ite.getCause() instanceof UnsupportedOperationException); + } + } + +} diff --git a/core/src/test/java/fj/function/StringsTest.java b/core/src/test/java/fj/function/StringsTest.java index 92cf44d3..db20fd68 100644 --- a/core/src/test/java/fj/function/StringsTest.java +++ b/core/src/test/java/fj/function/StringsTest.java @@ -3,11 +3,12 @@ import fj.Function; import org.junit.Test; -import static fj.F1Functions.o; import static fj.Function.compose; import static fj.function.Strings.*; import static org.junit.Assert.*; import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + public class StringsTest { @Test @@ -17,7 +18,7 @@ public void testLines() { @Test public void testLinesEmpty() { - assertThat(o(unlines(), lines()).f(""), is("")); + assertThat(unlines().o(lines()).f(""), is("")); } @Test diff --git a/core/src/test/java/fj/function/TestEffect.java b/core/src/test/java/fj/function/TestEffect.java index 00da26f0..de2f5c5f 100644 --- a/core/src/test/java/fj/function/TestEffect.java +++ b/core/src/test/java/fj/function/TestEffect.java @@ -1,11 +1,7 @@ package fj.function; -import fj.F; import org.junit.Test; -/** - * Created by mperry on 28/08/2014. - */ public class TestEffect { @Test diff --git a/core/src/test/java/fj/function/VisitorTest.java b/core/src/test/java/fj/function/VisitorTest.java index 54efcf24..da92a4bb 100644 --- a/core/src/test/java/fj/function/VisitorTest.java +++ b/core/src/test/java/fj/function/VisitorTest.java @@ -11,7 +11,7 @@ import static fj.data.Option.some; import static fj.function.Visitor.*; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class VisitorTest { @Test diff --git a/core/src/test/java/fj/parser/ParserTest.java b/core/src/test/java/fj/parser/ParserTest.java index a3f623f5..f2664a1f 100644 --- a/core/src/test/java/fj/parser/ParserTest.java +++ b/core/src/test/java/fj/parser/ParserTest.java @@ -8,7 +8,7 @@ import static fj.parser.Result.result; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; public class ParserTest { @Test diff --git a/demo/build.gradle b/demo/build.gradle index b79cb246..87bf7a5c 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -5,12 +5,11 @@ mainClassName = "fj.demo.euler.Problem2" archivesBaseName = "${project.projectName}-${project.name}" -configureAllRetroLambda() - dependencies { - compile project(":core") - compile project(":quickcheck") - testCompile dependencyJunit + api project(":core") + api project(":quickcheck") + testImplementation junitCompile + testImplementation junitRuntime } test { diff --git a/demo/src/main/java/fj/demo/Comonad_example.java b/demo/src/main/java/fj/demo/Comonad_example.java index 8d5bc683..3b5661f8 100644 --- a/demo/src/main/java/fj/demo/Comonad_example.java +++ b/demo/src/main/java/fj/demo/Comonad_example.java @@ -1,6 +1,5 @@ package fj.demo; -import fj.F1Functions; import fj.P; import static fj.data.List.asString; import static fj.data.List.fromString; @@ -26,7 +25,7 @@ public static Stream> perms(final Stream s) { for (final Zipper z : fromStream(s)) r = join(z.cobind(zp -> perms(zp.lefts().reverse().append(zp.rights())).map( - F1Functions.o(Stream.cons().f(zp.focus()), P.p1()) + Stream.cons().f(zp.focus()).o(P.p1()) ) ).toStream()); return r; diff --git a/demo/src/main/java/fj/demo/IODemo.java b/demo/src/main/java/fj/demo/IODemo.java index 6451a46b..aad8e41b 100644 --- a/demo/src/main/java/fj/demo/IODemo.java +++ b/demo/src/main/java/fj/demo/IODemo.java @@ -4,16 +4,12 @@ import fj.data.IOFunctions; import fj.data.LazyString; -import static fj.F1W.lift; import static fj.data.IOFunctions.interact; import static fj.data.IOFunctions.runSafe; import static fj.data.LazyString.lines_; import static fj.data.LazyString.unlines_; import static java.lang.System.out; -/** - * Created by MarkPerry on 13/06/2015. - */ public class IODemo { public static void main(String[] args) { @@ -29,7 +25,7 @@ public static void main(String[] args) { * and prints that last line. */ public final void readFirstShortLine() { - F f = lift(lines_()).andThen(l -> l.filter(s -> s.length() < 3)).andThen(unlines_()); + F f = lines_().andThen(l -> l.filter(s -> s.length() < 3)).andThen(unlines_()); runSafe(interact(f)); } @@ -37,7 +33,7 @@ public final void readFirstShortLine() { * Read a stream of input lazily using interact, in effect reading the first line */ public final void readFirstLine() { - F f = lift(LazyString::lines).andThen(unlines_()); + F f = lines_().andThen(unlines_()); runSafe(interact(f)); } diff --git a/demo/src/main/java/fj/demo/IOWalkthrough.java b/demo/src/main/java/fj/demo/IOWalkthrough.java index 609d2502..2f1cb9eb 100644 --- a/demo/src/main/java/fj/demo/IOWalkthrough.java +++ b/demo/src/main/java/fj/demo/IOWalkthrough.java @@ -1,12 +1,9 @@ package fj.demo; import fj.F; -import fj.F1Functions; -import fj.F1W; import fj.Unit; import fj.data.IO; import fj.data.IOFunctions; -import fj.data.IOW; import java.io.BufferedReader; import java.io.IOException; @@ -49,7 +46,7 @@ public static void main(String[] args) { // now we create a function which takes a string, upper cases it and creates an IO value that would print the upper cased string if executed - final F> upperCaseAndPrint = F1Functions., String>o(IOFunctions::stdoutPrintln).f(String::toUpperCase); + final F> upperCaseAndPrint = s -> stdoutPrintln(s.toUpperCase()); // we now want to compose reading the name with printing it, for that we need to have access to the runtime value that is returned when the // IO value for read is executed, hence we use fj.data.IOFunctions.bind instead of fj.data.IOFunctions.append @@ -73,10 +70,11 @@ public static void main(String[] args) { // assigning the functions to variables like above, but you can use the fj.F1W syntax wrapper for composing single-argument functions and fj.data.IOW // for composing IO values instead, the entire program can be written like so: - IOW.lift(stdoutPrintln("What's your name again?")) + F f = String::toUpperCase; + stdoutPrintln("What's your name again?") .append(stdoutPrint("Name: ")) .append(stdinReadLine()) - .bind(F1W.lift((String s) -> s.toUpperCase()).andThen(IOFunctions::stdoutPrintln)) + .bind(f.andThen(IOFunctions::stdoutPrintln)) .safe().run().on((IOException e) -> { e.printStackTrace(); return Unit.unit(); }); } } diff --git a/demo/src/main/java/fj/demo/Primes2.java b/demo/src/main/java/fj/demo/Primes2.java index 96a852f3..37bfb249 100644 --- a/demo/src/main/java/fj/demo/Primes2.java +++ b/demo/src/main/java/fj/demo/Primes2.java @@ -1,7 +1,5 @@ package fj.demo; -import fj.F1Functions; - import static fj.data.Enumerator.naturalEnumerator; import fj.Show; @@ -21,7 +19,7 @@ public class Primes2 { // Finds primes in a given stream. public static Stream sieve(final Stream xs) { - return cons(xs.head(), () -> sieve(xs.tail()._1().removeAll(F1Functions.o(naturalOrd.equal().eq(ZERO), mod.f(xs.head()))))); + return cons(xs.head(), () -> sieve(xs.tail()._1().removeAll(naturalOrd.equal().eq(ZERO).o(mod.f(xs.head()))))); } // A stream of all primes less than n. diff --git a/demo/src/main/java/fj/demo/StateDemo_Greeter.java b/demo/src/main/java/fj/demo/StateDemo_Greeter.java index 578f414e..707a1f9f 100644 --- a/demo/src/main/java/fj/demo/StateDemo_Greeter.java +++ b/demo/src/main/java/fj/demo/StateDemo_Greeter.java @@ -3,9 +3,6 @@ import fj.P; import fj.data.State; -/** - * Created by mperry on 4/08/2014. - */ public class StateDemo_Greeter { public static void main(String args[]) { diff --git a/demo/src/main/java/fj/demo/StateDemo_VendingMachine.java b/demo/src/main/java/fj/demo/StateDemo_VendingMachine.java index ef1a09cd..75de4823 100644 --- a/demo/src/main/java/fj/demo/StateDemo_VendingMachine.java +++ b/demo/src/main/java/fj/demo/StateDemo_VendingMachine.java @@ -6,9 +6,6 @@ import static fj.demo.StateDemo_VendingMachine.Input.COIN; import static fj.demo.StateDemo_VendingMachine.Input.TURN; -/** - * Created by MarkPerry on 20/07/2014. - */ public class StateDemo_VendingMachine { public enum Input { COIN, TURN } diff --git a/demo/src/main/java/fj/demo/WriterDemo_Halver.java b/demo/src/main/java/fj/demo/WriterDemo_Halver.java index a2a270f6..9e6c6dac 100644 --- a/demo/src/main/java/fj/demo/WriterDemo_Halver.java +++ b/demo/src/main/java/fj/demo/WriterDemo_Halver.java @@ -4,12 +4,8 @@ import fj.P2; import fj.data.Writer; -import static fj.F1Functions.map; import static fj.Monoid.stringMonoid; -/** - * Created by mperry on 4/08/2014. - */ public class WriterDemo_Halver { public static void main(String args[]) { @@ -24,7 +20,7 @@ static void testWriter() { Integer init = 32; P2 p1 = half().f(init).flatMap(half()).flatMap(half()).run(); System.out.println(p1); - System.out.println(map(half(), w -> w.flatMap(half()).flatMap(half()).run()).f(init)); + System.out.println(half().map(w -> w.flatMap(half()).flatMap(half()).run()).f(init)); } } diff --git a/demo/src/main/java/fj/demo/concurrent/MapReduce.java b/demo/src/main/java/fj/demo/concurrent/MapReduce.java index 3cb2d761..38a62caa 100644 --- a/demo/src/main/java/fj/demo/concurrent/MapReduce.java +++ b/demo/src/main/java/fj/demo/concurrent/MapReduce.java @@ -35,14 +35,15 @@ public static Promise countWords(final List> documents, // Main program does the requisite IO gymnastics public static void main(final String[] args) { + F z = fileName -> { + try { + return new BufferedReader(new FileReader(new File(fileName))); + } catch (FileNotFoundException e) { + throw new Error(e); + } + }; final List> documents = list(args).map( - F1Functions.andThen(fileName -> { - try { - return new BufferedReader(new FileReader(new File(fileName))); - } catch (FileNotFoundException e) { - throw new Error(e); - } - }, new F>() { + z.andThen(new F>() { public Stream f(final BufferedReader reader) { final Option s; try { diff --git a/demo/src/main/java/fj/demo/euler/Problem2.java b/demo/src/main/java/fj/demo/euler/Problem2.java index eda4a72d..a5639ab9 100644 --- a/demo/src/main/java/fj/demo/euler/Problem2.java +++ b/demo/src/main/java/fj/demo/euler/Problem2.java @@ -1,8 +1,6 @@ package fj.demo.euler; -import fj.F1Functions; import fj.F2; -import fj.F2Functions; import fj.data.Stream; import static fj.data.Stream.cons; import static fj.function.Integers.even; @@ -23,13 +21,13 @@ public static void main(final String[] args) { static void java7() { final Stream fibs = new F2>() { public Stream f(final Integer a, final Integer b) { - return cons(a, F1Functions.lazy(F2Functions.curry(this).f(b)).f(a + b)); + return cons(a, this.curry().f(b).lazy().f(a + b)); } }.f(1, 2); out.println(sum(fibs.filter(even).takeWhile(intOrd.isLessThan(4000001)).toList())); } - static F2> fibsJava8 = (a, b) -> cons(a, F1Functions.lazy(F2Functions.curry(Problem2.fibsJava8).f(b)).f(a + b)); + static F2> fibsJava8 = (a, b) -> cons(a, Problem2.fibsJava8.curry().f(b).lazy().f(a + b)); static void java8() { out.println(sum(fibsJava8.f(1, 2).filter(even).takeWhile(intOrd.isLessThan(4000001)).toList())); diff --git a/demo/src/main/java/fj/demo/optic/LensPerson.java b/demo/src/main/java/fj/demo/optic/LensPerson.java index 51f5e103..641543e9 100644 --- a/demo/src/main/java/fj/demo/optic/LensPerson.java +++ b/demo/src/main/java/fj/demo/optic/LensPerson.java @@ -6,9 +6,6 @@ import static org.junit.Assert.assertTrue; -/** - * Created by MarkPerry on 23/06/2015. - */ public class LensPerson { static final class Person { diff --git a/demo/src/main/java/fj/demo/realworld/Chapter7.java b/demo/src/main/java/fj/demo/realworld/Chapter7.java index c239a5bf..bc414e4f 100644 --- a/demo/src/main/java/fj/demo/realworld/Chapter7.java +++ b/demo/src/main/java/fj/demo/realworld/Chapter7.java @@ -8,8 +8,6 @@ import static fj.data.IOFunctions.*; /** - * Created by MarkPerry on 11/06/2015. - * * Examples from Chapter 7 of Real World Haskell, http://book.realworldhaskell.org/. * * Currently just ch07/toupper-lazy4.hs. diff --git a/demo/src/test/java/fj/EmptyTest.java b/demo/src/test/java/fj/EmptyTest.java index e112a97a..13675822 100644 --- a/demo/src/test/java/fj/EmptyTest.java +++ b/demo/src/test/java/fj/EmptyTest.java @@ -5,9 +5,6 @@ import org.junit.Assert; -/** - * Created by MarkPerry on 30/08/2015. - */ public class EmptyTest { @Ignore @Test diff --git a/etc/release-notes/release-notes-4.8.1.adoc b/etc/release-notes/release-notes-4.8.1.adoc new file mode 100644 index 00000000..45deb658 --- /dev/null +++ b/etc/release-notes/release-notes-4.8.1.adoc @@ -0,0 +1,34 @@ + += Release 4.8.1 + +Released: 8 Oct 2018 + +== Enhancements + +- Add Trampoline.suspend(final F0> a). See #367 https://github.com/functionaljava/functionaljava/pull/367. + +== Fixes + +- Fix regression in lifted semigroup sum. Fix #365, see #366 https://github.com/functionaljava/functionaljava/pull/366. + +== Internal + +- Fix compile under jdk11. Enable jdk11 travis build, see #361 https://github.com/functionaljava/functionaljava/pull/361. +- Fix warnings, see #369 https://github.com/functionaljava/functionaljava/pull/369. +- Add P tests, see #360 https://github.com/functionaljava/functionaljava/pull/360. +- Exclude consume/ from coverage, see #357 https://github.com/functionaljava/functionaljava/pull/357. +- Add DList tests, see #356 https://github.com/functionaljava/functionaljava/pull/356 +- Add Visitor tests, see #354 https://github.com/functionaljava/functionaljava/pull/354. + +== Breaking Changes + +* None. + +== Documentation + +* None. + +== Contributors + +* Jean Baptiste Giraudeau +* Gabor Liptak diff --git a/etc/release-notes/release-notes-4.8.adoc b/etc/release-notes/release-notes-4.8.adoc index 22dbc552..7e8aed61 100644 --- a/etc/release-notes/release-notes-4.8.adoc +++ b/etc/release-notes/release-notes-4.8.adoc @@ -1,28 +1,53 @@ -= Release += Release 4.8 -Proposed release: +Released: 18 Aug 2018 == Enhancements -* TODO. +- Enable upload of snapshot artifacts, see https://github.com/functionaljava/functionaljava/commit/e834e8b. +- Add append methods to all Px classes. Fix #326, see https://github.com/functionaljava/functionaljava/commit/065ed43. +- Introduce the Eval monad, see https://github.com/functionaljava/functionaljava/commit/98294fc. +- Fluent Equal/Ord construction, see #333 https://github.com/functionaljava/functionaljava/pull/333 +- Implement Zipper Eq and Hash and add tests, see #343 https://github.com/functionaljava/functionaljava/pull/343. +- Implement Vector equals, see #350 https://github.com/functionaljava/functionaljava/pull/350. == Fixes -* TODO. +- Fixed a bug in the NonEmptyList Semigroup implementation that resulted in the same NonEmptyList appended to itself. Regression in 4.7, see https://github.com/functionaljava/functionaljava/commit/07f94fa. +- Fixes #334: exception in Either.LeftProjection.traverseIO, see #335 https://github.com/functionaljava/functionaljava/pull/335 == Internal -* TODO. +- Added Scalacheck Arbitrary implementations for Natural and NonEmptyList, see https://github.com/functionaljava/functionaljava/commit/405c3ec +- Added unit test coverage for Semigroup implementations. The StringBuffer and StringBuilder tests fail because both of those types are mutable. The IO test fails because the ArbitraryIO implementation does not implement equals. See https://github.com/functionaljava/functionaljava/commit/ef81130. +- Fixed the ArbitraryIO implementation and created a Properties object for testing the IO semigroup. See https://github.com/functionaljava/functionaljava/commit/a8e979f. +- Equal: remove reference to static field of LazyString. Fix #321, see https://github.com/functionaljava/functionaljava/commit/6c6dabd. +- Add IOFunctions tests, see #340 https://github.com/functionaljava/functionaljava/pull/340. +- Add Stream tests, see #341 https://github.com/functionaljava/functionaljava/pull/341. +- Add tests for Try, F, FW, Digit. See #346 https://github.com/functionaljava/functionaljava/pull/346 +- Add Vector tests, see #347 https://github.com/functionaljava/functionaljava/pull/347 +- Add Optic tests, see #348 https://github.com/functionaljava/functionaljava/pull/348 +- Add Parser tests, see #349 https://github.com/functionaljava/functionaljava/pull/349 +- Add FingerTree tests, see #351 https://github.com/functionaljava/functionaljava/pull/351 +- Add TreeZipper tests, see #352 https://github.com/functionaljava/functionaljava/pull/352 +- Add Reader/Writer tests, see #353 https://github.com/functionaljava/functionaljava/pull/353 == Breaking Changes -* TODO. +None. == Documentation -* TODO. +None. == Contributors -* TODO. +* Jean Baptiste Giraudeau +* Ryan Johnson +* l1cache (cache@bk.ru) +* Gabor Liptak +* janbols +* Iaroslav Zeigerman +* Signey Quitorio + diff --git a/etc/release-notes/release-notes-4.9.adoc b/etc/release-notes/release-notes-4.9.adoc new file mode 100644 index 00000000..62db8e99 --- /dev/null +++ b/etc/release-notes/release-notes-4.9.adoc @@ -0,0 +1,42 @@ + += Release 4.9 + +Released: 14 March 2021 + +== Enhancements + +* Added Gen.streamOf(Gen) +* Added Option.sequence(Validation>) +* Added Gen.sequence(Validation>) +* Added Validation sequence and traverse functions to support various types. Added success and fails functions. +* Added Option sequence and traverse functions for various types. +* Added Seq.bind. +* Added List sequence and traverse functions for various types. +* Added Ord.seqOrd +* Added Seq sequence and traverse functions for various types. +* Added functions to Either. +* Added State bind synonym for flatMap. +* Added Steam sequence and traverse functions for various types. + +== Fixes + +* Fixed Validation.accumulate functions. + +== Internal + +* Support JPMS modules through 'Automatic-Module-Name'. + +== Breaking Changes + +* None. + +== Documentation + +* None. + +== Contributors + +* Jean Baptiste Giraudeau +* Gregoire Neuville +* Drew Taylor +* Mark Perry diff --git a/etc/release-notes/release-notes-5.0.adoc b/etc/release-notes/release-notes-5.0.adoc new file mode 100644 index 00000000..86cfc69e --- /dev/null +++ b/etc/release-notes/release-notes-5.0.adoc @@ -0,0 +1,49 @@ + += Release 5.0 + +Released: TODO + +== Enhancements +* The functions classes F, F2, F0, Effect1 and Effect2 extend the corresponding Java 8 function interface. Removed the corresponding classes F1W, F1Functions, F2W and F2Functions. Similarly for IO and IOW. +* Moved the function wrapper classes F1W and F2W into F and F2 as default functions. +* Added lifting a semigroup to an option monoid, using none as zero. +* Added Trampoline.suspend(F0>) +* Added sum, product and fromString to Longs. +* Added Bounded definition. +* Added toStream of Bounded in Enumerator. +* Added intersection monoid for sets. +* Added set intersection semigroup. +* Added FunctionalInterface annotations for interfaces F0, F, F2 to F8, IO and SafeIO. +* Added functions to IO. +* Added Either3. +* Updated IO and SafeIO inheritance. +* Added conversion functions for Effect, F, Try and TryEffect for low arities. + +== Fixes +* Fixed BitSet properties test. + +== Internal +* Upgraded to Gradle 6.8.3. +* Added Strategy, Validation, Integers, monoid, semigroup and monoid tests. +* Switch from the uptodate-gradle-plugin to gradle-versions-plugin. +* Speed up Gradle tests by running in parallel and not generating reports. + +== Breaking Changes +* Removed Ord parameter from Monoid's setIntersectionMonoid function. +* Removed the classes F1W, F1Functions, F2W, F2Functions, F3W, F3Functions, F4W, F4Functions, F5W, F5Functions, F6W, F6Functions, F7W, F7Functions, F8W and F8Functions. +* Removed deprecated Monoid, Ord, P, P1, Semigroup, Array, HashMap, Java, List, Seq, Set, Stream, Gen, Rand and TreeMap functions. + +== Documentation +* Fixed the javadoc on Either's iif function. +* Fixed doc for union and intersection monoid for sets. +* Fixed semigroup docs. +* Fixed List.headOption. + +== Contributors +* Gabor Liptak +* Jean-Baptiste Giraudeau +* Soundharya Kamaraj +* Yaroslav Atroshenko +* Mark Perry +* Chen Zhang + diff --git a/etc/release-notes/release-notes-5.1.adoc b/etc/release-notes/release-notes-5.1.adoc new file mode 100644 index 00000000..22dbc552 --- /dev/null +++ b/etc/release-notes/release-notes-5.1.adoc @@ -0,0 +1,28 @@ + += Release + +Proposed release: + +== Enhancements + +* TODO. + +== Fixes + +* TODO. + +== Internal + +* TODO. + +== Breaking Changes + +* TODO. + +== Documentation + +* TODO. + +== Contributors + +* TODO. diff --git a/etc/release-process.txt b/etc/release-process.txt index e9bdbb15..f5699a5b 100644 --- a/etc/release-process.txt +++ b/etc/release-process.txt @@ -1,18 +1,16 @@ -Current Release Process -======================= +Release Process +=============== Go through the issues and pull requests and set the Label and Milestone field. Add information to /etc/release-notes/release-notes-.adoc. -Update build.gradle: -* set isSnapshot to false -* set useRetroLambda to true - Update gradle.properties: +* set isSnapshot to false * Set signingEnabled to true + Run the build command: -gradlew clean build upload +gradlew clean build publishAllPublicationsToMavenRepository Login to Sonatype and verify the release: * Login to https://oss.sonatype.org @@ -20,12 +18,13 @@ Login to Sonatype and verify the release: * Tick the release and click Close * Wait until closed * Tick the release and click Release +* It takes ~24 hours for Sonatype to sync to Maven Central where it should appear Commit changes Increase the version: -* Edit build.gradle: update isSnapshot to true, increase fjBaseVersion, update fjConsumeVersion, update useRetroLambda. -* Edit gradle.properties: set signingEnabled to false +* Edit build.gradle: increase fjBaseVersion, update fjConsumeVersion. +* Edit gradle.properties: set signingEnabled to false, set isSnapshot to true Commit changes and push. Notes that CI builds using Travis and Jenkins will fail with the release due to lack of configured signing. @@ -35,9 +34,75 @@ Create tag: Create the next version of the release notes with empty fields using the template. -Copy the generated javadoc for each component to the website repositories' master branch under /javadoc/. Commit the javadoc and push. +Copy the generated javadoc for each component to the website repositories' master branch, see https://github.com/functionaljava/functionaljava.github.io, under /javadoc/. Copy: +- core to functionaljava +- java-core to functionaljava-java-core +- quickcheck to functionaljava-quickcheck + +Commit the javadoc and push. Update the website and Github README.adoc. This includes adding any features to the home page and features page. Updating the doc page with javadoc links. Update the download page with a link to the latest release notes. Send a message to the group and social media about the release, TODO. +Setup Sonatype +====================== +You need to be able to login to https://oss.sonatype.org. Register or check your login details. + +Create/edit the file %UserProfile%\.gradle\gradle.properties, set the values below: + +sonatypeUsername = +sonatypePassword = + + +Setup Artifact Signing +====================== +The below text is a summary from https://gist.github.com/phit/bd3c6d156a2fa5f3b1bc15fa94b3256c. + +As of 2022-02-11, for Windows download Gpg4win 3.1.16 from https://files.gpg4win.org/. + +Open a command prompt and run "gpg --gen-key" and follow the prompts. + +Get your key id by running: "gpg --list-key" + +Example output: + +gpg: checking the trustdb +gpg: marginals needed: 3 completes needed: 1 trust model: pgp +gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u +gpg: next trustdb check due at 2019-06-17 +C:/Users/phit/AppData/Roaming/gnupg/pubring.kbx +----------------------------------------------- +pub rsa2048 2017-06-17 [SC] [expires: 2019-06-17] + 77273D57FA5140E5A91905087A1B92B81840D019 +uid [ultimate] phit@hush.com +sub rsa2048 2017-06-17 [E] [expires: 2019-06-17] + +In this case we only have one key, 77273D57FA5140E5A91905087A1B92B81840D019 or short* 1840D019 which is basically just the last 8 characters of the long ID. + +Export the key using "gpg --export-secret-key >%UserProfile%\secring.gpg" + +Create the file %UserProfile%\.gradle\gradle.properties, set the values below: + +signing.keyId=XXXXXXXX +signing.password=mypassword +signing.secretKeyRingFile=path/to/secring.gpg + +Upload your key + +C:\repos\functionaljava>gpg --list-key +C:/Users/maper/AppData/Roaming/gnupg/pubring.kbx +------------------------------------------------ +pub rsa3072 2021-02-12 [SC] [expires: 2023-02-12] + E86A4EC34F25A9CF6118582A7985AAE03F41B2F9 +uid [ultimate] Mark Perry +sub rsa3072 2021-02-12 [E] [expires: 2023-02-12] + + +C:\repos\functionaljava>gpg --keyserver hkp://keyserver.ubuntu.com --send-keys E86A4EC34F25A9CF6118582A7985AAE03F41B2F9 +gpg: sending key 7985AAE03F41B2F9 to hkp://keyserver.ubuntu.com + + +gradle upload (takes about 3 mins) + + diff --git a/gradle.properties b/gradle.properties index d4552a30..e96743fd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,12 @@ +signingEnabled = false + sonatypeUsername = incorrectUser sonatypePassword = incorrectPwd -signingEnabled = false +#signing.keyId= +#signing.password= +#signing.secretKeyRingFile= + +org.gradle.parallel = true +org.gradle.caching = true diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 29953ea1..5c2d1cf0 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index d76b502e..b1159fc5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index cccdd3d5..83f2acfd 100755 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -109,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` diff --git a/gradlew.bat b/gradlew.bat index e95643d6..24467a14 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/java-core/build.gradle b/java-core/build.gradle index b5ee71ec..f6a6146a 100644 --- a/java-core/build.gradle +++ b/java-core/build.gradle @@ -3,17 +3,15 @@ archivesBaseName = "${project.projectName}-${project.name}" ext { signModule = true + uploadModule = true } dependencies { - compile project(":core") - testCompile dependencyJunit + api project(":core") + testImplementation junitCompile + testRuntimeOnly junitRuntime } performSigning(signingEnabled, signModule) -configureUpload(signingEnabled, signModule) +configureUpload(signingEnabled, signModule, uploadModule) - -uploadArchives.enabled = true - -configureAllRetroLambda() diff --git a/java-core/src/main/java/fj/java/util/ListUtil.java b/java-core/src/main/java/fj/java/util/ListUtil.java index 17fb5519..ffab7968 100644 --- a/java-core/src/main/java/fj/java/util/ListUtil.java +++ b/java-core/src/main/java/fj/java/util/ListUtil.java @@ -5,9 +5,6 @@ import java.util.List; -/** - * Created by MarkPerry on 28/08/2015. - */ public class ListUtil { public static List map(List list, F f) { diff --git a/java-core/src/test/java/fj/EmptyTest.java b/java-core/src/test/java/fj/EmptyTest.java index d1088c03..1c126bfa 100644 --- a/java-core/src/test/java/fj/EmptyTest.java +++ b/java-core/src/test/java/fj/EmptyTest.java @@ -4,9 +4,6 @@ import org.junit.Test; import org.junit.Assert; -/** - * Created by MarkPerry on 30/08/2015. - */ public class EmptyTest { @Ignore @Test diff --git a/java8/build.gradle b/java8/build.gradle deleted file mode 100644 index d6d4c485..00000000 --- a/java8/build.gradle +++ /dev/null @@ -1,18 +0,0 @@ - -archivesBaseName = "${project.projectName}-${project.name}" - -ext { - signModule = true -} - -dependencies { - compile project(":core") - testCompile dependencyJunit -} - -performSigning(signingEnabled, signModule) -configureUpload(signingEnabled, signModule) - -if (!useRetroLambda) { - uploadArchives.enabled = true -} diff --git a/java8/src/test/java/fj/EmptyTest.java b/java8/src/test/java/fj/EmptyTest.java deleted file mode 100644 index e187cccb..00000000 --- a/java8/src/test/java/fj/EmptyTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package fj; - -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; - -/** - * Created by MarkPerry on 30/08/2015. - */ -public class EmptyTest { - - @Ignore @Test - public void missing() { - Assert.fail("not implemented"); - - } -} diff --git a/lib.gradle b/lib.gradle index 253ed251..5ff91f01 100644 --- a/lib.gradle +++ b/lib.gradle @@ -20,7 +20,7 @@ String findJavaCommand(String command) { Boolean doSigning(String signingAllowed, Boolean doModule) { def b = signingAllowed.trim() == "true" && doModule -// println("signModule: ${project.name} signingEnabled: $signingAllowed module: $doModule") +// println("signModule: ${project.name} signingAllowed: $signingAllowed doModule: $doModule") b } @@ -31,72 +31,122 @@ void performSigning(String signingAllowed, Boolean doModule) { } } -void configureUpload(String signingEnabled, Boolean signModule) { +def customisePom(pom, gradleProject) { + pom.withXml { + def root = asNode() - uploadArchives { - enabled = false - repositories { - mavenDeployer { - if (doSigning(signingEnabled, signModule)) { - beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - } + // add all items necessary for maven central publication + root.children().last() + { + resolveStrategy = Closure.DELEGATE_FIRST - repository(url: sonatypeUploadUrl) { - authentication(userName: sonatypeUsername, password: sonatypePassword) + name project.pomProjectName + description project.projectDescription + url project.projectUrl + organization { + name project.pomOrganisation + url project.projectUrl + } + issueManagement { + system 'GitHub' + url project.issueUrl + } + licenses { + license { + name project.licenseName + url project.licenseUrl + distribution 'repo' } - pom { - groupId = project.group - project { - name pomProjectName - packaging 'jar' - description projectDescription - url projectUrl - organization { - name pomOrganisation - url projectUrl - } - scm { - url scmUrl - } - licenses { - license { - name "The BSD3 License" - url "https://github.com/functionaljava/functionaljava/blob/master/etc/LICENCE" - distribution 'repo' + } + scm { + url project.githubUrl + connection project.scmGitFile + developerConnection project.scmSshGitFile + } + } + } +} + + +void configureUpload(String signingEnabled, Boolean signModule, Boolean uploadModule) { + + if (uploadModule) { + + publishing { + + publications { + + mavenJava(MavenPublication) { + groupId project.group + artifactId project.name + version project.version + + from components.java + + customisePom(pom, rootProject) + + artifact sourcesJar + artifact javadocJar + + if (doSigning(signingEnabled, signModule)) { + // sign the pom + pom.withXml { + def pomFile = file("${project.buildDir}/generated-pom.xml.asc") + writeTo(pomFile) + def pomAscFile = signing.sign(pomFile).signatureFiles[0] + artifact(pomAscFile) { + classifier = null + extension = 'pom.asc' } + pomFile.delete() } - developers { - developer { - email primaryEmail + + // sign the artifacts + project.tasks.signArchives.signatureFiles.each { + artifact(it) { + def matcher = it.file =~ /-(sources|javadoc|jre8|jre9)\.jar\.asc$/ + if (matcher.find()) { + classifier = matcher.group(1) + } else { + classifier = null + } + extension = 'jar.asc' } } } + } - } - } - } -} -void configureAllRetroLambda() { - configureRetroLambda(useRetroLambda, newJdkEnvVar, oldJdkEnvVar, retroLambdaTarget) -} + } -void configureRetroLambda(boolean useRetroLambda, String newJdkEnvVar, String oldJdkEnvVar, JavaVersion retroLambdaTarget) { + repositories { + maven { + url project.sonatypeUploadUrl + credentials { + username sonatypeUsername + password sonatypePassword + } + } + } - if (useRetroLambda) { - apply plugin: 'me.tatarka.retrolambda' - retrolambda { - jdk System.getenv(newJdkEnvVar) - oldJdk System.getenv(oldJdkEnvVar) - javaVersion retroLambdaTarget - defaultMethods true } - dependencies { - retrolambdaConfig "net.orfjackal.retrolambda:retrolambda:$retrolambdaVersion" + + model { + tasks.publishMavenJavaPublicationToMavenLocal { + dependsOn(project.tasks.signArchives) + } + tasks.publishMavenJavaPublicationToMavenRepository { + dependsOn(project.tasks.signArchives) + } + tasks.publish { + dependsOn(project.tasks.build) + } +// tasks.install { +// dependsOn(project.tasks.build) +// } } - } else { - project.archivesBaseName = "${project.archivesBaseName}_1.8" + } + } ext { @@ -104,6 +154,4 @@ ext { doSigning = this.&doSigning performSigning = this.&performSigning configureUpload = this.&configureUpload - configureRetroLambda = this.&configureRetroLambda - configureAllRetroLambda = this.&configureAllRetroLambda } diff --git a/performance/build.gradle b/performance/build.gradle index b5bc2916..7efbde2e 100644 --- a/performance/build.gradle +++ b/performance/build.gradle @@ -1,7 +1,6 @@ -configureAllRetroLambda() - dependencies { - compile project(":core") - testCompile dependencyJunit + api project(":core") + testImplementation junitCompile + testRuntimeOnly junitRuntime } diff --git a/performance/src/main/java/fj/data/dummy/DummyClass.java b/performance/src/main/java/fj/data/dummy/DummyClass.java index 72cc5c34..a94dcdde 100644 --- a/performance/src/main/java/fj/data/dummy/DummyClass.java +++ b/performance/src/main/java/fj/data/dummy/DummyClass.java @@ -1,8 +1,5 @@ package fj.data.dummy; -/** - * Created by MarkPerry on 5 Feb 16. - */ public class DummyClass { } diff --git a/performance/src/test/java/fj/data/dummy/DummyTest.java b/performance/src/test/java/fj/data/dummy/DummyTest.java index d3865433..49722bcd 100644 --- a/performance/src/test/java/fj/data/dummy/DummyTest.java +++ b/performance/src/test/java/fj/data/dummy/DummyTest.java @@ -3,9 +3,6 @@ import org.junit.Ignore; import org.junit.Test; -/** - * Created by MarkPerry on 5 Feb 16. - */ public class DummyTest { @Test diff --git a/props-core-scalacheck/build.gradle b/props-core-scalacheck/build.gradle index 10e494c9..396d19d4 100644 --- a/props-core-scalacheck/build.gradle +++ b/props-core-scalacheck/build.gradle @@ -4,19 +4,33 @@ archivesBaseName = "${project.projectName}-${project.name}" apply plugin: 'scala' ext { - scalaVersion = "2.11.12" - scalacheckScalaVersion = "2.11" +// scalaVersion = "2.11.12" +// scalacheckScalaVersion = "2.11" + scalaVersion = "2.12.15" + scalacheckScalaVersion = "2.12" +// scalaVersion = "2.13.8" +// scalacheckScalaVersion = "2.13" + scalacheckVersion = "1.12.6" +// scalacheckVersion = "1.13.5" +// scalacheckVersion = "1.14.0" +// scalacheckVersion = "1.15.2" + signModule = true } dependencies { - compile project(":core") - compile "org.scala-lang:scala-library:$scalaVersion" - compile "org.scalacheck:scalacheck_$scalacheckScalaVersion:$scalacheckVersion" + api project(":core") + api "org.scala-lang:scala-library:$scalaVersion" + api "org.scalacheck:scalacheck_$scalacheckScalaVersion:$scalacheckVersion" + + testImplementation junitCompile + testRuntimeOnly junitRuntime +} - testCompile dependencyJunit +tasks.withType(ScalaCompile) { + scalaCompileOptions.additionalParameters = ["-feature", "-language:implicitConversions", "-language:postfixOps"] } performSigning(signingEnabled, signModule) -configureUpload(signingEnabled, signModule) +configureUpload(signingEnabled, signModule, project.uploadModule) diff --git a/props-core-scalacheck/src/test/scala/fj/Tests.scala b/props-core-scalacheck/src/test/scala/fj/Tests.scala index d94661f9..c5ec3977 100644 --- a/props-core-scalacheck/src/test/scala/fj/Tests.scala +++ b/props-core-scalacheck/src/test/scala/fj/Tests.scala @@ -18,7 +18,7 @@ object Tests { fj.control.parallel.CheckParModule.properties ).flatten - def main(args: Array[String]) { + def main(args: Array[String]): scala.Unit = { run(tests) // System.exit(0) } diff --git a/props-core-scalacheck/src/test/scala/fj/control/parallel/CheckParModule.scala b/props-core-scalacheck/src/test/scala/fj/control/parallel/CheckParModule.scala index 8e13eb1b..bba1feff 100644 --- a/props-core-scalacheck/src/test/scala/fj/control/parallel/CheckParModule.scala +++ b/props-core-scalacheck/src/test/scala/fj/control/parallel/CheckParModule.scala @@ -46,11 +46,4 @@ object CheckParModule extends Properties("ParModule") { property("parMapArray") = forAll((s: Array[String], p: ParModule) => arrayEqual(stringEqual).eq(s.map(rev), p.parMap(s, rev).claim)) - property("parFlatMap") = forAll((s: Stream[String], p: ParModule) => { - val f = (x: String) => Stream.stream(x, rev(x)) : Stream[String] - streamEqual(stringEqual).eq(s.bind(f), p.parFlatMap(s, f).claim)}) - - property("parFoldMap") = forAll((s: Stream[String], p: ParModule) => { - val chunk = (x: Stream[String]) => P.p(Stream.stream(x.head), x.tail._1) - stringEqual.eq(stringMonoid.sumLeft(s.map(rev)), p.parFoldMap(s, rev, stringMonoid, chunk).claim)}) } diff --git a/props-core-scalacheck/src/test/scala/fj/data/CheckArray.scala b/props-core-scalacheck/src/test/scala/fj/data/CheckArray.scala index f8cce111..b116a6a9 100755 --- a/props-core-scalacheck/src/test/scala/fj/data/CheckArray.scala +++ b/props-core-scalacheck/src/test/scala/fj/data/CheckArray.scala @@ -63,7 +63,7 @@ object CheckArray extends Properties("Array") { a.reverse.foldRight((a: String, b: Array[String]) => array[String](scala.Array(a): _*).append(b), empty[String]))) property("scans") = forAll((a: Array[Int], z: Int) => { - val add = (x: Int, y: Int) => x + y + val add: F2[Int, Int, Int] = (x: Int, y: Int) => x + y val left = a.scanLeft(add, z) val right = a.reverse().scanRight(add, z).reverse() @@ -72,7 +72,7 @@ object CheckArray extends Properties("Array") { property("scans1") = forAll((a: Array[Int]) => (a.length() > 0) ==> { - val add = (x: Int, y: Int) => x + y + val add: F2[Int, Int, Int] = (x: Int, y: Int) => x + y val left = a.scanLeft1(add) val right = a.reverse().scanRight1(add).reverse() diff --git a/props-core-scalacheck/src/test/scala/fj/data/CheckHashMap.scala b/props-core-scalacheck/src/test/scala/fj/data/CheckHashMap.scala index dd95dabf..c629a10f 100755 --- a/props-core-scalacheck/src/test/scala/fj/data/CheckHashMap.scala +++ b/props-core-scalacheck/src/test/scala/fj/data/CheckHashMap.scala @@ -1,119 +1,110 @@ -package fj -package data - -import fj.function.Effect1 -import org.scalacheck.Prop._ -import ArbitraryHashMap._ -import Equal._ -import Hash._ -import Ord._ -import fj.data.Option._ -import scala.collection.JavaConversions._ -import org.scalacheck.{Arbitrary, Properties} -import data.ArbitraryList._ -import org.scalacheck.Arbitrary._ -import java.util.Map - -object CheckHashMap extends Properties("HashMap") { - implicit val equalInt: Equal[Int] = intEqual contramap ((x: Int) => (x: java.lang.Integer)) - implicit val hashInt: Hash[Int] = intHash contramap ((x: Int) => (x: java.lang.Integer)) - - implicit def arbitraryListOfIterableP2: Arbitrary[java.lang.Iterable[P2[Int, String]]] = - Arbitrary(listOf(arbitrary[(Int, String)]) - .map(_.map((tuple: (Int, String)) => P.p(tuple._1, tuple._2)) - .asInstanceOf[java.lang.Iterable[P2[Int, String]]])) - - property("eq") = forAll((m: HashMap[Int, String], x: Int, y: Int) => m.eq(x, y) == equalInt.eq(x, y)) - - property("hash") = forAll((m: HashMap[Int, String], x: Int) => m.hash(x) == hashInt.hash(x)) - - property("get") = forAll((m: HashMap[Int, String], k: Int) => optionEqual(stringEqual).eq(m.get(k), m.get.f(k))) - - property("set") = forAll((m: HashMap[Int, String], k: Int, v: String) => { - m.set(k, v) - m.get(k).some == v - }) - - property("clear") = forAll((m: HashMap[Int, String], k: Int) => { - m.clear - m.get(k).isNone - }) - - property("contains") = forAll((m: HashMap[Int, String], k: Int) => m.get(k).isSome == m.contains(k)) - - property("keys") = forAll((m: HashMap[Int, String]) => m.keys.forall((k: Int) => (m.get(k).isSome): java.lang.Boolean)) - - property("isEmpty") = forAll((m: HashMap[Int, String], k: Int) => m.get(k).isNone || !m.isEmpty) - - property("size") = forAll((m: HashMap[Int, String], k: Int) => m.get(k).isNone || m.size != 0) - - property("delete") = forAll((m: HashMap[Int, String], k: Int) => { - m.delete(k) - m.get(k).isNone - }) - - property("toList") = forAll((m: HashMap[Int, String]) => { - val list = m.toList - list.length() == m.keys().length() && list.forall((entry: P2[Int, String]) => - optionEqual(stringEqual).eq(m.get(entry._1()), some(entry._2())).asInstanceOf[java.lang.Boolean]) - }) - - property("getDelete") = forAll((m: HashMap[Int, String], k: Int) => { - val x = m.get(k) - val y = m.getDelete(k) - val z = m.get(k) - - z.isNone && optionEqual(stringEqual).eq(x, y) - }) - - property("from") = forAll((entries: java.lang.Iterable[P2[Int, String]]) => { - val map = HashMap.iterableHashMap[Int, String](equalInt, hashInt, entries) - entries.groupBy(_._1) - .forall((e: (Int, Iterable[P2[Int, String]])) => e._2 - .exists((elem: P2[Int, String]) => optionEqual(stringEqual).eq(map.get(e._1), Option.some(elem._2)))) - }) - - property("map") = forAll((m: HashMap[Int, String]) => { - val keyFunction: F[Int, String] = (i: Int) => i.toString - val valueFunction: (String) => String = (s: String) => s + "a" - val mapped = m.map(keyFunction, valueFunction, stringEqual, stringHash) - val keysAreEqual = m.keys().map(keyFunction).toSet == mapped.keys.toSet - val appliedFunctionsToKeysAndValues: Boolean = m.keys().forall((key: Int) => { - val mappedValue = mapped.get(keyFunction.f(key)) - val oldValueMapped = some(valueFunction.f(m.get(key).some())) - Equal.optionEqual(stringEqual).eq(mappedValue, oldValueMapped) - }) - - keysAreEqual && appliedFunctionsToKeysAndValues - }) - - property("toMap") = forAll((m: HashMap[Int, String]) => { - val toMap: Map[Int, String] = m.toMap - m.keys().forall((key: Int) => m.get(key).some() == toMap.get(key)) - }) - - property("fromMap") = forAll((m: HashMap[Int, String]) => { - val map = new java.util.HashMap[Int, String]() - m.keys().foreach((key: Int) => { - map.put(key, m.get(key).some()) - Unit.unit() - }) - val fromMap: HashMap[Int, String] = new HashMap[Int, String](map) - val keysAreEqual = m.keys.toSet == fromMap.keys.toSet - val valuesAreEqual = m.keys().forall((key: Int) => - optionEqual(stringEqual).eq(m.get(key), fromMap.get(key))) - keysAreEqual && valuesAreEqual - }) - - property("No null values") = forAll((m: List[Int]) => { - val map = HashMap.hashMap[Int, Int]() - m.foreachDoEffect(new Effect1[Int] { - def f(a: Int) { - map.set(a, null.asInstanceOf[Int]) - } - }) - m.forall(new F[Int, java.lang.Boolean]() { - def f(a: Int) = map.contains(a) == false - }) - }) +package fj +package data + +import fj.function.Effect1 +import org.scalacheck.Prop._ +import ArbitraryHashMap._ +import Equal._ +import Hash._ +import Ord._ +import fj.data.Option._ +import scala.collection.JavaConversions._ +//import scala.collection.JavaConverters._ + +import org.scalacheck.{Arbitrary, Properties} +import data.ArbitraryList._ +import org.scalacheck.Arbitrary._ +import java.util.Map + +object CheckHashMap extends Properties("HashMap") { + implicit val equalInt: Equal[Int] = intEqual contramap ((x: Int) => (x: java.lang.Integer)) + implicit val hashInt: Hash[Int] = intHash contramap ((x: Int) => (x: java.lang.Integer)) + + implicit def arbitraryListOfIterableP2: Arbitrary[java.lang.Iterable[P2[Int, String]]] = + Arbitrary(listOf(arbitrary[(Int, String)]) + .map(_.map((tuple: (Int, String)) => P.p(tuple._1, tuple._2)) + .asInstanceOf[java.lang.Iterable[P2[Int, String]]])) + + property("eq") = forAll((m: HashMap[Int, String], x: Int, y: Int) => m.eq(x, y) == equalInt.eq(x, y)) + + property("hash") = forAll((m: HashMap[Int, String], x: Int) => m.hash(x) == hashInt.hash(x)) + + property("get") = forAll((m: HashMap[Int, String], k: Int) => optionEqual(stringEqual).eq(m.get(k), m.get.f(k))) + + property("set") = forAll((m: HashMap[Int, String], k: Int, v: String) => { + m.set(k, v) + m.get(k).some == v + }) + + property("clear") = forAll((m: HashMap[Int, String], k: Int) => { + m.clear + m.get(k).isNone + }) + + property("contains") = forAll((m: HashMap[Int, String], k: Int) => m.get(k).isSome == m.contains(k)) + + property("keys") = forAll((m: HashMap[Int, String]) => m.keys.forall((k: Int) => (m.get(k).isSome): java.lang.Boolean)) + + property("isEmpty") = forAll((m: HashMap[Int, String], k: Int) => m.get(k).isNone || !m.isEmpty) + + property("size") = forAll((m: HashMap[Int, String], k: Int) => m.get(k).isNone || m.size != 0) + + property("delete") = forAll((m: HashMap[Int, String], k: Int) => { + m.delete(k) + m.get(k).isNone + }) + + property("toList") = forAll((m: HashMap[Int, String]) => { + val list = m.toList + list.length() == m.keys().length() && list.forall((entry: P2[Int, String]) => + optionEqual(stringEqual).eq(m.get(entry._1()), some(entry._2())).asInstanceOf[java.lang.Boolean]) + }) + + property("getDelete") = forAll((m: HashMap[Int, String], k: Int) => { + val x = m.get(k) + val y = m.getDelete(k) + val z = m.get(k) + + z.isNone && optionEqual(stringEqual).eq(x, y) + }) + + property("from") = forAll((entries: java.lang.Iterable[P2[Int, String]]) => { + val map = HashMap.iterableHashMap[Int, String](equalInt, hashInt, entries) + entries.groupBy(_._1) + .forall((e: (Int, Iterable[P2[Int, String]])) => e._2 + .exists((elem: P2[Int, String]) => optionEqual(stringEqual).eq(map.get(e._1), Option.some(elem._2)))) + }) + + property("map") = forAll((m: HashMap[Int, String]) => { + val keyFunction: F[Int, String] = (i: Int) => i.toString + val valueFunction: (String) => String = (s: String) => s + "a" + val mapped = m.map(keyFunction, valueFunction, stringEqual, stringHash) + val keysAreEqual = m.keys().map(keyFunction).toSet == mapped.keys.toSet + val appliedFunctionsToKeysAndValues: Boolean = m.keys().forall((key: Int) => { + val mappedValue = mapped.get(keyFunction.f(key)) + val oldValueMapped = some(valueFunction.f(m.get(key).some())) + Equal.optionEqual(stringEqual).eq(mappedValue, oldValueMapped) + }) + + keysAreEqual && appliedFunctionsToKeysAndValues + }) + + property("toMap") = forAll((m: HashMap[Int, String]) => { + val toMap: Map[Int, String] = m.toMap + m.keys().forall((key: Int) => m.get(key).some() == toMap.get(key)) + }) + + property("fromMap") = forAll((m: HashMap[Int, String]) => { + val map = new java.util.HashMap[Int, String]() + m.keys().foreach((key: Int) => { + map.put(key, m.get(key).some()) + Unit.unit() + }) + val fromMap: HashMap[Int, String] = new HashMap[Int, String](map) + val keysAreEqual = m.keys.toSet == fromMap.keys.toSet + val valuesAreEqual = m.keys().forall((key: Int) => + optionEqual(stringEqual).eq(m.get(key), fromMap.get(key))) + keysAreEqual && valuesAreEqual + }) + } \ No newline at end of file diff --git a/props-core-scalacheck/src/test/scala/fj/data/CheckIteratee.scala b/props-core-scalacheck/src/test/scala/fj/data/CheckIteratee.scala index 86feeb7a..ff085f87 100644 --- a/props-core-scalacheck/src/test/scala/fj/data/CheckIteratee.scala +++ b/props-core-scalacheck/src/test/scala/fj/data/CheckIteratee.scala @@ -7,7 +7,6 @@ import Unit.unit import List.{list, nil} import Option.{some, none} import fj.Function._ -import fj.F1Functions import fj.data.Iteratee._ import org.scalacheck.Prop._ @@ -50,7 +49,7 @@ object CheckIteratee extends Properties("Iteratee") { var tail = l while(!isDone(i) && !tail.isEmpty) { val input = Input.el(tail.head) - val cont: F[F[Input[E], IterV[E, A]], P1[IterV[E, A]]] = F1Functions.`lazy`(Function.apply(input)) + val cont: F[F[Input[E], IterV[E, A]], P1[IterV[E, A]]] = Function.apply(input).`lazy`() i = i.fold(done, cont)._1 tail = tail.tail } diff --git a/props-core/build.gradle b/props-core/build.gradle index d697cff5..534a051b 100644 --- a/props-core/build.gradle +++ b/props-core/build.gradle @@ -1,9 +1,8 @@ archivesBaseName = "${project.projectName}-${project.name}" -configureAllRetroLambda() - dependencies { - compile project(":quickcheck") - testCompile dependencyJunit + api project(":quickcheck") + testImplementation junitCompile + testRuntimeOnly junitRuntime } diff --git a/props-core/src/test/java/fj/MemoisationTest.java b/props-core/src/test/java/fj/MemoisationTest.java index 865f76a3..605546ef 100644 --- a/props-core/src/test/java/fj/MemoisationTest.java +++ b/props-core/src/test/java/fj/MemoisationTest.java @@ -12,9 +12,6 @@ import static fj.test.Property.property; import static org.junit.Assert.assertTrue; -/** - * Created by mperry on 14/07/2014. - */ @RunWith(PropertyTestRunner.class) public class MemoisationTest { diff --git a/props-core/src/test/java/fj/control/parallel/CheckParModuleTest.java b/props-core/src/test/java/fj/control/parallel/CheckParModuleTest.java new file mode 100644 index 00000000..0c4f418f --- /dev/null +++ b/props-core/src/test/java/fj/control/parallel/CheckParModuleTest.java @@ -0,0 +1,48 @@ +package fj.control.parallel; + +import fj.Equal; +import fj.F; +import fj.Monoid; +import fj.P; +import fj.P1; +import fj.P2; +import fj.data.Stream; +import fj.function.Strings; +import fj.test.Arbitrary; +import fj.test.Property; +import fj.test.runner.PropertyTestRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static fj.Equal.stringEqual; +import static fj.Monoid.stringMonoid; +import static fj.test.Arbitrary.arbInteger; +import static fj.test.Arbitrary.arbP1; +import static fj.test.Arbitrary.arbParModule; +import static fj.test.Arbitrary.arbStream; +import static fj.test.Arbitrary.arbString; +import static fj.test.Property.prop; +import static fj.test.Property.property; + +@RunWith(PropertyTestRunner.class) +public class CheckParModuleTest { + + public Property parFlatMap() { + return property(arbStream(arbString), arbParModule(), (str, pm) -> { + F> f = s3 -> Stream.stream(s3, Strings.reverse().f(s3)); + return prop(Equal.streamEqual(stringEqual).eq(str.bind(f), pm.parFlatMap(str, f).claim())); + }); + } + + public Property parFoldMap() { + return property(arbStream(arbString), arbParModule(), (str, pm) -> { + F, P2, Stream>> chunk = x -> P.p(Stream.stream(x.head()), x.tail()._1()); + return prop(stringEqual.eq( + stringMonoid.sumLeft(str.map(Strings.reverse())), + pm.parFoldMap(str, Strings.reverse(), stringMonoid, chunk).claim() + )); + }); + } + + +} diff --git a/props-core/src/test/java/fj/data/ReaderTest.java b/props-core/src/test/java/fj/data/ReaderTest.java index 87e2b52a..48352620 100644 --- a/props-core/src/test/java/fj/data/ReaderTest.java +++ b/props-core/src/test/java/fj/data/ReaderTest.java @@ -5,19 +5,15 @@ import fj.test.*; import org.junit.Test; -import static fj.F1Functions.bind; -import static fj.F1Functions.map; import static fj.test.Arbitrary.*; import static fj.test.Cogen.cogenInteger; import static fj.test.Property.prop; import static fj.test.Property.property; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertTrue; -/** - * Created by MarkPerry on 4/12/2014. - */ + public class ReaderTest { @Test @@ -44,7 +40,7 @@ public void testMapProp() { arbF(cogenInteger, arbInteger), arbInteger, (f, g, i) -> { - int expected = map(f, g).f(i); + int expected = f.map(g).f(i); // System.out.println(String.format("input: %d, result: %d", i, expected)); return prop(expected == Reader.unit(f).map(g).f(i)); }); @@ -59,7 +55,7 @@ public void testFlatMapProp() { a, arbInteger, (f, g, i) -> { - int expected = bind(f, j -> g.f(j).getFunction()).f(i); + int expected = f.bind(j -> g.f(j).getFunction()).f(i); // System.out.println(String.format("input: %d, result: %d", i, expected)); return prop(expected == Reader.unit(f).flatMap(g).f(i)); } diff --git a/props-core/src/test/java/fj/data/TestRngState.java b/props-core/src/test/java/fj/data/TestRngState.java index 5f5f81bb..41143c17 100644 --- a/props-core/src/test/java/fj/data/TestRngState.java +++ b/props-core/src/test/java/fj/data/TestRngState.java @@ -17,9 +17,6 @@ import static fj.test.Property.property; import static fj.test.Variant.variant; -/** - * Created by mperry on 4/08/2014. - */ public class TestRngState { static List expected1 = List.list(4,4,2,2,2,5,3,3,1,5); diff --git a/props-core/src/test/java/fj/data/WriterTest.java b/props-core/src/test/java/fj/data/WriterTest.java index 25f99b22..50134da3 100644 --- a/props-core/src/test/java/fj/data/WriterTest.java +++ b/props-core/src/test/java/fj/data/WriterTest.java @@ -16,9 +16,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -/** - * Created by MarkPerry on 17/12/2014. - */ public class WriterTest { @Test diff --git a/props-core/src/test/java/fj/data/fingertrees/FingerTreeProperties.java b/props-core/src/test/java/fj/data/fingertrees/FingerTreeProperties.java index b7ed83f5..446716c2 100644 --- a/props-core/src/test/java/fj/data/fingertrees/FingerTreeProperties.java +++ b/props-core/src/test/java/fj/data/fingertrees/FingerTreeProperties.java @@ -1,6 +1,5 @@ package fj.data.fingertrees; -import fj.P2; import fj.data.Stream; import fj.test.Property; import fj.test.reflect.CheckParams; @@ -12,9 +11,6 @@ import static fj.test.Property.prop; import static fj.test.Property.property; -/** - * Created by MarkPerry on 10/10/2015. - */ @RunWith(PropertyTestRunner.class) @CheckParams(maxSize = 10000) public class FingerTreeProperties { diff --git a/props-core/src/test/java/fj/data/fingertrees/FingerTreeTest.java b/props-core/src/test/java/fj/data/fingertrees/FingerTreeTest.java index b25fff51..8cc94825 100644 --- a/props-core/src/test/java/fj/data/fingertrees/FingerTreeTest.java +++ b/props-core/src/test/java/fj/data/fingertrees/FingerTreeTest.java @@ -11,13 +11,9 @@ import static fj.Monoid.intMinMonoid; import static fj.data.fingertrees.FingerTree.measured; import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; - -/** - * Created by MarkPerry on 10/10/2015. - */ public class FingerTreeTest { public static final int SIZE = 10; diff --git a/props-core/src/test/java/fj/data/hamt/BitSetProperties.java b/props-core/src/test/java/fj/data/hamt/BitSetProperties.java index f3527972..fbda987b 100644 --- a/props-core/src/test/java/fj/data/hamt/BitSetProperties.java +++ b/props-core/src/test/java/fj/data/hamt/BitSetProperties.java @@ -31,10 +31,6 @@ import static fj.test.Property.prop; import static fj.test.Property.property; -/** - * Created by maperr on 31/05/2016. - */ - @RunWith(PropertyTestRunner.class) @CheckParams(maxSize = 10000) public class BitSetProperties { @@ -156,15 +152,19 @@ Property rangeTest() { int h = list.index(2); int m = Math.max(l, Math.min(list.index(1), h - 1)); int vh = list.index(3); - + BitSet bs1 = longBitSet(x); BitSet bs2 = bs1.range(l, h); + if(l==h){ + return prop(true); + } boolean b = - bs1.isSet(m) == bs2.isSet(m - l) && + bs1.isSet(m) == bs2.isSet(m - l) && bs2.isSet(vh - l) == false; return prop(b); }); } + Property setTest() { return property(arbNaturalLong, arbBitSetSize, (l, i) -> prop(longBitSet(l).set(i).isSet(i))); diff --git a/props-core/src/test/java/fj/data/properties/NonEmptyListProperties.java b/props-core/src/test/java/fj/data/properties/NonEmptyListProperties.java index 256fb3a8..64ce1934 100644 --- a/props-core/src/test/java/fj/data/properties/NonEmptyListProperties.java +++ b/props-core/src/test/java/fj/data/properties/NonEmptyListProperties.java @@ -25,9 +25,6 @@ import static fj.test.Property.property; import static java.lang.Math.min; -/** - * Created by Zheka Kozlov on 02.06.2015. - */ @RunWith(PropertyTestRunner.class) @CheckParams(maxSize = 10000) public class NonEmptyListProperties { diff --git a/props-core/src/test/java/fj/data/properties/PriorityQueueProperties.java b/props-core/src/test/java/fj/data/properties/PriorityQueueProperties.java index 2bde7e6e..5460bfdf 100644 --- a/props-core/src/test/java/fj/data/properties/PriorityQueueProperties.java +++ b/props-core/src/test/java/fj/data/properties/PriorityQueueProperties.java @@ -24,11 +24,8 @@ import static fj.test.Property.prop; import static fj.test.Property.property; import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; -/** - * Created by MarkPerry on 18 Jun 16. - */ @RunWith(PropertyTestRunner.class) @CheckParams(maxSize = 100) public class PriorityQueueProperties { diff --git a/props-core/src/test/java/fj/data/properties/SetProperties.java b/props-core/src/test/java/fj/data/properties/SetProperties.java index 5349b650..2657b937 100644 --- a/props-core/src/test/java/fj/data/properties/SetProperties.java +++ b/props-core/src/test/java/fj/data/properties/SetProperties.java @@ -15,9 +15,6 @@ import static fj.test.Property.prop; import static fj.test.Property.property; -/** - * Created by MarkPerry on 18/08/2015. - */ @RunWith(PropertyTestRunner.class) @CheckParams(maxSize = 10000) public class SetProperties { diff --git a/props-core/src/test/java/fj/data/properties/TreeMapProperties.java b/props-core/src/test/java/fj/data/properties/TreeMapProperties.java index eb71749b..50b323ff 100644 --- a/props-core/src/test/java/fj/data/properties/TreeMapProperties.java +++ b/props-core/src/test/java/fj/data/properties/TreeMapProperties.java @@ -23,9 +23,6 @@ import static fj.test.Arbitrary.arbTreeMap; import static fj.test.Property.*; -/** - * Created by MarkPerry on 29/08/2015. - */ @RunWith(PropertyTestRunner.class) @CheckParams(maxSize = 10000) public class TreeMapProperties { diff --git a/props-core/src/test/java/fj/data/properties/ValidationProperties.java b/props-core/src/test/java/fj/data/properties/ValidationProperties.java index f5205d73..a47383ab 100644 --- a/props-core/src/test/java/fj/data/properties/ValidationProperties.java +++ b/props-core/src/test/java/fj/data/properties/ValidationProperties.java @@ -13,9 +13,6 @@ import static fj.test.Property.implies; import static fj.test.Property.prop; -/** - * Created by MarkPerry on 3/07/2015. - */ @RunWith(PropertyTestRunner.class) public class ValidationProperties { diff --git a/quickcheck/build.gradle b/quickcheck/build.gradle index 45e379da..c9851596 100644 --- a/quickcheck/build.gradle +++ b/quickcheck/build.gradle @@ -1,18 +1,15 @@ ext { signModule = true + uploadModule = true } archivesBaseName = "${project.projectName}-${project.name}" -configureAllRetroLambda() - dependencies { - compile project(":core") - compile dependencyJunit + api project(":core") + api junitCompile } performSigning(signingEnabled, signModule) -configureUpload(signingEnabled, signModule) - -uploadArchives.enabled = true +configureUpload(signingEnabled, signModule, uploadModule) diff --git a/quickcheck/src/main/java/fj/data/test/PropertyAssert.java b/quickcheck/src/main/java/fj/data/test/PropertyAssert.java index 00187b15..d37c69a3 100644 --- a/quickcheck/src/main/java/fj/data/test/PropertyAssert.java +++ b/quickcheck/src/main/java/fj/data/test/PropertyAssert.java @@ -5,9 +5,6 @@ import fj.test.Property; import org.junit.Assert; -/** - * Created by MarkPerry on 18/12/2014. - */ public final class PropertyAssert { private PropertyAssert(){} diff --git a/quickcheck/src/main/java/fj/test/Arbitrary.java b/quickcheck/src/main/java/fj/test/Arbitrary.java index 95bdedca..04f1e20b 100644 --- a/quickcheck/src/main/java/fj/test/Arbitrary.java +++ b/quickcheck/src/main/java/fj/test/Arbitrary.java @@ -23,6 +23,9 @@ import fj.P6; import fj.P7; import fj.P8; +import fj.Unit; +import fj.control.parallel.ParModule; +import fj.control.parallel.Strategy; import fj.data.*; import fj.LcgRng; import fj.Ord; @@ -65,9 +68,12 @@ import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; /** * Common Gen helper functions. @@ -98,6 +104,24 @@ public static Gen> arbState(Gen as, Cogen cs, Gen aa return arbF(cs, arbP2(as, aa)).map(State::unit); } + public static Gen arbParModule() { + return Arbitrary.arbStrategy().map(s -> ParModule.parModule(s)); + } + + public static Gen> arbStrategy() { + Strategy s = Strategy.executorStrategy(fixedThreadsExecutorService(2)); + return Gen.elements(s); + } + + private static ExecutorService fixedThreadsExecutorService(int n) { + return Executors.newFixedThreadPool(n, r -> { + ThreadFactory tf = Executors.defaultThreadFactory(); + Thread t = tf.newThread(r); + t.setDaemon(true); + return t; + }); + } + /** * An arbitrary for the LcgRng. */ @@ -261,12 +285,12 @@ public static Gen> arbF4Invariant(final Gen */ public static Gen> arbF6Invariant(final Gen a) { return a.map(compose(Function.uncurryF6(), - compose(Function.>>>>>constant(), - compose(Function.>>>>constant(), - compose(Function.>>>constant(), - compose(Function.>>constant(), - compose(Function.>constant(), - Function.constant()))))))); + compose(Function.>>>>>constant(), + compose(Function.>>>>constant(), + compose(Function.>>>constant(), + compose(Function.>>constant(), + compose(Function.>constant(), + Function.constant()))))))); } /** @@ -302,13 +326,13 @@ public static Gen> arbF4Invariant(final Gen */ public static Gen> arbF7Invariant(final Gen a) { return a.map(compose(Function.uncurryF7(), - compose(Function.>>>>>>constant(), - compose(Function.>>>>>constant(), - compose(Function.>>>>constant(), - compose(Function.>>>constant(), - compose(Function.>>constant(), - compose(Function.>constant(), - Function.constant())))))))); + compose(Function.>>>>>>constant(), + compose(Function.>>>>>constant(), + compose(Function.>>>>constant(), + compose(Function.>>>constant(), + compose(Function.>>constant(), + compose(Function.>constant(), + Function.constant())))))))); } /** @@ -345,18 +369,18 @@ public static Gen> arbF4Invariant(final Gen * @return An arbitrary for function-8. */ public static Gen> arbF8Invariant( - final Gen a) { + final Gen a) { return a.map(compose(Function.uncurryF8(), - compose(Function.>>>>>>>constant(), - compose(Function.>>>>>>constant(), - compose(Function.>>>>>constant(), - compose( - Function.>>>>constant(), - compose(Function.>>>constant(), - compose( - Function.>>constant(), - compose(Function.>constant(), - Function.constant()))))))))); + compose(Function.>>>>>>>constant(), + compose(Function.>>>>>>constant(), + compose(Function.>>>>>constant(), + compose( + Function.>>>>constant(), + compose(Function.>>>constant(), + compose( + Function.>>constant(), + compose(Function.>constant(), + Function.constant()))))))))); } /** diff --git a/quickcheck/src/main/java/fj/test/Gen.java b/quickcheck/src/main/java/fj/test/Gen.java index c375874d..8fe1e6f5 100644 --- a/quickcheck/src/main/java/fj/test/Gen.java +++ b/quickcheck/src/main/java/fj/test/Gen.java @@ -9,6 +9,8 @@ import fj.data.Array; import fj.data.List; import fj.data.Option; +import fj.data.Stream; +import fj.data.Validation; import fj.function.Effect1; import static fj.Bottom.error; @@ -318,6 +320,19 @@ public static Gen> sequenceN(final int n, final Gen g) { return sequence(replicate(n, g)); } + /** + * Transform a validation for a generator into a generator of validations: if the given validation is a failure, the + * generator produces that failure value; if the given validation is a success, the generator produces success values. + * + * @param gv The validation for a generator. + * @param the type of the value + * @param the type of the failure + * @return if the given validation is a failure, the generator produces that failure value; if the given validation is a success, the generator produces success values. + */ + public static Gen> sequence(final Validation> gv) { + return gen(i -> r -> gv.map(g -> g.gen(i, r))); + } + /** * Constructs a generator that can access its construction arguments — size and random * generator. @@ -497,6 +512,18 @@ public static Gen> listOf1(final Gen g) { return listOf(g, 1); } + /** + * Returns a generator of streams whose values come from the given generator. + * + * @param g the generator to produce values from for the returned generator + * @param the type of the generator + * + * @return A generator of streams whose values come from the given generator. + */ + public static Gen> streamOf(final Gen g) { + return gen(i -> r -> Stream.cons(g.gen(i, r), () -> streamOf(g).gen(i, r))); + } + /** * Returns a generator that picks one element from the given list. If the given list is empty, then the * returned generator will never produce a value. @@ -509,24 +536,6 @@ public static Gen pickOne(List as) { return wordOf(1, as).map(List::head); } - /** - * Returns a generator of lists that picks the given number of elements from the given list. If - * the given number is less than zero or greater than the length of the given list, then the - * returned generator will never produce a value. - *

    - * Note: pick is synonymous with combinationOf - * - * @deprecated As of release 4.6, use {@link #combinationOf} - * - * @param n The number of elements to pick from the given list. - * @param as The list from which to pick elements. - * @return A generator of lists that picks the given number of elements from the given list. - */ - @Deprecated - public static Gen> pick(int n, List as) { - return combinationOf(n, as); - } - /** * Returns a generator of lists that picks the given number of elements from the given list. The selection is * a combination without replacement of elements from the given list, i.e. @@ -651,21 +660,6 @@ private static Gen> pick(Gen> indexesGen, Array as) indexes.foldLeft((acc, index) -> cons(as.get(index), acc), List.nil()).reverse()); } - /** - * Returns a generator of lists that produces some of the values of the given list. - *

    - * Note: someOf is synonymous with someCombinationOf - * - * @deprecated As of release 4.6, use {@link #someCombinationOf} - * - * @param as The list from which to pick values. - * @return A generator of lists that produces some of the values of the given list. - */ - @Deprecated - public static Gen> someOf(List as) { - return someCombinationOf(as); - } - /** * Returns a generator of lists that produces some of the values of the given list. The selection is * a combination without replacement of elements from the given list, i.e. diff --git a/quickcheck/src/main/java/fj/test/Rand.java b/quickcheck/src/main/java/fj/test/Rand.java index b622dfe7..e47aa8a8 100644 --- a/quickcheck/src/main/java/fj/test/Rand.java +++ b/quickcheck/src/main/java/fj/test/Rand.java @@ -5,7 +5,6 @@ import java.util.Random; -import static fj.data.Option.none; import static fj.data.Option.some; import static java.lang.Math.max; import static java.lang.Math.min; @@ -18,18 +17,16 @@ public final class Rand { private final F, F>> f; private final F, F>> g; - - // TODO Change to F when rand(f,g) is removed - private final Option> optOnReseed; + private final F onReseed; private Rand( F, F>> f, F, F>> g, - Option> optOnReseed) { + F onReseed) { this.f = f; this.g = g; - this.optOnReseed = optOnReseed; + this.onReseed = onReseed; } /** @@ -88,33 +85,7 @@ public double choose(final double from, final double to) { * @return A random generator with the given seed. */ public Rand reseed(long seed) { - return optOnReseed.option( - () -> { - throw new IllegalStateException("reseed() called on a Rand created with deprecated rand() method"); - }, - onReseed -> onReseed.f(seed)); - } - - /** - * Constructs a random generator from the given functions that supply a range to produce a - * result. - *

    - * Calling {@link #reseed(long)} on an instance returned from this method will - * result in an exception being thrown. - * - * @deprecated As of release 4.6, use {@link #rand(F, F, F)}. - * - * @param f The integer random generator. - * @param g The floating-point random generator. - * @return A random generator from the given functions that supply a range to produce a result. - */ - // TODO Change Option> optOnReseed to F onReseed when removing this method - @Deprecated - public static Rand rand( - F, F>> f, - F, F>> g) { - - return new Rand(f, g, none()); + return onReseed.f(seed); } /** @@ -131,7 +102,7 @@ public static Rand rand( F, F>> g, F onReseed) { - return new Rand(f, g, some(onReseed)); + return new Rand(f, g, onReseed); } /** diff --git a/quickcheck/src/test/java/fj/data/test/TestNull.java b/quickcheck/src/test/java/fj/data/test/TestNull.java index fe9dc843..0d547149 100644 --- a/quickcheck/src/test/java/fj/data/test/TestNull.java +++ b/quickcheck/src/test/java/fj/data/test/TestNull.java @@ -8,9 +8,6 @@ import static fj.test.Property.prop; import static fj.test.Property.property; -/** - * Created by MarkPerry on 3/07/2014. - */ public class TestNull { @Test diff --git a/quickcheck/src/test/java/fj/test/GenTest.java b/quickcheck/src/test/java/fj/test/GenTest.java index 900369a7..c13480a1 100644 --- a/quickcheck/src/test/java/fj/test/GenTest.java +++ b/quickcheck/src/test/java/fj/test/GenTest.java @@ -1,16 +1,29 @@ package fj.test; +import fj.Equal; import fj.data.List; +import fj.data.NonEmptyList; +import fj.data.Option; +import fj.data.Stream; +import fj.data.Validation; import fj.function.Effect1; import org.junit.Test; import static fj.Ord.charOrd; import static fj.data.List.list; import static fj.data.List.range; -import static fj.test.Gen.selectionOf; +import static fj.data.NonEmptyList.nel; +import static fj.data.Option.somes; +import static fj.data.Validation.fail; +import static fj.data.Validation.success; import static fj.test.Gen.combinationOf; -import static fj.test.Gen.wordOf; +import static fj.test.Gen.listOf; import static fj.test.Gen.permutationOf; +import static fj.test.Gen.pickOne; +import static fj.test.Gen.selectionOf; +import static fj.test.Gen.sequence; +import static fj.test.Gen.streamOf; +import static fj.test.Gen.wordOf; import static fj.test.Rand.standard; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -175,6 +188,24 @@ public void testWordOf_four() { }); } + @Test + public void testStreamOf() { + final Gen> instance = streamOf(pickOne(AS)); + testPick(100, instance.map(stream -> stream.take(4).toList()), actual -> { + assertEquals(4, actual.length()); + assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA)))); + }); + } + + @Test + public void testSequenceValidation() { + final Gen, Character>>> success = listOf(sequence(success(pickOne(AS))), 4); + testPick(100, success, list -> assertEquals(list.length(),somes(list.map(v -> Option.sequence(v.map(c -> AS.elementIndex(Equal.anyEqual(), c))))).length())); + + final Gen, Gen>>> failure = listOf(sequence(fail(nel(new Exception()))), 4); + testPick(100, failure, list -> assertTrue(list.forall(a -> a.isFail()))); + } + private static void testPick(int n, Gen> instance, Effect1> test) { range(0, n).map(ignore -> instance.gen(0, standard)).foreachDoEffect(test); } diff --git a/quickcheck/src/test/java/fj/test/TestRand.java b/quickcheck/src/test/java/fj/test/TestRand.java index 1ca7c749..ea2cbce8 100644 --- a/quickcheck/src/test/java/fj/test/TestRand.java +++ b/quickcheck/src/test/java/fj/test/TestRand.java @@ -9,9 +9,6 @@ import static org.junit.Assert.assertTrue; -/** - * Created by MarkPerry on 4/06/2015. - */ public class TestRand { @Test diff --git a/settings.gradle b/settings.gradle index d231d998..f1a6bd56 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,5 @@ rootProject.name = "functionaljava" -include "core", "demo", "consume", "java8", "quickcheck", "props-core", "props-core-scalacheck", "java-core", "performance" +include "core", "demo", "consume", "quickcheck", "props-core", "props-core-scalacheck", "java-core", "performance"