-
Notifications
You must be signed in to change notification settings - Fork 23
/
functional.html
213 lines (180 loc) · 15.7 KB
/
functional.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"-->
<link rel="stylesheet" href="/gfx/bootstrap.min.css">
<link rel="stylesheet" href="/gfx/main.css">
<link rel="stylesheet" href="/gfx/code.css">
<title>Functions and Predicates</title>
</head>
<body class="page">
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-PMJSKV"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PMJSKV');</script>
<!-- End Google Tag Manager -->
<header>
<div class="container">
<a href="/">Immutables</a> ←
<h1>Functions and Predicates <iframe src="https://ghbtns.com/github-btn.html?user=immutables&repo=immutables&type=star&count=true&size=large" frameborder="0" scrolling="0" width="160px" height="30px"></iframe>
</h1>
</div>
</header>
<aside id="toc"></aside>
<section class="documentation">
<h3 id="overview">Overview</h3>
<p>Java 8 provides lambda expressions, which are very handy for functional programming:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">people</span><span class="o">.</span><span class="na">stream</span><span class="o">()</span>
<span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="n">p</span> <span class="o">-></span> <span class="n">p</span><span class="o">.</span><span class="na">name</span><span class="o">())</span>
</code></pre></div></div>
<p>They are particularly useful for filtering and transforming immutable value objects,
but many programmers, due to customer requirements or other reasons, cannot use Java 8. <a href="https://github.com/google/guava">Guava</a> provides many of the functional capabilities of Java 8 using their <a href="https://github.com/google/guava/wiki/FunctionalExplained"><code class="language-plaintext highlighter-rouge">Function</code> and <code class="language-plaintext highlighter-rouge">Predicate</code></a> interfaces:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">PersonNameFunction</span> <span class="kd">implements</span> <span class="nc">Function</span><span class="o"><</span><span class="nc">Person</span><span class="o">,</span> <span class="nc">String</span><span class="o">></span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">apply</span><span class="o">(</span><span class="nc">Person</span> <span class="n">person</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">person</span><span class="o">.</span><span class="na">name</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="nc">ImmutableList</span><span class="o"><</span><span class="nc">String</span><span class="o">></span> <span class="n">names</span> <span class="o">=</span> <span class="nc">FluentIterables</span><span class="o">.</span><span class="na">from</span><span class="o">(</span><span class="n">people</span><span class="o">)</span>
<span class="o">.</span><span class="na">transform</span><span class="o">(</span><span class="k">new</span> <span class="nc">PersonNameFunction</span><span class="o">());</span>
</code></pre></div></div>
<p>However, without lambdas, writing <code class="language-plaintext highlighter-rouge">Function</code>s and <code class="language-plaintext highlighter-rouge">Predicate</code>s is verbose and often results in functional code less clear than its imperative equivalent.</p>
<h3 id="generate-functions-and-predicates">Generate functions and predicates</h3>
<p>With <code class="language-plaintext highlighter-rouge">org.immutables:func</code>, you can easily generate Guava <code class="language-plaintext highlighter-rouge">Function</code>s and <code class="language-plaintext highlighter-rouge">Predicate</code>s for field access without the clutter. Special class <code class="language-plaintext highlighter-rouge">*Functions</code> is generated and provides function and predicate instances.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@Value</span><span class="o">.</span><span class="na">Immutable</span>
<span class="nd">@Functional</span>
<span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Person</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">String</span> <span class="nf">name</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">String</span> <span class="nf">jobTitle</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">boolean</span> <span class="nf">speaksFrench</span><span class="o">();</span>
<span class="o">}</span>
<span class="nc">List</span><span class="o"><</span><span class="nc">String</span><span class="o">></span> <span class="n">names</span> <span class="o">=</span> <span class="nc">Lists</span><span class="o">.</span><span class="na">transform</span><span class="o">(</span><span class="n">people</span><span class="o">,</span> <span class="nc">PersonFunctions</span><span class="o">.</span><span class="na">name</span><span class="o">());</span>
<span class="c1">// Boolean attributes become `Predicates`</span>
<span class="nc">Iterable</span><span class="o"><</span><span class="nc">Person</span><span class="o">></span> <span class="n">frenchSpeakers</span> <span class="o">=</span> <span class="nc">Iterables</span><span class="o">.</span><span class="na">filter</span><span class="o">(</span><span class="n">people</span><span class="o">,</span> <span class="nc">PersonFunctions</span><span class="o">.</span><span class="na">speaksFrench</span><span class="o">());</span>
</code></pre></div></div>
<p>By placing <code class="language-plaintext highlighter-rouge">@Functional</code> on a method instead of the class, you can restrict which <code class="language-plaintext highlighter-rouge">Functions</code> and <code class="language-plaintext highlighter-rouge">Predicates</code> are generated:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@Value</span><span class="o">.</span><span class="na">Immutable</span>
<span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Person</span> <span class="o">{</span>
<span class="nd">@Functional</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">String</span> <span class="nf">name</span><span class="o">();</span>
<span class="c1">// no Function will be generated for jobTitle</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">String</span> <span class="nf">jobTitle</span><span class="o">();</span>
<span class="nd">@Functional</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">boolean</span> <span class="nf">speaksFrench</span><span class="o">();</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Of course, you can use static imports to further reduce clutter.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">...PersonFunctions.*</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">...FluentIterable.*</span><span class="o">;</span>
<span class="nc">List</span><span class="o"><</span><span class="nc">String</span><span class="o">></span> <span class="n">frenchSpeakerNames</span> <span class="o">=</span>
<span class="n">from</span><span class="o">(</span><span class="n">people</span><span class="o">)</span>
<span class="o">.</span><span class="na">filter</span><span class="o">(</span><span class="n">speaksFrench</span><span class="o">())</span>
<span class="o">.</span><span class="na">transform</span><span class="o">(</span><span class="n">name</span><span class="o">())</span>
<span class="o">.</span><span class="na">toList</span><span class="o">();</span>
</code></pre></div></div>
<h3 id="generate-functions-with-bound-parameters">Generate functions with bound parameters</h3>
<p><code class="language-plaintext highlighter-rouge">@Functional.BindParameters</code> annotation can be place on non-accessor methods of abstract value type to generate function to which parameters can be bound.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@Value</span><span class="o">.</span><span class="na">Immutable</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Xyz</span> <span class="o">{</span>
<span class="nd">@Functional</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">String</span> <span class="nf">getX</span><span class="o">();</span>
<span class="nd">@Functional</span><span class="o">.</span><span class="na">BindParameters</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">computeZ</span><span class="o">(</span><span class="nc">String</span> <span class="n">y</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nf">getX</span><span class="o">()</span> <span class="o">+</span> <span class="n">y</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="c1">// Generated functions</span>
<span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="nc">XyzFunctions</span> <span class="o">{</span>
<span class="o">...</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Function</span><span class="o"><</span><span class="nc">Xyz</span><span class="o">,</span> <span class="nc">String</span><span class="o">></span> <span class="nf">computeZ</span><span class="o">(</span><span class="nc">String</span> <span class="n">y</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nc">Function</span><span class="o"><</span><span class="nc">Xyz</span><span class="o">,</span> <span class="nc">String</span><span class="o">>()</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">apply</span><span class="o">(</span><span class="nc">Xyz</span> <span class="n">input</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">input</span><span class="o">.</span><span class="na">computeZ</span><span class="o">(</span><span class="n">y</span><span class="o">);</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">toString</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="s">"XyzFunctions.computeZ(y)"</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="c1">// use as</span>
<span class="nc">Function</span><span class="o"><</span><span class="nc">Xyz</span><span class="o">,</span> <span class="nc">String</span><span class="o">></span> <span class="n">fn</span> <span class="o">=</span> <span class="nc">XyzFunctions</span><span class="o">.</span><span class="na">computeZ</span><span class="o">(</span><span class="s">"Y"</span><span class="o">);</span>
</code></pre></div></div>
<h3 id="dependencies">Dependencies</h3>
<p>This feature has the following compile-time dependencies in addition to a runtime dependency on Guava:</p>
<ul>
<li><a href="https://search.maven.org/artifact/org.immutables/value/2.10.1/jar">org.immutables:value:2.10.1</a></li>
<li><a href="https://search.maven.org/artifact/org.immutables/func/2.10.1/jar">org.immutables:func:2.10.1</a></li>
</ul>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>value<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.10.1<span class="nt"></version></span>
<span class="nt"><scope></span>provided<span class="nt"></scope></span>
<span class="nt"></dependency></span>
<span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>func<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.10.1<span class="nt"></version></span>
<span class="nt"><scope></span>provided<span class="nt"></scope></span>
<span class="nt"></dependency></span>
</code></pre></div></div>
</section>
<footer class="jumbotron">
<div class="container">
<h2 id="guides">Guides</h2>
<ul>
<li><a href="/getstarted.html">Get started!</a></li>
<li><a href="/intro.html">Inception</a></li>
<li><a href="/immutable.html">Immutable objects</a></li>
<li><a href="/factory.html">Factory builders</a></li>
<li><a href="/functional.html">Functions and Predicates (for Java 7)</a></li>
<li><a href="/style.html">Style customization</a></li>
<li><a href="/json.html">JSON serialization</a></li>
<li><a href="/criteria.html">Criteria</a></li>
<li><a href="/mongo.html">MongoDB repositories</a></li>
<li><a href="/dynamodb.html">DynamoDB integration</a></li>
<li><a href="/encoding.html">Encoding: Customizing attributes and builders (experimental)</a></li>
<li><a href="/apt.html">Using annotation processor in IDE</a></li>
</ul>
<h2 id="get-involved">Get involved</h2>
<ul>
<li>Clone source repository, contribute bug reports and fixes on <a href="https://github.com/immutables/immutables">GitHub immutables/immutables</a></li>
<li>Issue reports, questions and feedback is welcome on issue tracker <a href="https://github.com/immutables/immutables/issues">GitHub immutables/immutables/issues</a></li>
<li>News and announcements on twitter <a href="https://twitter.com/ImmutablesOrg">@ImmutablesOrg</a></li>
</ul>
<p><a href="/license.html">Apache License 2.0</a></p>
<!--<div><h2>Posts</h2>
<ul>
</ul>
</div>-->
</div>
</footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script defer src="/gfx/jquery.toc.min.js"></script>
<script>
$(function() {
$('#toc').toc({
container: '.documentation',
selectors: 'h1,h2,h3,h4',
anchorName: function(i, heading, prefix) {
heading = $(heading).text();
if (heading.trim) heading = heading.trim();
return heading.toLowerCase().replace(/ /g, '-').replace(/[^a-z^\-]+/g, '');
},
})
})
</script>
</body>
</html>