Java Code Examples for io.vavr.Function1#apply()
The following examples show how to use
io.vavr.Function1#apply() .
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: MultiWriteProtocol.java From ts-reaktive with MIT License | 4 votes |
@Override public Writer<CsvEvent, T> writer() { @SuppressWarnings("unchecked") Seq<Writer<CsvEvent, Object>> writers = protocols.map(c -> (Writer<CsvEvent,Object>) c.writer()); return new Writer<CsvEvent,T>() { @Override public Seq<CsvEvent> apply(T value) { return emit(i -> writers.apply(i).apply(getters.apply(i).apply(value))); } @Override public Seq<CsvEvent> reset() { return emit(i -> writers.apply(i).reset()); } private Seq<CsvEvent> emit(Function1<Integer, Seq<CsvEvent>> getEvents) { List<Seq<CsvEvent>> resultRows = new ArrayList<>(); for (int writerIdx = 0; writerIdx < writers.size(); writerIdx++) { Seq<CsvEvent> events = getEvents.apply(writerIdx); int row = 0; if (events.isEmpty()) { log.warn("{} did not emit any events. Emitting empty column instead.", protocols.apply(writerIdx)); events = Vector.of(CsvEvent.endRecord()); } else { if (!events.endsWith(Vector.of(CsvEvent.endValue(), CsvEvent.endRecord()))) { throw new IllegalArgumentException("Expecting nested writer to end its write with endValue, endRecord but did not: " + events); } events = events.dropRight(1); while (!events.isEmpty()) { Seq<CsvEvent> rowEvents = events.takeWhile(e -> !(e instanceof CsvEvent.EndRecord)); log.debug("Row {}, writer {}: {}", row, writerIdx, rowEvents); events = events.drop(rowEvents.size()); if (!events.isEmpty()) { // drop the endRecord() separator between two rows of a single inner write events = events.drop(1); } while (row >= resultRows.size()) { if (row == resultRows.size()) { // empty columns until current writer resultRows.add(Vector.fill(writerIdx, () -> CsvEvent.endValue())); } else { // completely empty row resultRows.add(Vector.fill(writers.size(), () -> CsvEvent.endValue())); } } resultRows.set(row, resultRows.get(row).appendAll(rowEvents)); row++; } } // fill the rows that weren't emitted from this writer (but were from others) with empty columns while (row < resultRows.size()) { resultRows.set(row, resultRows.get(row).append(CsvEvent.endValue())); row++; } } return Vector.ofAll(resultRows).map(row -> row.append(CsvEvent.endRecord())).fold(Vector.empty(), (a,b) -> a.appendAll(b)); } }; }
Example 2
Source File: JSONProtocol.java From ts-reaktive with MIT License | 4 votes |
/** * Returns a protocol for a JSON object with a single field [p1], using [f] to turn it into a Java object, and [g1] to get the field when writing. */ public static <F1,T> ObjectProtocol<T> object(Protocol<JSONEvent, F1> p1, Function1<F1, T> f, Function1<T, F1> g1) { return new ObjectProtocol<>(Arrays.asList(p1), args -> f.apply((F1) args.get(0)), Arrays.asList(g1)); }
Example 3
Source File: JSONProtocol.java From ts-reaktive with MIT License | 4 votes |
/** * Returns a read-only protocol for a JSON object with a single field [p1], using [f] to turn it into a Java object. */ public static <F1,T> ObjectReadProtocol<T> object(ReadProtocol<JSONEvent, F1> p1, Function1<F1, T> f) { return new ObjectReadProtocol<>(Arrays.asList(p1), args -> f.apply((F1) args.get(0))); }
Example 4
Source File: XMLProtocol.java From ts-reaktive with MIT License | 4 votes |
/** * Reads and writes a tag and one child element (tag or attribute) using [p1], using [f] to create the result on reading, getting values using [g1] for writing. */ public static <F1,T> TagProtocol<T> tag(QName qname, Protocol<XMLEvent,F1> p1, Function1<F1,T> f, Function1<T,F1> g1) { return new TagProtocol<>(Option.of(qname), Vector.of(p1), args -> f.apply((F1) args.get(0)), Vector.of(g1)); }
Example 5
Source File: XMLProtocol.java From ts-reaktive with MIT License | 4 votes |
/** * Reads a tag and one child element (tag or attribute) using [p1], creating its result using [f]. */ public static <F1,T> TagReadProtocol<T> tag(QName name, ReadProtocol<XMLEvent,F1> p1, Function1<F1,T> f) { return new TagReadProtocol<>(Option.of(name), Vector.of(p1), args -> f.apply((F1) args.get(0))); }
Example 6
Source File: XMLProtocol.java From ts-reaktive with MIT License | 4 votes |
/** * Reads and writes a tag with any name, using [f] to create the result on reading, getting values using [g1] for writing. */ public static <T> TagProtocol<T> tagName(Function1<QName,T> f, Function1<T,QName> g1) { return new TagProtocol<>(Option.none(), Vector.empty(), args -> f.apply((QName) args.get(0)), Vector.of(g1)); }
Example 7
Source File: XMLProtocol.java From ts-reaktive with MIT License | 4 votes |
/** * Reads a tag with any name, creating its result using [f]. */ public static <T> TagReadProtocol<T> readTagName(Function1<QName,T> f) { return new TagReadProtocol<>(Option.none(), Vector.empty(), args -> f.apply((QName) args.get(0))); }
Example 8
Source File: VavrUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void givenVavrFunction_whenWorks_thenCorrect() { Function1<Integer, Integer> square = (num) -> num * num; Integer result = square.apply(2); assertEquals(Integer.valueOf(4), result); }