All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unit16.z.math.ToDouble Maven / Gradle / Ivy

package com.unit16.z.math;

import com.unit16.z.indexed.Indexed;

public interface ToDouble {

	public double apply(A val);
	
	public static final class DSL
	{
		private final ToDouble td;
		public DSL(ToDouble td_) { td = td_; }
		
		public final double[] from(Indexed idx)
		{
			final double[] r = new double[idx.size()];
			into(idx, r);
			return r;
		}
		
		public void into(Indexed idx, double[] dst)
		{
			final int n = idx.size();
			for (int i = 0; i < n; i++) { dst[i] = td.apply(idx.get(i)); }
		}
		
		public double min(Iterable src)
		{
			double m = Double.MAX_VALUE;
			for (B b : src) { m = Math.min(m, td.apply(b)); }
			return m;
		}
		
		public double max(Iterable src)
		{
			double m = Double.MIN_VALUE;
			for (B b : src) { m = Math.max(m, td.apply(b)); }
			return m;
		}
	}
}