
fj.data.vector.V3 Maven / Gradle / Ivy
package fj.data.vector;
import fj.*;
import static fj.Function.curry;
import static fj.P.p2;
import fj.data.Array;
import fj.data.NonEmptyList;
import fj.data.Stream;
import java.util.Iterator;
/**
* A vector-3.
*/
public final class V3 implements Iterable {
private final V2 tail;
private final P1 head;
private V3(final P1 head, final V2 tail) {
this.head = head;
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.
*
* @param p The product-3 from which to create a vector.
* @return A new vector-3.
*/
public static V3 p(final P3 p) {
return new V3<>(
P.lazy(p::_1),
V2.p(P.lazy(p::_2, p::_3))
);
}
/**
* Creates a vector-3 from a head and a tail.
*
* @param head The value to put as the first element of the vector.
* @param tail The vector representing all but the first element of the new vector.
* @return The new vector.
*/
public static V3 cons(final P1 head, final V2 tail) {
return new V3<>(head, tail);
}
/**
* Returns the first element of this vector.
*
* @return the first element of this vector.
*/
public A _1() {
return head._1();
}
/**
* Returns the second element of this vector.
*
* @return the second element of this vector.
*/
public A _2() {
return tail._1();
}
/**
* Returns the third element of this vector.
*
* @return the third element of this vector.
*/
public A _3() {
return tail._2();
}
/**
* Returns all but the first element of this vector, as a vector-2.
*
* @return all but the first element of this vector, as a vector-2.
*/
public V2 tail() {
return tail;
}
/**
* Returns the first element of this vector, as a product-1.
*
* @return the first element of this vector, as a product-1.
*/
public P1 head() {
return head;
}
/**
* Returns a homogeneous product-3 equivalent to this vector.
*
* @return a homogeneous product-3 equivalent to this vector.
*/
public P3 p() {
return new P3() {
public A _1() {
return V3.this._1();
}
public A _2() {
return V3.this._2();
}
public A _3() {
return V3.this._3();
}
};
}
/**
* Returns an array with the elements of this vector.
*
* @return an array with the elements of this vector.
*/
@SuppressWarnings("unchecked")
public Array toArray() {
return Array.array(_1(), _2(), _3());
}
/**
* Performs function application within a vector (applicative functor pattern).
*
* @param vf The vector of functions to apply.
* @return A new vector after zipping the given vector of functions over this vector.
*/
public V3 apply(final V3> vf) {
return new V3<>(head.apply(vf.head()), tail.apply(vf.tail()));
}
/**
* Zips this vector with the given vector using the given function to produce a new vector.
*
* @param bs The vector to zip this vector with.
* @param f The function to zip this vector and the given vector with.
* @return A new vector with the results of the function.
*/
public V3 zipWith(final F> f, final V3 bs) {
return bs.apply(map(f));
}
/**
* Zips this vector with the given vector to produce a vector of pairs.
*
* @param bs The vector to zip this vector with.
* @return A new vector with a length the same as the shortest of this vector and the given
* vector.
*/
public V3> zip(final V3 bs) {
final F>> __2 = p2();
return zipWith(__2, bs);
}
/**
* Zips this vector with the given vector to produce a vector of vectors.
*
* @param bs The vector to zip this vector with.
* @return A new vector of vectors.
*/
public V3> vzip(final V3 bs) {
final F2> __2 = V.v2();
return zipWith(curry(__2), bs);
}
/**
* Returns an iterator for the elements of this vector.
*
* @return an iterator for the elements of this vector.
*/
public Iterator iterator() {
return toStream().iterator();
}
/**
* Returns a nonempty list with the elements of this vector.
*
* @return a nonempty list with the elements of this vector.
*/
public NonEmptyList toNonEmptyList() {
return NonEmptyList.nel(head()._1(), tail().toNonEmptyList().toList());
}
/**
* Returns a stream of the elements of this vector.
*
* @return a stream of the elements of this vector.
*/
public Stream toStream() {
return Stream.cons(head()._1(), () -> tail().toStream());
}
/**
* Maps the given function across this vector.
*
* @param f The function to map across this vector.
* @return A new vector after the given function has been applied to each element.
*/
public V3 map(final F f) {
return new V3<>(head().map(f), tail().map(f));
}
/**
* Returns a function that transforms a vector-3 to a stream of its elements.
*
* @return a function that transforms a vector-3 to a stream of its elements.
*/
public static F, Stream> toStream_() {
return V3::toStream;
}
/**
* Returns a function that transforms a vector-3 to the equivalent product-3.
*
* @return a function that transforms a vector-3 to the equivalent product-3.
*/
public static F, P3> p_() {
return V3::p;
}
/**
* A first-class function to get the first element of a vector.
*
* @return a function that gets the first element of a given vector.
*/
public static F, A> __1() {
return V3::_1;
}
/**
* A first-class function to get the second element of a vector.
*
* @return a function that gets the second element of a given vector.
*/
public static F, A> __2() {
return V3::_2;
}
/**
* A first-class function to get the third element of a vector.
*
* @return a function that gets the third element of a given vector.
*/
public static F, A> __3() {
return V3::_3;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy