本文整理了Java中fj.data.Option.some()
方法的一些代码示例,展示了Option.some()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Option.some()
方法的具体详情如下:
包路径:fj.data.Option
类名称:Option
方法名:some
[英]Constructs an optional value that has a value of the given argument.
[中]构造具有给定参数值的可选值。
代码示例来源:origin: org.functionaljava/functionaljava
public static IO<Reader> fileReader(final File f, final Option<Charset> encoding) {
return () -> {
final FileInputStream fis = new FileInputStream(f);
return encoding.isNone() ? new InputStreamReader(fis) : new InputStreamReader(fis, encoding.some());
};
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns a NonEmptyList of the sublists of this list.
*
* @return a NonEmptyList of the sublists of this list.
*/
public NonEmptyList<NonEmptyList<A>> sublists() {
return fromList(
somes(toList().toStream().substreams()
.map(F1Functions.o(NonEmptyList::fromList, Conversions.Stream_List())).toList())).some();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns the natural number equal to the given BigInteger
*
* @param i A given BigInteger
* @return An optional natural number, or none if the given BigInteger is less than zero.
*/
public static Option<Natural> natural(final BigInteger i) {
return i.compareTo(BigInteger.ZERO) < 0
? Option.none()
: Option.some(new Natural(i));
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns the value of this optional value or the given argument.
*
* @param a The argument to return if this optiona value has no value.
* @return The value of this optional value or the given argument.
*/
public final A orSome(final F0<A> a) {
return isSome() ? some() : a.f();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Divide a natural number by another yielding both the quotient and the remainder.
*
* @param n A natural number to divide this one by.
* @return The quotient and the remainder, in that order.
*/
public V2<Natural> divmod(final Natural n) {
final BigInteger[] x = value.divideAndRemainder(n.value);
return V.v(natural(x[0]).some(), natural(x[1]).some());
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns this projection's value in <code>Some</code> if it exists, otherwise
* <code>None</code>.
*
* @return This projection's value in <code>Some</code> if it exists, otherwise
* <code>None</code>.
*/
public Option<B> toOption() {
return e.isRight() ? some(value()) : Option.none();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Inserts the given key and value association into the tree map.
* If the given key is already mapped to a value, the old value is replaced with the given one.
*
* @param k The key to insert.
* @param v The value to insert.
* @return A new tree map with the given value mapped to the given key.
*/
public TreeMap<K, V> set(final K k, final V v) {
return new TreeMap<>(tree.insert(p(k, Option.some(v))));
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Performs a side-effect for the value of this optional value.
*
* @param f The side-effect to perform for the given element.
* @return The unit value.
*/
public final Unit foreach(final F<A, Unit> f) {
return isSome() ? f.f(some()) : unit();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Binds the given function across the element of this optional value with a final join.
*
* @param f The function to apply to the element of this optional value.
* @return A new optional value after performing the map, then final join.
*/
public final <B> Option<B> bind(final F<A, Option<B>> f) {
return isSome() ? f.f(some()) : Option.none();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns an array projection of this optional value.
*
* @return An array projection of this optional value.
*/
@SuppressWarnings("unchecked")
public final Array<A> toArray() {
return isSome() ? Array.array(some()) : Array.empty();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns the head of the list, if any. Equivalent to {@link #toOption()} .
*
* @return The optional head of the list.
*/
public final Option<A> headOption() {
return isEmpty() ? Option.none() : some(head());
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* 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<Zipper<A>> next() {
return right.isEmpty() ? Option.none() : some(tryNext());
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns a parser that produces a digit (0 to 9).
*
* @param missing The error if there is no character on the stream to produce a digit with.
* @param sat The error if the produced character is not a digit.
* @return A parser that produces a digit (0 to 9).
*/
public static <E> Parser<Stream<Character>, Digit, E> digit(final F0<E> missing, final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, Character::isDigit).map(c1 -> Digit.fromChar(c1).some());
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Filters elements from this optional value by returning only elements which produce
* <code>true</code> when the given function is applied to them.
*
* @param f The predicate function to filter on.
* @return A new optional value whose value matches the given predicate if it has one.
*/
public final Option<A> filter(final F<A, Boolean> f) {
return isSome() ? f.f(some()) ? this : Option.none() : Option.none();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns a stream projection of this optional value.
*
* @return A stream projection of this optional value.
*/
public final Stream<A> toStream() {
return isSome() ? Stream.<A>nil().cons(some()) : Stream.nil();
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns the minimum (key, value) pair in the tree if the tree is not empty.
*/
public Option<P2<K, V>> min() {
return tree.min().map(p -> p(p._1(), p._2().some()));
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Maps across a nonempty list in parallel.
*
* @param as A NonEmptyList to map across in parallel.
* @param f A function to map across the given NonEmptyList.
* @return A Promise of a new NonEmptyList with the given function applied to each element.
*/
public <A, B> Promise<NonEmptyList<B>> parMap(final NonEmptyList<A> as, final F<A, B> f) {
return mapM(as.toList(), promise(f)).fmap(list -> NonEmptyList.fromList(list).some());
}
代码示例来源:origin: org.functionaljava/functionaljava
public <A, B> HashMap<A, B> map(F<K, A> keyFunction,
F<V, B> valueFunction,
Equal<A> equal, Hash<A> hash) {
final HashMap<A, B> hashMap = new HashMap<>(equal, hash);
for (K key : keys()) {
final A newKey = keyFunction.f(key);
final B newValue = valueFunction.f(get(key).some());
hashMap.set(newKey, newValue);
}
return hashMap;
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* 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<TreeZipper<A>> right() {
return rights.isEmpty() ? Option.none()
: some(treeZipper(rights.head(), lefts.cons(tree), rights.tail()._1(), parents));
}
代码示例来源:origin: org.functionaljava/functionaljava
P3<Option<Digit<V, A>>, A, Option<Digit<V, A>>> split1(final F<V, Boolean> predicate, final V acc) {
final Measured<V, A> m = measured();
final MakeTree<V, A> mk = mkTree(m);
final V acc1 = m.sum(acc, m.measure().f(as._1()));
if (predicate.f(acc1)) {
return P.p(none(), as._1(), some(mk.two(as._2(), as._3())));
} else if (predicate.f(m.sum(acc1, m.measure().f(as._2())))) {
return P.p(some(mk.one(as._1())), as._2(), some(mk.one(as._3())));
} else {
return P.p(some(mk.two(as._1(), as._2())), as._3(), none());
}
}
内容来源于网络,如有侵权,请联系作者删除!