Java Code Examples for java.util.function.Function#compose()
The following examples show how to use
java.util.function.Function#compose() .
These examples are extracted from open source projects.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Java-11-Cookbook-Second-Edition File: Chapter04Functional.java License: MIT License | 6 votes |
private static void compose2() { //Function<Integer, Double> multiplyBy30 = i -> i * 30d; Function<Integer, Double> multiplyBy30 = createMultiplyBy(30d); System.out.println(multiplyBy30.apply(1)); //prints: 30 //Function<Double,Double> subtract7 = d-> d - 7.; Function<Double,Double> subtract7 = createSubtract(7.0); System.out.println(subtract7.apply(10.0)); //prints: 3.0 Function<Integer, Double> multiplyBy30AndSubtract7 = subtract7.compose(multiplyBy30); System.out.println(multiplyBy30AndSubtract7.apply(1)); //prints: 23.0 multiplyBy30AndSubtract7 = multiplyBy30.andThen(subtract7); System.out.println(multiplyBy30AndSubtract7.apply(1)); //prints: 23.0 Function<Integer, Double> multiplyBy30Subtract7Twice = subtract7.compose(multiplyBy30).andThen(subtract7); System.out.println(multiplyBy30Subtract7Twice.apply(1)); //prints: 16 }
Example 2
Source Project: Learn-Java-12-Programming File: StandardFunctionalInterfaces.java License: MIT License | 5 votes |
private static void function(){ Function<Integer, Double> multiplyByTen = i -> i * 10.0; System.out.println(multiplyByTen.apply(1)); //prints: 10.0 Supplier<Integer> supply7 = () -> 7; Function<Integer, Double> multiplyByFive = i -> i * 5.0; Consumer<String> printResult = printWithPrefixAndPostfix("Result: ", " Great!"); printResult.accept(multiplyByFive.apply(supply7.get()).toString()); //prints: Result: 35.0 Great! Function<Double, Long> divideByTwo = d -> Double.valueOf(d / 2.).longValue(); Function<Long, String> incrementAndCreateString = l -> String.valueOf(l + 1); Function<Double, String> divideByTwoIncrementAndCreateString = divideByTwo.andThen(incrementAndCreateString); printResult.accept(divideByTwoIncrementAndCreateString.apply(4.)); //prints: Result: 3 Great! divideByTwoIncrementAndCreateString = incrementAndCreateString.compose(divideByTwo); printResult.accept(divideByTwoIncrementAndCreateString.apply(4.)); //prints: Result: 3 Great! Function<Double, Double> multiplyByTwo = d -> d * 2.0; System.out.println(multiplyByTwo.apply(2.)); //prints: 4.0 Function<Double, Long> subtract7 = d -> Math.round(d - 7); System.out.println(subtract7.apply(11.0)); //prints: 4 long r = multiplyByTwo.andThen(subtract7).apply(2.); System.out.println(r); //prints: -3 multiplyByTwo = Function.identity(); System.out.println(multiplyByTwo.apply(2.)); //prints: 2.0 r = multiplyByTwo.andThen(subtract7).apply(2.); System.out.println(r); //prints: -5 }
Example 3
Source Project: j2objc File: FunctionTest.java License: Apache License 2.0 | 5 votes |
public void testCompose_null() throws Exception { Function<Double, Double> plusOne = x -> x + 1.0d; try { plusOne.compose(null); fail(); } catch (NullPointerException expected) {} }
Example 4
Source Project: Java-Coding-Problems File: Main.java License: MIT License | 4 votes |
public static void main(String[] args) { List<Melon> melons1 = Arrays.asList(new Melon("Gac", 2000), new Melon("Horned", 1600), new Melon("Apollo", 3000), new Melon("Gac", 3000), new Melon("Hemi", 1600)); Comparator<Melon> byWeight = Comparator.comparing(Melon::getWeight); Comparator<Melon> byType = Comparator.comparing(Melon::getType); Comparator<Melon> byWeightAndType = Comparator.comparing(Melon::getWeight) .thenComparing(Melon::getType); List<Melon> sortedMelons1 = melons1.stream() .sorted(byWeight) .collect(Collectors.toList()); List<Melon> sortedMelons2 = melons1.stream() .sorted(byType) .collect(Collectors.toList()); List<Melon> sortedMelons3 = melons1.stream() .sorted(byWeightAndType) .collect(Collectors.toList()); System.out.println("Unsorted melons: " + melons1); System.out.println("\nSorted by weight melons: " + sortedMelons1); System.out.println("Sorted by type melons: " + sortedMelons2); System.out.println("Sorted by weight and type melons: " + sortedMelons3); List<Melon> melons2 = Arrays.asList(new Melon("Gac", 2000), new Melon("Horned", 1600), new Melon("Apollo", 3000), new Melon("Gac", 3000), new Melon("hemi", 1600)); Comparator<Melon> byWeightAndType2 = Comparator.comparing(Melon::getWeight) .thenComparing(Melon::getType, String.CASE_INSENSITIVE_ORDER); List<Melon> sortedMelons4 = melons2.stream() .sorted(byWeightAndType2) .collect(Collectors.toList()); System.out.println("\nSorted by weight and type (case insensitive) melons: " + sortedMelons4); Predicate<Melon> p2000 = m -> m.getWeight() > 2000; Predicate<Melon> p2000GacApollo = p2000.and(m -> m.getType().equals("Gac")) .or(m -> m.getType().equals("Apollo")); Predicate<Melon> restOf = p2000GacApollo.negate(); Predicate<Melon> pNot2000 = Predicate.not(m -> m.getWeight() > 2000); List<Melon> result1 = melons1.stream() .filter(p2000GacApollo) .collect(Collectors.toList()); List<Melon> result2 = melons1.stream() .filter(restOf) .collect(Collectors.toList()); List<Melon> result3 = melons1.stream() .filter(pNot2000) .collect(Collectors.toList()); System.out.println("\nAll melons of type Apollo or Gac heavier than 2000 grams:\n" + result1); System.out.println("\nNegation of the above predicate:\n" + result2); System.out.println("\nAll melons lighter than (or equal to) 2000 grams:\n" + result3); Function<Double, Double> f = x -> x * 2; Function<Double, Double> g = x -> Math.pow(x, 2); Function<Double, Double> gf = f.andThen(g); Function<Double, Double> fg = f.compose(g); double resultgf = gf.apply(4d); double resultfg = fg.apply(4d); System.out.println("\ng(f(x)): " + resultgf); System.out.println("f(g(x)): " + resultfg); Function<String, String> introduction = Editor::addIntroduction; Function<String, String> editor = introduction.andThen(Editor::addBody) .andThen(Editor::addConclusion); String article = editor.apply("\nArticle name\n"); System.out.println(article); }
Example 5
Source Project: arcusplatform File: KafkaStream.java License: Apache License 2.0 | 4 votes |
public <K1> Builder<K1, V> deserializeByteKeys(Function<byte[], K1> keyDeserializer) { return new Builder<>(keyDeserializer.compose(ByteDeserializer), this.valueDeserializer, this); }
Example 6
Source Project: arcusplatform File: KafkaStream.java License: Apache License 2.0 | 4 votes |
public <K1> Builder<K1, V> deserializeStringKeys(Function<String, K1> keyDeserializer) { return new Builder<>(keyDeserializer.compose(StringDeserializer), this.valueDeserializer, this); }
Example 7
Source Project: arcusplatform File: KafkaStream.java License: Apache License 2.0 | 4 votes |
public <K1> Builder<K1, V> transformKeys(Function<K, K1> fn) { return new Builder<>(fn.compose(keyDeserializer), valueDeserializer); }
Example 8
Source Project: arcusplatform File: KafkaStream.java License: Apache License 2.0 | 4 votes |
public <V1> Builder<K, V1> deserializeByteValues(Function<byte[], V1> valueDeserializer) { return new Builder<>(this.keyDeserializer, valueDeserializer.compose(ByteDeserializer), this); }
Example 9
Source Project: arcusplatform File: KafkaStream.java License: Apache License 2.0 | 4 votes |
public <V1> Builder<K, V1> deserializeStringValues(Function<String, V1> valueDeserializer) { return new Builder<>(this.keyDeserializer, valueDeserializer.compose(StringDeserializer), this); }
Example 10
Source Project: AVM File: ClassF.java License: MIT License | 4 votes |
public Function<Integer, Integer> getIncrementorLambda() { String str = "aaaaaaaaaaaaaa"; func = (x) -> str.substring(0, x); Function<String, Integer> func2 = (inputStr) -> onlyCalledByLambda(inputStr); return func2.compose(func); }
Example 11
Source Project: Java-11-Cookbook-Second-Edition File: Chapter04Functional.java License: MIT License | 4 votes |
private static void compose1() { Function<Integer, Double> multiplyBy30 = createMultiplyBy(30d); System.out.println(multiplyBy30.apply(1)); //prints: 30 Function<Double,Double> subtract7 = createSubtract(7.0); System.out.println(subtract7.apply(10.0)); //prints: 3.0 Function<Integer, Double> multiplyBy30AndSubtract7 = subtract7.compose(multiplyBy30); System.out.println(multiplyBy30AndSubtract7.apply(1)); //prints: 23.0 multiplyBy30AndSubtract7 = multiplyBy30.andThen(subtract7); System.out.println(multiplyBy30AndSubtract7.apply(1)); //prints: 23.0 Function<Integer, Double> multiplyBy30Subtract7Twice = subtract7.compose(multiplyBy30).andThen(subtract7); System.out.println(multiplyBy30Subtract7Twice.apply(1)); //prints: 16 }
Example 12
Source Project: archie File: OdinSerializer.java License: Apache License 2.0 | 4 votes |
public OdinSerializer(OdinStringBuilder builder, Function<Object, Object> odinObjectMapper) { this.builder = builder; this.objectMapper = odinObjectMapper.compose(this::checkNotNull); }
Example 13
Source Project: cyclops File: PublisherFlatMappingSpliterator.java License: Apache License 2.0 | 4 votes |
public static <T2,T,R> PublisherFlatMappingSpliterator<T2,R> compose(FunctionSpliterator<T2,T> fnS,Function<? super T, ? extends Publisher<? extends R>> mapper){ Function<? super T2,? extends T> fn = fnS.function(); return new PublisherFlatMappingSpliterator<T2,R>(CopyableSpliterator.copy(fnS.source()),mapper.<T2>compose(fn)); }
Example 14
Source Project: cyclops File: StreamFlatMappingSpliterator.java License: Apache License 2.0 | 4 votes |
public static <T2,T,R> StreamFlatMappingSpliterator<T2,R> compose(FunctionSpliterator<T2,T> fnS,Function<? super T, ? extends Stream<? extends R>> mapper){ Function<? super T2,? extends T> fn = fnS.function(); return new StreamFlatMappingSpliterator<T2,R>(CopyableSpliterator.copy(fnS.source()),mapper.<T2>compose(fn)); }
Example 15
Source Project: cyclops File: IterableFlatMappingSpliterator.java License: Apache License 2.0 | 4 votes |
public static <T2,T,R> IterableFlatMappingSpliterator<T2,R> compose(FunctionSpliterator<T2,T> fnS,Function<? super T, ? extends Iterable<? extends R>> mapper){ Function<? super T2,? extends T> fn = fnS.function(); return new IterableFlatMappingSpliterator<T2,R>(CopyableSpliterator.copy(fnS.source()),mapper.<T2>compose(fn)); }
Example 16
Source Project: highj File: CokleisliProfunctor.java License: MIT License | 4 votes |
@Override default <A, B, C> Cokleisli<W, A, C> rmap(Function<B, C> g, __<__<__<Cokleisli.µ, W>, A>, B> p) { return new Cokleisli<>(g.compose(asCokleisli(p))); }
Example 17
Source Project: tutorials File: FunctionalInterfaceUnitTest.java License: MIT License | 3 votes |
@Test public void whenComposingTwoFunctions_thenFunctionsExecuteSequentially() { Function<Integer, String> intToString = Object::toString; Function<String, String> quote = s -> "'" + s + "'"; Function<Integer, String> quoteIntToString = quote.compose(intToString); assertEquals("'5'", quoteIntToString.apply(5)); }
Example 18
Source Project: ditto File: DefaultAdapterResolver.java License: Eclipse Public License 2.0 | 2 votes |
/** * Convert an extracting function for TopicPath into one for Adaptable. * * @param extractor the extracting function for TopicPath. * @return the extracting function for Adaptable. */ private static <T> Function<Adaptable, T> forTopicPath(final Function<TopicPath, T> extractor) { return extractor.compose(Adaptable::getTopicPath); }
Example 19
Source Project: durian File: Functions.java License: Apache License 2.0 | 2 votes |
/** * Returns the composition of two functions. For {@code f: A->B} and {@code g: B->C}, composition * is defined as the function h such that {@code h(a) == g(f(a))} for each {@code a}. * * @param g the second function to apply * @param f the first function to apply * @return the composition of {@code f} and {@code g} * @see <a href="//en.wikipedia.org/wiki/Function_composition">function composition</a> */ public static <A, B, C> Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f) { return g.compose(f); }