io.vavr.collection.Vector类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(155)

本文整理了Java中io.vavr.collection.Vector类的一些代码示例,展示了Vector类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vector类的具体详情如下:
包路径:io.vavr.collection.Vector
类名称:Vector

Vector介绍

[英]Vector is the default Seq implementation that provides effectively constant time access to any element. Many other operations (e.g. tail, drop, slice) are also effectively constant. The implementation is based on a bit-mapped trie, a very wide and shallow tree (i.e. depth ≤ 6).
[中]Vector是默认的Seq实现,它有效地提供对任何元素的恒定时间访问。许多其他操作(例如,“tail”、“drop”、“slice”)实际上也是常量。该实现基于“位图trie”,一种非常宽且浅的树(即深度)≤ 6).

代码示例

代码示例来源:origin: vavr-io/vavr

@Override
public <R> Vector<R> collect(PartialFunction<? super T, ? extends R> partialFunction) {
  return ofAll(iterator().<R> collect(partialFunction));
}

代码示例来源:origin: vavr-io/vavr

@Override
public Vector<Vector<T>> permutations() {
  if (isEmpty()) {
    return empty();
  } else if (length() == 1) {
    return of(this);
  } else {
    Vector<Vector<T>> results = empty();
    for (T t : distinct()) {
      for (Vector<T> ts : remove(t).permutations()) {
        results = results.append(of(t).appendAll(ts));
      }
    }
    return results;
  }
}

代码示例来源:origin: vavr-io/vavr

static <T> Vector<Vector<T>> apply(Vector<T> elements, int k) {
    return (k == 0)
        ? Vector.of(Vector.empty())
        : elements.zipWithIndex().flatMap(
        t -> apply(elements.drop(t._2 + 1), (k - 1)).map((Vector<T> c) -> c.prepend(t._1)));
  }
}

代码示例来源:origin: vavr-io/vavr

@Override
public Vector<T> slice(int beginIndex, int endIndex) {
  if ((beginIndex >= endIndex) || (beginIndex >= size()) || isEmpty()) {
    return empty();
  } else if ((beginIndex <= 0) && (endIndex >= length())) {
    return this;
  } else {
    return take(endIndex).drop(beginIndex);
  }
}

代码示例来源:origin: vavr-io/vavr

@Override
public Vector<T> reverse() {
  return (length() <= 1) ? this : ofAll(reverseIterator());
}

代码示例来源:origin: vavr-io/vavr

@Override
public Vector<T> remove(T element) {
  for (int i = 0; i < length(); i++) {
    if (Objects.equals(get(i), element)) {
      return removeAt(i);
    }
  }
  return this;
}

代码示例来源:origin: vavr-io/vavr

@Override
public <T1, T2> Tuple2<Vector<T1>, Vector<T2>> unzip(Function<? super T, Tuple2<? extends T1, ? extends T2>> unzipper) {
  Objects.requireNonNull(unzipper, "unzipper is null");
  Vector<T1> xs = empty();
  Vector<T2> ys = empty();
  for (int i = 0; i < length(); i++) {
    final Tuple2<? extends T1, ? extends T2> t = unzipper.apply(get(i));
    xs = xs.append(t._1);
    ys = ys.append(t._2);
  }
  return Tuple.of(xs, ys);
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("ObjectEquality")
private Vector<T> wrap(BitMappedTrie<T> trie) {
  return (trie == this.trie)
      ? this
      : ofAll(trie);
}

代码示例来源:origin: vavr-io/vavr

/**
 * Reduces many {@code Lazy} values into a single {@code Lazy} by transforming an
 * {@code Iterable<Lazy<? extends T>>} into a {@code Lazy<Seq<T>>}.
 *
 * @param <T>    Type of the lazy values.
 * @param values An iterable of lazy values.
 * @return A lazy sequence of values.
 * @throws NullPointerException if values is null
 */
@SuppressWarnings("Convert2MethodRef") // TODO should be fixed in JDK 9 and Idea
public static <T> Lazy<Seq<T>> sequence(Iterable<? extends Lazy<? extends T>> values) {
  Objects.requireNonNull(values, "values is null");
  return Lazy.of(() -> Vector.ofAll(values).map(lazy -> lazy.get()));
}

代码示例来源:origin: vavr-io/vavr

@Override
public Tuple2<Vector<T>, Vector<T>> partition(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  final ArrayList<T> left = new ArrayList<>(), right = new ArrayList<>();
  for (int i = 0; i < length(); i++) {
    final T t = get(i);
    (predicate.test(t) ? left : right).add(t);
  }
  return Tuple.of(ofAll(left), ofAll(right));
}

代码示例来源:origin: vavr-io/vavr

@Override
public Tuple2<Vector<T>, Vector<T>> splitAtInclusive(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  for (int i = 0; i < length(); i++) {
    final T value = get(i);
    if (predicate.test(value)) {
      return (i == (length() - 1)) ? Tuple.of(this, empty())
                     : Tuple.of(take(i + 1), drop(i + 1));
    }
  }
  return Tuple.of(this, empty());
}

代码示例来源:origin: vavr-io/vavr

/**
 * Reduces many {@code Option}s into a single {@code Option} by transforming an
 * {@code Iterable<Option<? extends T>>} into a {@code Option<Seq<T>>}. If any of
 * the Options are {@link Option.None}, then this returns {@link Option.None}.
 *
 * @param values An {@code Iterable} of {@code Option}s
 * @param <T>    type of the Options
 * @return An {@code Option} of a {@link Seq} of results
 * @throws NullPointerException if {@code values} is null
 */
static <T> Option<Seq<T>> sequence(Iterable<? extends Option<? extends T>> values) {
  Objects.requireNonNull(values, "values is null");
  Vector<T> vector = Vector.empty();
  for (Option<? extends T> value : values) {
    if (value.isEmpty()) {
      return Option.none();
    }
    vector = vector.append(value.get());
  }
  return Option.some(vector);
}

代码示例来源:origin: vavr-io/vavr

@Override
public Vector<Vector<T>> combinations() { return rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity()); }

