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

com.aol.cyclops.functions.Functions Maven / Gradle / Ivy

There is a newer version: 7.3.1
Show newest version
package com.aol.cyclops.functions;

import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import com.aol.cyclops.lambda.monads.AnyM;

public class Functions extends Uncurry {
	/**
	 * Convert a Supplier into one that caches it's result
	 * 
	 * @param s Supplier to memoise
	 * @return Memoised Supplier
	 */
	public static  Supplier memoiseSupplier(Supplier s){
		return Memoise.memoiseSupplier(s);
	}
	/**
	 * Convert a Callable into one that caches it's result
	 * 
	 * @param s Callable to memoise
	 * @return Memoised Callable
	 */
	public static  Callable memoiseCallable(Callable s){
		return Memoise.memoiseCallable(s);
	}
	
	/**
	 * Convert a Function into one that caches it's result
	 * 
	 * @param fn Function to memoise
	 * @return Memoised Function
	 */
	public static  Function memoiseFunction(Function fn){
		return Memoise.memoiseFunction(fn);
	}
	
	/**
	 * Convert a BiFunction into one that caches it's result
	 * 
	 * @param fn BiFunction to memoise
	 * @return Memoised BiFunction
	 */
	public static  BiFunction memoiseBiFunction(BiFunction fn) {
		return Memoise.memoiseBiFunction(fn);
	}
	/**
	 * Convert a TriFunction into one that caches it's result
	 * 
	 * @param fn TriFunction to memoise
	 * @return Memoised TriFunction
	 */
	public static  TriFunction memoiseTriFunction(TriFunction fn) {
		return Memoise.memoiseTriFunction(fn);
	}
	/**
	 * Convert a QuadFunction into one that caches it's result
	 * 
	 * @param fn QuadFunction to memoise
	 * @return Memoised TriFunction
	 */
	public static  QuadFunction memoiseQuadFunction(QuadFunction fn) {
		return Memoise.memoiseQuadFunction(fn);
	}
	/**
	 * Convert a Predicate into one that caches it's result
	 * 
	 * @param p Predicate to memoise
	 * @return Memoised Predicate
	 */
	public static  Predicate memoisePredicate(Predicate p) {
		return Memoise.memoisePredicate(p);
	}
	/**
	 * Lift a function so it accepts a Monad and returns a Monad (simplex view of a wrapped Monad)
	 * Simplex view simplifies type related challenges. The actual native type is not specified here.
	 * 
	 * @param fn
	 * @return
	 */
	public static  Function,AnyM> liftM(Function fn){
		return LiftMFunctions.liftM(fn);
	}
	
	/**
	 * Lift a function so it accepts a Monad and returns a Monad (simplex view of a wrapped Monad)
	 * AnyM view simplifies type related challenges. The actual native type is not specified here.
	 * 
	 * Example of lifting an existing method to add error handling (Try Monad) and iteration (Stream monad)
	 * 
	 * {@code
	 	//using Lombok val to simplify verbose generics
	    val divide = LiftMFunctions.liftM2(this::divide);
		
		AnyM result = divide.apply(anyM(Try.of(2, ArithmeticException.class)), anyM(Stream.of(10,1,2,3)));
		
		assertThat(result.,ArithmeticException>>unwrapMonad().get(),equalTo(Arrays.asList(0, 2, 1, 0)));
	  
	  	private Integer divide(Integer a, Integer b){
			return a/b;
		}
	 * }
	 * 
* * @param fn * @return */ public static BiFunction,AnyM,AnyM> liftM2(BiFunction fn){ return LiftMFunctions.liftM2(fn); } /** * Lift a TriFunction into Monadic form. A good use case it to take an existing method and lift it so it can accept and return monads * *
	 * {@code
	 * TriFunction,AnyM,AnyM,AnyM> fn = liftM3(this::myMethod);
	 *    
	 * }
	 * 
* * Now we can execute the Method with Streams, Optional, Futures, Try's etc to transparently inject iteration, null handling, async execution and / or error handling * * @param fn Function to lift * @return Lifted function */ public static TriFunction,AnyM,AnyM,AnyM> liftM3(TriFunction fn){ return LiftMFunctions.liftM3(fn); } /** * Lift a QuadFunction into Monadic form. * * @param fn Quad funciton to lift * @return Lifted Quad function */ public static QuadFunction,AnyM,AnyM,AnyM,AnyM> liftM4(QuadFunction fn){ return LiftMFunctions.liftM4(fn); } /** * Lift a QuintFunction (5 parameters) into Monadic form * * @param fn Function to lift * @return Lifted Function */ public static QuintFunction,AnyM,AnyM,AnyM,AnyM,AnyM> liftM5(QuintFunction fn){ return LiftMFunctions.liftM5(fn); } /** * Lift a Curried Function {@code (2 levels a->b->fn.apply(a,b) )} into Monadic form * * @param fn Function to lift * @return Lifted function */ public static Function,Function,AnyM>> liftM2(Function> fn){ return LiftMFunctions.liftM2(fn); } /** * Lift a Curried Function {@code (3 levels a->b->c->fn.apply(a,b,c) )} into Monadic form * * @param fn Function to lift * @return Lifted function */ public static Function,Function,Function,AnyM>>> liftM3(Function>> fn){ return LiftMFunctions.liftM3(fn); } /** * Lift a Curried Function {@code (4 levels a->b->c->d->fn.apply(a,b,c,d) )} into Monadic form * * @param fn Function to lift * @return Lifted function */ public static Function,Function,Function,Function,AnyM>>>> liftM4(Function>>> fn){ return LiftMFunctions.liftM4(fn); } /** * Lift a Curried Function {@code (5 levels a->b->c->d->e->fn.apply(a,b,c,d,e) )} into Monadic form * * @param fn Function to lift * @return Lifted function */ public static Function,Function,Function,Function,Function,AnyM>>>>> liftM5(Function>>>> fn){ return LiftMFunctions.liftM5(fn); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy