use of scala.Function2 in project enumerable by hraberg.
the class ClojureTest method interactingWithScala.
@SuppressWarnings("unchecked")
@Test
public void interactingWithScala() throws Exception {
ScalaInterpreter scala = ScalaTest.getScalaInterpreter();
Function2<Long, Long, Long> f = (Function2<Long, Long, Long>) scala.eval("(n: Long, m: Long) => n * m");
IFn times = toIFn(LambdaScala.toFn2(f));
assertEquals(6L, times.invoke(2L, 3L));
defn("times-scala", times);
assertEquals(120L, clj.eval("(reduce times-scala 1 [1, 2, 3, 4, 5])"));
}
use of scala.Function2 in project enumerable by hraberg.
the class JavaScriptTest method interactingWithScala.
@SuppressWarnings("unchecked")
@Test
public void interactingWithScala() throws Exception {
ScalaInterpreter scala = ScalaTest.getScalaInterpreter();
Function2<Integer, Integer, Integer> f = (Function2<Integer, Integer, Integer>) scala.eval("(n: Double, m: Double) => n * m");
js.put("f", toFunction(LambdaScala.toFn2(f)));
assertEquals(6.0, js.eval("f(2, 3)"));
}
use of scala.Function2 in project enumerable by hraberg.
the class JRubyTest method interactingWithScala.
@SuppressWarnings("unchecked")
@Test
public void interactingWithScala() throws Exception {
Ruby ruby = Ruby.getGlobalRuntime();
ScalaInterpreter scala = ScalaTest.getScalaInterpreter();
Function2<Integer, Integer, Integer> f = (Function2<Integer, Integer, Integer>) scala.eval("(n: Long, m: Long) => n * m");
RubyProc proc = toProc(LambdaScala.toFn2(f));
assertEquals(ruby.newFixnum(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
rb.put("block", proc);
assertEquals(120L, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
use of scala.Function2 in project enumerable by hraberg.
the class GroovyTest method interactingWithScala.
@SuppressWarnings("unchecked")
@Test
public void interactingWithScala() throws Exception {
ScalaInterpreter scala = ScalaTest.getScalaInterpreter();
Function2<Integer, Integer, Integer> f = (Function2<Integer, Integer, Integer>) scala.eval("(n: Int, m: Int) => n * m");
Closure<?> closure = toClosure(LambdaScala.toFn2(f));
groovy.put("closure", closure);
assertEquals(120, groovy.eval("[1, 2, 3, 4, 5].inject(1, closure)"));
}
use of scala.Function2 in project samza by apache.
the class KafkaSystemAdmin method getSSPMetadata.
/**
* Given a set of SystemStreamPartition, fetch metadata from Kafka for each
* of them, and return a map from ssp to SystemStreamPartitionMetadata for
* each of them. This method will return null for oldest and newest offsets
* if a given SystemStreamPartition is empty. This method will block and
* retry indefinitely until it gets a successful response from Kafka.
* @param ssps a set of strings of SSP
* @param retryBackoff retry backoff strategy
* @return a map from ssp to sspMetadata which has offsets
*/
Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> getSSPMetadata(Set<SystemStreamPartition> ssps, ExponentialSleepStrategy retryBackoff) {
LOG.info("Fetching SSP metadata for: {}", ssps);
List<TopicPartition> topicPartitions = ssps.stream().map(ssp -> new TopicPartition(ssp.getStream(), ssp.getPartition().getPartitionId())).collect(Collectors.toList());
Function1<ExponentialSleepStrategy.RetryLoop, Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>> fetchTopicPartitionMetadataOperation = new AbstractFunction1<ExponentialSleepStrategy.RetryLoop, Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>>() {
@Override
public Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> apply(ExponentialSleepStrategy.RetryLoop loop) {
OffsetsMaps topicPartitionsMetadata = fetchTopicPartitionsMetadata(topicPartitions);
Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> sspToSSPMetadata = new HashMap<>();
for (SystemStreamPartition ssp : ssps) {
String oldestOffset = topicPartitionsMetadata.getOldestOffsets().get(ssp);
String newestOffset = topicPartitionsMetadata.getNewestOffsets().get(ssp);
String upcomingOffset = topicPartitionsMetadata.getUpcomingOffsets().get(ssp);
sspToSSPMetadata.put(ssp, new SystemStreamMetadata.SystemStreamPartitionMetadata(oldestOffset, newestOffset, upcomingOffset));
}
loop.done();
return sspToSSPMetadata;
}
};
Function2<Exception, ExponentialSleepStrategy.RetryLoop, BoxedUnit> onExceptionRetryOperation = new AbstractFunction2<Exception, ExponentialSleepStrategy.RetryLoop, BoxedUnit>() {
@Override
public BoxedUnit apply(Exception exception, ExponentialSleepStrategy.RetryLoop loop) {
if (loop.sleepCount() < MAX_RETRIES_ON_EXCEPTION) {
LOG.warn(String.format("Fetching SSP metadata for: %s threw an exception. Retrying.", ssps), exception);
} else {
LOG.error(String.format("Fetching SSP metadata for: %s threw an exception.", ssps), exception);
loop.done();
throw new SamzaException(exception);
}
return null;
}
};
Function0<Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>> fallbackOperation = new AbstractFunction0<Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata>>() {
@Override
public Map<SystemStreamPartition, SystemStreamMetadata.SystemStreamPartitionMetadata> apply() {
throw new SamzaException("Failed to get SSP metadata");
}
};
return retryBackoff.run(fetchTopicPartitionMetadataOperation, onExceptionRetryOperation).getOrElse(fallbackOperation);
}
Aggregations