Functional java: what is this P1?

I am looking for functional Java, and I do not understand what P1 is . Can someone explain and / or give an example?

(background: I know what currying and closing are)

+7
source share
3 answers

This runs directly from the Google Code project for functional Java :

Types of a merged union (tuples) are products of other types. Grades 1-8 are provided (fj.P1 - fj.P8). They are useful when you want to return more than one value from a function or when you want to accept multiple values ​​when implementing an interface method that takes only one argument. They can also be used to obtain products for other types of data, such as lists (zip function).
// Regular Java public Integer albuquerqueToLA(Map<String, Map<String, Integer>> map) { Map m = map.get("Albuquerque"); if (m != null) return m.get("Los Angeles"); // May return null. } // Functional Java with product and option types. public Option<Integer> albuquerqueToLA(TreeMap<P2<String, String>, Integer>() map) { return m.get(p("Albuquerque", "Los Angeles")); } 
+6
source

P1 looks like a 1-element, trivial type of product . In Haskell, it will be written as:

 data P1 a = P1 a 

( Identity type in Haskell).

those. this is a container that contains some other type a .

This type also implements the simplest monad, Identity , which allows you to opaquely apply functions to the contents of a field.

Computably, there is no reason to use the Identity monad instead of the much simpler act of simply applying functions to their arguments, but this can be useful in designing monad transformer stacks.

The monadic realization of the identical monad is trivial,

 return a = P1 a (P1 m) >>= k = km 

As you can see, this is just a functional application.

+4
source

aha, found this post :

 >>> Also, P1 is potentially lazy. We use it for the implementation of >>> Stream, for example. 

Therefore, instead of immediately returning the type T, I can get something that returns P1<T> , like Google Collections Supplier<T> , and it calculates the contained value only when P1._1() called.

(yes, this Lazy Error Handling blog post in Java was interesting too .....)

+4
source

All Articles