本文整理了Java中io.vavr.control.Option.map()
方法的一些代码示例,展示了Option.map()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Option.map()
方法的具体详情如下:
包路径:io.vavr.control.Option
类名称:Option
方法名:map
[英]Maps the value and wraps it in a new Some if this is a Some, returns None.
[中]映射该值并将其包装为新的Some。如果这是Some,则返回None。
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Option.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(Function<? super T1, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return ts1.map(f);
}
代码示例来源:origin: vavr-io/vavr
/**
* Folds either the {@code None} or the {@code Some} side of the Option value.
*
* @param ifNone maps the left value if this is a None
* @param f maps the value if this is a Some
* @param <U> type of the folded value
* @return A value of type U
*/
default <U> U fold(Supplier<? extends U> ifNone, Function<? super T, ? extends U> f) {
return this.<U>map(f).getOrElse(ifNone);
}
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Options.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(BiFunction<? super T1, ? super T2, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return
ts1.flatMap(t1 ->
ts2.map(t2 -> f.apply(t1, t2)));
}
代码示例来源:origin: vavr-io/vavr
/**
* Returns the underlying exception of this Future, syntactic sugar for {@code future.getValue().map(Try::getCause)}.
*
* @return None if the Future is not completed yet. Returns Some(Throwable) if the Future was completed with a failure.
* @throws UnsupportedOperationException if the Future was successfully completed with a value
*/
default Option<Throwable> getCause() {
return getValue().map(Try::getCause);
}
代码示例来源:origin: vavr-io/vavr
@Override
default boolean contains(Tuple2<K, V> element) {
return get(element._1).map(v -> Objects.equals(v, element._2)).getOrElse(false);
}
代码示例来源:origin: vavr-io/vavr
@Override
public Option<V> get(K key) {
final V ignored = null;
return entries.find(new Tuple2<>(key, ignored)).map(Tuple2::_2);
}
代码示例来源:origin: vavr-io/vavr
@Override
public String toString() {
return "Promise(" + future.getValue().map(String::valueOf).getOrElse("?") + ")";
}
}
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Options.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(Function3<? super T1, ? super T2, ? super T3, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return
ts1.flatMap(t1 ->
ts2.flatMap(t2 ->
ts3.map(t3 -> f.apply(t1, t2, t3))));
}
代码示例来源:origin: vavr-io/vavr
@Override
Option<V> lookup(int shift, int keyHash, K key) {
if (hash != keyHash) {
return Option.none();
}
return nodes().find(node -> Objects.equals(node.key(), key)).map(LeafNode::value);
}
代码示例来源:origin: vavr-io/vavr
@Override
public Vector<T> replace(T currentElement, T newElement) {
return indexOfOption(currentElement)
.map(i -> update(i, newElement))
.getOrElse(this);
}
代码示例来源:origin: vavr-io/vavr
@Override
default boolean contains(Tuple2<K, V> element) {
return get(element._1).map(v -> v.contains(element._2)).getOrElse(false);
}
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Options.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(Function4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return
ts1.flatMap(t1 ->
ts2.flatMap(t2 ->
ts3.flatMap(t3 ->
ts4.map(t4 -> f.apply(t1, t2, t3, t4)))));
}
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Options.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(Function5<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return
ts1.flatMap(t1 ->
ts2.flatMap(t2 ->
ts3.flatMap(t3 ->
ts4.flatMap(t4 ->
ts5.map(t5 -> f.apply(t1, t2, t3, t4, t5))))));
}
代码示例来源:origin: apache/incubator-pinot
@Override
public Map<String, ?> apply(Map<String, ?> config, String keyPrefix) {
// Generate keys for table types based on table.types
// eg. table.types=[offline,realtime] -> table.type.realtime=realtime, table.type.offline=offline
config = ((Map<String, Object>) config).merge(config.get("table.types").map(typeList -> {
if (typeList instanceof String) {
return List.of((String) typeList);
} else if (typeList instanceof Collection) {
return List.ofAll((Collection<String>) typeList);
} else {
return List.empty();
}
}).map(typeList -> typeList.map(type -> Tuple.of("table.type." + type.toString().toLowerCase(), type))
.toMap(Function.identity())).getOrElse(HashMap::empty));
config = config.remove("table.types");
return config;
}
代码示例来源:origin: vavr-io/vavr
@SuppressWarnings("unchecked")
static <K, V, U extends V, M extends Map<K, V>> M merge(
M map, OfEntries<K, V, M> ofEntries,
Map<? extends K, U> that, BiFunction<? super V, ? super U, ? extends V> collisionResolution) {
Objects.requireNonNull(that, "that is null");
Objects.requireNonNull(collisionResolution, "collisionResolution is null");
if (map.isEmpty()) {
return ofEntries.apply(Map.narrow(that));
} else if (that.isEmpty()) {
return map;
} else {
return that.foldLeft(map, (result, entry) -> {
final K key = entry._1;
final U value = entry._2;
final V newValue = result.get(key).map(v -> (V) collisionResolution.apply(v, value)).getOrElse(value);
return (M) result.put(key, newValue);
});
}
}
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Options.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(Function6<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return
ts1.flatMap(t1 ->
ts2.flatMap(t2 ->
ts3.flatMap(t3 ->
ts4.flatMap(t4 ->
ts5.flatMap(t5 ->
ts6.map(t6 -> f.apply(t1, t2, t3, t4, t5, t6)))))));
}
代码示例来源:origin: apache/incubator-pinot
@Override
public Map<String, ?> apply(Map<String, ?> childKeys, String pathPrefix) {
// Adjust the name to add the table suffix to table.name.realtime/table.name.offline
List<String> tableTypes = childKeys.get("table.types").map(tableTypesListOrString -> {
if (tableTypesListOrString instanceof String) {
return List.of((String) tableTypesListOrString);
} else if (tableTypesListOrString instanceof Collection) {
return List.ofAll((Collection<String>) tableTypesListOrString);
} else {
return List.empty();
}
}).getOrElse(List.empty()).map(Object::toString);
String tableName = childKeys.get("table.name").map(Object::toString).getOrElse(
() -> childKeys.get("table.name.realtime").map(Object::toString)
.getOrElse(() -> childKeys.get("table.name.offline").map(Object::toString).getOrNull()));
Map<String, Object> remappedConfig = (Map<String, Object>) childKeys;
for (String tableType : tableTypes) {
String tableNameKey = "table.name." + tableType.toLowerCase();
CommonConstants.Helix.TableType type = CommonConstants.Helix.TableType.valueOf(tableType.toUpperCase());
remappedConfig = remappedConfig.put(tableNameKey, TableNameBuilder.forType(type).tableNameWithType(tableName));
}
remappedConfig = remappedConfig.remove("table.name");
return remappedConfig;
}
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Options.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(Function7<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return
ts1.flatMap(t1 ->
ts2.flatMap(t2 ->
ts3.flatMap(t3 ->
ts4.flatMap(t4 ->
ts5.flatMap(t5 ->
ts6.flatMap(t6 ->
ts7.map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7))))))));
}
代码示例来源:origin: vavr-io/vavr
/**
* Yields a result for elements of the cross product of the underlying Options.
*
* @param f a function that maps an element of the cross product to a result
* @param <R> type of the resulting {@code Option} elements
* @return an {@code Option} of mapped results
*/
public <R> Option<R> yield(Function8<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? extends R> f) {
Objects.requireNonNull(f, "f is null");
return
ts1.flatMap(t1 ->
ts2.flatMap(t2 ->
ts3.flatMap(t3 ->
ts4.flatMap(t4 ->
ts5.flatMap(t5 ->
ts6.flatMap(t6 ->
ts7.flatMap(t7 ->
ts8.map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8)))))))));
}
代码示例来源:origin: vavr-io/vavr
/**
* Matches each element with a unique key that you extract from it.
* If the same key is present twice, the function will return {@code None}.
*
* @param getKey A function which extracts a key from elements
* @param <K> key class type
* @return A Map containing the elements arranged by their keys.
* @throws NullPointerException if {@code getKey} is null.
* @see #groupBy(Function)
*/
default <K> Option<Map<K, T>> arrangeBy(Function<? super T, ? extends K> getKey) {
return Option.of(groupBy(getKey).mapValues(Traversable<T>::singleOption))
.filter(map -> !map.exists(kv -> kv._2.isEmpty()))
.map(map -> Map.narrow(map.mapValues(Option::get)));
}
内容来源于网络,如有侵权,请联系作者删除!