From eeed7f2d2eed53bb349b20ec93760612ed85fd74 Mon Sep 17 00:00:00 2001 From: samskivert Date: Thu, 30 Sep 2010 21:02:37 +0000 Subject: [PATCH] More functional utilities from Mr Greenwell (with some name tweaks by MDB). He so wants to be using Scala. For example, in Java: Folds.sum(0, values) where values must implement Iterable and all you get is sum. In Scala: scala> Array(1, 2, 3).getClass // a real array res1: java.lang.Class[_] = class [I scala> Array(1, 2, 3).sum res2: Int = 6 scala> Vector(1, 2, 3).sum // more like ArrayList res3: Int = 6 scala> Map(1 -> 2, 3 -> 4).keysIterator.sum // even works on Iterator res4: Int = 4 and you can do arbitrary folds just as easily: scala> Array(1, 2, 3).reduceLeft(math.max) res5: Int = 3 scala> Array(2, 3, 4).reduceLeft(_*_) res6: Int = 24 Want to specify the type of the result? Can do: scala> Array(1, 2, 3).foldLeft(0L)(_+_) res7: Long = 6 Have a list of maps you need to merge? No problem: scala> List(Map(1 -> 2), Map(2 -> 3), Map(3 -> 4)).reduceLeft(_++_) res8: scala.collection.immutable.Map[Int,Int] = Map((1,2), (2,3), (3,4)) Functional programming truly does start with fun! git-svn-id: https://samskivert.googlecode.com/svn/trunk@2895 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/main/java/com/samskivert/util/Folds.java | 112 +++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/main/java/com/samskivert/util/Folds.java diff --git a/src/main/java/com/samskivert/util/Folds.java b/src/main/java/com/samskivert/util/Folds.java new file mode 100644 index 00000000..156331fb --- /dev/null +++ b/src/main/java/com/samskivert/util/Folds.java @@ -0,0 +1,112 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2010 Michael Bayne, et al. +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +import java.math.BigInteger; +import java.util.Collection; +import java.util.Map; + +/** + * Various useful folds over iterables. + */ +public class Folds +{ + /** + * Sums the supplied collection of numbers to an int with a left to right fold. + */ + public static int sum (int zero, Iterable values) + { + for (Number value : values) { + zero += value.intValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a long with a left to right fold. + */ + public static long sum (long zero, Iterable values) + { + for (Number value : values) { + zero += value.longValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a float with a left to right fold. + */ + public static float sum (float zero, Iterable values) + { + for (Number value : values) { + zero += value.floatValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a double with a left to right fold. + */ + public static double sum (double zero, Iterable values) + { + for (Number value : values) { + zero += value.doubleValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a bigint with a left to right fold. + */ + public static BigInteger sum (BigInteger zero, Iterable values) + { + for (BigInteger value : values) { + zero = zero.add(value); + } + return zero; + } + + /** + * Merges the supplied collections into zero using a left to right fold of + * {@link Collection#addAll}. + */ + public static > C addAll ( + C zero, Iterable> values) + { + for (Collection value : values) { + zero.addAll(value); + } + return zero; + } + + /** + * Merges the supplied maps into zero using a left to right fold of + * {@link Map#putAll}. + */ + public static > M putAll ( + M zero, Iterable> values) + { + for (Map value : values) { + zero.putAll(value); + } + return zero; + } +}