com.linkedin.parseq.Task.flatMap()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(182)

本文整理了Java中com.linkedin.parseq.Task.flatMap()方法的一些代码示例,展示了Task.flatMap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Task.flatMap()方法的具体详情如下:
包路径:com.linkedin.parseq.Task
类名称:Task
方法名:flatMap

Task.flatMap介绍

[英]Equivalent to flatMap("flatMap", func).
[中]相当于flatMap(“flatMap”,func)。

代码示例

代码示例来源:origin: linkedin/parseq

Task<String> branch(final Function<Long, Task<String>> client, Long base) {
 return client.apply(base).flatMap("first", x -> Task.par(client.apply(base + 1), client.apply(base + 2))).flatMap("second", x -> client.apply(base + 3));
}

代码示例来源:origin: linkedin/parseq

Task<String> branch(final Function<Long, Task<String>> client, Long base) {
 return client.apply(base).flatMap("first", x -> Task.par(client.apply(base + 1), client.apply(base + 2))).flatMap("second", x -> client.apply(base + 3));
}

代码示例来源:origin: linkedin/parseq

/** Tasks composition */
//create extended summary for a person: "<first name> <last name> working at <company name>"
Task<String> createExtendedSummary(int id) {
 return fetchPerson(id)
   .flatMap("createExtendedSummary", this::createExtendedSummary);
}

代码示例来源:origin: linkedin/parseq

/**
 * Equivalent to {@code flatMap("flatMap", func)}.
 * @see #flatMap(String, Function1)
 */
default <R> Task<R> flatMap(final Function1<? super T, Task<R>> func) {
 return flatMap("flatMap: " + _taskDescriptor.getDescription(func.getClass().getName()), func);
}

代码示例来源:origin: linkedin/parseq

@Override
   public Task<Map<Integer, Try<String>>> taskForBatch(Integer group, Set<Integer> keys) {
    return super.taskForBatch(group, keys).flatMap(map -> delayedValue(map, 250, TimeUnit.MILLISECONDS));
   }
};

代码示例来源:origin: linkedin/parseq

@Override
   public Task<Map<Integer, Try<String>>> taskForBatch(Integer group, Set<Integer> keys) {
    return super.taskForBatch(group, keys).flatMap(map -> delayedValue(map, 250, TimeUnit.MILLISECONDS));
   }
};

代码示例来源:origin: linkedin/parseq

Task<List<String>> createConnectionsSummaries(int id) {
 return fetchPerson(id).flatMap("createConnectionsSummaries", person -> createConnectionsSummaries(person.getConnections()));
}

代码示例来源:origin: linkedin/parseq

@Test
public void testStackFrames() {
 Task<String> successTask = getSuccessTask();
 try {
  runAndWait("TestTask.testStackFrames", successTask.flatMap("nested", x -> getFailureTask()));
  fail("should have failed");
 } catch (Exception ex) {
  String stackTrace = Arrays.toString(ex.getCause().getStackTrace());
  assertFalse(stackTrace.contains("BaseTask"));
  assertTrue(stackTrace.contains("\"failure\""));
  assertTrue(stackTrace.contains("\"nested\""));
 }
}

代码示例来源:origin: linkedin/parseq

@Test
public void testFailureInNestedFlatMap() {
 final Exception failureReason = new Exception();
 Task<String> failing = getSuccessTask()
   .flatMap(Task::value)
   .flatMap(Task::value)
   .flatMap(i -> Task.failure(failureReason));
 Task<String> nested = failing
   .flatMap(Task::value)
   .flatMap(Task::value);
 try {
  runAndWait("AbstractTaskTest.testFailureInNestedFlatMap", nested);
  fail("should have failed");
 } catch (Exception ex) {
  assertTrue(nested.isFailed());
 }
 assertSame(nested.getError(), failureReason);
}

代码示例来源:origin: linkedin/parseq

public void testFlatMapFuncReturnNulll() {
 Task<String> task = getSuccessTask().flatMap(str -> null);
 runAndWaitException("AbstractTaskTest.testFlatMapFuncReturnNulll", task, RuntimeException.class);
 assertTrue(task.getError().getMessage().contains("returned null"));
}

代码示例来源:origin: linkedin/parseq

public void testFlatMap(int expectedNumberOfTasks) {
 Task<String> task = getSuccessTask().flatMap(str -> Task.callable("strlenstr", () -> String.valueOf(str.length())));
 runAndWait("AbstractTaskTest.testFlatMap", task);
 assertEquals(task.get(), String.valueOf(TASK_VALUE.length()));
 assertEquals(countTasks(task.getTrace()), expectedNumberOfTasks);
}

代码示例来源:origin: linkedin/parseq

