/** * Returns an order instance that uses the given minimal equality test and ordering definition. * * @param def The order definition. * @return An order instance. */ public static <A> Ord<A> ordDef(final AltDefinition<A> def) { return new Ord<>(def); }
/** * Static version of {@link #contramap(F)} */ public static <A, B> Ord<A> contramap(final F<A, B> f, final Ord<B> ord) { return ordDef(contramapDef(f, ord.def)); }
public static <A> Ordering compareTreeNodes( Tree<PackageFileSystemObject<A>> a, Tree<PackageFileSystemObject<A>> b ) { return RelativePath.ord.compare( a.root().getUnixFsObject().path, b.root().getUnixFsObject().path ); }
/** * An order instance for the {@link Array} type. * * @param oa Order across the elements of the array. * @return An order instance for the {@link Array} type. */ public static <A> Ord<Array<A>> arrayOrd(final Ord<A> oa) { return ordDef((a1, a2) -> { int i = 0; //noinspection ForLoopWithMissingComponent for (; i < a1.length() && i < a2.length(); i++) { final Ordering c = oa.compare(a1.get(i), a2.get(i)); if (c == Ordering.GT || c == Ordering.LT) return c; } return i == a1.length() ? i == a2.length() ? Ordering.EQ : Ordering.LT : i == a1.length() ? Ordering.EQ : Ordering.GT; }); }
/** * Returns a stream using the given enumerator from the given value to the other given value stepping at the given * increment. * * @param e The enumerator to compute successors from. * @param from The value to begin computing successors from. * @param to The value to stop computing successors from. * @param step The increment to step. * @return A stream using the given enumerator from the given value to the other given value stepping at the given * increment. */ public static <A> Stream<A> range(final Enumerator<A> e, final A from, final A to, final long step) { final Ordering o = e.order().compare(from, to); return o == EQ || step > 0L && o == GT || step < 0L && o == LT ? single(from) : cons(from, () -> join(e.plus(from, step).filter(a -> !(o == LT ? e.order().isLessThan(to, a) : e.order().isGreaterThan(to, a))).map(a1 -> range(e, a1, to, step)).toStream())); } /**
/** * An order instance for the {@link Stream} type. * * @param oa Order across the elements of the stream. * @return An order instance for the {@link Stream} type. */ public static <A> Ord<Stream<A>> streamOrd(final Ord<A> oa) { return ordDef((s1, s2) -> { if (s1.isEmpty()) return s2.isEmpty() ? Ordering.EQ : Ordering.LT; else if (s2.isEmpty()) return s1.isEmpty() ? Ordering.EQ : Ordering.GT; else { final Ordering c = oa.compare(s1.head(), s2.head()); // FIXME: not stack safe return c == Ordering.EQ ? streamOrd(oa).def.compare(s1.tail()._1()).f(s2.tail()._1()) : c; } }); }
/** * Checks if the given element is a member of this set. * * @param x An element to check for membership in this set. * @return true if the given element is a member of this set. */ public final boolean member(final A x) { return !isEmpty() && (ord.isLessThan(x, head()) ? l().member(x) : ord.eq(head(), x) || r().member(x)); }
/** * An order instance for the {@link Validation} type. * * @param oa Order across the failing side of {@link Validation}. * @param ob Order across the succeeding side of {@link Validation}. * @return An order instance for the {@link Validation} type. */ public static <A, B> Ord<Validation<A, B>> validationOrd(final Ord<A> oa, final Ord<B> ob) { return eitherOrd(oa, ob).contramap(Validation.either()); }
/** * An order instance for the {@link NonEmptyList} type. * * @param oa Order across the elements of the non-empty list. * @return An order instance for the {@link NonEmptyList} type. */ public static <A> Ord<NonEmptyList<A>> nonEmptyListOrd(final Ord<A> oa) { return listOrd(oa).contramap(NonEmptyList.toList_()); }
/** * A mutable map projection of this tree map. * * @return A new mutable map isomorphic to this tree map. */ public Map<K, V> toMutableMap() { final F<K, P2<K, Option<V>>> fakePair = k -> p(k, Option.none()); final Comparator<K> comparator = tree.ord().contramap(fakePair).toComparator(); final Map<K, V> m = new java.util.TreeMap<>(comparator); for (final P2<K, V> e : this) { m.put(e._1(), e._2()); } return m; }
/** * An order instance for a product-1. * * @param oa Order across the produced type. * @return An order instance for a product-1. */ public static <A> Ord<P1<A>> p1Ord(final Ord<A> oa) { return oa.contramap(P1.__1()); }
public boolean isGreaterThan(Ord<K> ok, K k) { return top().option(false, p -> ok.isGreaterThan(p._1(), k)); }
/** * 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 <B> TreeMap<B, List<A>> groupBy(final F<A, B> keyFunction) { return groupBy(keyFunction, Ord.hashOrd()); }
/** * Begin definition of an ord instance. * @see Definition#then(F, Equal) */ public static <A, B> Definition<A> on(final F<A, B> f, final Ord<B> ord) { return contramapDef(f, ord.def); }
public boolean isEqual(Ord<K> ok, K k) { return top().option(false, p -> ok.eq(p._1(), k)); }
/** * Removes duplicates according to the given ordering. This function is O(n). * * @param o An ordering for the elements. * @return A list without duplicates. */ @SuppressWarnings("unchecked") public final List<A> nub(final Ord<A> o) { return sort(o).group(o.equal()).map(List.head_()); }
public static void main(String[] args) { List<Integer> naturals = naturals().take(20000).filter(i -> i > 10000).take(100); System.out.println(naturals.length()); List<Integer> naturals2 = naturals().take(100).map(i -> i - 1); Lists.listShow(Show.intShow).println(naturals); System.out.println(Lists.listEqual(Equal.intEqual).eq(naturals, naturals)); System.out.println(Lists.listOrd(Ord.intOrd).compare(naturals, naturals2)); }