代码示例来源:origin: vavr-io/vavr

/**
 * Alias for {@link Vector#empty()}
 *
 * @param <T> Component type of element.
 * @return A singleton instance of empty {@link Vector}
 */
public static <T> Vector<T> Vector() {
  return Vector.empty();
}

代码示例来源:origin: vavr-io/vavr

/**
 * Chooses one of the given generators according to their frequency.
 * Only generators with positive frequencies ares used in returned
 * generator.
 *
 * @param generators A non-empty traversable of Tuples (frequency, generator)
 * @param <T>        Type to be generated
 * @return A new T generator
 * @throws java.lang.NullPointerException     if generators is null
 * @throws java.lang.IllegalArgumentException if generators doesn't contain any generator with positive frequency
 */
static <T> Gen<T> frequency(Iterable<Tuple2<Integer, Gen<T>>> generators) {
  Objects.requireNonNull(generators, "generators is null");
  final Vector<Tuple2<Integer, Gen<T>>> filtered = Iterator.ofAll(generators)
      .filter(t -> t._1() > 0).toVector();
  if (filtered.isEmpty()) {
    throw new IllegalArgumentException("no generator with positive weight");
  }
  final int size = filtered.map(t -> t._1).sum().intValue();
  return choose(1, size).flatMap(n -> GenModule.frequency(n, filtered.iterator()));
}

代码示例来源:origin: vavr-io/vavr

@Override
public Vector<T> appendAll(Iterable<? extends T> iterable) {
  Objects.requireNonNull(iterable, "iterable is null");
  if (isEmpty()) {
    return ofAll(iterable);
  }
  if (io.vavr.collection.Collections.isEmpty(iterable)){
    return this;
  }
  return new Vector<>(trie.appendAll(iterable));
}

代码示例来源:origin: vavr-io/vavr

@Override
public int indexOf(T element, int from) {
  for (int i = from; i < length(); i++) {
    if (Objects.equals(get(i), element)) {
      return i;
    }
  }
  return -1;
}

代码示例来源:origin: vavr-io/vavr

@Override
public IndexedSeq<CharSeq> combinations() {
  return Vector.rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity());
}

代码示例来源:origin: vavr-io/vavr

/**
 * Alias for {@link Vector#of(Object)}
 *
 * @param <T>     Component type of element.
 * @param element An element.
 * @return A new {@link Vector} instance containing the given element
 */
public static <T> Vector<T> Vector(T element) {
  return Vector.of(element);
}

代码示例来源:origin: vavr-io/vavr

@Override
public Vector<T> sorted() {
  if (isEmpty()) {
    return this;
  } else {
    @SuppressWarnings("unchecked")
    final T[] list = (T[]) toJavaArray();
    Arrays.sort(list);
    return Vector.of(list);
  }
}

相关文章