diff --git a/.changelogged.yaml b/.changelogged.yaml new file mode 100644 index 00000000..46180488 --- /dev/null +++ b/.changelogged.yaml @@ -0,0 +1,14 @@ +changelogs: + - changelog: ChangeLog.md + + ignore_files: + - "*ChangeLog*.md" + - .changelogged.yaml + + ignore_commits: [] + +branch: upstream/master + +entry_format: " - %message% (see [%link%](%id%));" + +editor_command: "nano -EiT 2" diff --git a/.travis.yml b/.travis.yml index 4a7c9290..2fb30bf7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,33 +1,31 @@ -language: java +# Use faster, Docker-based container (instead of OpenVZ) +sudo: false -sudo: required +language: java jdk: - - oraclejdk8 - -addons: - apt: - packages: - - oracle-java8-installer - -before_script: - - sudo service postgresql stop || true - - sudo service mysql stop || true - - sudo service memcached stop || true - - sudo service bootlogd stop || true - - sudo service elasticsearch stop || true - - sudo service mongodb stop || true - - sudo service neo4j stop || true - - sudo service cassandra stop || true - - sudo service riak stop || true - - sudo service rsync stop || true - - sudo service x11-common stop || true + - openjdk8 + +matrix: + fast_finish: true + + include: + + allow_failures: script: - - jdk_switcher use openjdk6 && export JAVA6_HOME=$JAVA_HOME - - jdk_switcher use oraclejdk7 && export JAVA7_HOME=$JAVA_HOME - - jdk_switcher use oraclejdk8 && export JAVA8_HOME=$JAVA_HOME - - ./gradlew build coverage -s -i + - ./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/ -after_success: - - bash <(curl -s https://codecov.io/bash) +cache: + directories: + - $HOME/.gradle/caches/ + - $HOME/.gradle/wrapper/ diff --git a/README.adoc b/README.adoc index 418605de..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 @@ -32,15 +33,14 @@ 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`) -* Java 8 specific support (`functionaljava-java8`) * property based testing (`functionaljava-quickcheck`) +* a small amount of Java 8 support (`functionaljava-java-core`) -The latest stable version is `4.6`. 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.6" -compile "org.functionaljava:functionaljava-java8:4.6" -compile "org.functionaljava:functionaljava-quickcheck:4.6" -compile "org.functionaljava:functionaljava-java-core:4.6" +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.6 - - - org.functionaljava - functionaljava-java8 - 4.6 + 5.0 org.functionaljava functionaljava-quickcheck - 4.6 + 5.0 org.functionaljava functionaljava-java-core - 4.6 + 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 1ccd524a..c89c9536 100644 --- a/build.gradle +++ b/build.gradle @@ -1,29 +1,26 @@ defaultTasks 'build' -ext { -} +apply plugin: "com.github.ben-manes.versions" buildscript { - ext { - uptodateVersion = "1.6.0" - retrolambdaPluginVersion = "3.2.5" - retrolambdaVersion = "2.3.0" - } - 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 = "7.4" + distributionType = Wrapper.DistributionType.ALL } } -apply plugin: "jacoco" if (JavaVersion.current().isJava8Compatible()) { allprojects { @@ -35,26 +32,24 @@ if (JavaVersion.current().isJava8Compatible()) { allprojects { - + apply plugin: "jacoco" jacoco { -// toolVersion = "0.7.1.201405082137" - toolVersion = "0.7.6.201602180812" - + toolVersion = "0.8.7" } defaultTasks "build" - ext { + ext { isSnapshot = true - fjBaseVersion = "4.7" + fjBaseVersion = "5.1" snapshotAppendix = "-SNAPSHOT" fjVersion = fjBaseVersion + (isSnapshot ? snapshotAppendix : "") - fjConsumeVersion = "4.6" + fjConsumeVersion = "5.0" signModule = false - useRetroLambda = false + uploadModule = false projectTitle = "Functional Java" projectName = "functionaljava" @@ -64,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 = "junit:junit:4.12" + junitCompile = "junit:junit:4.13.2" + junitRuntime = "org.junit.vintage:junit-vintage-engine:5.8.2" displayCompilerWarnings = true - - newJdkEnvVar = "JAVA8_HOME" - oldJdkEnvVar = "JAVA6_HOME" - retroLambdaTarget = JavaVersion.VERSION_1_6 + generateTestReports = false } repositories { - jcenter() - mavenCentral() mavenLocal() + mavenCentral() } version = fjVersion @@ -97,65 +97,64 @@ subprojects { buildscript { repositories { + mavenLocal() mavenCentral() } } - apply plugin: "jacoco" - 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" - } - } + } + } - jacocoTestReport { - reports { - html.enabled = true - xml.enabled = true + tasks.withType(Test).configureEach { + maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1 + if (!generateTestReports) { + reports.html.required = false + reports.junitXml.required = false } } - task coverage(dependsOn: ["test", "jacocoTestReport"]) << {} } task coverage(type: org.gradle.testing.jacoco.tasks.JacocoReport) { - dependsOn = subprojects.coverage + 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" @@ -173,16 +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+'"' - } - } + 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 { @@ -194,7 +191,7 @@ configure(subprojects.findAll { it.name != "props-core" }) { } // Output MANIFEST.MF statically so eclipse can see it for plugin development - task eclipsePluginManifest(dependsOn: jar) << { + task eclipsePluginManifest(dependsOn: jar) doLast { file("META-INF").mkdirs() jar.manifest.writeTo(file("META-INF/MANIFEST.MF")) } @@ -202,6 +199,6 @@ configure(subprojects.findAll { it.name != "props-core" }) { eclipseProject.dependsOn eclipsePluginManifest } -task env << { +task env doLast { println System.getenv() } diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..b7ddeec5 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,4 @@ +ignore: + - "demo/.*" + - "consume/.*" + - "performance/.*" 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 b3e1756e..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.191' - testCompile 'commons-dbutils:commons-dbutils:1.6' + 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 b64c758d..cba4a589 100644 --- a/core/src/main/java/fj/Equal.java +++ b/core/src/main/java/fj/Equal.java @@ -1,21 +1,9 @@ package fj; -import fj.data.Array; -import fj.data.Either; -import fj.data.LazyString; -import fj.data.List; -import fj.data.Natural; -import fj.data.NonEmptyList; -import fj.data.Option; -import fj.data.Seq; -import fj.data.Set; -import fj.data.Stream; -import fj.data.Tree; -import fj.data.TreeMap; -import fj.data.Validation; -import fj.data.Writer; +import fj.data.*; import fj.data.hamt.BitSet; import fj.data.hlist.HList; +import fj.data.optic.Traversal; import fj.data.vector.V2; import fj.data.vector.V3; import fj.data.vector.V4; @@ -23,6 +11,7 @@ import fj.data.vector.V6; import fj.data.vector.V7; import fj.data.vector.V8; +import fj.parser.Result; import java.math.BigDecimal; import java.math.BigInteger; @@ -33,8 +22,6 @@ /** * Tests for equality between two objects. - * - * @version %build.number% */ public final class Equal { @@ -48,6 +35,39 @@ public interface Definition { default boolean equal(A a1, A a2) { return equal(a1).f(a2); } + + /** + * Refine this equal definition, to tests equality of self and the mapped object in "and" manner. + * @see #equal() + * + * @param f The function to map the original object + * @param eq Equality for the mapped object + * @return A new equal definition + */ + default Definition then(final F f, final Equal eq) { + Definition bEqDef = eq.def; + return new Definition() { + @Override + public F equal(A a1) { + F fa = Definition.this.equal(a1); + F fb = bEqDef.equal(f.f(a1)); + return a2 -> fa.f(a2) && fb.f(f.f(a2)); + } + + @Override + public boolean equal(A a1, A a2) { + return Definition.this.equal(a1, a2) && bEqDef.equal(f.f(a1), f.f(a2)); + } + }; + } + + /** + * Build an equal instance from this definition. + * to be called after some successive {@link #then(F, Equal)} calls. + */ + default Equal equal() { + return equalDef(this); + } } /** @@ -118,18 +138,46 @@ public F eq(final A a) { * @return A new equal. */ public Equal contramap(final F f) { - Definition eaDef = def; - return equalDef(new Definition(){ + return equalDef(contramapDef(f, def)); + } + + /** + * An equal instance, which reverts equality for self + * + * @return A new equal instance + */ + public final Equal not() { + return equalDef((a1, a2) -> !def.equal(a1, a2)); + } + + + private static Definition contramapDef(F f, Definition aEqDef) { + return new Definition(){ @Override public F equal(B b) { - return compose(eaDef.equal(f.f(b)), f); + return compose(aEqDef.equal(f.f(b)), f); } @Override public boolean equal(B b1, B b2) { - return eaDef.equal(f.f(b1), f.f(b2)); + return aEqDef.equal(f.f(b1), f.f(b2)); } - }); + }; + } + + /** + * Static version of {@link #contramap(F)} + */ + public static Equal contramap(final F f, final Equal eq) { + return eq.contramap(f); + } + + /** + * Begin definition of an equal instance. + * @see Definition#then(F, Equal) + */ + public static Definition on(final F f, final Equal eq) { + return contramapDef(f, eq.def); } /** @@ -307,6 +355,21 @@ 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; + return equalDef((r1, r2) -> eaDef.equal(r1.value(), r2.value()) && eiDef.equal(r1.rest(), r2.rest())); + } + + /** * An equal instance for the {@link Validation} type. * @@ -395,6 +458,40 @@ public static Equal> streamEqual(final Equal ea) { } /** + * An equal instance for the {@link Zipper} type. + * + * @param ea Equality across the elements of the zipper. + * @return An equal instance for the {@link Zipper} type. + */ + public static Equal> zipperEqual(final Equal ea) { + Equal> se = Equal.streamEqual(ea); + return equalDef((a1, a2) -> + se.eq(a1.lefts(), a2.lefts()) && + ea.eq(a1.focus(), a2.focus()) && + se.eq(a1.rights(), a2.rights()) + ); + } + + /** + * An equal instance for the {@link TreeZipper} type. + * + * @param ea Equality across the elements of the tree zipper. + * @return An equal instance for the {@link TreeZipper} type. + */ + public static Equal> treeZipperEqual(final Equal ea) { + final Equal> te = Equal.treeEqual(ea); + final Equal>> st = streamEqual(Equal.treeEqual(ea)); + final Equal>, A, Stream>>>> sp = + streamEqual(p3Equal(streamEqual(treeEqual(ea)), ea, streamEqual(treeEqual(ea)))); + return equalDef((a1, a2) -> + te.eq(a1.focus(), a2.focus()) && + st.eq(a1.lefts(), a2.lefts()) && + st.eq(a1.rights(), a2.rights()) && + sp.eq(a1.parents(), a2.parents()) + ); + } + + /** * An equal instance for the {@link Array} type. * * @param ea Equality across the elements of the array. @@ -674,7 +771,7 @@ public static Equal> v8Equal(final Equal ea) { /** * An equal instance for lazy strings. */ - public static final Equal eq = streamEqual(charEqual).contramap(LazyString.toStream); + public static final Equal eq = streamEqual(charEqual).contramap(LazyString::toStream); /** * An equal instance for the empty heterogeneous list. 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 071cb6bc..00000000 --- a/core/src/main/java/fj/F8Functions.java +++ /dev/null @@ -1,21 +0,0 @@ -package fj; - -/** - * Created by MarkPerry on 6/04/2014. - */ -public final class F8Functions { - - private F8Functions() { - } - - /** - * Partial application. - * - * @param a The A to which to apply this function. - * @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 f95b1c87..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() { @@ -778,11 +776,11 @@ public static F join(final F> f) { /** * Partial application of the second argument to the supplied function to get a function of type - * A -> C. Same as flip(f).f(b). + * {@code A -> C}. Same as {@code flip(f).f(b)}. * * @param f The function to partially apply. * @param b The value to apply to the function. - * @return A new function based on f with its second argument applied. + * @return A new function based on {@code f} with its second argument applied. */ public static F partialApply2(final F> f, final B b) { return a -> uncurryF2(f).f(a, b); @@ -790,11 +788,11 @@ public static F partialApply2(final F> f, final B b) /** * Partial application of the third argument to the supplied function to get a function of type - * A -> B -> D. + * {@code A -> B -> D}. * * @param f The function to partially apply. * @param c The value to apply to the function. - * @return A new function based on f with its third argument applied. + * @return A new function based on {@code f} with its third argument applied. */ public static F> partialApply3(final F>> f, final C c) { return a -> b -> uncurryF3(f).f(a, b, c); @@ -802,11 +800,11 @@ public static F> partialApply3(final F>> /** * Partial application of the fourth argument to the supplied function to get a function of type - * A -> B -> C -> E. + * {@code A -> B -> C -> E}. * * @param f The function to partially apply. * @param d The value to apply to the function. - * @return A new function based on f with its fourth argument applied. + * @return A new function based on {@code f} with its fourth argument applied. */ public static F>> partialApply4(final F>>> f, final D d) { return a -> b -> c -> uncurryF4(f).f(a, b, c, d); @@ -814,11 +812,11 @@ public static F>> partialApply4(final FA -> B -> C -> D -> F$. + * {@code A -> B -> C -> D -> F$}. * * @param f The function to partially apply. * @param e The value to apply to the function. - * @return A new function based on f with its fifth argument applied. + * @return A new function based on {@code f} with its fifth argument applied. */ public static F>>> partialApply5(final F>>>> f, final E e) { @@ -827,11 +825,11 @@ public static F>> partialApply4(final FA -> B -> C -> D -> E -> G. + * {@code A -> B -> C -> D -> E -> G}. * * @param f The function to partially apply. * @param f$ The value to apply to the function. - * @return A new function based on f with its sixth argument applied. + * @return A new function based on {@code f} with its sixth argument applied. */ public static F>>>> partialApply6( final F>>>>> f, final F$ f$) { @@ -840,11 +838,11 @@ public static F>> partialApply4(final FA -> B -> C -> D -> E -> F$ -> H. + * {@code A -> B -> C -> D -> E -> F$ -> H}. * * @param f The function to partially apply. * @param g The value to apply to the function. - * @return A new function based on f with its seventh argument applied. + * @return A new function based on {@code f} with its seventh argument applied. */ public static F>>>>> partialApply7( final F>>>>>> f, final G g) { @@ -853,11 +851,11 @@ public static F>> partialApply4(final FA -> B -> C -> D -> E -> F$ -> G -> I. + * {@code A -> B -> C -> D -> E -> F$ -> G -> I}. * * @param f The function to partially apply. * @param h The value to apply to the function. - * @return A new function based on f with its eigth argument applied. + * @return A new function based on {@code f} with its eigth argument applied. */ public static F>>>>>> partialApply8( final F>>>>>>> f, final H h) { diff --git a/core/src/main/java/fj/Hash.java b/core/src/main/java/fj/Hash.java index 14f8a9d7..241bcefb 100644 --- a/core/src/main/java/fj/Hash.java +++ b/core/src/main/java/fj/Hash.java @@ -10,14 +10,13 @@ import fj.data.vector.V6; import fj.data.vector.V7; import fj.data.vector.V8; +import fj.parser.Result; import java.math.BigDecimal; import java.math.BigInteger; /** * Produces a hash code for an object which should attempt uniqueness. - * - * @version %build.number% */ public final class Hash { private final F f; @@ -162,6 +161,30 @@ 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. + * @param hi Hash the Result remainder. + * @return A hash instance for the {@link Result} type. + */ + public static Hash> resultHash(Hash ha, Hash hi) { + return hash(res -> { + final int p = 419; + int r = 239; + + r = p * r + ha.hash(res.value()); + r = p * r + hi.hash(res.rest()); + + return r; + }); + } + /** * A hash instance for the {@link Validation} type. * @@ -263,6 +286,50 @@ public static Hash> arrayHash(final Hash ha) { } /** + * A hash instance for the {@link Zipper} type. + * + * @param ha A hash for the elements of the zipper. + * @return A hash instance for the {@link Zipper} type. + */ + public static Hash> zipperHash(final Hash ha) { + Hash> sh = streamHash(ha); + return hash(as -> { + final int p = 419; + int r = 239; + + r = p * r + sh.hash(as.lefts()); + r = p * r + ha.hash(as.focus()); + r = p * r + sh.hash(as.rights()); + + return r; + }); + } + + /** + * A hash instance for the {@link TreeZipper} type. + * + * @param ha A hash for the elements of the tree zipper. + * @return A hash instance for the {@link TreeZipper} type. + */ + public static Hash> treeZipperHash(final Hash ha) { + Hash> th = treeHash(ha); + Hash>> sth = streamHash(treeHash(ha)); + Hash>, A, Stream>>>> tsp = + streamHash(p3Hash(streamHash(treeHash(ha)), ha, streamHash(treeHash(ha)))); + return hash(as -> { + final int p = 419; + int r = 239; + + r = p * r + th.hash(as.focus()); + r = p * r + sth.hash(as.lefts()); + r = p * r + sth.hash(as.rights()); + r = p * r + tsp.hash(as.parents()); + + return r; + }); + } + + /** * A hash instance for the {@link Tree} type. * * @param ha A hash for the elements of the tree. 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 3fcf1775..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; @@ -17,6 +8,7 @@ import static fj.data.List.nil; import static fj.data.Natural.natural; import static fj.data.Option.none; +import static fj.data.Option.some; import static fj.data.Stream.iterableStream; import java.math.BigInteger; @@ -30,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
    { @@ -152,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 @@ -245,7 +235,7 @@ public A zero() { /** * Returns a value summed n times (a + a + ... + a). * The default definition uses peasant multiplication, exploiting - * associativity to only require `O(log n)` uses of + * associativity to only require {@code O(log n)} uses of * {@link #sum(Object, Object)}. * * @param n multiplier @@ -474,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. */ @@ -537,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. */ @@ -868,14 +832,33 @@ public List sum(F0>> as) { } /** - * A monoid for options. - * @deprecated since 4.7. Use {@link #firstOptionMonoid()}. + * Lift a {@code Semigroup} for A to a {@code Monoid>}, using Option.none() as zero. * - * @return A monoid for options. + * @return A monoid for option. */ - @Deprecated - public static Monoid> optionMonoid() { - return firstOptionMonoid(); + public static Monoid> optionMonoid(Semigroup aSemigroup) { + return monoidDef(new Monoid.Definition>() { + @Override + public Option empty() { + return none(); + } + + @Override + public Option append(Option a1, Option a2) { + return a1.liftM2(a2, aSemigroup::sum).orElse(a1).orElse(a2); + } + + @Override + public Option multiply(int n, Option oa) { + return n > 0 ? oa.map(a -> aSemigroup.multiply1p(n - 1, a)) : none(); + } + + @Override + public Option sum(F0>> oas) { + Stream as = oas.f().bind(Option::toStream); + return as.uncons(none(), h -> tail -> some(aSemigroup.sumStream(h, tail::_1))); + } + }); } /** @@ -1059,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. @@ -1079,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 a3e55edc..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 { @@ -84,6 +70,42 @@ public Definition dual() { } }; } + + /** + * Refine this ord definition: compares using self and if objects are equal compares using given Ord. + * @see #ord() + * + * @param bOrd Ord for subsequent comparison + * @return A new ord definition. + */ + default Definition then(final F f, final Ord bOrd) { + Definition bOrdDef = bOrd.def; + return new Definition() { + @Override + public F compare(A a1) { + F fa = Definition.this.compare(a1); + F fb = bOrdDef.compare(f.f(a1)); + return a2 -> { + Ordering aOrdering = fa.f(a2); + return aOrdering != Ordering.EQ ? aOrdering : fb.f(f.f(a2)); + }; + } + + @Override + public Ordering compare(A a1, A a2) { + Ordering aOrdering = Definition.this.compare(a1, a2); + return aOrdering != Ordering.EQ ? aOrdering : bOrdDef.compare(f.f(a1), f.f(a2)); + } + }; + } + + /** + * Build an ord instance from this definition. + * to be called after some successive {@link #then(F, Ord)} calls. + */ + default Ord ord() { + return ordDef(this); + } } /** @@ -160,18 +182,7 @@ public Equal equal() { * @return A new ord. */ public Ord contramap(final F f) { - Definition selfDef = def; - return ordDef(new Definition() { - @Override - public F compare(B b) { - return compose(selfDef.compare(f.f(b)), f); - } - - @Override - public Ordering compare(B b1, B b2) { - return selfDef.compare(f.f(b1), f.f(b2)); - } - }); + return ordDef(contramapDef(f, def)); } /** @@ -287,6 +298,35 @@ public final Ord reverse() { return ordDef(def.dual()); } + /** + * Begin definition of an ord instance. + * @see Definition#then(F, Equal) + */ + public static Definition on(final F f, final Ord ord) { + return contramapDef(f, ord.def); + } + + /** + * Static version of {@link #contramap(F)} + */ + public static Ord contramap(final F f, final Ord ord) { + return ordDef(contramapDef(f, ord.def)); + } + + private static Definition contramapDef(F f, Definition def) { + return new Definition() { + @Override + public F compare(B b) { + return compose(def.compare(f.f(b)), f); + } + + @Override + public Ordering compare(B b1, B b2) { + return def.compare(f.f(b1), f.f(b2)); + } + }; + } + /** * Returns an order instance that uses the given equality test and ordering function. * @@ -487,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. * @@ -576,15 +647,15 @@ public static Ord> p1Ord(final Ord oa) { * @return An order instance for a product-2, with the first factor considered most significant. */ public static Ord> p2Ord(final Ord oa, final Ord ob) { - return ordDef((a, b) -> oa.eq(a._1(), b._1()) ? ob.compare(a._2(), b._2()) : oa.compare(a._1(), b._1())); + return on(P2.__1(), oa).then(P2.__2(), ob).ord(); } public static Ord> p2Ord1(Ord oa) { - return ordDef((p1, p2) -> oa.compare(p1._1(), p2._1())); + return on(P2.__1(), oa).ord(); } public static Ord> p2Ord2(Ord ob) { - return ordDef((p1, p2) -> ob.compare(p1._2(), p2._2())); + return on(P2.__2(), ob).ord(); } /** @@ -596,9 +667,7 @@ public static Ord> p2Ord2(Ord ob) { * @return An order instance for a product-3, with the first factor considered most significant. */ public static Ord> p3Ord(final Ord oa, final Ord ob, final Ord oc) { - return ordDef((a, b) -> oa.eq(a._1(), b._1()) ? - p2Ord(ob, oc).compare(P.p(a._2(), a._3()), P.p(b._2(), b._3())) - : oa.compare(a._1(), b._1())); + return on(P3.__1(), oa).then(P3.__2(), ob).then(P3.__3(), oc).ord(); } /** @@ -616,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 555a28ae..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,17 +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. - */ - public static F, P1> fmap(final F f) { - return map_(f); - } - /** * Promote any function to a transformation between P1s. * @@ -107,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()); } /** @@ -242,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(); } @@ -266,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 e503ff40..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,10 +173,71 @@ 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); } - /** + /** + * Creates a {@link P3} by adding the given element to the current {@link P2} + * + * @param el the element to append + * @return A {@link P3} containing the original {@link P2} with the extra element added at the end + */ + public final P3 append(C el) { + return P.p(_1(), _2(), el); + } + + /** + * Creates a {@link P4} by adding the given element to the current {@link P2} + * + * @param el the element to append + * @return A {@link P4} containing the original {@link P2} with the extra element added at the end + */ + public final P4 append(P2 el) { + return P.p(_1(), _2(), el._1(), el._2()); + } + + /** + * Creates a {@link P5} by adding the given element to the current {@link P2} + * + * @param el the element to append + * @return A {@link P5} containing the original {@link P2} with the extra element added at the end + */ + public final P5 append(P3 el) { + return P.p(_1(), _2(), el._1(), el._2(), el._3()); + } + + /** + * Creates a {@link P6} by adding the given element to the current {@link P2} + * + * @param el the element to append + * @return A {@link P6} containing the original {@link P2} with the extra element added at the end + */ + public final P6 append(P4 el) { + return P.p(_1(), _2(), el._1(), el._2(), el._3(), el._4()); + } + + /** + * Creates a {@link P7} by adding the given element to the current {@link P2} + * + * @param el the element to append + * @return A {@link P7} containing the original {@link P2} with the extra element added at the end + */ + public final P7 append(P5 el) { + return P.p(_1(), _2(), el._1(), el._2(), el._3(), el._4(), el._5()); + } + + /** + * Creates a {@link P8} by adding the given element to the current {@link P2} + * + * @param el the element to append + * @return A {@link P8} containing the original {@link P2} with the extra element added at the end + */ + public final P8 append(P6 el) { + return P.p(_1(), _2(), el._1(), el._2(), el._3(), el._4(), el._5(), el._6()); + } + + + /** * Provides a memoising P2 that remembers its values. * * @return A P2 that calls this P2 once for any given element and remembers the value for subsequent calls. diff --git a/core/src/main/java/fj/P3.java b/core/src/main/java/fj/P3.java index ec277346..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,9 +117,61 @@ 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); } + + /** + * Creates a {@link P4} by adding the given element to the current {@link P3} + * + * @param el the element to append + * @return A {@link P4} containing the original {@link P3} with the extra element added at the end + */ + public final P4 append(D el) { + return P.p(_1(), _2(), _3(), el); + } + + /** + * Creates a {@link P5} by adding the given element to the current {@link P3} + * + * @param el the element to append + * @return A {@link P5} containing the original {@link P3} with the extra element added at the end + */ + public final P5 append(P2 el) { + return P.p(_1(), _2(), _3(), el._1(), el._2()); + } + + /** + * Creates a {@link P6} by adding the given element to the current {@link P3} + * + * @param el the element to append + * @return A {@link P6} containing the original {@link P3} with the extra element added at the end + */ + public final P6 append(P3 el) { + return P.p(_1(), _2(), _3(), el._1(), el._2(), el._3()); + } + + /** + * Creates a {@link P7} by adding the given element to the current {@link P3} + * + * @param el the element to append + * @return A {@link P7} containing the original {@link P3} with the extra element added at the end + */ + public final P7 append(P4 el) { + return P.p(_1(), _2(), _3(), el._1(), el._2(), el._3(), el._4()); + } + + /** + * Creates a {@link P8} by adding the given element to the current {@link P3} + * + * @param el the element to append + * @return A {@link P8} containing the original {@link P3} with the extra element added at the end + */ + public final P8 append(P5 el) { + return P.p(_1(), _2(), _3(), el._1(), el._2(), el._3(), el._4(), el._5()); + } + + /** * Provides a memoising P3 that remembers its values. * diff --git a/core/src/main/java/fj/P4.java b/core/src/main/java/fj/P4.java index 3f7236fe..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,9 +171,53 @@ 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); } + + /** + * Creates a {@link P5} by adding the given element to the current {@link P4} + * + * @param el the element to append + * @return A {@link P5} containing the original {@link P4} with the extra element added at the end + */ + public final P5 append(E el) { + return P.p(_1(), _2(), _3(), _4(), el); + } + + /** + * Creates a {@link P6} by adding the given element to the current {@link P4} + * + * @param el the element to append + * @return A {@link P6} containing the original {@link P4} with the extra element added at the end + */ + public final P6 append(P2 el) { + return P.p(_1(), _2(), _3(), _4(), el._1(), el._2()); + } + + /** + * Creates a {@link P7} by adding the given element to the current {@link P4} + * + * @param el the element to append + * @return A {@link P7} containing the original {@link P4} with the extra element added at the end + */ + public final P7 append(P3 el) { + return P.p(_1(), _2(), _3(), _4(), el._1(), el._2(), el._3()); + } + + /** + * Creates a {@link P8} by adding the given element to the current {@link P4} + * + * @param el the element to append + * @return A {@link P8} containing the original {@link P4} with the extra element added at the end + */ + public final P8 append(P4 el) { + return P.p(_1(), _2(), _3(), _4(), el._1(), el._2(), el._3(), el._4()); + } + + + + /** * Provides a memoising P4 that remembers its values. * diff --git a/core/src/main/java/fj/P5.java b/core/src/main/java/fj/P5.java index 393b0632..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,37 @@ 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); + } + + /** + * Creates a {@link P6} by adding the given element to the current {@link P5} + * + * @param el the element to append + * @return A {@link P6} containing the original {@link P5} with the extra element added at the end + */ + public final P6 append(F el) { + return P.p(_1(), _2(), _3(), _4(), _5(), el); + } + + /** + * Creates a {@link P7} by adding the given element to the current {@link P5} + * + * @param el the element to append + * @return A {@link P7} containing the original {@link P5} with the extra element added at the end + */ + public final P7 append(P2 el) { + return P.p(_1(), _2(), _3(), _4(), _5(), el._1(), el._2()); + } + + /** + * Creates a {@link P8} by adding the given element to the current {@link P5} + * + * @param el the element to append + * @return A {@link P8} containing the original {@link P5} with the extra element added at the end + */ + public final P8 append(P3 el) { + return P.p(_1(), _2(), _3(), _4(), _5(), el._1(), el._2(), el._3()); } /** diff --git a/core/src/main/java/fj/P6.java b/core/src/main/java/fj/P6.java index 1e8c3055..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,27 @@ 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); + } + + /** + * Creates a {@link P7} by adding the given element to the current {@link P6} + * + * @param el the element to append + * @return A {@link P7} containing the original {@link P6} with the extra element added at the end + */ + public final P7 append(G el) { + return P.p(_1(), _2(), _3(), _4(), _5(), _6(), el); + } + + /** + * Creates a {@link P8} by adding the given element to the current {@link P6} + * + * @param el the element to append + * @return A {@link P8} containing the original {@link P6} with the extra element added at the end + */ + public final P8 append(P2 el) { + return P.p(_1(), _2(), _3(), _4(), _5(), _6(), el._1(), el._2()); } /** diff --git a/core/src/main/java/fj/P7.java b/core/src/main/java/fj/P7.java index ca24b40e..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,17 @@ 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); + } + + /** + * Creates a {@link P8} by adding the given element to the current {@link P7} + * + * @param el the element to append + * @return A {@link P8} containing the original {@link P7} with the extra element added at the end + */ + public final P8 append(H el) { + return P.p(_1(), _2(), _3(), _4(), _5(), _6(), _7(), el); } /** 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 74d7de9b..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
    { @@ -134,7 +129,7 @@ public F> sum() { /** * Returns a value summed n + 1 times ( * a + a + ... + a) The default definition uses peasant - * multiplication, exploiting associativity to only require `O(log n)` uses of + * multiplication, exploiting associativity to only require {@code O(log n)} uses of * {@link #sum(Object, Object)}. * * @param n multiplier @@ -172,29 +167,7 @@ public Semigroup dual() { * Lifts the semigroup to obtain a trivial monoid. */ public Monoid> lift() { - Definition def = this.def; - return monoidDef(new Monoid.Definition>() { - @Override - public Option empty() { - return none(); - } - - @Override - public Option append(Option a1, Option a2) { - return a1.liftM2(a1, def::append).orElse(a1).orElse(a2); - } - - @Override - public Option multiply(int n, Option oa) { - return n > 0 ? oa.map(a -> def.multiply1p(n - 1, a)) : none(); - } - - @Override - public Option sum(F0>> oas) { - Stream as = oas.f().bind(Option::toStream); - return as.uncons(none(), h -> tail -> some(def.sum(h, tail::_1))); - } - }); + return Monoid.optionMonoid(this); } /** @@ -215,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 @@ -317,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. */ @@ -534,7 +495,7 @@ public static Semigroup> nonEmptyListSemigroup() { return semigroupDef(new Definition>() { @Override public NonEmptyList append(NonEmptyList a1, NonEmptyList a2) { - return a1.append(a1); + return a1.append(a2); } @Override @@ -545,16 +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. - */ - public static Semigroup> optionSemigroup() { - return firstOptionSemigroup(); - } - /** * A semigroup for optional values that take the first available value. * @@ -642,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. */ @@ -650,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 2d2c4707..8a21a281 100644 --- a/core/src/main/java/fj/Show.java +++ b/core/src/main/java/fj/Show.java @@ -12,6 +12,7 @@ import fj.data.vector.V6; import fj.data.vector.V7; import fj.data.vector.V8; +import fj.parser.Result; import java.math.BigDecimal; import java.math.BigInteger; @@ -26,8 +27,6 @@ /** * Renders an object for display. - * - * @version %build.number% */ public final class Show { private final F> f; @@ -255,6 +254,29 @@ 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. + * + * @param sa Show for the {@link Result} value. + * @param si Show for the {@link Result} remainder. + * @return A show instance for the {@link Result} type. + */ + public static Show> resultShow(Show sa, Show si) { + return show(res -> + fromString("Result(").append(sa.f.f(res.value())) + .append(single(',')).append(si.f.f(res.rest())).append(single(')'))); + } + /** * A show instance for the {@link Validation} 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 37491c3b..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 { @@ -43,10 +43,10 @@ public R fold(final F, R> n, return gs.f(this); } - // The monadic bind constructs a new Codense whose subcomputation is still `sub`, and Kleisli-composes the + // The monadic bind constructs a new Codense whose subcomputation is still {@code sub}, and Kleisli-composes the // continuations. public Trampoline bind(final F> f) { - return codense(sub, o -> suspend(P.lazy(() -> cont.f(o).bind(f)))); + return codense(sub, o -> suspend(() -> cont.f(o).bind(f))); } // The resumption of a Codense is the resumption of its subcomputation. If that computation is done, its result @@ -110,7 +110,7 @@ private static Codense codense(final Normal a, final F F> pure() { return Trampoline::pure; @@ -126,6 +126,16 @@ public static Trampoline pure(final A a) { return new Pure<>(a); } + /** + * Suspends the given computation in a thunk. + * + * @param a A trampoline suspended in a thunk. + * @return A trampoline whose next step runs the given thunk. + */ + public static Trampoline suspend(final F0> a) { + return new Suspend<>(P.lazy(a)); + } + /** * Suspends the given computation in a thunk. * @@ -136,8 +146,9 @@ public static Trampoline suspend(final P1> a) { return new Suspend<>(a); } + /** - * @return The first-class version of `suspend`. + * @return The first-class version of {@code suspend}. */ public static F>, Trampoline> suspend_() { return Trampoline::suspend; @@ -160,25 +171,25 @@ 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)); } /** - * @return The first-class version of `bind`. + * @return The first-class version of {@code bind}. */ public static F>, F, Trampoline>> bind_() { return f -> a -> a.bind(f); } /** - * @return The first-class version of `map`. + * @return The first-class version of {@code map}. */ public static F, F, Trampoline>> map_() { return f -> a -> a.map(f); } /** - * @return The first-class version of `resume`. + * @return The first-class version of {@code resume}. */ public static F, Either>, A>> resume_() { return Trampoline::resume; @@ -255,18 +266,19 @@ 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(P.lazy(() -> 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()) { for (final B y : eb.right()) { - return suspend(P.lazy(() -> pure(f.f(x, y)))); + return suspend(() -> 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/$.java b/core/src/main/java/fj/data/$.java index 17b2ac9e..a1aa77bf 100644 --- a/core/src/main/java/fj/data/$.java +++ b/core/src/main/java/fj/data/$.java @@ -17,14 +17,8 @@ public final class $ extends P1 { /** * Returns a function that given an argument, returns a function that ignores its argument. - * @deprecated JDK 8 warns '_' may not be supported after SE 8. As of release 4.4, use {@link #constant} (note the synonym {@link #__}). * @return A function that given an argument, returns a function that ignores its argument. */ - @Deprecated - public static $ _(final B b) { - return constant(b); - } - public static $ __(final B b) { return constant(b); } 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/DList.java b/core/src/main/java/fj/data/DList.java index af9d68b0..597b6dda 100644 --- a/core/src/main/java/fj/data/DList.java +++ b/core/src/main/java/fj/data/DList.java @@ -126,6 +126,6 @@ public DList append(DList other) { } private static F> kleisliTrampCompose(F> bc, F> ab) { - return (A a) -> ab.f(a).bind((B b) -> Trampoline.suspend(P.lazy(() -> bc.f(b)))); + return (A a) -> ab.f(a).bind((B b) -> Trampoline.suspend(() -> bc.f(b))); } } diff --git a/core/src/main/java/fj/data/Either.java b/core/src/main/java/fj/data/Either.java index 7d8e0bb7..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; /** @@ -316,11 +305,24 @@ public List> traverseList(final F> f) { * @return An either after traversing through this projection. */ public IO> traverseIO(final F> f) { - return e.isRight() ? + return e.isLeft() ? IOFunctions.map(f.f(value()), Either::left) : 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 1666d7b2..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; @@ -18,18 +14,18 @@ /** * Abstracts over a type that may have a successor and/or predecessor value. This implies ordering for that type. A user - * may construct an enumerator with an optimised version for plus, otherwise a default is implemented using + * may construct an enumerator with an optimised version for {@code plus}, otherwise a default is implemented using * the given successor/predecessor implementations. *

    * For any enumerator e, the following laws must satisfy: *

      - *
    • forall a. e.successor(a).forall(\t -> e.predecessor(t).forall(\z -> z == a))
    • - *
    • forall a. e.predecessor(a).forall(\t -> e.successor(t).forall(\z -> z == a))
    • - *
    • e.max().forall(\t -> e.successor(t).isNone)
    • - *
    • e.min().forall(\t -> e.predecessor(t).isNone)
    • - *
    • forall a n. e.plus(a, 0) == Some(a)
    • - *
    • forall a n | n > 0. e.plus(a, n) == e.plus(a, n - 1)
    • - *
    • forall a n | n < 0. e.plus(a, n) == e.plus(a, n + 1)
    • + *
    • {@code forall a. e.successor(a).forall(\t -> e.predecessor(t).forall(\z -> z == a))}
    • + *
    • {@code forall a. e.predecessor(a).forall(\t -> e.successor(t).forall(\z -> z == a))}
    • + *
    • {@code e.max().forall(\t -> e.successor(t).isNone)}
    • + *
    • {@code e.min().forall(\t -> e.predecessor(t).isNone)}
    • + *
    • {@code forall a n. e.plus(a, 0) == Some(a)}
    • + *
    • {@code forall a n | n > 0. e.plus(a, n) == e.plus(a, n - 1)}
    • + *
    • {@code forall a n | n < 0. e.plus(a, n) == e.plus(a, n + 1)}
    • *
    * * @version %build.number% @@ -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/Eval.java b/core/src/main/java/fj/data/Eval.java new file mode 100644 index 00000000..5d09f913 --- /dev/null +++ b/core/src/main/java/fj/data/Eval.java @@ -0,0 +1,238 @@ +package fj.data; + +import fj.F; +import fj.F0; +import fj.P; +import fj.P1; +import fj.control.Trampoline; + +/** + * Eval is an abstraction over different models of evaluation. + * The data constructors: + *
      + *
    • Now - the value is evaluated immediately.
    • + *
    • Later - the value is evaluated only once when it's requested (lazy evaluation).
    • + *
    • Always - the value is evaluated every time when it's requested.
    • + *
    + * + * Both Later and Always are lazy computations, while Now is eager. + * + * + * @version %build.number% + */ +public abstract class Eval
    { + + /** + * Constructs an eager evaluation by wrapping the given value. + * + * @param a the evaluated value. + * @return an eval with computed value. + */ + public static Eval now(A a) { + return new Now<>(a); + } + + /** + * Constructs a lazy evaluation with caching. + * + * @param a the supplier that evaluates a value. + * @return a lazy evaluation. + */ + public static Eval later(F0 a) { + return new Later<>(a); + } + + /** + * Constructs a lazy evaluation without caching. + * + * @param a the supplier that evaluates a value. + * @return a lazy evaluation. + */ + public static Eval always(F0 a) { + return new Always<>(a); + } + + /** + * Constructs a lazy evaluation of an expression that produces Eval. + * This operation is stack-safe and can be used for recursive computations. + * + * @param a the supplier that produces an Eval. + * @return a lazily evaluated nested evaluation. + */ + public static Eval defer(F0> a) { + return new DeferEval<>(a); + } + + /** + * Evaluates the computation and return its result. + * Depending on whether the current instance is lazy or eager the + * computation may or may not happen at this point. + * + * @return a result of this computation. + */ + public abstract A value(); + + /** + * Transforms Eval into a Eval using + * the given function. + * + * Note: the computation of the given transformation is always lazy, + * even if it invoked for an eager Now instance. This computation + * is performed in O(1) stack space. + * + * @param f the transformation function. + * @return a transformed evaluation. + */ + public final Eval map(final F f) { + return bind(a -> now(f.f(a))); + } + + /** + * Alias for {@link #bind(F)}. + */ + public final Eval flatMap(final F> f) { + return bind(f); + } + + /** + * Transforms Eval into a Eval using + * the given function that directly produces Eval. + * + * Note: the computation of the given transformation is always lazy, + * even if it invoked for an eager Now instance. This computation + * is performed in O(1) stack space. + * + * @param f the transformation function. + * @return a transformed evaluation. + */ + public final Eval bind(final F> f) { + return new BindTrampolineEval<>(f, asTrampoline()); + } + + /** + * Transforms the current instance into a trampoline instance. + */ + abstract TrampolineEval asTrampoline(); + + /** + * Represents an eager computation. + */ + private static final class Now extends Eval { + private final A a; + + Now(A a) { + this.a = a; + } + + @Override + public final A value() { + return a; + } + + @Override + final TrampolineEval asTrampoline() { + return new PureTrampolineEval<>(this); + } + } + + /** + * Represents a lazy computation that is evaluated only once. + */ + private static final class Later extends Eval { + private final P1 memo; + + Later(F0 producer) { + this.memo = P.hardMemo(producer); + } + + @Override + public final A value() { + return memo._1(); + } + + @Override + final TrampolineEval asTrampoline() { + return new PureTrampolineEval<>(this); + } + } + + /** + * Represents a lazy computation that is evaluated every time when it's requested. + */ + private static final class Always extends Eval { + private final F0 supplier; + + Always(F0 supplier) { + this.supplier = supplier; + } + + @Override + public final A value() { + return supplier.f(); + } + + @Override + final TrampolineEval asTrampoline() { + return new PureTrampolineEval<>(this); + } + } + + /** + * A helper abstraction that allows to perform recursive lazy transformations in O(1) stack space. + */ + private static abstract class TrampolineEval extends Eval { + + protected abstract Trampoline trampoline(); + + @Override + public final A value() { + return trampoline().run(); + } + + @Override + final TrampolineEval asTrampoline() { + return this; + } + } + + private static final class PureTrampolineEval extends TrampolineEval { + private final Eval start; + + PureTrampolineEval(Eval start) { + this.start = start; + } + + @Override + protected final Trampoline trampoline() { + return Trampoline.suspend(() -> Trampoline.pure(start.value())); + } + } + + private static final class BindTrampolineEval extends TrampolineEval { + private final TrampolineEval next; + private final F> f; + + BindTrampolineEval(F> f, TrampolineEval next) { + this.next = next; + this.f = f; + } + + @Override + protected final Trampoline trampoline() { + return Trampoline.suspend(() -> next.trampoline().bind(v -> f.f(v).asTrampoline().trampoline())); + } + } + + private static final class DeferEval extends TrampolineEval { + private final P1> memo; + + DeferEval(F0> producer) { + this.memo = P.hardMemo(producer); + } + + @Override + protected final Trampoline trampoline() { + return Trampoline.suspend(() -> memo._1().asTrampoline().trampoline()); + } + } +} 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 92263d09..9573deea 100644 --- a/core/src/main/java/fj/data/IOFunctions.java +++ b/core/src/main/java/fj/data/IOFunctions.java @@ -1,8 +1,18 @@ package fj.data; -import static fj.Bottom.errorF; -import static fj.Function.constant; -import static fj.Function.partialApply2; +import fj.F; +import fj.F0; +import fj.F2; +import fj.Function; +import fj.P; +import fj.P1; +import fj.P2; +import fj.Try; +import fj.Unit; +import fj.data.Iteratee.Input; +import fj.data.Iteratee.IterV; +import fj.function.Try0; +import fj.function.Try1; import java.io.BufferedReader; import java.io.Closeable; @@ -14,11 +24,9 @@ import java.nio.charset.Charset; import java.util.Arrays; -import fj.*; -import fj.data.Iteratee.Input; -import fj.data.Iteratee.IterV; -import fj.function.Try0; -import fj.function.Try1; +import static fj.Bottom.errorF; +import static fj.Function.constant; +import static fj.Function.partialApply2; /** * IO monad for processing files, with main methods {@link #enumFileLines }, @@ -170,38 +178,37 @@ public static SafeIO lazySafe(final F0 f) { * A function that feeds an iteratee with lines read from a {@link BufferedReader}. */ public static F, IO>>> lineReader() { - final F, Boolean> isDone = - new F, Boolean>() { - final F>, P1> done = constant(P.p(true)); - final F, IterV>, P1> cont = constant(P.p(false)); - - @Override - public Boolean f(final IterV i) { - return i.fold(done, cont)._1(); - } - }; - - return r -> new F, IO>>() { - final F>, P1>> done = errorF("iteratee is done"); //$NON-NLS-1$ - - @Override - public IO> f(final IterV it) { - // use loop instead of recursion because of missing TCO - return () -> { - IterV i = it; - while (!isDone.f(i)) { - final String s = r.readLine(); - if (s == null) { - return i; - } - final Input input = Input.el(s); - final F, IterV>, P1>> cont = F1Functions.lazy(Function.apply(input)); - i = i.fold(done, cont)._1(); + return LineReader::new; + } + + private static class LineReader implements F, IO>> { + + private final BufferedReader r; + + private final F, Boolean> isDone = i -> i.fold(constant(P.p(true)), constant(P.p(false)))._1(); + private final F>, P1>> done = errorF("iteratee is done"); //$NON-NLS-1$ + + private LineReader(BufferedReader r) { + this.r = r; + } + + @Override + public IO> f(IterV it) { + // use loop instead of recursion because of missing TCO + return () -> { + IterV i = it; + while (!isDone.f(i)) { + final String s = r.readLine(); + if (s == null) { + return i; } - return i; - }; - } - }; + final Input input = Input.el(s); + final F, IterV>, P1>> cont = Function., IterV>apply(input).lazy(); + i = i.fold(done, cont)._1(); + } + return i; + }; + } } /** @@ -209,91 +216,91 @@ public IO> f(final IterV it) { * (char[] of size {@link #DEFAULT_BUFFER_SIZE}). */ public static F, IO>>> charChunkReader() { - final F, Boolean> isDone = - new F, Boolean>() { - final F>, P1> done = constant(P.p(true)); - final F, IterV>, P1> cont = constant(P.p(false)); - - @Override - public Boolean f(final IterV i) { - return i.fold(done, cont)._1(); + return CharChunkReader::new; + } + + private static class CharChunkReader implements F, IO>> { + + private final Reader r; + + private final F, Boolean> isDone = i -> i.fold(constant(P.p(true)), constant(P.p(false)))._1(); + private final F>, P1>> done = errorF("iteratee is done"); //$NON-NLS-1$ + + CharChunkReader(Reader r) { + this.r = r; + } + + @Override + public IO> f(IterV it) { + // use loop instead of recursion because of missing TCO + return () -> { + + IterV i = it; + while (!isDone.f(i)) { + char[] buffer = new char[DEFAULT_BUFFER_SIZE]; + final int numRead = r.read(buffer); + if (numRead == -1) { + return i; } - }; - - return r -> new F, IO>>() { - final F>, P1>> done = errorF("iteratee is done"); //$NON-NLS-1$ - - @Override - public IO> f(final IterV it) { - // use loop instead of recursion because of missing TCO - return () -> { - - IterV i = it; - while (!isDone.f(i)) { - char[] buffer = new char[DEFAULT_BUFFER_SIZE]; - final int numRead = r.read(buffer); - if (numRead == -1) { - return i; - } - if (numRead < buffer.length) { - buffer = Arrays.copyOfRange(buffer, 0, numRead); - } - final Input input = Input.el(buffer); - final F, IterV>, P1>> cont = - F1Functions.lazy(Function.apply(input)); - i = i.fold(done, cont)._1(); + if (numRead < buffer.length) { + buffer = Arrays.copyOfRange(buffer, 0, numRead); } - return i; - }; - } - }; + final Input input = Input.el(buffer); + final F, IterV>, P1>> cont = + Function., IterV>apply(input).lazy(); + i = i.fold(done, cont)._1(); + } + return i; + }; + } } + + /** * A function that feeds an iteratee with characters read from a {@link Reader} * (chars are read in chunks of size {@link #DEFAULT_BUFFER_SIZE}). */ public static F, IO>>> charChunkReader2() { - final F, Boolean> isDone = - new F, Boolean>() { - final F>, P1> done = constant(P.p(true)); - final F, IterV>, P1> cont = constant(P.p(false)); - - @Override - public Boolean f(final IterV i) { - return i.fold(done, cont)._1(); + return CharChunkReader2::new; + } + + private static class CharChunkReader2 implements F, IO>> { + + private final Reader r; + + private final F, Boolean> isDone = i -> i.fold(constant(P.p(true)), constant(P.p(false)))._1(); + private final F>, IterV> done = errorF("iteratee is done"); //$NON-NLS-1$ + + CharChunkReader2(Reader r) { + this.r = r; + } + + @Override + public IO> f(IterV it) { + // use loop instead of recursion because of missing TCO + return () -> { + + IterV i = it; + while (!isDone.f(i)) { + char[] buffer = new char[DEFAULT_BUFFER_SIZE]; + final int numRead = r.read(buffer); + if (numRead == -1) { + return i; } - }; - - return r -> new F, IO>>() { - final F>, IterV> done = errorF("iteratee is done"); //$NON-NLS-1$ - - @Override - public IO> f(final IterV it) { - // use loop instead of recursion because of missing TCO - return () -> { - - IterV i = it; - while (!isDone.f(i)) { - char[] buffer = new char[DEFAULT_BUFFER_SIZE]; - final int numRead = r.read(buffer); - if (numRead == -1) { - return i; - } - if (numRead < buffer.length) { - buffer = Arrays.copyOfRange(buffer, 0, numRead); - } - for (char c : buffer) { - final Input input = Input.el(c); - final F, IterV>, IterV> cont = - Function.apply(input); - i = i.fold(done, cont); - } + if (numRead < buffer.length) { + buffer = Arrays.copyOfRange(buffer, 0, numRead); } - return i; - }; - } - }; + for (char c : buffer) { + final Input input = Input.el(c); + final F, IterV>, IterV> cont = + Function.apply(input); + i = i.fold(done, cont); + } + } + return i; + }; + } } public static IO map(final IO io, final F f) { 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 487e9703..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() { @@ -404,27 +402,7 @@ public static F, SynchronousQueue> Array_SynchronousQueue(final * @return A function that converts streams to iterable. */ public static F, Iterable> Stream_Iterable() { - return as -> () -> new Iterator() { - private Stream x = as; - - public boolean hasNext() { - return x.isNotEmpty(); - } - - public A next() { - if (x.isEmpty()) - throw new NoSuchElementException("Empty iterator"); - else { - final A a = x.head(); - x = x.tail()._1(); - return a; - } - } - - public void remove() { - throw new UnsupportedOperationException(); - } - }; + return as -> as; } /** @@ -1434,16 +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. - */ - 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 f58fd73f..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(P.lazy(() -> 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 1b30a55e..c5b638a3 100644 --- a/core/src/main/java/fj/data/State.java +++ b/core/src/main/java/fj/data/State.java @@ -5,14 +5,10 @@ import fj.Unit; import fj.control.Trampoline; -import static fj.P.lazy; import static fj.P.p; 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) { @@ -75,7 +71,7 @@ public static State> traverse(List list, F State suspended(F>> runF) { - return new State<>(s -> suspend(lazy(() -> runF.f(s)))); + return new State<>(s -> suspend(() -> runF.f(s))); } private final F>> runF; @@ -112,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 4b0ce7eb..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,15 +125,17 @@ 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())); } /** + *
    {@code
        * flatten :: Tree a -> [a]
        * flatten t = squish t []
    +   * }
    * where squish (Node x ts) xs = x:Prelude.foldr squish xs ts * Puts the elements of the tree into a Stream, in pre-order. * @@ -303,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); } /** @@ -346,4 +346,4 @@ public int length() { return 1 + subForest._1().map(Tree::length).foldLeft((acc, i) -> acc + i, 0); } -} \ No newline at end of file +} 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 c94287cf..545238f3 100644 --- a/core/src/main/java/fj/data/TreeZipper.java +++ b/core/src/main/java/fj/data/TreeZipper.java @@ -1,672 +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; - } - - /** - * 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; - } - - /** - * 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() { - return tz -> P.p(tz, new P1>>() { - private F>, Option, Option>>>> fwd() { - return o -> { - Option, Option>>> r = none(); - for (final TreeZipper c : o) { - r = some(P.p(c, c.right())); - } - return r; - }; - } - - public Stream> _1() { - return 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 24414ed2..4b96377f 100644 --- a/core/src/main/java/fj/data/Zipper.java +++ b/core/src/main/java/fj/data/Zipper.java @@ -1,586 +1,585 @@ -package fj.data; - -import fj.Equal; -import fj.F; -import fj.F2; -import fj.F2Functions; -import fj.F3; -import fj.Function; -import fj.Ord; -import fj.P; -import fj.P1; -import fj.P2; -import fj.P3; -import fj.Show; -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_()); - } - - /** - * 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/hlist/HPre.java b/core/src/main/java/fj/data/hlist/HPre.java index 6a2556de..6c6174a4 100644 --- a/core/src/main/java/fj/data/hlist/HPre.java +++ b/core/src/main/java/fj/data/hlist/HPre.java @@ -57,7 +57,7 @@ public static HFalse hFalse() { } /** - * Type-level boolean conjunction. A value of this type represents evidence that AB -> C + * Type-level boolean conjunction. A value of this type represents evidence that {@code AB -> C} * * @param A boolean * @param A boolean @@ -92,7 +92,7 @@ public static HAnd hAnd(final HTrue a, final HTrue b) { } /** - * Type-level boolean disjunction. A value of this type represents evidence that A+B -> C + * Type-level boolean disjunction. A value of this type represents evidence that {@code A+B -> C} * * @param A boolean * @param A boolean diff --git a/core/src/main/java/fj/data/optic/Iso.java b/core/src/main/java/fj/data/optic/Iso.java index ea9babac..4b41b1ad 100644 --- a/core/src/main/java/fj/data/optic/Iso.java +++ b/core/src/main/java/fj/data/optic/Iso.java @@ -119,10 +119,10 @@ public static Iso iso(final F get, final F reverseGet) * create an {@link Iso} between any type and itself. id is the zero element of optics composition, for all optics o of type O * (e.g. Lens, Iso, Prism, ...): * - *

    +   * 
    {@code
        *  o composeIso Iso.id == o
        *  Iso.id composeO o == o
    -   * 
    + * }
    * * (replace composeO by composeLens, composeIso, composePrism, ...) */ diff --git a/core/src/main/java/fj/data/optic/PIso.java b/core/src/main/java/fj/data/optic/PIso.java index e10b47bb..b97b416f 100644 --- a/core/src/main/java/fj/data/optic/PIso.java +++ b/core/src/main/java/fj/data/optic/PIso.java @@ -21,18 +21,18 @@ /** * A {@link PIso} defines an isomorphism between types S, A and B, T: * - *
    + * 
    {@code
      *              get                           reverse.get
      *     -------------------->             -------------------->
      *   S                       A         T                       B
      *     <--------------------             <--------------------
      *       reverse.reverseGet                   reverseGet
    - * 
    + * }
    * - * In addition, if f and g forms an isomorphism between `A` and `B`, i.e. if `f . g = id` and `g . f = id`, then a {@link PIso} - * defines an isomorphism between `S` and `T`: + * In addition, if f and g forms an isomorphism between {@code A} and {@code B}, i.e. if {@code f . g = id} and {@code g . f = id}, then a {@link PIso} + * defines an isomorphism between {@code S} and {@code T}: * - *
    + * 
    {@code
      *     S           T                                   S           T
      *     |           |                                   |           |
      *     |           |                                   |           |
    @@ -40,7 +40,7 @@
      *     |           |                                   |           |
      *     |     f     |                                   |     g     |
      *     A --------> B                                   A <-------- B
    - * 
    + * }
    * * A {@link PIso} is also a valid {@link Getter}, {@link Fold}, {@link PLens}, {@link PPrism}, {@link POptional}, * {@link PTraversal} and {@link PSetter} @@ -552,10 +552,10 @@ public PIso reverse() { * create a {@link PIso} between any type and itself. id is the zero element of optics composition, for all optics o of type O * (e.g. Lens, Iso, Prism, ...): * - *
    +   * 
    {@code
        *  o composeIso Iso.id == o
        *  Iso.id composeO o == o
    -   * 
    + * }
    * * (replace composeO by composeLens, composeIso, composePrism, ...) */ diff --git a/core/src/main/java/fj/data/optic/PLens.java b/core/src/main/java/fj/data/optic/PLens.java index 971f76ff..4dcad20b 100644 --- a/core/src/main/java/fj/data/optic/PLens.java +++ b/core/src/main/java/fj/data/optic/PLens.java @@ -17,18 +17,20 @@ import fj.data.vector.V2; /** - * A {@link PLens} can be seen as a pair of functions: - `get: S => A` i.e. from an `S`, we can extract an `A` - `set: (B, S) => - * T` i.e. if we replace an `A` by a `B` in an `S`, we obtain a `T` - * - * A {@link PLens} could also be defined as a weaker {@link PIso} where set requires an additional parameter than reverseGet. - * - * {@link PLens} stands for Polymorphic Lens as it set and modify methods change a type `A` to `B` and `S` to `T`. {@link Lens} - * is a {@link PLens} restricted to monomoprhic updates. - * - * A {@link PLens} is also a valid {@link Getter}, {@link Fold}, {@link POptional}, {@link PTraversal} and {@link PSetter} - * + * A {@link PLens} can be seen as a pair of functions:
      + *
    • {@code get: S => A} i.e. from an {@code S}, we can extract an {@code A}
    • + *
    • {@code set: (B, S) => T} i.e. if we replace an {@code A} by a {@code B} in an {@code S}, we obtain a {@code T}
    • + *
    + *

    + * A {@link PLens} could also be defined as a weaker {@link PIso} where set requires an additional parameter than reverseGet.

    + *

    + * {@link PLens} stands for Polymorphic Lens as it set and modify methods change a type {@code A} to {@code B} and {@code S} to {@code T}. {@link Lens} + * is a {@link PLens} restricted to monomoprhic updates.

    + *

    + * A {@link PLens} is also a valid {@link Getter}, {@link Fold}, {@link POptional}, {@link PTraversal} and {@link PSetter}

    + *

    * Typically a {@link PLens} or {@link Lens} can be defined between a Product (e.g. case class, tuple, HList) and one of it is - * component. + * component.

    * * @param the source of a {@link PLens} * @param the modified source of a {@link PLens} @@ -500,4 +502,4 @@ public F modify(final F f) { } }; } -} \ No newline at end of file +} diff --git a/core/src/main/java/fj/data/optic/POptional.java b/core/src/main/java/fj/data/optic/POptional.java index d35979d8..c70aa4c1 100644 --- a/core/src/main/java/fj/data/optic/POptional.java +++ b/core/src/main/java/fj/data/optic/POptional.java @@ -20,13 +20,15 @@ import fj.data.vector.V2; /** - * A {@link POptional} can be seen as a pair of functions: - `getOrModify: S => T \/ A` - `set : (B, S) => T` - * - * A {@link POptional} could also be defined as a weaker {@link PLens} and weaker {@link PPrism} - * - * {@link POptional} stands for Polymorphic Optional as it set and modify methods change a type `A` to `B` and `S` to `T`. - * {@link Optional} is a {@link POptional} restricted to monomoprhic updates: {{{ type Optional[S, A] = POptional[S, S, A, A] - * }}} + * A {@link POptional} can be seen as a pair of functions:
      + *
    • {@code getOrModify: S => T \/ A}
    • + *
    • {@code set : (B, S) => T}
    • + *
    + *

    + * A {@link POptional} could also be defined as a weaker {@link PLens} and weaker {@link PPrism}

    + *

    + * {@link POptional} stands for Polymorphic Optional as it set and modify methods change a type {@code A} to {@code B} and {@code S} to {@code T}. + * {@link Optional} is a {@link POptional} restricted to monomoprhic updates: {@code type Optional[S, A] = POptional[S, S, A, A]}

    * * @param the source of a {@link POptional} * @param the modified source of a {@link POptional} @@ -479,4 +481,4 @@ public F modify(final F f) { }; } -} \ No newline at end of file +} diff --git a/core/src/main/java/fj/data/optic/PPrism.java b/core/src/main/java/fj/data/optic/PPrism.java index c4a65a68..ffa87ba6 100644 --- a/core/src/main/java/fj/data/optic/PPrism.java +++ b/core/src/main/java/fj/data/optic/PPrism.java @@ -19,14 +19,17 @@ import fj.data.vector.V2; /** - * A {@link PPrism} can be seen as a pair of functions: - `getOrModify: S => T \/ A` - `reverseGet : B => T` - * - * A {@link PPrism} could also be defined as a weaker {@link PIso} where get can fail. - * + * A {@link PPrism} can be seen as a pair of functions:
      + *
    • {@code getOrModify: S => T \/ A}
    • + *
    • {@code reverseGet : B => T}
    • + *
    + *

    + * A {@link PPrism} could also be defined as a weaker {@link PIso} where get can fail.

    + *

    * Typically a {@link PPrism} or {@link Prism} encodes the relation between a Sum or CoProduct type (e.g. sealed trait) and one - * of it is element. + * of it is element.

    * - * {@link PPrism} stands for Polymorphic Prism as it set and modify methods change a type `A` to `B` and `S` to `T`. + * {@link PPrism} stands for Polymorphic Prism as it set and modify methods change a type {@code A} to {@code B} and {@code S} to {@code T}. * {@link Prism} is a {@link PPrism} where the type of target cannot be modified. * * A {@link PPrism} is also a valid {@link Fold}, {@link POptional}, {@link PTraversal} and {@link PSetter} @@ -436,4 +439,4 @@ public Option
    getOption(final S s) { }; } -} \ No newline at end of file +} diff --git a/core/src/main/java/fj/data/optic/PSetter.java b/core/src/main/java/fj/data/optic/PSetter.java index 012920cb..13be7739 100644 --- a/core/src/main/java/fj/data/optic/PSetter.java +++ b/core/src/main/java/fj/data/optic/PSetter.java @@ -5,12 +5,14 @@ import fj.data.Either; /** - * A {@link PSetter} is a generalisation of Functor map: - `map: (A => B) => F[A] => F[B]` - `modify: (A => B) => S => - * T` - * - * {@link PSetter} stands for Polymorphic Setter as it set and modify methods change a type `A` to `B` and `S` to `T`. - * - * {@link PTraversal}, {@link POptional}, {@link PPrism}, {@link PLens} and {@link PIso} are valid {@link PSetter} + * A {@link PSetter} is a generalisation of Functor map:
      + *
    • {@code map: (A => B) => F[A] => F[B]}
    • + *
    • {@code modify: (A => B) => S => T}
    • + *
    + *

    + * {@link PSetter} stands for Polymorphic Setter as it set and modify methods change a type {@code A} to {@code B} and {@code S} to {@code T}.

    + *

    + * {@link PTraversal}, {@link POptional}, {@link PPrism}, {@link PLens} and {@link PIso} are valid {@link PSetter}

    * * @param the source of a {@link PSetter} * @param the modified source of a {@link PSetter} diff --git a/core/src/main/java/fj/data/optic/PTraversal.java b/core/src/main/java/fj/data/optic/PTraversal.java index ee16760f..6087f707 100644 --- a/core/src/main/java/fj/data/optic/PTraversal.java +++ b/core/src/main/java/fj/data/optic/PTraversal.java @@ -24,9 +24,9 @@ /** * A {@link PTraversal} can be seen as a {@link POptional} generalised to 0 to n targets where n can be infinite. - * - * {@link PTraversal} stands for Polymorphic Traversal as it set and modify methods change a type `A` to `B` and `S` to `T`. - * {@link Traversal} is a {@link PTraversal} restricted to monomoprhic updates. + *

    + * {@link PTraversal} stands for Polymorphic Traversal as it set and modify methods change a type {@code A} to {@code B} and {@code S} to {@code T}. + * {@link Traversal} is a {@link PTraversal} restricted to monomoprhic updates.

    * * @param the source of a {@link PTraversal} * @param the modified source of a {@link PTraversal} 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/data/vector/V2.java b/core/src/main/java/fj/data/vector/V2.java index fdab115c..d1ec5e74 100644 --- a/core/src/main/java/fj/data/vector/V2.java +++ b/core/src/main/java/fj/data/vector/V2.java @@ -23,6 +23,16 @@ private V2(final P2 inner) { this.inner = inner; } + @Override + public final boolean equals(Object other) { + return Equal.equals0(V2.class, this, other, () -> Equal.v2Equal(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.v2Hash(Hash.
    anyHash()).hash(this); + } + /** * Creates a vector-2 from a homogeneous product-2. * diff --git a/core/src/main/java/fj/data/vector/V3.java b/core/src/main/java/fj/data/vector/V3.java index 9b25258f..523c08ae 100644 --- a/core/src/main/java/fj/data/vector/V3.java +++ b/core/src/main/java/fj/data/vector/V3.java @@ -23,6 +23,16 @@ private V3(final P1 head, final V2 tail) { this.tail = tail; } + @Override + public final boolean equals(Object other) { + return Equal.equals0(V3.class, this, other, () -> Equal.v3Equal(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.v3Hash(Hash.anyHash()).hash(this); + } + /** * Creates a vector-3 from a homogeneous product-3. * diff --git a/core/src/main/java/fj/data/vector/V4.java b/core/src/main/java/fj/data/vector/V4.java index 1f39346b..808e7bab 100644 --- a/core/src/main/java/fj/data/vector/V4.java +++ b/core/src/main/java/fj/data/vector/V4.java @@ -23,6 +23,16 @@ private V4(final P1 head, final V3 tail) { this.tail = tail; } + @Override + public final boolean equals(Object other) { + return Equal.equals0(V4.class, this, other, () -> Equal.v4Equal(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.v4Hash(Hash.anyHash()).hash(this); + } + /** * Creates a vector-4 from a homogeneous product-4. * diff --git a/core/src/main/java/fj/data/vector/V5.java b/core/src/main/java/fj/data/vector/V5.java index 0119dcf9..8f0583a3 100644 --- a/core/src/main/java/fj/data/vector/V5.java +++ b/core/src/main/java/fj/data/vector/V5.java @@ -23,6 +23,16 @@ private V5(final P1 head, final V4 tail) { this.tail = tail; } + @Override + public final boolean equals(Object other) { + return Equal.equals0(V5.class, this, other, () -> Equal.v5Equal(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.v5Hash(Hash.anyHash()).hash(this); + } + /** * Creates a vector-5 from a homogeneous product-5. * diff --git a/core/src/main/java/fj/data/vector/V6.java b/core/src/main/java/fj/data/vector/V6.java index 47a0c4e7..880deaff 100644 --- a/core/src/main/java/fj/data/vector/V6.java +++ b/core/src/main/java/fj/data/vector/V6.java @@ -23,6 +23,16 @@ private V6(final P1 head, final V5 tail) { this.tail = tail; } + @Override + public final boolean equals(Object other) { + return Equal.equals0(V6.class, this, other, () -> Equal.v6Equal(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.v6Hash(Hash.anyHash()).hash(this); + } + /** * Creates a vector-6 from a homogeneous product-6. * diff --git a/core/src/main/java/fj/data/vector/V7.java b/core/src/main/java/fj/data/vector/V7.java index c0dfa4b6..2127d5f4 100644 --- a/core/src/main/java/fj/data/vector/V7.java +++ b/core/src/main/java/fj/data/vector/V7.java @@ -23,6 +23,16 @@ private V7(final P1 head, final V6 tail) { this.tail = tail; } + @Override + public final boolean equals(Object other) { + return Equal.equals0(V7.class, this, other, () -> Equal.v7Equal(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.v7Hash(Hash.anyHash()).hash(this); + } + /** * Creates a vector-7 from a homogeneous product-7. * diff --git a/core/src/main/java/fj/data/vector/V8.java b/core/src/main/java/fj/data/vector/V8.java index f01dc2fe..9ca7b618 100644 --- a/core/src/main/java/fj/data/vector/V8.java +++ b/core/src/main/java/fj/data/vector/V8.java @@ -23,6 +23,16 @@ private V8(final P1 head, final V7 tail) { this.tail = tail; } + @Override + public final boolean equals(Object other) { + return Equal.equals0(V8.class, this, other, () -> Equal.v8Equal(Equal.anyEqual())); + } + + @Override + public final int hashCode() { + return Hash.v8Hash(Hash.anyHash()).hash(this); + } + /** * Creates a vector-8 from a homogeneous product-8. * 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 51e115f6..843dd558 100644 --- a/core/src/main/java/fj/parser/Result.java +++ b/core/src/main/java/fj/parser/Result.java @@ -1,6 +1,9 @@ package fj.parser; +import fj.Equal; import fj.F; +import fj.Hash; +import fj.Show; import static fj.Function.curry; @@ -9,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; @@ -21,6 +22,22 @@ private Result(final I i, final A a) { this.a = a; } + @Override + public final int hashCode() { + return Hash.resultHash(Hash.anyHash(), Hash.anyHash()).hash(this); + } + + @Override + public final String toString() { + return Show.resultShow(Show.anyShow(), Show.anyShow()).showS(this); + } + + @Override + public final boolean equals(Object other) { + return Equal.equals0(Result.class, this, other, () -> Equal.resultEqual(Equal.anyEqual(), Equal.anyEqual())); + } + + /** * The remainder of the parse input. * 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 new file mode 100644 index 00000000..13169aca --- /dev/null +++ b/core/src/test/java/fj/DigitTest.java @@ -0,0 +1,30 @@ +package fj; + +import fj.data.Option; +import org.junit.Test; + +import static fj.data.Array.range; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class DigitTest { + @Test + public void testInteger() { + for (Integer i: range(0, 10)) { + assertThat(Digit.fromLong(i).toLong(), is(i.longValue())); + } + } + + @Test + public void testChar() { + for (Integer i: range(0, 10)) { + Character c = Character.forDigit(i, 10); + assertThat(Digit.fromChar(c).some().toChar(), is(c)); + } + } + + @Test + public void testCharNone() { + assertThat(Digit.fromChar('x'), is(Option.none())); + } +} diff --git a/core/src/test/java/fj/EqualTest.java b/core/src/test/java/fj/EqualTest.java new file mode 100644 index 00000000..bacecc46 --- /dev/null +++ b/core/src/test/java/fj/EqualTest.java @@ -0,0 +1,26 @@ +package fj; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class EqualTest { + @Test + public void contramapShouldWork() { + Equal equalByLength = Equal.contramap(String::length, Equal.intEqual); + + assertThat(equalByLength.eq("str1", "str2"), is(true)); + assertThat(equalByLength.eq("str1", "str11"), is(false)); + } + + @Test + public void thenShouldWork() { + Equal equalByLengthThenLastDigit = Equal.on(String::length, Equal.intEqual) + .then(s -> s.charAt(s.length() - 1), Equal.charEqual).equal(); + + assertThat(equalByLengthThenLastDigit.eq("str1", "spr1"), is(true)); + assertThat(equalByLengthThenLastDigit.eq("str1", "str2"), is(false)); + assertThat(equalByLengthThenLastDigit.eq("str1", "strr1"), is(false)); + } +} diff --git a/core/src/test/java/fj/FFunctionsTest.java b/core/src/test/java/fj/FFunctionsTest.java new file mode 100644 index 00000000..4f01a0db --- /dev/null +++ b/core/src/test/java/fj/FFunctionsTest.java @@ -0,0 +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.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/IOTest.java b/core/src/test/java/fj/IOTest.java deleted file mode 100644 index f965204c..00000000 --- a/core/src/test/java/fj/IOTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package fj; - -import fj.data.IO; -import fj.data.IOFunctions; -import org.junit.Test; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - - -public class IOTest { - - @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); - final IO readAndPrintUpperCasedName = IOFunctions.bind(readName, upperCaseAndPrint); - assertThat(readAndPrintUpperCasedName.run(), is("FOO")); - } - - public IO println(final String s) { - return () -> { - return s; - }; - } -} diff --git a/core/src/test/java/fj/MonoidTest.java b/core/src/test/java/fj/MonoidTest.java new file mode 100644 index 00000000..68457f09 --- /dev/null +++ b/core/src/test/java/fj/MonoidTest.java @@ -0,0 +1,58 @@ +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.hamcrest.MatcherAssert.assertThat; + +public class MonoidTest { + + @Test + public void lifted_sum_of_two_numbers() { + Monoid> optionMonoid = Semigroup.intAdditionSemigroup.lift(); + 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/OrdTest.java b/core/src/test/java/fj/OrdTest.java index d218387a..4addec65 100644 --- a/core/src/test/java/fj/OrdTest.java +++ b/core/src/test/java/fj/OrdTest.java @@ -24,4 +24,22 @@ public void isLessThan() { assertThat(pred.f(1L), is(false)); assertThat(pred.f(2L), is(false)); } + + @Test + public void contramapShouldWork() { + Ord lengthOrd = Ord.contramap(String::length, Ord.intOrd); + + assertThat(lengthOrd.compare("str", "rts"), is(Ordering.EQ)); + assertThat(lengthOrd.compare("strlong", "str"), is(Ordering.GT)); + } + + @Test + public void thenShouldWork() { + Ord lengthThenLastDigitOrd = Ord.on(String::length, Ord.intOrd) + .then(s -> s.charAt(s.length() - 1), Ord.charOrd).ord(); + + assertThat(lengthThenLastDigitOrd.compare("str", "dyr"), is(Ordering.EQ)); + assertThat(lengthThenLastDigitOrd.compare("stt", "str"), is(Ordering.GT)); + assertThat(lengthThenLastDigitOrd.compare("str", "strr"), is(Ordering.LT)); + } } 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 new file mode 100644 index 00000000..00220f9d --- /dev/null +++ b/core/src/test/java/fj/PTest.java @@ -0,0 +1,159 @@ +package fj; + +import org.junit.Test; + +import static fj.Function.identity; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PTest { + @Test + public void testPF(){ + final F> p1f = P.p1(); + final P1 p1 = p1f.f(1); + F>> p2f = P.p2(); + final P2 p2 = p2f.f(1).f(2); + assertThat(P2.__1().f(p2), is(P1.__1().f(p1))); + final F>>> p3f = P.p3(); + final P3 p3 = p3f.f(1).f(2).f(3); + assertThat(P3.__1().f(p3), is(P2.__1().f(p2))); + assertThat(P3.__2().f(p3), is(P2.__2().f(p2))); + final F>>>> p4f = P.p4(); + final P4 p4 = p4f.f(1).f(2).f(3).f(4); + assertThat(P4.__1().f(p4), is(P3.__1().f(p3))); + assertThat(P4.__2().f(p4), is(P3.__2().f(p3))); + assertThat(P4.__3().f(p4), is(P3.__3().f(p3))); + final F>>>>> p5f = P.p5(); + final P5 p5 = p5f.f(1).f(2).f(3).f(4).f(5); + assertThat(P5.__1().f(p5), is(P4.__1().f(p4))); + assertThat(P5.__2().f(p5), is(P4.__2().f(p4))); + assertThat(P5.__3().f(p5), is(P4.__3().f(p4))); + assertThat(P5.__4().f(p5), is(P4.__4().f(p4))); + final F>>>>>> p6f = P.p6(); + final P6 p6 = p6f.f(1).f(2).f(3).f(4).f(5).f(6); + assertThat(P6.__1().f(p6), + is(P5.__1().f(p5))); + assertThat(P6.__2().f(p6), + is(P5.__2().f(p5))); + assertThat(P6.__3().f(p6), + is(P5.__3().f(p5))); + assertThat(P6.__4().f(p6), + is(P5.__4().f(p5))); + assertThat(P6.__5().f(p6), + is(P5.__5().f(p5))); + final F>>>>>>> p7f = P.p7(); + final P7 p7 = + p7f.f(1).f(2).f(3).f(4).f(5).f(6).f(7); + assertThat(P7.__1().f(p7), + is(P6.__1().f(p6))); + assertThat(P7.__2().f(p7), + is(P6.__2().f(p6))); + assertThat(P7.__3().f(p7), + is(P6.__3().f(p6))); + assertThat(P7.__4().f(p7), + is(P6.__4().f(p6))); + assertThat(P7.__5().f(p7), + is(P6.__5().f(p6))); + assertThat(P7.__6().f(p7), + is(P6.__6().f(p6))); + final F>>>>>>>> p8f = P.p8(); + final P8 p8 = + p8f.f(1).f(2).f(3).f(4).f(5).f(6).f(7).f(8); + assertThat(P8.__1().f(p8), + is(P7.__1().f(p7))); + assertThat(P8.__2().f(p8), + is(P7.__2().f(p7))); + assertThat(P8.__3().f(p8), + is(P7.__3().f(p7))); + assertThat(P8.__4().f(p8), + is(P7.__4().f(p7))); + assertThat(P8.__5().f(p8), + is(P7.__5().f(p7))); + assertThat(P8.__6().f(p8), + is(P7.__6().f(p7))); + assertThat(P8.__7().f(p8), + is(P7.__7().f(p7))); + assertThat(P8.__8().f(p8), is(8)); + } + + @Test + public void testPProject1() { + final P1 p1 = P.p(1); + assertThat(p1.map(identity()), is(p1)); + } + + @Test + public void testPProject2() { + final P2 p2 = P.p(1, 2); + assertThat(p2.map1(identity()), is(p2)); + assertThat(p2.map2(identity()), is(p2)); + } + + @Test + public void testPProject3() { + final P3 p3 = P.p(1, 2, 3); + assertThat(p3.map1(identity()), is(p3)); + assertThat(p3.map2(identity()), is(p3)); + assertThat(p3.map3(identity()), is(p3)); + } + + @Test + public void testPProject4() { + final P4 p4 = P.p(1, 2, 3, 4); + assertThat(p4.map1(identity()), is(p4)); + assertThat(p4.map2(identity()), is(p4)); + assertThat(p4.map3(identity()), is(p4)); + assertThat(p4.map4(identity()), is(p4)); + } + + @Test + public void testPProject5() { + final P5 p5 = P.p(1, 2, 3, 4, 5); + assertThat(p5.map1(identity()), is(p5)); + assertThat(p5.map2(identity()), is(p5)); + assertThat(p5.map3(identity()), is(p5)); + assertThat(p5.map4(identity()), is(p5)); + assertThat(p5.map5(identity()), is(p5)); + } + + @Test + public void testPProject6() { + final P6 p6 = P.p(1, 2, 3, 4, 5, 6); + assertThat(p6.map1(identity()), is(p6)); + assertThat(p6.map2(identity()), is(p6)); + assertThat(p6.map3(identity()), is(p6)); + assertThat(p6.map4(identity()), is(p6)); + assertThat(p6.map5(identity()), is(p6)); + assertThat(p6.map6(identity()), is(p6)); + } + + @Test + public void testPProject7() { + final P7 p7 = + P.p(1, 2, 3, 4, 5, 6, 7); + assertThat(p7.map1(identity()), is(p7)); + assertThat(p7.map2(identity()), is(p7)); + assertThat(p7.map3(identity()), is(p7)); + assertThat(p7.map4(identity()), is(p7)); + assertThat(p7.map5(identity()), is(p7)); + assertThat(p7.map6(identity()), is(p7)); + assertThat(p7.map7(identity()), is(p7)); + } + + @Test + public void testPProject8() { + final P8 p8 = + P.p(1, 2, 3, 4, 5, 6, 7, 8); + assertThat(p8.map1(identity()), is(p8)); + assertThat(p8.map2(identity()), is(p8)); + assertThat(p8.map3(identity()), is(p8)); + assertThat(p8.map4(identity()), is(p8)); + assertThat(p8.map5(identity()), is(p8)); + assertThat(p8.map6(identity()), is(p8)); + assertThat(p8.map7(identity()), is(p8)); + assertThat(p8.map8(identity()), is(p8)); + } +} 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 e271f5a1..3d6d8354 100644 --- a/core/src/test/java/fj/ShowTest.java +++ b/core/src/test/java/fj/ShowTest.java @@ -6,20 +6,11 @@ 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() { Array a = array(3, 5, 7); String s = Show.arrayShow(Show.intShow).showS(a); - System.out.println(s); assertTrue(s.equals("Array(3,5,7)")); - } - } diff --git a/core/src/test/java/fj/TryEffectTest.java b/core/src/test/java/fj/TryEffectTest.java new file mode 100644 index 00000000..6fd46fd1 --- /dev/null +++ b/core/src/test/java/fj/TryEffectTest.java @@ -0,0 +1,80 @@ +package fj; + +import fj.data.Validation; +import fj.function.TryEffect0; +import fj.function.TryEffect1; +import org.junit.Test; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class TryEffectTest { + + @Test + public void testTryEffect0Success() { + F, Validation> f = TryEffect.f(TryEffect0::f); + Validation v = f.f(new AlwaysSucceed0()); + assertThat(v.isSuccess(), is(true)); + assertThat(v.success(), is(Unit.unit())); + } + + @Test + public void testTryEffect0Fail() { + F, Validation> f = TryEffect.f(TryEffect0::f); + Validation v = f.f(new AlwaysFail0()); + assertThat(v.isFail(), is(true)); + assertThat(v.fail(), is(new TryEffectException())); + } + + @Test + public void testTryEffect1Success() { + F2, Integer, Validation> f = + TryEffect.f(TryEffect1::f); + Validation v = f.f(new AlwaysSucceed1(), 1); + assertThat(v.isSuccess(), is(true)); + assertThat(v.success(), is(Unit.unit())); + } + + @Test + public void testTryEffect1Fail() { + F2, Integer, Validation> f = + TryEffect.f(TryEffect1::f); + Validation v = f.f(new AlwaysFail1(), 1); + assertThat(v.isFail(), is(true)); + assertThat(v.fail(), is(new TryEffectException())); + } + + class AlwaysSucceed0 implements TryEffect0 { + @Override + public void f() throws TryEffectException { + // SUCCESS + } + } + + class AlwaysSucceed1 implements TryEffect1 { + @Override + public void f(Integer i) throws TryEffectException { + // SUCCESS; + } + } + + class AlwaysFail0 implements TryEffect0 { + @Override + public void f() throws TryEffectException { + throw new TryEffectException(); + } + } + + class AlwaysFail1 implements TryEffect1 { + @Override + public void f(Integer i) throws TryEffectException { + throw new TryEffectException(); + } + } + + class TryEffectException extends Exception { + @Override + public boolean equals (Object obj) { + return (obj instanceof TryEffectException); + } + } +} diff --git a/core/src/test/java/fj/TryTest.java b/core/src/test/java/fj/TryTest.java new file mode 100644 index 00000000..46fb2dd3 --- /dev/null +++ b/core/src/test/java/fj/TryTest.java @@ -0,0 +1,50 @@ +package fj; + +import fj.data.Validation; +import fj.function.Try0; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class TryTest { + + @Test + public void testTrySuccess() { + F, Validation> f = + Try.f(Try0::f); + Validation v = f.f(new AlwaysSucceed()); + assertThat(v.isSuccess(), is(true)); + assertThat(v.success(), is(99)); + } + + @Test + public void testTryFail() { + F, Validation> f = + Try.f(Try0::f); + Validation v = f.f(new AlwaysFail()); + assertThat(v.isFail(), is(true)); + assertThat(v.fail(), is(new TryException())); + } + + class AlwaysSucceed implements Try0 { + @Override + public Integer f() throws TryException { + return 99; + } + } + + class AlwaysFail implements Try0 { + @Override + public Integer f() throws TryException { + throw new TryException(); + } + } + + class TryException extends Exception { + @Override + public boolean equals (Object obj) { + return (obj instanceof TryException); + } + } +} 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 new file mode 100644 index 00000000..1f5e7bba --- /dev/null +++ b/core/src/test/java/fj/control/parallel/StrategyTest.java @@ -0,0 +1,69 @@ +package fj.control.parallel; + +import fj.Ord; +import fj.P; +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; + +import java.util.concurrent.*; + +import static fj.control.parallel.Callables.callable; +import static fj.control.parallel.Strategy.*; +import static fj.data.Stream.range; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class StrategyTest { + + @Test + public void testStrategySeq() { + final Stream s = range(Enumerator.intEnumerator, 99, -99, -1); + assertThat(s.sort(Ord.intOrd, seqStrategy()), is(s.sort(Ord.intOrd))); + } + + @Test + public void testStrategyThread() { + final Stream s = range(Enumerator.intEnumerator, 99, -99, -1); + assertThat(s.sort(Ord.intOrd, simpleThreadStrategy()), is(s.sort(Ord.intOrd))); + } + + @Test + public void testStrategyExecutor() { + final Stream s = range(Enumerator.intEnumerator, 99, -99, -1); + final ExecutorService es = Executors.newFixedThreadPool(10); + assertThat(s.sort(Ord.intOrd, executorStrategy(es)), is(s.sort(Ord.intOrd))); + } + + @Test + public void testStrategyCompletion() { + final Stream s = range(Enumerator.intEnumerator, 99, -99, -1); + final ExecutorService es = Executors.newFixedThreadPool(10); + final CompletionService cs = new ExecutorCompletionService<>(es); + assertThat(s.sort(Ord.intOrd, completionStrategy(cs)), is(s.sort(Ord.intOrd))); + } + + @Test + public void testStrategyMergeAll() { + final List l = List.range(0, 100); + 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); + final Strategy> cs = callableStrategy(s); + assertThat(callableStrategy(s).par(P.p(callable(1)))._1().call(), is(1)); + } +} 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 new file mode 100644 index 00000000..77f4db6c --- /dev/null +++ b/core/src/test/java/fj/data/DListTest.java @@ -0,0 +1,32 @@ +package fj.data; + +import org.junit.Test; + +import static fj.data.DList.*; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + + +public class DListTest { + @Test + public void testConsSnoc() { + assertThat(nil().snoc(2).cons(1).toJavaList(), is(single(1).snoc(2).toJavaList())); + } + + @Test + public void testListDList() { + DList d = listDList(List.range(0, 1000)); + assertThat(d.toJavaList(), is(List.range(0, 1000).toJavaList())); + } + + @Test + public void testArrayDList() { + DList d = arrayDList(Array.range(0, 1000).array(Integer[].class)); + assertThat(d.toJavaList(), is(Array.range(0, 1000).toJavaList())); + } + @Test + public void testIter() { + DList d = iteratorDList(List.range(0, 1000).iterator()); + assertThat(d.toJavaList(), is(List.range(0, 1000).toJavaList())); + } +} 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/EvalTest.java b/core/src/test/java/fj/data/EvalTest.java new file mode 100644 index 00000000..a3302e55 --- /dev/null +++ b/core/src/test/java/fj/data/EvalTest.java @@ -0,0 +1,83 @@ +package fj.data; + +import fj.F0; +import fj.F2; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; + +public class EvalTest { + + @Test + public void testNow() { + Eval eval = Eval.now(1); + Assert.assertEquals(eval.value().intValue(), 1); + Assert.assertEquals(eval.map(a -> a.toString()).value(), "1"); + Assert.assertEquals(eval.bind(a -> Eval.now(a * 3)).value().intValue(), 3); + } + + @Test + public void testLater() { + InvocationTrackingF tracker = new InvocationTrackingF<>(1); + Eval eval = Eval.later(tracker); + + Assert.assertEquals(tracker.getInvocationCounter(), 0); + Assert.assertEquals(eval.value().intValue(), 1); + Assert.assertEquals(tracker.getInvocationCounter(), 1); + eval.value(); + Assert.assertEquals(tracker.getInvocationCounter(), 1); + } + + @Test + public void testAlways() { + InvocationTrackingF tracker = new InvocationTrackingF<>(1); + Eval eval = Eval.always(tracker); + + Assert.assertEquals(tracker.getInvocationCounter(), 0); + Assert.assertEquals(eval.value().intValue(), 1); + Assert.assertEquals(tracker.getInvocationCounter(), 1); + eval.value(); + eval.value(); + Assert.assertEquals(tracker.getInvocationCounter(), 3); + } + + @Test + public void testDefer() { + // Make sure that a recursive computation is actually stack-safe. + int targetValue = 200000; + Iterator it = Enumerator.intEnumerator.toStream(0).iterator(); + Eval result = foldRight(it, (v, acc) -> v == targetValue ? Eval.now(true) : acc, false); + Assert.assertTrue(result.value()); + } + + private static Eval foldRight(Iterator iterator, + F2, Eval> f, + A zero) { + if (!iterator.hasNext()) { + return Eval.now(zero); + } + return f.f(iterator.next(), Eval.defer(() -> foldRight(iterator, f, zero))); + } + + private static class InvocationTrackingF implements F0 { + + private final A value; + private int invocationCounter; + + public InvocationTrackingF(A value) { + this.value = value; + this.invocationCounter = 0; + } + + @Override + public A f() { + invocationCounter++; + return value; + } + + public int getInvocationCounter() { + return invocationCounter; + } + } +} diff --git a/core/src/test/java/fj/data/IOFunctionsTest.java b/core/src/test/java/fj/data/IOFunctionsTest.java index dcafc221..8ae18b1a 100644 --- a/core/src/test/java/fj/data/IOFunctionsTest.java +++ b/core/src/test/java/fj/data/IOFunctionsTest.java @@ -1,7 +1,6 @@ package fj.data; -import fj.Unit; -import org.hamcrest.core.Is; +import fj.*; import org.junit.Assert; import org.junit.Test; @@ -9,7 +8,12 @@ import java.io.Reader; import java.util.concurrent.atomic.AtomicBoolean; -import static org.junit.Assert.*; +import static fj.data.IOFunctions.*; +import static fj.data.Stream.cons; +import static fj.data.Stream.nil_; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; public class IOFunctionsTest { @@ -30,8 +34,8 @@ public void close() { r -> () -> new BufferedReader(r).readLine() ); - Assert.assertThat(bracketed.run(), Is.is("Read OK")); - Assert.assertThat(closed.get(), Is.is(true)); + assertThat(bracketed.run(), is("Read OK")); + assertThat(closed.get(), is(true)); } @Test @@ -56,9 +60,64 @@ public void close() { bracketed.run(); fail("Exception expected"); } catch (IllegalArgumentException e) { - Assert.assertThat(e.getMessage(), Is.is("OoO")); + assertThat(e.getMessage(), is("OoO")); } - Assert.assertThat(closed.get(), Is.is(true)); + assertThat(closed.get(), is(true)); + } + + @Test + public void testTraverseIO() throws IOException { + String[] as = {"foo1", "bar2", "foobar3"}; + Stream stream = Stream.arrayStream(as); + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + PrintStream originalOut = System.out; + System.setOut(new PrintStream(outContent)); + stream.traverseIO(IOFunctions::stdoutPrint).run(); + System.setOut(originalOut); + assertThat(outContent.toString(), is("foo1bar2foobar3")); + } + + @Test + public void testSequenceWhile() throws IOException { + BufferedReader r = new BufferedReader(new StringReader("foo1\nbar2\nfoobar3")); + Stream> s1 = Stream.repeat(() -> r.readLine()); + IO> io = sequenceWhile(s1, s -> !s.equals("foobar3")); + assertThat(io.run(), is(cons("foo1", () -> cons("bar2", () -> Stream.nil())))); + } + + @Test + public void testForeach() throws IOException { + Stream> s1 = Stream.repeat(() -> "foo1"); + IO> io = sequence(s1.take(2)); + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + PrintStream originalOut = System.out; + System.setOut(new PrintStream(outContent)); + runSafe(io).foreach(s -> runSafe(stdoutPrint(s))); + System.setOut(originalOut); + assertThat(outContent.toString(), is("foo1foo1")); + } + + + @Test + public void testReplicateM() throws IOException { + final IO is = () -> new BufferedReader(new StringReader("foo")).readLine(); + assertThat(replicateM(is, 3).run(), is(List.list("foo", "foo", "foo"))); + } + + + @Test + public void testLift() throws IOException { + final IO readName = () -> new BufferedReader(new StringReader("foo")).readLine(); + F> f = this::println; + final F> upperCaseAndPrint = f.o().f(String::toUpperCase); + final IO readAndPrintUpperCasedName = IOFunctions.bind(readName, upperCaseAndPrint); + assertThat(readAndPrintUpperCasedName.run(), is("FOO")); + } + + private IO println(final String s) { + return () -> { + return s; + }; } } \ No newline at end of file diff --git a/core/src/test/java/fj/data/JavaTest.java b/core/src/test/java/fj/data/JavaTest.java index 3b5a5785..b8d99510 100644 --- a/core/src/test/java/fj/data/JavaTest.java +++ b/core/src/test/java/fj/data/JavaTest.java @@ -6,17 +6,16 @@ import java.util.EnumSet; import static fj.Show.listShow; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; -/** - * Created by MarkPerry on 14/07/2014. - */ public class JavaTest { @Test public void test1() { // #33: Fixes ClassCastException final List colors = Java.EnumSet_List().f(EnumSet.allOf(Colors.class)); - listShow(Show.anyShow()).print(colors); + assertThat(listShow(Show.anyShow()).showS(colors), is("List(red,green,blue)")); } enum Colors { 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 5d2778e5..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() { @@ -23,8 +45,389 @@ public void traverseList() { int max = 3; List> actual = some(max).traverseList(a -> List.range(1, a + 1)); List> expected = List.range(1, max + 1).map(i -> some(i)); - System.out.println(String.format("actual: %s, expected: %s", actual.toString(), expected.toString())); 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 27ec26e2..8a78ab31 100644 --- a/core/src/test/java/fj/data/SeqTest.java +++ b/core/src/test/java/fj/data/SeqTest.java @@ -1,15 +1,38 @@ package fj.data; import fj.P2; +import fj.control.Trampoline; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +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.*; -/** - * Created by MarkPerry on 16/01/2015. - */ public class SeqTest { @Test @@ -40,7 +63,228 @@ public void convertToString() { @Test public void test() { P2, Seq> p2 = Seq.single(1).split(5); - System.out.println(p2); + assertThat(p2._1(), is(Seq.single(1))); + 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 a37764c2..febd4567 100644 --- a/core/src/test/java/fj/data/StreamTest.java +++ b/core/src/test/java/fj/data/StreamTest.java @@ -1,27 +1,49 @@ package fj.data; +import fj.Equal; +import fj.P2; +import fj.control.Trampoline; import org.junit.Test; import java.io.IOException; -import java.util.ArrayList; import java.util.ConcurrentModificationException; -import static fj.data.IOFunctions.stdinReadLine; -import static java.lang.System.out; +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.assertEquals; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; -/** - * Created by Zheka Kozlov on 27.05.2015. - */ public class StreamTest { @Test public void infiniteStream() { Stream s = Stream.forever(Enumerator.intEnumerator, 0).bind(Stream::single); - assertEquals(List.range(0, 5), s.take(5).toList()); + assertThat(List.range(0, 5), is(s.take(5).toList())); } @Test @@ -48,5 +70,271 @@ public void iterableStreamWithStructureUpdate() { x = s2.head(); } + @Test(expected=Error.class) + public void testCycleNil(){ + cycle(Stream.nil()); + } + + @Test + public void testCycle() { + Stream s = stream(new Character[]{'a', 'b'}); + assertThat(cycle(s).take(5), + is(stream(new Character[]{'a', 'b', 'a', 'b', 'a'}))); + } + + @Test + public void testIterate() { + assertThat(iterate(a -> 2 * a + 1, 1).take(5), + is(stream(new Integer[]{1, 3, 7, 15, 31}))); + } + + @Test + public void testArrayStreamEmpty() { + assertThat(arrayStream(new Integer[]{}), is(Stream.nil())); + } + + @Test(expected=Error.class) + public void testNilHead() { + Stream.nil().head(); + } + + @Test(expected=Error.class) + public void testNilTail() { + Stream.nil().tail(); + } + + @Test + public void testArray() { + Character[] a = new Character[]{'a', 'b', 'c'}; + Stream s = stream(a); + assertThat(s.array(Character[].class), is(a)); + } + + @Test + public void testZipIndex() { + Character[] a = new Character[]{'a', 'b', 'c'}; + P2, Stream> p = unzip(stream(a).zipIndex().drop(1)); + assertThat(p._1(), is(stream(new Character[]{'b', 'c'}))); + assertThat(p._2(), is(stream(new Integer[]{1, 2}))); + } + + @Test + public void testMinus() { + Character[] a1 = new Character[]{'a', 'b', 'c', 'd', 'e'}; + Stream s1 = stream(a1); + Character[] a2 = new Character[]{'b', 'e'}; + Stream s2 = stream(a2); + 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 d3e60936..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 @@ -38,10 +35,6 @@ public void split() { Show> ss = Show.setShow(Show.stringShow); Show> so = Show.optionShow(Show.stringShow); Show, Option, Set>> sp3 = Show.p3Show(ss, so, ss); - if (true) { - st.println(m2); - sp3.println(p); - } // assert equals Equal> seq = Equal.setEqual(Equal.stringEqual); @@ -70,13 +63,6 @@ public void splitLookup() { List rightList = List.range(pivot + 1, max + 1); TreeMap rightMap = iterableTreeMap(Ord.intOrd, rightList.zip(rightList.map(i -> i.toString()))); - // debug info - if (true) { - Show> st = Show.treeMapShow(Show.intShow, Show.stringShow); - Show, Option, TreeMap>> sp3 = Show.p3Show(st, Show.optionShow(Show.stringShow), st); - sp3.println(p3); - } - // do the assert Equal> tme = Equal.treeMapEqual(Equal.intEqual, Equal.stringEqual); Equal, Option, TreeMap>> eq = Equal.p3Equal(tme, Equal.optionEqual(Equal.stringEqual), tme); 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 new file mode 100644 index 00000000..c2549963 --- /dev/null +++ b/core/src/test/java/fj/data/TreeZipperTest.java @@ -0,0 +1,32 @@ +package fj.data; + +import org.junit.Test; + +import static fj.data.Option.none; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class TreeZipperTest { + @Test + public void testDelete() { + final Tree t = Tree.node(1, List.single(Tree.leaf(2))); + final TreeZipper tz = TreeZipper.fromTree(t); + assertThat(tz.delete(), is(none())); + } + + @Test + public void testDeleteForest() { + final Tree t = Tree.node(1, List.single(Tree.leaf(2))); + final TreeZipper tz = TreeZipper.fromForest(Stream.single(t)).some(); + assertThat(tz.delete(), is(none())); + + } + + @Test + public void testHash() { + final Tree t = Tree.node(1, List.single(Tree.leaf(2))); + final TreeZipper tz1 = TreeZipper.fromForest(Stream.single(t)).some(); + final TreeZipper tz2 = TreeZipper.fromForest(Stream.single(t)).some(); + assertThat(tz1.hashCode(), is(tz2.hashCode())); + } +} 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 new file mode 100644 index 00000000..78d837aa --- /dev/null +++ b/core/src/test/java/fj/data/ZipperTest.java @@ -0,0 +1,110 @@ +package fj.data; + +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class ZipperTest { + @Test + public void testZipper() { + Zipper z = Zipper.zipper(Stream.nil(), 0, Stream.range(1, 9)); + assertThat(z.map(i -> i + 13).toStream(), is(Stream.range(13, 22))); + } + + @Test + public void testNext() { + Zipper z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); + z = z.next().some(); + assertThat(z.lefts(), is(Stream.arrayStream(new Integer[]{2, 1}))); + assertThat(z.focus(), is(3)); + assertThat(z.rights(), is(Stream.nil())); + } + + @Test + public void testNextNone() { + Zipper z = Zipper.zipper(Stream.single(1), 2, Stream.nil()); + assertThat(z.next().isNone(), is(true)); + } + + @Test + public void testCycleNext() { + Zipper z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); + assertThat(z.cycleNext(), is(z.next().some())); + } + + @Test + public void testCycleNextLast() { + Zipper z = Zipper.zipper(Stream.single(1), 2, Stream.nil()); + z = z.cycleNext(); + assertThat(z.lefts(), is(Stream.nil())); + assertThat(z.focus(), is(1)); + assertThat(z.rights(), is(Stream.single(2))); + } + + @Test + public void testPrevious() { + Zipper z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); + z = z.previous().some(); + assertThat(z.lefts(), is(Stream.nil())); + assertThat(z.focus(), is(1)); + assertThat(z.rights(), is(Stream.arrayStream(new Integer[]{2, 3}))); + } + + @Test + public void testPreviousNone() { + Zipper z = Zipper.zipper(Stream.nil(), 2, Stream.single(3)); + assertThat(z.previous().isNone(), is(true)); + } + + @Test + public void testCyclePrevious() { + Zipper z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); + assertThat(z.cyclePrevious(), is(z.previous().some())); + } + + @Test + public void testCyclePreviousFirst() { + Zipper z = Zipper.zipper(Stream.nil(), 1, Stream.single(2)); + z = z.cyclePrevious(); + assertThat(z.lefts(), is(Stream.single(1))); + assertThat(z.focus(), is(2)); + assertThat(z.rights(), is(Stream.nil())); + } + + @Test + public void testInsertLeft() { + Zipper z = Zipper.single(2); + z = z.insertLeft(1); + assertThat(z.lefts(), is(Stream.nil())); + assertThat(z.focus(), is(1)); + assertThat(z.rights(), is(Stream.single(2))); + } + + @Test + public void testInsertRight() { + Zipper z = Zipper.single(2); + z = z.insertRight(3); + assertThat(z.lefts(), is(Stream.single(2))); + assertThat(z.focus(), is(3)); + assertThat(z.rights(), is(Stream.nil())); + } + + @Test + public void testDeleteOthers() { + Zipper z = Zipper.zipper(Stream.single(1), 2, Stream.single(3)); + z = z.deleteOthers(); + assertThat(z.lefts(), is(Stream.nil())); + assertThat(z.focus(), is(2)); + assertThat(z.rights(), is(Stream.nil())); + } + + @Test + public void testFind() { + Zipper z = Zipper.zipper(Stream.nil(), 0, Stream.range(1)); + z = z.find(i -> i == 4).some(); + assertThat(z.lefts(), is(Stream.arrayStream(new Integer[]{3, 2, 1, 0}))); + assertThat(z.focus(), is(4)); + assertThat(z.rights().take(3), is(Stream.range(5, 8))); + } +} 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 new file mode 100644 index 00000000..f9559d3f --- /dev/null +++ b/core/src/test/java/fj/data/optic/IsoTest.java @@ -0,0 +1,44 @@ +package fj.data.optic; + +import fj.P; +import fj.P2; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + + +public class IsoTest { + @Test + public void testIso() { + final int oldNumber = 10; + final String oldStreet = "Main St"; + final Address oldAddress = new Address(oldNumber, oldStreet); + final Iso> addressIso = Iso.iso(p -> P.p(p.number, p.street), + p -> new Address(p._1(), p._2())); + final Address a = addressIso.reverseGet(addressIso.get(oldAddress)); + assertThat(a.number, is(oldAddress.number)); + assertThat(a.street, is(oldAddress.street)); + } + + static final class Person { + String name; + Address address; + + Person(String name, Address address) { + this.name = name; + this.address = address; + } + } + + static final class Address { + int number; + String street; + + public Address(int number, String street) { + this.number = number; + this.street = street; + } + } + +} diff --git a/core/src/test/java/fj/data/optic/LensTest.java b/core/src/test/java/fj/data/optic/LensTest.java new file mode 100644 index 00000000..98be7fca --- /dev/null +++ b/core/src/test/java/fj/data/optic/LensTest.java @@ -0,0 +1,124 @@ +package fj.data.optic; + +import fj.F; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + + +public class LensTest { + @Test + public void testLensPersonGet() { + final String oldName = "Joe"; + final int oldNumber = 10; + final String oldStreet = "Main St"; + final Address oldAddress = new Address(oldNumber, oldStreet); + final Person oldPerson = new Person(oldName, oldAddress); + final Lens personNameLens = Lens.lens(p -> p.name, s -> p -> new Person(s, p.address)); + final Lens personAddressLens = Lens.lens(p -> p.address, a -> p -> new Person(p.name, a)); + final Lens addressNumberLens = Lens.lens(a -> a.number, n -> a -> new Address(n, a.street)); + final Lens addressStreetLens = Lens.lens(a -> a.street, s -> a -> new Address(a.number, s)); + final Lens personNumberLens = personAddressLens.composeLens(addressNumberLens); + final Lens personStreetLens = personAddressLens.composeLens(addressStreetLens); + assertThat(personNameLens.get(oldPerson), is(oldName)); + assertThat(personNumberLens.get(oldPerson), is(oldNumber)); + assertThat(personStreetLens.get(oldPerson), is(oldStreet)); + } + + @Test + public void testLensPersonSetName() { + final String oldName = "Joe"; + final int oldNumber = 10; + final String oldStreet = "Main St"; + final Address oldAddress = new Address(oldNumber, oldStreet); + final Person oldPerson = new Person(oldName, oldAddress); + final Lens personNameLens = Lens.lens(p -> p.name, s -> p -> new Person(s, p.address)); + String newName = "Bill"; + Person p = personNameLens.set(newName).f(oldPerson); + assertThat(p.name, is(newName)); + assertThat(p.address, is(oldPerson.address)); + } + + @Test + public void testLensPersonSetNumber() { + final String oldName = "Joe"; + final int oldNumber = 10; + final String oldStreet = "Main St"; + final Address oldAddress = new Address(oldNumber, oldStreet); + final Person oldPerson = new Person(oldName, oldAddress); + final Lens personAddressLens = Lens.lens(p -> p.address, a -> p -> new Person(p.name, a)); + final Lens addressNumberLens = Lens.lens(a -> a.number, n -> a -> new Address(n, a.street)); + final Lens personNumberLens = personAddressLens.composeLens(addressNumberLens); + int newNumber = 20; + Person p = personNumberLens.set(newNumber).f(oldPerson); + assertThat(p.name, is(oldName)); + assertThat(p.address.number, is(newNumber)); + assertThat(p.address.street, is(oldStreet)); + } + + @Test + public void testLensPersonSetStreet() { + final String oldName = "Joe"; + final int oldNumber = 10; + final String oldStreet = "Main St"; + final Address oldAddress = new Address(oldNumber, oldStreet); + final Person oldPerson = new Person(oldName, oldAddress); + final Lens personAddressLens = Lens.lens(p -> p.address, a -> p -> new Person(p.name, a)); + final Lens addressNumberLens = Lens.lens(a -> a.number, n -> a -> new Address(n, a.street)); + final Lens addressStreetLens = Lens.lens(a -> a.street, s -> a -> new Address(a.number, s)); + final Lens personStreetLens = personAddressLens.composeLens(addressStreetLens); + String newStreet = "First St"; + Person p = personStreetLens.set(newStreet).f(oldPerson); + assertThat(p.name, is(oldName)); + assertThat(p.address.number, is(oldPerson.address.number)); + assertThat(p.address.street, is(newStreet)); + } + + @Test + public void testLensPersonSetter() { + final String oldName = "Joe"; + final int oldNumber = 10; + final String oldStreet = "Main St"; + final Address oldAddress = new Address(oldNumber, oldStreet); + final Person oldPerson = new Person(oldName, oldAddress); + final Lens personNameLens = Lens.lens(p -> p.name, s -> p -> new Person(s, p.address)); + String newName = "Bill"; + F setter = personNameLens.asSetter().set(newName); + Person p = setter.f(oldPerson); + assertThat(p.name, is(newName)); + assertThat(p.address, is(oldPerson.address)); + } + + @Test + public void testLensPersonGetter() { + final String oldName = "Joe"; + final int oldNumber = 10; + final String oldStreet = "Main St"; + final Address oldAddress = new Address(oldNumber, oldStreet); + final Person oldPerson = new Person(oldName, oldAddress); + final Lens personNameLens = Lens.lens(p -> p.name, s -> p -> new Person(s, p.address)); + assertThat(personNameLens.asGetter().get(oldPerson), is(oldName)); + } + + static final class Person { + String name; + Address address; + + Person(String name, Address address) { + this.name = name; + this.address = address; + } + } + + static final class Address { + int number; + String street; + + public Address(int number, String street) { + this.number = number; + this.street = street; + } + } + +} diff --git a/core/src/test/java/fj/data/optic/OptionalTest.java b/core/src/test/java/fj/data/optic/OptionalTest.java new file mode 100644 index 00000000..7fddb31b --- /dev/null +++ b/core/src/test/java/fj/data/optic/OptionalTest.java @@ -0,0 +1,30 @@ +package fj.data.optic; + +import fj.data.Option; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + + +public class OptionalTest { + @Test + public void testOptionalSome() { + Optional o = Optional.optional(this::decode, i -> s -> s); + assertThat(o.getOption("18"), is(Option.some(18))); + } + + @Test + public void testOptionalNone() { + Optional o = Optional.optional(this::decode, i -> s -> s); + assertThat(o.getOption("Z"), is(Option.none())); + } + + private Option decode(String s) { + try { + return Option.some(Integer.decode(s)); + } catch (NumberFormatException nfe) { + return Option.none(); + } + } +} diff --git a/core/src/test/java/fj/data/optic/PrismTest.java b/core/src/test/java/fj/data/optic/PrismTest.java new file mode 100644 index 00000000..ce6dddb4 --- /dev/null +++ b/core/src/test/java/fj/data/optic/PrismTest.java @@ -0,0 +1,29 @@ +package fj.data.optic; + +import fj.data.Option; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PrismTest { + @Test + public void testPrismSome() { + Prism prism = Prism.prism(s -> decode(s), i -> i.toString()); + assertThat(prism.getOption("18"), is(Option.some(18))); + } + + @Test + public void testPrismNone() { + Prism prism = Prism.prism(s -> decode(s), i -> i.toString()); + assertThat(prism.getOption("Z"), is(Option.none())); + } + + private Option decode(String s) { + try { + return Option.some(Integer.decode(s)); + } catch (NumberFormatException nfe) { + return Option.none(); + } + } +} diff --git a/core/src/test/java/fj/data/optic/TraversalTest.java b/core/src/test/java/fj/data/optic/TraversalTest.java new file mode 100644 index 00000000..9cfc25cc --- /dev/null +++ b/core/src/test/java/fj/data/optic/TraversalTest.java @@ -0,0 +1,23 @@ +package fj.data.optic; + +import fj.Monoid; +import fj.data.Either; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class TraversalTest { + @Test + public void testTraversalLeft() { + final Traversal, Integer> t = Traversal.codiagonal(); + assertThat(t.fold(Monoid.intMinMonoid).f(Either.left(3)), is(3)); + } + + @Test + public void testTraversalRight() { + final Traversal, Integer> t = Traversal.codiagonal(); + assertThat(t.fold(Monoid.intMinMonoid).f(Either.right(2)), is(2)); + } + +} diff --git a/core/src/test/java/fj/data/vector/VTest.java b/core/src/test/java/fj/data/vector/VTest.java new file mode 100644 index 00000000..34f8686c --- /dev/null +++ b/core/src/test/java/fj/data/vector/VTest.java @@ -0,0 +1,107 @@ +package fj.data.vector; + +import fj.*; +import fj.data.Array; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + + +public class VTest { + @Test + public void testVectorUp(){ + final P2 p2 = P.p(2, 1); + final V2 v2 = V2.p(p2); + final V3 v3 = V3.cons(P.p(3), v2); + final V4 v4 = V4.cons(P.p(4), v3); + final V5 v5 = V5.cons(P.p(5), v4); + final V6 v6 = V6.cons(P.p(6), v5); + final V7 v7 = V7.cons(P.p(7), v6); + final V8 v8 = V8.cons(P.p(8), v7); + assertThat(v8.toArray(), is(Array.range(1, 9).reverse())); + } + + @Test + public void testVectorP(){ + final P2 p2 = P.p(1, 2); + final V2 v2 = V2.p(p2); + assertThat(v2.toArray(), is(Array.range(1, 3))); + assertThat(v2.p(), is(p2)); + final P3 p3 = p2.append(3); + final V3 v3 = V3.p(p3); + assertThat(v3.toArray(), is(Array.range(1, 4))); + assertThat(v3.p(), is(p3)); + final P4 p4 = p3.append(4); + final V4 v4 = V4.p(p4); + assertThat(v4.toArray(), is(Array.range(1, 5))); + assertThat(v4.p(), is(p4)); + final P5 p5 = p4.append(5); + final V5 v5 = V5.p(p5); + assertThat(v5.toArray(), is(Array.range(1, 6))); + assertThat(v5.p(), is(p5)); + final P6 p6 = p5.append(6); + final V6 v6 = V6.p(p6); + assertThat(v6.toArray(), is(Array.range(1, 7))); + assertThat(v6.p(), is(p6)); + final P7 p7 = p6.append(7); + final V7 v7 = V7.p(p7); + assertThat(v7.toArray(), is(Array.range(1, 8))); + assertThat(v7.p(), is(p7)); + final P8 p8 = p7.append(8); + final V8 v8 = V8.p(p8); + assertThat(v8.toArray(), is(Array.range(1, 9))); + assertThat(v8.p(), is(p8)); + } + + @Test + public void testVectorFunc2() { + V2 v2 = V.v(() -> 2, () -> 1); + F2> fv2 = V.v2(); + V2 vf2 = fv2.f(2, 1); + assertThat(vf2, is(v2)); + } + + @Test + public void testVectorFunc3() { + V3 v3 = V.v(P.p(3), () -> 2, () -> 1); + F3> fv3 = V.v3(); + V3 vf3 = fv3.f(3, 2, 1); + assertThat(vf3, is(v3)); + } + + @Test + public void testVectorFunc4() { + V4 v4 = V.v(P.p(4), P.p(3), () -> 2, () -> 1); + F4> fv4 = V.v4(); + V4 vf4 = fv4.f(4, 3, 2, 1); + assertThat(vf4, is(v4)); + } + + @Test + public void testVectorFunc5() { + V5 v5 = V.v(P.p(5), P.p(4), P.p(3), () -> 2, () -> 1); + F5> fv5 = V.v5(); + V5 vf5 = fv5.f(5, 4, 3, 2, 1); + assertThat(vf5, is(v5)); + } + + @Test + public void testVectorMap() { + final V2 v2 = V.v(() -> 2, () -> 1); + assertThat(v2, is(v2.map(i -> i * 1))); + final V3 v3 = V3.cons(P.p(3), v2); + assertThat(v3, is(v3.map(i -> i * 1))); + final V4 v4 = V4.cons(P.p(4), v3); + assertThat(v4, is(v4.map(i -> i * 1))); + final V5 v5 = V5.cons(P.p(5), v4); + assertThat(v5, is(v5.map(i -> i * 1))); + final V6 v6 = V6.cons(P.p(6), v5); + assertThat(v6, is(v6.map(i -> i * 1))); + final V7 v7 = V7.cons(P.p(7), v6); + assertThat(v7, is(v7.map(i -> i * 1))); + final V8 v8 = V8.cons(P.p(8), v7); + assertThat(v8, is(v8.map(i -> i * 1))); + } + +} 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 new file mode 100644 index 00000000..da92a4bb --- /dev/null +++ b/core/src/test/java/fj/function/VisitorTest.java @@ -0,0 +1,80 @@ +package fj.function; + +import fj.Equal; +import fj.F; +import fj.P; +import fj.P1; +import fj.data.List; +import org.junit.Test; + +import static fj.data.Option.none; +import static fj.data.Option.some; +import static fj.function.Visitor.*; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class VisitorTest { + @Test + public void testFindFirst() { + assertThat(findFirst(List.list(none(), some(1), none()), () -> -1), is(1)); + } + + @Test + public void testFindFirstDef() { + assertThat(findFirst(List.list(none(), none(), none()), () -> -1), is(-1)); + } + + @Test + public void testNullableFindFirst() { + assertThat(nullablefindFirst(List.list(null, 1, null), () -> -1), is(1)); + } + + @Test + public void testNullableFindFirstDef() { + assertThat(nullablefindFirst(List.list(null, null, null), () -> -1), is(-1)); + } + + @Test + public void testVisitor() { + assertThat(visitor(List.list(i -> some(2 * i)), () -> -1, 10), is(20)); + } + + @Test + public void testVisitorDef() { + assertThat(visitor(List.list(i -> none()), () -> "foo", 10), is("foo")); + } + + @Test + public void testNullableVisitor() { + assertThat(nullableVisitor(List.list(i -> 2 * i), () -> -1, 10), is(20)); + } + + @Test + public void testNullableVisitorDef() { + assertThat(nullableVisitor(List.list(i -> null), () -> "foo", 10), is("foo")); + } + + @Test + public void testAssociation() { + final F> a = association(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); + assertThat(a.f("foo").f(2), is("two")); + } + + @Test + public void testAssociationDef() { + final F> a = association(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); + assertThat(a.f("foo").f(3), is("foo")); + } + + @Test + public void testAssociationLazy() { + final F, F> a = associationLazy(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); + assertThat(a.f(P.p("foo")).f(2), is("two")); + } + + @Test + public void testAssociationLazyDef() { + final F, F> a = associationLazy(List.list(P.p(1, "one"), P.p(2, "two")), Equal.intEqual); + assertThat(a.f(P.p("foo")).f(3), is("foo")); + } +} diff --git a/core/src/test/java/fj/parser/ParserTest.java b/core/src/test/java/fj/parser/ParserTest.java new file mode 100644 index 00000000..f2664a1f --- /dev/null +++ b/core/src/test/java/fj/parser/ParserTest.java @@ -0,0 +1,52 @@ +package fj.parser; + +import fj.F; +import fj.F0; +import fj.data.Stream; +import fj.data.Validation; +import org.junit.Test; + +import static fj.parser.Result.result; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class ParserTest { + @Test + public void testParserFail() { + final Parser fail = Parser.fail(new ParseException()); + assertThat(fail.parse("").fail(), is(new ParseException())); + } + + @Test + public void testParserValue() { + final Parser p = Parser.parser(s -> s.isEmpty() ? + Validation.fail(new ParseException()) : + Validation.success(result(s.substring(1), s.substring(0, 1))) + ); + final Result r = p.parse("abc").success(); + assertThat(r.value(), is("a")); + assertThat(r.rest(), is("bc")); + } + + @Test + public void testParserBind() { + final Parser p = Parser.value("a"); + final Parser fail = Parser.fail(new ParseException()); + assertThat(p.bind(o -> fail).parse("aaaa").fail(), is(new ParseException())); + } + + @Test + public void testParserStream() { + Stream s = Stream.fromString("abc"); + Result, Character> r = Parser.CharsParser.character('a').parse(s).success(); + assertThat(r, is(Result.result(Stream.fromString("bc"), 'a'))); + } + + class ParseException extends Exception { + @Override + public boolean equals (Object obj) { + return (obj instanceof ParseException); + } + } + +} diff --git a/demo/build.gradle b/demo/build.gradle index 41841cfa..87bf7a5c 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -5,16 +5,15 @@ 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 { jacoco { enabled = false } -} \ No newline at end of file +} 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.7.adoc b/etc/release-notes/release-notes-4.7.adoc index 3c1b265f..428a0726 100644 --- a/etc/release-notes/release-notes-4.7.adoc +++ b/etc/release-notes/release-notes-4.7.adoc @@ -1,28 +1,58 @@ = Release 4.7 -Proposed release: September 2016 +Released: 27 March 2017 == Enhancements -* TODO. +* Added Hash Array Mapped Trie (#284). +* Improve performance of List#groupBy via a mutable buffer (#288). +* Avoid unnecessary wrapping of F0 into P1.lazy where applicable. Use static P factories for this (#284). +* Added semigroup constraint to List.traverseValidation (#287). +* Added first class fold for either (catamorphism) (#284). +* Added first class fold for Option (catamorphism) (#284). +* Added optimisable definitions for Equal (#284). +* Added uncurried version of foldRight, foldLeft and append to NonEmptyList (#284). +* Added optimisable definitions for Monoid and Semigroup (#284). +* Improved performance of List.minimum and maximum by using uncurried Ord.max and Ord.min (#284). +* Removed deprecation of Monoid, Ord and Semigroup constructors for non Java 8 versions (#284). +* Added safe List minimumOption and maximumOption and NonEmptyList minimum and maximum. +* Added Set.lookup, Set.lookupLT, Set.lookupGT, Set.lookupLE and Set.lookupGE from Haskell (#305). == Fixes -* TODO. +* Make Stream conversions respect laziness (#284). +* Fixed Ord#isGreaterThan(A)/isLesserThan(A) not using strict inequality (#293). +* Correctly convert Option.some(null) to Optional (#284). +* Fixed override in P.p(A) (#284). +* Avoid unnecessary currying in uncurried version of foldRight (#284). == Internal -* TODO. +* Deprecated unsafe Ord instance and List#groupBy without an Ord parameter (#290). +* Added tests for Strings class (#295). +* Use more method references (#284). +* Converted memoisation tests from JUnit to PropertyTestRunner (#284) for hard memoisation. +* Improved implementation of P1#_1 method (#284). +* Make use of weak memoisation explicit (#284). +* Enable backport of default methods using Retro Lambda (#284). +* Upgrade to Gradle 2.14.1 (#310). +* Switch from Coveralls to cover.io (#308). +* Force strict evaluation in TreeMap.map (#309). == Breaking Changes -* TODO. +* none. == Documentation -* TODO. +* none. == Contributors -* TODO. +* Jean-Baptiste Giradeau +* Mark Perry +* Gabor Liptak +* Shimi Bandiel +* Siro Mateos + 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 new file mode 100644 index 00000000..7e8aed61 --- /dev/null +++ b/etc/release-notes/release-notes-4.8.adoc @@ -0,0 +1,53 @@ + += Release 4.8 + +Released: 18 Aug 2018 + +== Enhancements + +- 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 + +- 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 + +- 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 + +None. + +== Documentation + +None. + +== Contributors + +* 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 05ef575b..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 23175860..b1159fc5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Tue Dec 06 21:53:19 EST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-bin.zip diff --git a/gradlew b/gradlew index 9d82f789..83f2acfd 100755 --- a/gradlew +++ b/gradlew @@ -1,4 +1,20 @@ -#!/usr/bin/env bash +#!/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. +# ############################################################################## ## @@ -6,20 +22,38 @@ ## ############################################################################## -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null 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='"-Xmx64m" "-Xms64m"' + # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" -warn ( ) { +warn () { echo "$*" } -die ( ) { +die () { echo echo "$*" echo @@ -30,6 +64,7 @@ die ( ) { cygwin=false msys=false darwin=false +nonstop=false case "`uname`" in CYGWIN* ) cygwin=true @@ -40,26 +75,11 @@ case "`uname`" in MINGW* ) msys=true ;; + NONSTOP* ) + nonstop=true + ;; esac -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. @@ -85,7 +105,7 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then MAX_FD_LIMIT=`ulimit -H -n` if [ $? -eq 0 ] ; then if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then @@ -105,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"` @@ -150,11 +170,19 @@ if $cygwin ; then esac fi -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " } -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index aec99730..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 @@ -8,14 +24,14 @@ @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal -@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 DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=. 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="-Xmx64m" "-Xms64m" + @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome @@ -46,10 +62,9 @@ echo location of your Java installation. goto fail :init -@rem Get command-line arguments, handling Windowz variants +@rem Get command-line arguments, handling Windows variants if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args :win9xME_args @rem Slurp the command line arguments. @@ -60,11 +75,6 @@ set _SKIP=2 if "x%~1" == "x" goto execute set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ :execute @rem Setup the command line diff --git a/java-core/build.gradle b/java-core/build.gradle index 88e9a25f..f6a6146a 100644 --- a/java-core/build.gradle +++ b/java-core/build.gradle @@ -3,14 +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 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 88e9a25f..00000000 --- a/java8/build.gradle +++ /dev/null @@ -1,16 +0,0 @@ - -archivesBaseName = "${project.projectName}-${project.name}" - -ext { - signModule = true -} - -dependencies { - compile project(":core") - testCompile dependencyJunit -} - -performSigning(signingEnabled, signModule) -configureUpload(signingEnabled, signModule) - -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 28be5e34..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,70 +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) +// } } + } + } ext { @@ -102,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 4d1a04e9..396d19d4 100644 --- a/props-core-scalacheck/build.gradle +++ b/props-core-scalacheck/build.gradle @@ -4,27 +4,33 @@ archivesBaseName = "${project.projectName}-${project.name}" apply plugin: 'scala' ext { - scalaVersion = "2.11.7" - scalacheckScalaVersion = "2.11" - scalacheckVersion = "1.12.4" - signModule = true -} +// 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" -tasks.withType(ScalaCompile) { - scalaCompileOptions.useAnt = false + 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" - testCompile dependencyJunit + testImplementation junitCompile + testRuntimeOnly junitRuntime } -sourceCompatibility = "1.7" +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/main/scala/fj/data/ArbitraryIO.scala b/props-core-scalacheck/src/main/scala/fj/data/ArbitraryIO.scala new file mode 100644 index 00000000..aca5cbd1 --- /dev/null +++ b/props-core-scalacheck/src/main/scala/fj/data/ArbitraryIO.scala @@ -0,0 +1,18 @@ +package fj.data + +import org.scalacheck.Arbitrary + +/** + * + */ +object ArbitraryIO { + + + private case class ArbIO[T](value: T) extends IO[T] { + override def run(): T = value + } + + implicit def arbitraryIO[T](implicit arbT: Arbitrary[T]): Arbitrary[IO[T]] = + Arbitrary(arbT.arbitrary.map(ArbIO(_))) + +} diff --git a/props-core-scalacheck/src/main/scala/fj/data/ArbitraryNatural.scala b/props-core-scalacheck/src/main/scala/fj/data/ArbitraryNatural.scala new file mode 100644 index 00000000..4210eef7 --- /dev/null +++ b/props-core-scalacheck/src/main/scala/fj/data/ArbitraryNatural.scala @@ -0,0 +1,13 @@ +package fj.data + +import org.scalacheck.Arbitrary + +/** + * A Scalacheck [[Arbitrary]] for [[Natural]]. + */ +object ArbitraryNatural { + + implicit def arbitraryNatural: Arbitrary[Natural] = + Arbitrary(Arbitrary.arbBigInt.arbitrary.map(_.abs).map(bi => Natural.natural(bi.bigInteger).some())) + +} diff --git a/props-core-scalacheck/src/main/scala/fj/data/ArbitraryNonEmptyList.scala b/props-core-scalacheck/src/main/scala/fj/data/ArbitraryNonEmptyList.scala new file mode 100644 index 00000000..2258ba9b --- /dev/null +++ b/props-core-scalacheck/src/main/scala/fj/data/ArbitraryNonEmptyList.scala @@ -0,0 +1,16 @@ +package fj.data + +import fj.data.NonEmptyList.nel +import org.scalacheck.Arbitrary.arbitrary +import org.scalacheck.{Arbitrary, Gen} + +/** + * A Scalacheck [[Arbitrary]] for [[NonEmptyList]]. + */ +object ArbitraryNonEmptyList { + implicit def arbitraryNonEmptyList[A](implicit a: Arbitrary[A]): Arbitrary[NonEmptyList[A]] = + Arbitrary(nelOf(arbitrary[A])) + + def nelOf[A](g: => Gen[A]): Gen[NonEmptyList[A]] = + Gen.nonEmptyListOf(g).map(l => l.tail.foldRight(nel(l.head))((x, n) => n.cons(x))) +} diff --git a/props-core-scalacheck/src/test/scala/fj/CheckSemigroup.scala b/props-core-scalacheck/src/test/scala/fj/CheckSemigroup.scala new file mode 100644 index 00000000..8c9fe547 --- /dev/null +++ b/props-core-scalacheck/src/test/scala/fj/CheckSemigroup.scala @@ -0,0 +1,212 @@ +package fj + +import fj.ArbitraryP._ +import fj.ArbitraryUnit.arbitraryUnit +import fj.Ord.intOrd +import fj.data.ArbitraryArray.arbitraryArray +import fj.data.ArbitraryIO.arbitraryIO +import fj.data.ArbitraryList.arbitraryList +import fj.data.ArbitraryNatural.arbitraryNatural +import fj.data.ArbitraryNonEmptyList.arbitraryNonEmptyList +import fj.data.ArbitraryOption.arbitraryOption +import fj.data.ArbitrarySet.arbitrarySet +import fj.data.ArbitraryStream.arbitraryStream +import fj.data.IO +import org.scalacheck.Arbitrary._ +import org.scalacheck.Prop._ +import org.scalacheck.{Arbitrary, Properties} + +/** + * Scalacheck [[Properties]] parameterized for [[Semigroup]] implementations. + * + * @param s a Semigroup implementation. + * @param desc a description of the Semigroup implementation. + * @param arbitrary a Scalacheck [[Arbitrary]] implementation for the Semigroup's type. + * @tparam T the type to which the Semigroup applies. + */ +case class SemigroupProperties[T](s: Semigroup[T], desc: String)(implicit val arbitrary: Arbitrary[T]) extends Properties(desc) { + + + property("sum(x,y)") = forAll((x: T, y: T, z: T) => + s.sum(s.sum(x, y), z) == s.sum(x, s.sum(y, z))) + + property("sum()") = forAll((x: T, y: T, z: T) => { + val sf = s.sum() + sf.f(sf.f(x).f(y)).f(z) == sf.f(x).f(sf.f(y).f(z)) + }) + + property("dual()") = forAll((x: T, y: T, z: T) => { + val sd = s.dual() + sd.sum(sd.sum(x, y), z) == sd.sum(x, sd.sum(y, z)) + }) + + property("sum(x)") = forAll((x: T, y: T) => + s.sum(x).f(y) == s.sum(x, y)) + +} + +/** + * A specialized Scalacheck [[Properties]] object for testing the [[Semigroup.ioSemigroup()]] method. + */ +object CheckIOSemigroup extends Properties("ioSemigroup") { + + val s = Semigroup.ioSemigroup(Semigroup.stringSemigroup) + + property("sum(x,y)") = forAll((x: IO[String], y: IO[String], z: IO[String]) => + s.sum(s.sum(x, y), z).run() == s.sum(x, s.sum(y, z)).run()) + + property("sum()") = forAll((x: IO[String], y: IO[String], z: IO[String]) => { + val sf = s.sum() + sf.f(sf.f(x).f(y)).f(z).run() == sf.f(x).f(sf.f(y).f(z)).run() + }) + + property("dual()") = forAll((x: IO[String], y: IO[String], z: IO[String]) => { + val sd = s.dual() + sd.sum(sd.sum(x, y), z).run() == sd.sum(x, sd.sum(y, z)).run() + }) + + property("sum(x)") = forAll((x: IO[String], y: IO[String]) => + s.sum(x).f(y).run() == s.sum(x, y).run()) + +} + +/** + * A [[Properties]] implementation for testing [[Semigroup]] implementations that + * apply to mutable builder style classes such as [[java.lang.StringBuilder]] and + * [[java.lang.StringBuffer]]. + * + * @param s a Semigroup implementation to test. + * @param desc a description of the Semigroup implementation under test. + * @param conversion a function that converts a value of type T to a new instance of type S. + * @tparam T the type that the builder constructs. + * @tparam S the type to which the Semigroup applies. + */ +case class CheckMutableBuilder[S, T](s: Semigroup[S], desc: String, conversion: T => S)(implicit val arbitrary: Arbitrary[T]) extends Properties(desc) { + + implicit def toBuilder(t: T): S = conversion(t) + + property("sum(x,y)") = forAll((x: T, y: T, z: T) => + s.sum(s.sum(x, y), z).toString == s.sum(x, s.sum(y, z)).toString + ) + + property("sum()") = forAll((x: T, y: T, z: T) => { + val sf = s.sum() + sf.f(sf.f(x).f(y)).f(z).toString == sf.f(x).f(sf.f(y).f(z)).toString + }) + + property("dual()") = forAll((x: T, y: T, z: T) => { + val sd = s.dual() + sd.sum(sd.sum(x, y), z).toString == sd.sum(x, sd.sum(y, z)).toString + }) + + property("sum(x)") = forAll((x: T, y: T) => + s.sum(x).f(y).toString == s.sum(x, y).toString) + +} + + +/** + * A Scalacheck [[Properties]] object that aggregates the tests for all [[Semigroup]] implementations. + */ +object CheckSemigroup extends Properties("Semigroup") { + + + def idInt(n: Int): java.lang.Integer = n + + implicit def oi: Ord[Int] = intOrd.contramap(idInt _) + + implicit lazy val arbJavaBigDecimal: Arbitrary[java.math.BigDecimal] = Arbitrary( + Arbitrary.arbLong.arbitrary.map(l => new java.math.BigDecimal(l)) + ) + + implicit lazy val arbJavaBigInteger: Arbitrary[java.math.BigInteger] = Arbitrary( + Arbitrary.arbBigInt.arbitrary.map(_.bigInteger) + ) + + implicit lazy val arbJavaInteger: Arbitrary[Integer] = Arbitrary( + Arbitrary.arbInt.arbitrary.map(i => i) + ) + + implicit lazy val arbJavaLong: Arbitrary[java.lang.Long] = Arbitrary( + Arbitrary.arbLong.arbitrary.map(i => i) + ) + + implicit lazy val arbJavaBoolean: Arbitrary[java.lang.Boolean] = Arbitrary( + Arbitrary.arbBool.arbitrary.map(b => b) + ) + + implicit lazy val arbStringBuilder: Arbitrary[java.lang.StringBuilder] = Arbitrary( + Arbitrary.arbString.arbitrary.map(new java.lang.StringBuilder(_)) + ) + + implicit lazy val arbStringBuffer: Arbitrary[StringBuffer] = Arbitrary( + Arbitrary.arbString.arbitrary.map(new StringBuffer(_)) + ) + + include(SemigroupProperties(Semigroup.arraySemigroup[Int](), "arraySemigroup()")) + + include(SemigroupProperties(Semigroup.bigdecimalAdditionSemigroup, "bigdecimalAdditionSemigroup")) + include(SemigroupProperties(Semigroup.bigdecimalMultiplicationSemigroup, "bigdecimalMultiplicationSemigroup")) + include(SemigroupProperties(Semigroup.bigDecimalMaximumSemigroup, "bigDecimalMaximumSemigroup")) + include(SemigroupProperties(Semigroup.bigDecimalMinimumSemigroup, "bigDecimalMinimumSemigroup")) + + include(SemigroupProperties(Semigroup.bigintAdditionSemigroup, "bigintAdditionSemigroup")) + include(SemigroupProperties(Semigroup.bigintMultiplicationSemigroup, "bigintMultiplicationSemigroup")) + include(SemigroupProperties(Semigroup.bigintMaximumSemigroup, "bigintMaximumSemigroup")) + include(SemigroupProperties(Semigroup.bigintMinimumSemigroup, "bigintMinimumSemigroup")) + + include(SemigroupProperties(Semigroup.conjunctionSemigroup, "conjunctionSemigroup")) + include(SemigroupProperties(Semigroup.disjunctionSemigroup, "disjunctionSemigroup")) + include(SemigroupProperties(Semigroup.exclusiveDisjunctionSemiGroup, "exclusiveDisjunctionSemiGroup")) + + include(SemigroupProperties(Semigroup.firstOptionSemigroup[Int](), "firstOptionSemigroup()")) + include(SemigroupProperties(Semigroup.firstSemigroup[Int](), "firstSemigroup()")) + + include(SemigroupProperties(Semigroup.intAdditionSemigroup, "intAdditionSemigroup")) + include(SemigroupProperties(Semigroup.intMultiplicationSemigroup, "intMultiplicationSemigroup")) + include(SemigroupProperties(Semigroup.intMaximumSemigroup, "intMaximumSemigroup")) + include(SemigroupProperties(Semigroup.intMinimumSemigroup, "intMinimumSemigroup")) + + include(CheckIOSemigroup) + + include(SemigroupProperties(Semigroup.lastOptionSemigroup[Int](), "lastOptionSemigroup()")) + include(SemigroupProperties(Semigroup.lastSemigroup[Int](), "lastSemigroup()")) + + include(SemigroupProperties(Semigroup.longAdditionSemigroup, "longAdditionSemigroup")) + include(SemigroupProperties(Semigroup.longMultiplicationSemigroup, "longMultiplicationSemigroup")) + include(SemigroupProperties(Semigroup.longMaximumSemigroup, "longMaximumSemigroup")) + include(SemigroupProperties(Semigroup.longMinimumSemigroup, "longMinimumSemigroup")) + + + include(SemigroupProperties(Semigroup.listSemigroup[Int], "listSemigroup")) + + include(SemigroupProperties(Semigroup.naturalAdditionSemigroup, "naturalAdditionSemigroup")) + include(SemigroupProperties(Semigroup.naturalMaximumSemigroup, "naturalMaximumSemigroup")) + include(SemigroupProperties(Semigroup.naturalMinimumSemigroup, "naturalMinimumSemigroup")) + include(SemigroupProperties(Semigroup.naturalMultiplicationSemigroup, "naturalMultiplicationSemigroup")) + + include(SemigroupProperties(Semigroup.nonEmptyListSemigroup[Int], "nonEmptyListSemigroup")) + + include(SemigroupProperties(Semigroup.p1Semigroup(Semigroup.intAdditionSemigroup), "p1Semigroup(Semigroup)")) + include(SemigroupProperties(Semigroup.p2Semigroup(Semigroup.intAdditionSemigroup, Semigroup.stringSemigroup), "p2Semigroup(Semigroup,Semigroup)")) + + include(SemigroupProperties(Semigroup.semigroup[Int](new F2[Int, Int, Int] { + def f(x: Int, y: Int): Int = x + y + }), "semigroup(F)")) + + include(SemigroupProperties(Semigroup.semigroup[Int](new F[Int, F[Int, Int]] { + def f(x: Int): F[Int, Int] = (y: Int) => x + y + }), "semigroup(F>)")) + + include(SemigroupProperties(Semigroup.setSemigroup[Int](), "setSemigroup()")) + include(SemigroupProperties(Semigroup.streamSemigroup[Int](), "streamSemigroup")) + + include(SemigroupProperties(Semigroup.stringSemigroup, "stringSemigroup")) + include(CheckMutableBuilder(Semigroup.stringBufferSemigroup, "stringBufferSemigroup", (s: String) => new java.lang.StringBuffer(s))) + include(CheckMutableBuilder(Semigroup.stringBuilderSemigroup, "stringBuilderSemigroup", (s: String) => new java.lang.StringBuilder(s))) + + + include(SemigroupProperties(Semigroup.unitSemigroup, "unitSemigroup")) + + +} diff --git a/props-core-scalacheck/src/test/scala/fj/Tests.scala b/props-core-scalacheck/src/test/scala/fj/Tests.scala index 60c2f009..c5ec3977 100644 --- a/props-core-scalacheck/src/test/scala/fj/Tests.scala +++ b/props-core-scalacheck/src/test/scala/fj/Tests.scala @@ -1,8 +1,9 @@ package fj object Tests { - def tests = List ( + def tests = List( CheckP2.properties, + CheckSemigroup.properties, fj.data.CheckArray.properties, fj.data.CheckIO.properties, fj.data.CheckIteratee.properties, @@ -17,28 +18,29 @@ 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) + // System.exit(0) } - import org.scalacheck.Prop - import org.scalacheck.Test import org.scalacheck.Test.check + import org.scalacheck.{Prop, Test} def run(tests: List[(String, Prop)]) = tests foreach { case (name, p) => { - val c = check(new Test.Parameters.Default { override val maxSize = 20 }, p) - c.status match { - case Test.Passed => println("Passed " + name) - case Test.Proved(_) => println("Proved " + name) - case f @ Test.Failed(_, _) => sys.error(name + ": " + f) - case Test.Exhausted => println("Exhausted " + name) - case f @ Test.PropException(_, e, _) => { - e.printStackTrace - sys.error(name + ": " + f) - } + val c = check(new Test.Parameters.Default { + override val maxSize = 20 + }, p) + c.status match { + case Test.Passed => println("Passed " + name) + case Test.Proved(_) => println("Proved " + name) + case f@Test.Failed(_, _) => sys.error(name + ": " + f) + case Test.Exhausted => println("Exhausted " + name) + case f@Test.PropException(_, e, _) => { + e.printStackTrace + sys.error(name + ": " + f) } } } + } } 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 65fa83fc..605546ef 100644 --- a/props-core/src/test/java/fj/MemoisationTest.java +++ b/props-core/src/test/java/fj/MemoisationTest.java @@ -6,14 +6,12 @@ import org.junit.runner.RunWith; import static fj.test.Arbitrary.arbInteger; +import static fj.test.Arbitrary.arbString; import static fj.test.CheckResult.summary; import static fj.test.Property.prop; 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 { @@ -25,16 +23,16 @@ public Property test1() { } public Property test1_hardMemo() { - return property(arbInteger, a -> { - P1 t = P.hardMemo(() -> new Integer(a)); + return property(arbString, a -> { + P1 t = P.hardMemo(() -> new String(a)); return prop(t._1() == t._1()).and(prop(t._1().equals(a))); }); } @Test public Property test2() { - return property(arbInteger, arbInteger, (a, b) -> { - P2 t = P.lazy(u -> new Integer(a), u -> new Integer(b)).memo(); + return property(arbString, arbString, (a, b) -> { + P2 t = P.lazy(u -> new String(a), u -> new String(b)).memo(); return prop(t._1().equals(t._1()) && t._1().equals(a) && t._2().equals(t._2()) && t._2().equals(b) ); }); } 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 8494336e..48352620 100644 --- a/props-core/src/test/java/fj/data/ReaderTest.java +++ b/props-core/src/test/java/fj/data/ReaderTest.java @@ -5,17 +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.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertTrue; -/** - * Created by MarkPerry on 4/12/2014. - */ + public class ReaderTest { @Test @@ -24,7 +22,6 @@ public void testMap() { // example taken from http://learnyouahaskell.com/for-a-few-monads-more int x = Reader.unit((Integer i) -> i + 3).map(i -> i * 5).f(8); assertTrue(x == 55); -// System.out.println(x); // 55 } @Test @@ -43,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)); }); @@ -58,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)); } @@ -108,6 +105,19 @@ public void testAssociativity() { PropertyAssert.assertResult(p); } + @Test + public void testAndThen() { + final int y = Reader.unit((Integer i) -> i * 2).andThen(i -> i + 10).f(10); + assertThat(y, is(30)); + } + + @Test + public void testBind() { + final int y = Reader.unit((Integer i) -> i * 2) + .bind(a -> Reader.unit(i -> a + i + 11)).f(10); + assertThat(y, is(41)); + } + public Gen> arbReader() { return Arbitrary.arbReader(cogenInteger, arbInteger); } 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 5f0ac122..50134da3 100644 --- a/props-core/src/test/java/fj/data/WriterTest.java +++ b/props-core/src/test/java/fj/data/WriterTest.java @@ -2,8 +2,7 @@ import fj.Equal; import fj.F; -import fj.data.test.PropertyAssert; -import fj.test.Arbitrary; +import fj.P; import fj.test.Gen; import fj.test.Property; import org.junit.Assert; @@ -14,11 +13,9 @@ import static fj.test.Cogen.cogenInteger; import static fj.test.Property.prop; import static fj.test.Property.property; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; -/** - * Created by MarkPerry on 17/12/2014. - */ public class WriterTest { @Test @@ -107,7 +104,13 @@ public void testAssociativity() { assertResult(p); } - + @Test + public void testUnit() { + Writer w = Writer.unit("+").tell("foo").tell("bar"); + assertThat(w.run(), is(P.p("foobar", "+"))); + assertThat(w.log(), is("foobar")); + assertThat(w.value(), is("+")); + } } 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 2a64b44b..8cc94825 100644 --- a/props-core/src/test/java/fj/data/fingertrees/FingerTreeTest.java +++ b/props-core/src/test/java/fj/data/fingertrees/FingerTreeTest.java @@ -1,41 +1,56 @@ package fj.data.fingertrees; +import fj.Function; import fj.P; import fj.P2; -import fj.Show; import fj.data.List; -import fj.data.Stream; +import fj.data.Option; import org.junit.Test; -import static fj.P.p; -import static fj.Show.intShow; -import static fj.test.Property.prop; -import static fj.test.Property.property; -import static java.lang.System.out; +import static fj.Monoid.intAdditionMonoid; +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; @Test public void size() { - validateSize(List.list(-92, 68, 54, -77, -18, 67)); - validateSize(List.list(-92, 68, 54, -77, -18, 67, -60, 23, -70, 99, 66, -79, -5)); + validateOperations(List.list(-92, 68, 54, -77, -18, 67)); + validateOperations(List.list(-92, 68, 54, -77, -18, 67, -60, 23, -70, 99, 66, -79, -5)); } - void validateSize(List list) { + void validateOperations(List list) { FingerTree ft = list.foldLeft( (acc, i) -> acc.snoc(i), FingerTree.emptyIntAddition() ); assertThat(ft.measure(), equalTo(list.length())); + assertThat(ft.foldLeft((s, i) -> s + 1, 0), equalTo(list.length())); + assertThat(ft.foldRight((i, s) -> 1 + s, 0), equalTo(list.length())); + assertThat(ft.filter(e -> e.equals(-77)).head(), equalTo(-77)); assertThat(ft.length(), equalTo(list.length())); } + @Test + public void testHeadOption() { + assertThat(Empty.emptyIntAddition().headOption(), is(Option.none())); + FingerTree ft = new MakeTree(measured(intAdditionMonoid, Function.constant(1))) + .single(1); + assertThat(ft.headOption(), is(Option.some(1))); + } + + @Test + public void testUncons() { + assertThat(Empty.emptyIntAddition().uncons(0, (h, t) -> h), is(0)); + FingerTree ft = new MakeTree(measured(intAdditionMonoid, Function.constant(1))) + .single(1); + assertThat(ft.uncons(0, (h, t) -> h), is(1)); + } + public FingerTree midSeq() { FingerTree ft = FingerTree.emptyIntAddition(); return List.range(1, SIZE).foldLeft(ft2 -> i -> ft2.snoc(i), ft); 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 4d3a25be..c9851596 100644 --- a/quickcheck/build.gradle +++ b/quickcheck/build.gradle @@ -1,18 +1,15 @@ ext { signModule = true + uploadModule = true } -configureAllRetroLambda() - archivesBaseName = "${project.projectName}-${project.name}" 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 df922af2..04f1e20b 100644 --- a/quickcheck/src/main/java/fj/test/Arbitrary.java +++ b/quickcheck/src/main/java/fj/test/Arbitrary.java @@ -1,8 +1,6 @@ package fj.test; -import fj.Equal; import fj.F; -import fj.F1Functions; import fj.F2; import fj.F3; import fj.F4; @@ -13,7 +11,6 @@ import fj.Function; import fj.Bottom; -import static fj.Equal.longEqual; import static fj.Function.compose; import static fj.P.p; @@ -26,26 +23,21 @@ 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; -import static fj.data.Either.left; -import static fj.data.Either.right; import static fj.data.Enumerator.charEnumerator; import static fj.data.List.asString; import static fj.data.List.list; -import static fj.data.Option.some; import fj.data.List; import fj.data.Set; -import fj.data.TreeMap; -import fj.function.Booleans; -import fj.function.Effect1; -import fj.function.Longs; import static fj.data.Stream.range; -import static fj.function.Booleans.not; import static fj.test.Gen.choose; import static fj.test.Gen.elements; import static fj.test.Gen.fail; @@ -76,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. @@ -95,11 +90,7 @@ public final class Arbitrary { * @return An arbitrary for functions. */ public static Gen> arbF(final Cogen c, final Gen a) { - return promote(new F>() { - public Gen f(final A x) { - return c.cogen(x, a); - } - }); + return promote(x -> c.cogen(x, a)); } public static Gen> arbReader(Cogen aa, Gen ab) { @@ -113,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. */ @@ -276,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()))))))); } /** @@ -317,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())))))))); } /** @@ -360,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()))))))))); } /** @@ -389,19 +398,14 @@ public static Gen> arbF4Invariant(final Gen * max, min, max - 1, min + 1) with a frequency of 1% each then generates from {@link * #arbInteger} the remainder of the time (93%). */ - public static final Gen arbIntegerBoundaries = sized(new F>() { - @SuppressWarnings("unchecked") - public Gen f(final Integer i) { - return frequency(list(p(1, value(0)), - p(1, value(1)), - p(1, value(-1)), - p(1, value(Integer.MAX_VALUE)), - p(1, value(Integer.MIN_VALUE)), - p(1, value(Integer.MAX_VALUE - 1)), - p(1, value(Integer.MIN_VALUE + 1)), - p(93, arbInteger))); - } - }); + public static final Gen arbIntegerBoundaries = sized(i -> frequency(list(p(1, value(0)), + p(1, value(1)), + p(1, value(-1)), + p(1, value(Integer.MAX_VALUE)), + p(1, value(Integer.MIN_VALUE)), + p(1, value(Integer.MAX_VALUE - 1)), + p(1, value(Integer.MIN_VALUE + 1)), + p(93, arbInteger)))); /** * An arbitrary implementation for long values. @@ -414,19 +418,14 @@ public Gen f(final Integer i) { * min, max - 1, min + 1) with a frequency of 1% each then generates from {@link #arbLong} * the remainder of the time (93%). */ - public static final Gen arbLongBoundaries = sized(new F>() { - @SuppressWarnings("unchecked") - public Gen f(final Integer i) { - return frequency(list(p(1, value(0L)), - p(1, value(1L)), - p(1, value(-1L)), - p(1, value(Long.MAX_VALUE)), - p(1, value(Long.MIN_VALUE)), - p(1, value(Long.MAX_VALUE - 1L)), - p(1, value(Long.MIN_VALUE + 1L)), - p(93, arbLong))); - } - }); + public static final Gen arbLongBoundaries = sized(i -> frequency(list(p(1, value(0L)), + p(1, value(1L)), + p(1, value(-1L)), + p(1, value(Long.MAX_VALUE)), + p(1, value(Long.MIN_VALUE)), + p(1, value(Long.MAX_VALUE - 1L)), + p(1, value(Long.MIN_VALUE + 1L)), + p(93, arbLong)))); /** * An arbitrary implementation for byte values. @@ -438,19 +437,14 @@ public Gen f(final Integer i) { * min, max - 1, min + 1) with a frequency of 1% each then generates from {@link #arbByte} * the remainder of the time (93%). */ - public static final Gen arbByteBoundaries = sized(new F>() { - @SuppressWarnings("unchecked") - public Gen f(final Integer i) { - return frequency(list(p(1, value((byte) 0)), - p(1, value((byte) 1)), - p(1, value((byte) -1)), - p(1, value(Byte.MAX_VALUE)), - p(1, value(Byte.MIN_VALUE)), - p(1, value((byte) (Byte.MAX_VALUE - 1))), - p(1, value((byte) (Byte.MIN_VALUE + 1))), - p(93, arbByte))); - } - }); + public static final Gen arbByteBoundaries = sized(i -> frequency(list(p(1, value((byte) 0)), + p(1, value((byte) 1)), + p(1, value((byte) -1)), + p(1, value(Byte.MAX_VALUE)), + p(1, value(Byte.MIN_VALUE)), + p(1, value((byte) (Byte.MAX_VALUE - 1))), + p(1, value((byte) (Byte.MIN_VALUE + 1))), + p(93, arbByte)))); /** * An arbitrary implementation for short values. @@ -462,19 +456,14 @@ public Gen f(final Integer i) { * min, max - 1, min + 1) with a frequency of 1% each then generates from {@link #arbShort} * the remainder of the time (93%). */ - public static final Gen arbShortBoundaries = sized(new F>() { - @SuppressWarnings("unchecked") - public Gen f(final Integer i) { - return frequency(list(p(1, value((short) 0)), - p(1, value((short) 1)), - p(1, value((short) -1)), - p(1, value(Short.MAX_VALUE)), - p(1, value(Short.MIN_VALUE)), - p(1, value((short) (Short.MAX_VALUE - 1))), - p(1, value((short) (Short.MIN_VALUE + 1))), - p(93, arbShort))); - } - }); + public static final Gen arbShortBoundaries = sized(i -> frequency(list(p(1, value((short) 0)), + p(1, value((short) 1)), + p(1, value((short) -1)), + p(1, value(Short.MAX_VALUE)), + p(1, value(Short.MIN_VALUE)), + p(1, value((short) (Short.MAX_VALUE - 1))), + p(1, value((short) (Short.MIN_VALUE + 1))), + p(93, arbShort)))); /** * An arbitrary implementation for character values. @@ -486,16 +475,11 @@ public Gen f(final Integer i) { * max - 1, min + 1) with a frequency of 1% each then generates from {@link #arbCharacter} * the remainder of the time (96%). */ - public static final Gen arbCharacterBoundaries = sized(new F>() { - @SuppressWarnings("unchecked") - public Gen f(final Integer i) { - return frequency(list(p(1, value(Character.MIN_VALUE)), - p(1, value((char) (Character.MIN_VALUE + 1))), - p(1, value(Character.MAX_VALUE)), - p(1, value((char) (Character.MAX_VALUE - 1))), - p(95, arbCharacter))); - } - }); + public static final Gen arbCharacterBoundaries = sized(i -> frequency(list(p(1, value(Character.MIN_VALUE)), + p(1, value((char) (Character.MIN_VALUE + 1))), + p(1, value(Character.MAX_VALUE)), + p(1, value((char) (Character.MAX_VALUE - 1))), + p(95, arbCharacter)))); /** * An arbitrary implementation for double values. @@ -507,21 +491,16 @@ public Gen f(final Integer i) { * min, min (normal), NaN, -infinity, infinity, max - 1) with a frequency of 1% each then * generates from {@link #arbDouble} the remainder of the time (91%). */ - public static final Gen arbDoubleBoundaries = sized(new F>() { - @SuppressWarnings("unchecked") - public Gen f(final Integer i) { - return frequency(list(p(1, value(0D)), - p(1, value(1D)), - p(1, value(-1D)), - p(1, value(Double.MAX_VALUE)), - p(1, value(Double.MIN_VALUE)), - p(1, value(Double.NaN)), - p(1, value(Double.NEGATIVE_INFINITY)), - p(1, value(Double.POSITIVE_INFINITY)), - p(1, value(Double.MAX_VALUE - 1D)), - p(91, arbDouble))); - } - }); + public static final Gen arbDoubleBoundaries = sized(i -> frequency(list(p(1, value(0D)), + p(1, value(1D)), + p(1, value(-1D)), + p(1, value(Double.MAX_VALUE)), + p(1, value(Double.MIN_VALUE)), + p(1, value(Double.NaN)), + p(1, value(Double.NEGATIVE_INFINITY)), + p(1, value(Double.POSITIVE_INFINITY)), + p(1, value(Double.MAX_VALUE - 1D)), + p(91, arbDouble)))); /** * An arbitrary implementation for float values. @@ -533,21 +512,16 @@ public Gen f(final Integer i) { * min, NaN, -infinity, infinity, max - 1) with a frequency of 1% each then generates from * {@link #arbFloat} the remainder of the time (91%). */ - public static final Gen arbFloatBoundaries = sized(new F>() { - @SuppressWarnings("unchecked") - public Gen f(final Integer i) { - return frequency(list(p(1, value(0F)), - p(1, value(1F)), - p(1, value(-1F)), - p(1, value(Float.MAX_VALUE)), - p(1, value(Float.MIN_VALUE)), - p(1, value(Float.NaN)), - p(1, value(Float.NEGATIVE_INFINITY)), - p(1, value(Float.POSITIVE_INFINITY)), - p(1, value(Float.MAX_VALUE - 1F)), - p(91, arbFloat))); - } - }); + public static final Gen arbFloatBoundaries = sized(i -> frequency(list(p(1, value(0F)), + p(1, value(1F)), + p(1, value(-1F)), + p(1, value(Float.MAX_VALUE)), + p(1, value(Float.MIN_VALUE)), + p(1, value(Float.NaN)), + p(1, value(Float.NEGATIVE_INFINITY)), + p(1, value(Float.POSITIVE_INFINITY)), + p(1, value(Float.MAX_VALUE - 1F)), + p(91, arbFloat)))); /** * An arbitrary implementation for string values. @@ -670,7 +644,7 @@ public static Gen> arbNonEmptyList(final Gen aa) { * Returns an arbitrary Validation for the given arbitrary parameters. */ public static Gen> arbValidation(final Gen aa, final Gen ab) { - return arbBoolean.bind(bool -> bool ? ab.map(Validation::success) : aa.map(Validation::fail)); + return arbBoolean.bind(bool -> bool ? ab.map(Validation::success) : aa.map(Validation::fail)); } /** @@ -690,7 +664,7 @@ public static Gen> arbStream(final Gen aa) { * @return An arbitrary implementation for arrays. */ public static Gen> arbArray(final Gen aa) { - return arbList(aa).map(List::toArray); + return arbList(aa).map(List::toArray); } /** @@ -1029,8 +1003,8 @@ public static Gen> arbWeakHashMap(final Gen ak, fina */ public static Gen> arbArrayBlockingQueue(final Gen aa) { return arbArray(aa).bind(arbInteger, arbBoolean, - a -> capacity -> fair -> new ArrayBlockingQueue(a.length() + abs(capacity), - fair, a.asJavaList())); + a -> capacity -> fair -> new ArrayBlockingQueue<>(a.length() + abs(capacity), + fair, a.asJavaList())); } /** diff --git a/quickcheck/src/main/java/fj/test/Arg.java b/quickcheck/src/main/java/fj/test/Arg.java index 4187c15d..71df9e2d 100644 --- a/quickcheck/src/main/java/fj/test/Arg.java +++ b/quickcheck/src/main/java/fj/test/Arg.java @@ -1,6 +1,5 @@ package fj.test; -import fj.F; import fj.Show; import static fj.Show.anyShow; @@ -52,10 +51,6 @@ public int shrinks() { /** * The rendering of an argument (uses {@link Object#toString()} for the argument value). */ - public static final Show> argShow = showS(new F, String>() { - public String f(final Arg arg) { - return anyShow().showS(arg.value) + - (arg.shrinks > 0 ? " (" + arg.shrinks + " shrink" + (arg.shrinks == 1 ? "" : 's') + ')' : ""); - } - }); + public static final Show> argShow = showS(arg -> anyShow().showS(arg.value) + + (arg.shrinks > 0 ? " (" + arg.shrinks + " shrink" + (arg.shrinks == 1 ? "" : 's') + ')' : "")); } diff --git a/quickcheck/src/main/java/fj/test/CheckResult.java b/quickcheck/src/main/java/fj/test/CheckResult.java index c9c0a2fe..784fe4e9 100644 --- a/quickcheck/src/main/java/fj/test/CheckResult.java +++ b/quickcheck/src/main/java/fj/test/CheckResult.java @@ -286,16 +286,14 @@ else if (r.isPropException()) { * the result is a failure (falsified, property exception or generator exception). */ public static Show summaryEx(final Show> sa) { - return showS(new F() { - public String f(final CheckResult r) { - final String s = summary(sa).showS(r); - if (r.isProven() || r.isPassed() || r.isExhausted()) - return s; - else if (r.isFalsified() || r.isPropException() || r.isGenException()) - throw new Error(s); - else - throw decons(r.getClass()); - } + return showS(r -> { + final String s = summary(sa).showS(r); + if (r.isProven() || r.isPassed() || r.isExhausted()) + return s; + else if (r.isFalsified() || r.isPropException() || r.isGenException()) + throw new Error(s); + else + throw decons(r.getClass()); }); } } diff --git a/quickcheck/src/main/java/fj/test/Cogen.java b/quickcheck/src/main/java/fj/test/Cogen.java index c4c3b0a0..5770ea21 100644 --- a/quickcheck/src/main/java/fj/test/Cogen.java +++ b/quickcheck/src/main/java/fj/test/Cogen.java @@ -7,7 +7,6 @@ import fj.data.*; -import static fj.data.Array.array; import static fj.data.Array.iterableArray; import static fj.data.List.fromString; import static fj.data.List.nil; @@ -501,11 +500,7 @@ public Gen cogen(final Array as, final Gen g) { * @return A cogen for throwables. */ public static Cogen cogenThrowable(final Cogen cs) { - return cs.contramap(new F() { - public String f(final Throwable t) { - return t.getMessage(); - } - }); + return cs.contramap(Throwable::getMessage); } /** diff --git a/quickcheck/src/main/java/fj/test/Gen.java b/quickcheck/src/main/java/fj/test/Gen.java index 5b0a98bc..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; @@ -16,7 +18,6 @@ import static fj.Function.flip; import static fj.Monoid.intAdditionMonoid; import static fj.Ord.intOrd; -import static fj.P.lazy; import static fj.P2.__1; import static fj.control.Trampoline.pure; import static fj.control.Trampoline.suspend; @@ -34,7 +35,7 @@ * allowing various forms of composition of generators.

    A user typically creates an {@link * Arbitrary arbitrary} to return a generator using the 'combinator methods' below. For example, * suppose a class Person: -

    +
    {@code
     class Person {
       final int age;
       final String name;
    @@ -46,23 +47,23 @@ class Person {
         this.male = male;
       }
     }
    -
    +}
    *

    In a case like this one, a user may create a generator over Person by * invoking the {@link #bind(F)} methods — in this case, {@link #bind(Gen , Gen , F)} the one * that takes two generator arguments}, since the class has one more than two fields (the bind * method is invoked on a generator adding the extra one to the count as they are composed). The * class fields are of types for which there exist generators (on {@link Gen} so those can be * used to compose a generator for Person:

    -
    +
    {@code
     static Gen<Person> personArbitrary() {
       return arbInteger.bind(arbString(), arbBoolean(),
           // compose the generators
           {int age => {String name => {boolean male => new Person(age, name, male)}}};
     }
    -
    +}
    *

    * The example above uses Java 7 closure syntax. Here is the same example using objects instead: -

    +
    {@code
     static Gen<Person> personArbitrary() {
       return arbInteger.bind(arbString, arbBoolean,
           // compose the generators
    @@ -80,7 +81,7 @@ public Person f(final Boolean male) {
             }
           });
     }
    -
    +}
    * * @version %build.number% */ @@ -275,7 +276,7 @@ public Gen bind(final Gen gb, final Gen gc, final Gen g * @return A new generator after function application. */ public Gen apply(final Gen> gf) { - return gf.bind(f1 -> map(f1)); + return gf.bind(this::map); } /** @@ -319,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. @@ -498,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. @@ -510,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. @@ -551,7 +559,7 @@ final class Tramp { // Picks elements in constant stack space private Trampoline> tramp(List remainAs, int remainN, int remainALength) { - return suspend(lazy(() -> + return suspend(() -> (remainN == 0) ? // We have picked N elements; stop pure(nil()) : @@ -559,7 +567,7 @@ private Trampoline> tramp(List remainAs, int remainN, int remainALeng (r.choose(0, remainALength - 1) < remainN) ? tramp(remainAs.tail(), remainN - 1, remainALength - 1) .map(pickedTail -> cons(remainAs.head(), pickedTail)) : - tramp(remainAs.tail(), remainN, remainALength - 1))); + tramp(remainAs.tail(), remainN, remainALength - 1)); } } @@ -652,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/Property.java b/quickcheck/src/main/java/fj/test/Property.java index 0f77f85e..2a327ebc 100644 --- a/quickcheck/src/main/java/fj/test/Property.java +++ b/quickcheck/src/main/java/fj/test/Property.java @@ -501,11 +501,7 @@ public static Property property(final Gen aa, final F f) { * application of its arguments. */ public static Property propertyP(final Gen aa, final Gen ab, final Shrink sa, final Shrink sb, final F>> f) { - return property(aa, sa, a -> { - return propertyP(ab, sb, b -> { - return f.f(a).f(b); - }); - }); + return property(aa, sa, a -> propertyP(ab, sb, b -> f.f(a).f(b))); } /** @@ -633,9 +629,7 @@ public static Property property(final Gen aa, final Shrink sb, final Shrink sc, final F>> f) { - return property(aa, ab, sa, sb, a -> b -> property(ac, sc, c -> { - return f.f(a).f(b).f(c); - })); + return property(aa, ab, sa, sb, a -> b -> property(ac, sc, c -> f.f(a).f(b).f(c))); } /** @@ -723,9 +717,7 @@ public static Property property(final Gen aa, final Shrink sc, final Shrink sd, final F>>> f) { - return property(aa, ab, ac, sa, sb, sc, a -> b -> c -> property(ad, sd, d -> { - return f.f(a).f(b).f(c).f(d); - })); + return property(aa, ab, ac, sa, sb, sc, a -> b -> c -> property(ad, sd, d -> f.f(a).f(b).f(c).f(d))); } /** @@ -825,9 +817,7 @@ public static Property property(final Gen aa, final Shrink sd, final Shrink se, final F>>>> f) { - return property(aa, ab, ac, ad, sa, sb, sc, sd, a -> b -> c -> d -> property(ae, se, e -> { - return f.f(a).f(b).f(c).f(d).f(e); - })); + return property(aa, ab, ac, ad, sa, sb, sc, sd, a -> b -> c -> d -> property(ae, se, e -> f.f(a).f(b).f(c).f(d).f(e))); } /** @@ -939,9 +929,7 @@ public static Property property(final Gen aa, final Shrink se, final Shrink sf, final F>>>>> f) { - return property(aa, ab, ac, ad, ae, sa, sb, sc, sd, se, a -> b -> c -> d -> e -> property(af, sf, f$ -> { - return f.f(a).f(b).f(c).f(d).f(e).f(f$); - })); + return property(aa, ab, ac, ad, ae, sa, sb, sc, sd, se, a -> b -> c -> d -> e -> property(af, sf, f$ -> f.f(a).f(b).f(c).f(d).f(e).f(f$))); } /** @@ -1065,9 +1053,7 @@ public static Property property(final Gen aa, final Shrink sf, final Shrink sg, final F>>>>>> f) { - return property(aa, ab, ac, ad, ae, af, sa, sb, sc, sd, se, sf, a -> b -> c -> d -> e -> f$ -> property(ag, sg, g -> { - return f.f(a).f(b).f(c).f(d).f(e).f(f$).f(g); - })); + return property(aa, ab, ac, ad, ae, af, sa, sb, sc, sd, se, sf, a -> b -> c -> d -> e -> f$ -> property(ag, sg, g -> f.f(a).f(b).f(c).f(d).f(e).f(f$).f(g))); } /** @@ -1203,9 +1189,7 @@ public static Property property(final Gen aa, final Shrink sg, final Shrink sh, final F>>>>>>> f) { - return property(aa, ab, ac, ad, ae, af, ag, sa, sb, sc, sd, se, sf, sg, a -> b -> c -> d -> e -> f$ -> g -> property(ah, sh, h -> { - return f.f(a).f(b).f(c).f(d).f(e).f(f$).f(g).f(h); - })); + return property(aa, ab, ac, ad, ae, af, ag, sa, sb, sc, sd, se, sf, sg, a -> b -> c -> d -> e -> f$ -> g -> property(ah, sh, h -> f.f(a).f(b).f(c).f(d).f(e).f(f$).f(g).f(h))); } /** 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/main/java/fj/test/Shrink.java b/quickcheck/src/main/java/fj/test/Shrink.java index 77c95381..483293e8 100644 --- a/quickcheck/src/main/java/fj/test/Shrink.java +++ b/quickcheck/src/main/java/fj/test/Shrink.java @@ -23,7 +23,7 @@ import static fj.Primitive.Long_Integer; import static fj.Primitive.Long_Short; import static fj.Primitive.Short_Long; -import static fj.data.Array.array; + import fj.data.Conversions; import static fj.data.List.isNotEmpty_; import fj.data.Array; @@ -38,7 +38,6 @@ import static fj.data.Stream.iterate; import static fj.data.Stream.nil; -import static java.lang.System.arraycopy; import static java.math.BigInteger.ZERO; import java.math.BigDecimal; @@ -683,7 +682,7 @@ public static Shrink> shrinkSynchronousQueue(final Shrin final Stream is = cons(ZERO, () -> iterate(x -> x.divide(two), i) .takeWhile(x2 -> eq.notEq(x2, ZERO)) - .map(i::subtract)); + .map(x2 -> i.subtract(x2))); return Ord.bigintOrd.isLessThan(i, ZERO) ? cons(i.negate(), () -> is) : is; } diff --git a/quickcheck/src/main/java/fj/test/reflect/Check.java b/quickcheck/src/main/java/fj/test/reflect/Check.java index eb865e04..a1eea79b 100644 --- a/quickcheck/src/main/java/fj/test/reflect/Check.java +++ b/quickcheck/src/main/java/fj/test/reflect/Check.java @@ -1,6 +1,5 @@ package fj.test.reflect; -import fj.Class; import static fj.Class.clas; import fj.F; import fj.Function; @@ -24,8 +23,7 @@ import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; + import static java.lang.reflect.Modifier.isStatic; /** @@ -177,7 +175,7 @@ public static List> check(final java.lang.Class c */ public static List>> properties(final java.lang.Class c, final String... categories) { //noinspection ClassEscapesDefinedScope - final Array>> propFields = properties(array(c.getDeclaredFields()).map((F) f -> new PropertyMember() { + final Array>> propFields = properties(array(c.getDeclaredFields()).map(f -> new PropertyMember() { public java.lang.Class type() { return f.getType(); } @@ -205,7 +203,7 @@ public boolean isProperty() { }), c, categories); //noinspection ClassEscapesDefinedScope - final Array>> propMethods = properties(array(c.getDeclaredMethods()).map((F) m -> { + final Array>> propMethods = properties(array(c.getDeclaredMethods()).map(m -> { //noinspection ProhibitedExceptionDeclared return new PropertyMember() { public java.lang.Class type() { diff --git a/quickcheck/src/main/java/fj/test/reflect/CheckParams.java b/quickcheck/src/main/java/fj/test/reflect/CheckParams.java index 8c8445c1..7951b40e 100644 --- a/quickcheck/src/main/java/fj/test/reflect/CheckParams.java +++ b/quickcheck/src/main/java/fj/test/reflect/CheckParams.java @@ -1,5 +1,6 @@ package fj.test.reflect; +import fj.F0; import fj.test.Property; import java.lang.annotation.Documented; diff --git a/quickcheck/src/main/java/fj/test/reflect/Main.java b/quickcheck/src/main/java/fj/test/reflect/Main.java index 5f6f3dfc..b07997c2 100644 --- a/quickcheck/src/main/java/fj/test/reflect/Main.java +++ b/quickcheck/src/main/java/fj/test/reflect/Main.java @@ -1,10 +1,7 @@ package fj.test.reflect; -import fj.P2; import static fj.data.Array.array; -import fj.function.Effect1; -import fj.test.CheckResult; import static fj.test.CheckResult.summary; import static fj.test.reflect.Check.check; diff --git a/quickcheck/src/main/java/fj/test/runner/PropertyTestRunner.java b/quickcheck/src/main/java/fj/test/runner/PropertyTestRunner.java index 876dbff1..8b90d57c 100644 --- a/quickcheck/src/main/java/fj/test/runner/PropertyTestRunner.java +++ b/quickcheck/src/main/java/fj/test/runner/PropertyTestRunner.java @@ -58,11 +58,10 @@ private static String getLabel(Description d) { } private static CheckResult checkProperty(Property prop, Option params) { - for (CheckParams ps : params) { - return prop.check(ps.minSuccessful(), ps.maxDiscarded(), ps.minSize(), ps.maxSize()); - } - - return prop.check(); + return params.option( + prop::check, + ps -> prop.check(ps.minSuccessful(), ps.maxDiscarded(), ps.minSize(), ps.maxSize()) + ); } @Override diff --git a/quickcheck/src/test/java/fj/data/test/TestCheck.java b/quickcheck/src/test/java/fj/data/test/TestCheck.java index 935f1127..3a4e6a80 100644 --- a/quickcheck/src/test/java/fj/data/test/TestCheck.java +++ b/quickcheck/src/test/java/fj/data/test/TestCheck.java @@ -1,16 +1,10 @@ package fj.data.test; -import fj.F2; -import fj.F3; -import fj.data.List; -import fj.test.Arbitrary; import fj.test.CheckResult; import fj.test.Gen; import fj.test.Property; import org.junit.*; -import static fj.Function.compose; -import static fj.test.Arbitrary.arbLong; import static fj.test.Property.prop; import static fj.test.Property.property; import static org.junit.Assert.*; @@ -19,13 +13,11 @@ public class TestCheck { @Test(timeout=5000 /*ms*/) public void testExceptionsThrownFromGeneratorsArePropagated() { - Gen failingGen = Gen.value(0).map((i) -> { + Gen failingGen = Gen.value(0).map((i) -> { throw new RuntimeException("test failure"); }); - Property p = property(failingGen, (Integer i) -> { - return prop(i == 0); - }); + Property p = property(failingGen, (Integer i) -> prop(i == 0)); CheckResult res = p.check( 1, /*minSuccessful*/ diff --git a/quickcheck/src/test/java/fj/data/test/TestNull.java b/quickcheck/src/test/java/fj/data/test/TestNull.java index 0fd44c6a..0d547149 100644 --- a/quickcheck/src/test/java/fj/data/test/TestNull.java +++ b/quickcheck/src/test/java/fj/data/test/TestNull.java @@ -1,29 +1,18 @@ package fj.data.test; -import fj.F2; -import fj.F3; -import fj.data.List; -import fj.test.Arbitrary; import fj.test.CheckResult; import fj.test.Gen; import fj.test.Property; import org.junit.Test; -import static fj.Function.compose; -import static fj.test.Arbitrary.arbLong; import static fj.test.Property.prop; import static fj.test.Property.property; -/** - * Created by MarkPerry on 3/07/2014. - */ public class TestNull { @Test public void testShowNullParameters() { - Property p = property(Gen.value(null), (Integer i) -> { - return prop(i != null); - }); + Property p = property(Gen.value(null), (Integer i) -> prop(i != null)); CheckResult.summary.println(p.check()); } diff --git a/quickcheck/src/test/java/fj/test/GenTest.java b/quickcheck/src/test/java/fj/test/GenTest.java index 3bb723b8..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; @@ -22,9 +35,7 @@ public final class GenTest { @Test public void testCombinationOf_none() { Gen> instance = combinationOf(0, AS); - testPick(100, instance, actual -> { - assertTrue(actual.isEmpty()); - }); + testPick(100, instance, actual -> assertTrue(actual.isEmpty())); } @Test @@ -59,9 +70,7 @@ public void testCombinationOf_three() { @Test public void testSelectionOf_none() { Gen> instance = selectionOf(0, AS); - testPick(100, instance, actual -> { - assertTrue(actual.isEmpty()); - }); + testPick(100, instance, actual -> assertTrue(actual.isEmpty())); } @Test @@ -106,9 +115,7 @@ public void testSelectionOf_four() { @Test public void testPermutationOf_none() { Gen> instance = permutationOf(0, AS); - testPick(100, instance, actual -> { - assertTrue(actual.isEmpty()); - }); + testPick(100, instance, actual -> assertTrue(actual.isEmpty())); } @Test @@ -142,9 +149,7 @@ public void testPermutationOf_three() { @Test public void testWordOf_none() { Gen> instance = wordOf(0, AS); - testPick(100, instance, actual -> { - assertTrue(actual.isEmpty()); - }); + testPick(100, instance, actual -> assertTrue(actual.isEmpty())); } @Test @@ -183,8 +188,26 @@ 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::f); + 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"