@Override
protected void doRunExample(final Engine engine) throws Exception {
 final MockService<Integer> serviceX = getService();
 final Task<Integer> fetchX = fetchX(serviceX, 24);
 final Task<Integer> bigX = fetchX.flatMap("make x >= 42", x -> {
  if (x < 42) {
   final int toAdd = 42 - x;
   return add(x, toAdd);
  } else {
   return Task.value("x", x);
  }
 } );
 engine.run(bigX);
 bigX.await();
 System.out.println("Resulting value: " + bigX.get());
 printTracingResults(bigX);
}

代码示例来源:origin: linkedin/parseq

@Override
protected void doRunExample(final Engine engine) throws Exception {
 final MockService<Integer> serviceX = getService();
 final Task<Integer> fetchX = fetchX(serviceX, 420);
 final Task<Integer> bigX = fetchX.flatMap("make x >= 42", x -> {
  if (x < 42) {
   final int toAdd = 42 - x;
   return add(x, toAdd);
  } else {
   return Task.value("x", x);
  }
 } );
 engine.run(bigX);
 bigX.await();
 System.out.println("Resulting value: " + bigX.get());
 printTracingResults(bigX);
}

代码示例来源:origin: linkedin/parseq

private Task<?> task() {
 return Task.value("kldfjlajflskjflsjfslkajflkasj").map("length", s -> s.length()).map("+1", s -> s + 1)
   .map("+2", s -> s + 2).map("+3", s -> s + 3).shareable().recoverWith(t -> Task.value(0))
   .flatMap(x -> Task.value(x * 40)).map(x -> x -10);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testFlatMapTaskType() {
 Task<String> task = Task.value("Welcome");
 Task<String> flatMap = task.flatMap("+earth", s -> Task.callable(() -> s + " on earth!"));
 runAndWait("flatMapTaskType", flatMap);
 assertEquals(flatMap.getShallowTrace().getTaskType(), TaskType.FLATTEN.getName());
}

代码示例来源:origin: linkedin/parseq

@Test
public void testTaskReusedByTwoPlansAndMergedWithFlatMap() {
 final AtomicInteger counter = new AtomicInteger();
 Task<String> task = Task.callable(() -> {
  counter.incrementAndGet();
  return "hello";
 } );
 Task<String> plan1 = task.flatMap("+earth", s -> Task.callable(() -> s + " on earth!"));
 Task<String> plan2 = task.flatMap("+moon", s -> Task.callable(() -> s + " on moon!"));
 runAndWait("TestTaskReuse.testTaskReusedByTwoPlansAndMergedWithFlatMap-plan1", plan1);
 runAndWait("TestTaskReuse.testTaskReusedByTwoPlansAndMergedWithFlatMap-plan2", plan2);
 Task<Integer> length1 = plan1.map("length", s -> s.length());
 Task<Integer> length2 = plan2.map("length", s -> s.length());
 Task<Integer> merged = Task.par(length1, length2).map((a, b) -> a + b);
 runAndWait("TestTaskReuse.testTaskReusedByTwoPlansAndMergedWithFlatMap-merged", merged);
 assertEquals(counter.get(), 1);
 assertEquals(countTasks(merged.getTrace()), 9);
 assertEquals(countTasks(plan1.getTrace()), 5);
 assertEquals(countTasks(plan2.getTrace()), 5);
}

代码示例来源:origin: linkedin/parseq

@Override
protected void doRunExample(final Engine engine) throws Exception {
 final MockService<String> httpClient = getService();
 Task<String> stage1 = Task.par(fetchAndMap("http://www.bing.com", httpClient),
                 fetchAndMap("http://www.yahoo.com", httpClient))
                .map((a, b) -> a + b);
 Task<String> stage2 = Task.par(fetchAndMap("http://www.google.com", httpClient),
                 fetchAndMap("https://duckduckgo.com", httpClient))
                .map((a, b) -> a + b);
 Task<String> plan = stage1.flatMap("combine", s1 -> stage2.map(s2 -> s1 + s2));
 engine.run(plan);
 plan.await();
 printTracingResults(plan);
}

代码示例来源:origin: linkedin/parseq

private Task<int[]> mergeSort(final int[] toSort, final Range range) {
 if (range.size() == 0) {
  return Task.value("leaf", new int[0]);
 } else if (range.size() == 1) {
  return Task.value("leaf", new int[] { toSort[range.start()] });
 } else {
  // Neither base case applied, so recursively split this problem into
  // smaller problems and then merge the results.
  return Task.value("ranges", Tuples.tuple(range.firstHalf(), range.secondHalf()))
    .flatMap("split", ranges -> Task.par(mergeSort(toSort, ranges._1()), mergeSort(toSort, ranges._2()))
    .map("merge", parts -> merge(ranges._1(), parts._1(), ranges._2(), parts._2())));
 }
}

代码示例来源:origin: com.linkedin.parseq/parseq

/**
 * Equivalent to {@code flatMap("flatMap", func)}.
 * @see #flatMap(String, Function1)
 */
default <R> Task<R> flatMap(final Function1<? super T, Task<R>> func) {
 return flatMap("flatMap: " + _taskDescriptor.getDescription(func.getClass().getName()), func);
}

相关文章