io.vavr.Function2 Java Examples

The following examples show how to use io.vavr.Function2. 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 File: CsvProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Returns a read/write protocol for a CSV format consisting of several nested protocols (probably created
 * using {@link #column(String)} ), invoking the [produce] function on reading, and using the
 * specified getters for writing.
 */
public static <T,F1,F2> Protocol<CsvEvent, T> csv(
    Protocol<CsvEvent,F1> p1, Protocol<CsvEvent,F2> p2,
    Function2<F1,F2,T> produce,
    Function1<T,F1> g1, Function1<T,F2> g2) {
    return multi(Vector.of(p1, p2), args -> produce.apply((F1) args.get(0), (F2) args.get(1)), Vector.of(g1, g2));
}
 
Example #2
Source File: CsvProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Returns a read-only protocol for a CSV format consisting of several nested protocols (probably created
 * using {@link #column(String)} ), invoking the [produce] function on reading.
 */
public static <T,F1,F2> ReadProtocol<CsvEvent, T> csv(
    ReadProtocol<CsvEvent,F1> p1,
    ReadProtocol<CsvEvent,F2> p2,
    Function2<F1,F2,T> produce) {
    return multi(Vector.of(p1, p2), args -> produce.apply((F1) args.get(0), (F2) args.get(1)));
}
 
Example #3
Source File: ParserTest.java    From paleo with Apache License 2.0 5 votes vote down vote up
private static void assertSchemaRelativeToParentDirWithCharset(Function2<Schema, File, DataFrame> parseLogic) throws IOException {
    StringReader schemaReader = new StringReader(SCHEMA_WITH_CHARSET_NAME);
    Schema schema = Schema.parseJson(schemaReader);
    // some trickery to find physical location of resources folder...
    File resourceFolder = new File(ParserTest.class.getResource("/iso-8859-1.txt").getPath()).getParentFile();
    DataFrame dataFrame = parseLogic.apply(schema, resourceFolder);
    StringColumnId columnId = dataFrame.getColumnId(0, ColumnType.STRING);
    assertEquals("°", dataFrame.getValueAt(0, columnId));
}
 
Example #4
Source File: JSONProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Returns a protocol for a JSON object with a fields [p*], using [f] to turn it into a Java object, and [g*] to get the fields when writing.
 */
public static <F1,F2,T> ObjectProtocol<T> object(Protocol<JSONEvent, F1> p1, Protocol<JSONEvent, F2> p2, Function2<F1, F2, T> f, Function1<T, F1> g1, Function1<T, F2> g2) {
    return new ObjectProtocol<>(Arrays.asList(p1, p2), args -> f.apply((F1) args.get(0), (F2) args.get(1)), Arrays.asList(g1, g2));
}
 
Example #5
Source File: JSONProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Returns a read-only protocol for a JSON object with fields [p*], using [f] to turn it into a Java object.
 */
public static <F1,F2,T> ObjectReadProtocol<T> object(ReadProtocol<JSONEvent, F1> p1, ReadProtocol<JSONEvent, F2> p2, Function2<F1, F2, T> f) {
    return new ObjectReadProtocol<>(Arrays.asList(p1, p2), args -> f.apply((F1) args.get(0), (F2) args.get(1)));
}
 
Example #6
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag and child elements (tag or attribute) using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F1,F2,T> TagProtocol<T> tag(QName qname, Protocol<XMLEvent,F1> p1, Protocol<XMLEvent,F2> p2, Function2<F1,F2,T> f, Function1<T,F1> g1, Function1<T,F2> g2) {
    return new TagProtocol<>(Option.of(qname), Vector.of(p1, p2), args -> f.apply((F1) args.get(0), (F2) args.get(1)), Vector.of(g1, g2));
}
 
Example #7
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads a tag and child elements (tag or attribute) using [p*], using [f] to create the result on reading.
 */
public static <F1,F2,T> TagReadProtocol<T> tag(QName qname, ReadProtocol<XMLEvent,F1> p1, ReadProtocol<XMLEvent,F2> p2, Function2<F1,F2,T> f) {
    return new TagReadProtocol<>(Option.of(qname), Vector.of(p1, p2), args -> f.apply((F1) args.get(0), (F2) args.get(1)));
}
 
Example #8
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag with any name and inner protocols using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F2,T> TagProtocol<T> tagName(Protocol<XMLEvent,F2> p2, Function2<QName,F2,T> f, Function1<T,QName> g1, Function1<T,F2> g2) {
    return new TagProtocol<>(Option.none(), Vector.of(p2), args -> f.apply((QName) args.get(0), (F2) args.get(1)), Vector.of(g1, g2));
}
 
Example #9
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads a tag with any name and inner protocols using [p*], using [f] to create the result on reading.
 */
public static <F2,T> TagReadProtocol<T> tagName(ReadProtocol<XMLEvent,F2> p2, Function2<QName,F2,T> f) {
    return new TagReadProtocol<>(Option.none(), Vector.of(p2), args -> f.apply((QName) args.get(0), (F2) args.get(1)));
}
 
Example #10
Source File: Protocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Folds over a repeated nested protocol, merging the results into a single element.
 */
public static <E,T,U> IterableReadProtocol<E,U> foldLeft(ReadProtocol<E,T> inner, Supplier<U> initial, Function2<U,T,U> combine) {
    return FoldProtocol.read("*", inner, initial, combine);
}
 
Example #11
Source File: Window.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public Function2<MetricSource, Double, Double> get(String name) {
    return get(name, Collections.emptyMap());
}
 
Example #12
Source File: Window.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public Function2<MetricSource, Double, Double> get(String name, Map<String, String> labels) {
    ID id = new ID(name, ImmutableMap.copyOf(labels));
    return (source, sum) -> operateCounter(id, source, sum);
}
 
Example #13
Source File: VavrUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenVavrBiFunction_whenWorks_thenCorrect() {
    Function2<Integer, Integer, Integer> sum = (num1, num2) -> num1 + num2;
    Integer result = sum.apply(5, 7);
    assertEquals(Integer.valueOf(12), result);
}
 
Example #14
Source File: VavrUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenCreatesFunction_thenCorrect2() {
    Function2<Integer, Integer, Integer> sum = (a, b) -> a + b;
    int summed = sum.apply(5, 6);
    assertEquals(11, summed);
}
 
Example #15
Source File: VavrUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenCreatesFunctionFromMethodRef_thenCorrect() {
    Function2<Integer, Integer, Integer> sum = Function2.of(this::sum);
    int summed = sum.apply(5, 6);
    assertEquals(11, summed);
}