簡體   English   中英

番石榴是否具有Python的reduce功能?

[英]Does guava have an equivalent to Python's reduce function?

guava(或其他java庫)在Python中是否有類似reduce()函數的東西?

我正在尋找像這樣的東西http://docs.python.org/library/functions.html#reduce

不,它可能最終,盡管像這樣的功能不是番石榴的核心焦點。 看到這個問題

我還沒有設法找到任何支持mapreduce Java集合庫。 (我在並行/分布式處理框架中排除map / reduce功能......因為這些框架需要一個“大”問題才值得。)

可能,這種“缺乏”的原因是沒有閉包的map / reduce編碼太麻煩了。 太多的樣板代碼,太多的重量級語法。 因為在簡單集合上使用map / reduce原語的要點是讓你的代碼簡單而優雅......


@CurtainDog貢獻了一個鏈接lambdaj 這就是OP所追求的那種東西(盡管沒有專門稱為reduce的方法)。 但它說明了我對樣板文件的看法。 請注意,許多高階操作涉及創建擴展一個或另一個Closure類的類。

(FWIW,我認為Lambda.aggregate(...)方法的lambdaj模擬reduce 。)

Java 8流允許您執行此操作。

mylist.stream().map((x) -> x + 1).reduce((a,b) -> a + b)

有關更多信息,請訪問: http//docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

番石榴已經變換 (地圖)。 似乎還缺少減少?

我最近提交了一個問題 ,我要求/討論過類似的問題。 這是我實施中需要的

/**
 * Aggregate the selected values from the supplied {@link Iterable} using
 * the provided selector and aggregator functions.
 * 
 * @param <I>
 *            the element type over which to iterate
 * @param <S>
 *            type of the values to be aggregated
 * @param <A>
 *            type of the aggregated value
 * @param data
 *            elements for aggregation
 * @param selectorFunction
 *            a selector function that extracts the values to be aggregated
 *            from the elements
 * @param aggregatorFunction
 *            function that performs the aggregation on the selected values
 * @return the aggregated value
 */
public static <I, S, A> A aggregate(final Iterable<I> data,
    final Function<I, S> selectorFunction,
    final Function<Iterable<S>, A> aggregatorFunction){
    checkNotNull(aggregatorFunction);
    return aggregatorFunction.apply(
        Iterables.transform(data, selectorFunction)
    );
}

(選擇器函數可以拉取值從對象聚合到查詢,但在很多情況下它將是Functions.identity() ,即對象本身是聚合的)

這不是經典的折疊,但它需要一個Function<Iterable<X>,X>來完成工作。 但由於實際代碼是單行代碼,我選擇了請求一些標准聚合器函數(我將它們放在一個類似AggregatorsAggregatorFunctions甚至Functions.Aggregators ):

/** A Function that returns the average length of the Strings in an Iterable. */
public static Function<Iterable<String>,Integer> averageLength()

/** A Function that returns a BigDecimal that corresponds to the average
    of all numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigDecimal> averageOfFloats()

/** A Function that returns a BigInteger that corresponds to the average
    of all numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigInteger> averageOfIntegers()

/** A Function that returns the length of the longest String in an Iterable. */    
public static Function<Iterable<String>,Integer> maxLength()

/** A Function that returns the length of the shortest String in an Iterable. */
public static Function<Iterable<String>,Integer> minLength()

/** A Function that returns a BigDecimal that corresponds to the sum of all
    numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigDecimal> sumOfFloats()

/** A Function that returns a BigInteger that corresponds to the integer sum
    of all numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigInteger> sumOfIntegers()

(您可以在問題中看到我的示例實現)

這樣,你可以做這樣的事情:

int[] numbers = { 1, 5, 6, 9, 11111, 54764576, 425623 };
int sum = Aggregators.sumOfIntegers().apply(Ints.asList(numbers)).intValue();

這絕對不是你要求的,但在許多情況下它會變得更容易,並且會與你的請求重疊(即使方法不同)。

絕地有減少行動。 Jedi還通過使用注釋為您生成仿函數來幫助減少鍋爐板。 見這些例子

使用Totally Lazy ,它可以實現所有這些功能。 它基本上復制了Clojure的整個功能方法。

我開發了一個用標准J2SE進行map / filter / reduce的庫。 對不起,這是法語,但谷歌翻譯你可以閱讀: http//caron-yann.developpez.com/tutoriels/java/fonction-object-design-pattern-attendant-closures-java-8/

你可以這樣使用:

int sum = dogs.filter(new Predicate<Arguments2<Dog, Integer>>() {

    @Override
    public Boolean invoke(Arguments2<Dog, Integer> arguments) {
        // filter on male
        return arguments.getArgument1().getGender() == Dog.Gender.MALE;
    }
}).<Integer>map(new Function<Integer, Arguments2<Dog, Integer>>() {

    @Override
    public Integer invoke(Arguments2<Dog, Integer> arguments) {
        // get ages
        return arguments.getArgument1().getAge();
    }
}).reduce(new Function<Integer, Arguments2<Integer, Integer>>() {

    @Override
    public Integer invoke(Arguments2<Integer, Integer> arguments) {
        // sum âges
        return arguments.getArgument1() + arguments.getArgument2();
    }
});

System.out.println("Le cumul de l'âge des mâles est de : " + sum + " ans");

享受這個幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM