| <!doctype html> |
| <!-- Generated by FreeMarker/Docgen from DocBook --> |
| <html lang="en" class="page-type-section"> |
| <head prefix="og: http://ogp.me/ns#"> |
| <meta charset="utf-8"> |
| <title>Built-ins for sequences - Apache FreeMarker Manual</title> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| <meta name="format-detection" content="telephone=no"> |
| <meta property="og:site_name" content="Apache FreeMarker Manual"> |
| <meta property="og:title" content="Built-ins for sequences"> |
| <meta property="og:locale" content="en_US"> |
| <meta property="og:url" content="https://freemarker.apache.org/docs/ref_builtins_sequence.html"> |
| <link rel="canonical" href="https://freemarker.apache.org/docs/ref_builtins_sequence.html"> |
| <link rel="icon" href="favicon.png" type="image/png"> |
| <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:500,700,400,300|Droid+Sans+Mono"> |
| <link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1707770044859"> |
| <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/cookie-bar/cookiebar-latest.min.js"></script> |
| </head> |
| <body itemscope itemtype="https://schema.org/Code"> |
| <meta itemprop="url" content="https://freemarker.apache.org/docs/"> |
| <meta itemprop="name" content="Apache FreeMarker Manual"> |
| |
| <!--[if lte IE 9]> |
| <div class="oldBrowserWarning" style="display: block"> |
| Unsupported web browser - Use a modern browser to view this website! |
| </div> |
| <![endif]--> <div class="oldBrowserWarning"> |
| Unsupported web browser - Use a modern browser to view this website! |
| </div> |
| <div class="header-top-bg"><div class="site-width header-top"><div id="hamburger-menu" role="button"></div> <div class="logo"> |
| <a href="https://freemarker.apache.org" role="banner"><img itemprop="image" src="logo.png" alt="FreeMarker"></a> </div> |
| <ul class="tabs"><li><a href="https://freemarker.apache.org/">Home</a></li><li class="current"><a href="index.html">Manual</a></li><li><a class="external" href="api/index.html">Java API</a></li></ul><ul class="secondary-tabs"><li><a class="tab icon-heart" href="https://freemarker.apache.org/contribute.html" title="Contribute"><span>Contribute</span></a></li><li><a class="tab icon-bug" href="https://issues.apache.org/jira/projects/FREEMARKER" title="Report a Bug"><span>Report a Bug</span></a></li><li><a class="tab icon-download" href="https://freemarker.apache.org/freemarkerdownload.html" title="Download"><span>Download</span></a></li></ul></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">Manual</a><div class="navigation-header"></div><form method="get" class="search-form" action="search-results.html"><fieldset><legend class="sr-only">Search form</legend><label for="search-field" class="sr-only">Search query</label><input id="search-field" name="q" type="search" class="search-input" placeholder="Search" spellcheck="false" autocorrect="off" autocomplete="off"><button type="submit" class="search-btn"><span class="sr-only">Search</span></button></fieldset></form></div><div class="site-width breadcrumb-row"> <div class="breadcrumbs"> |
| <ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">Apache FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref.html"><span itemprop="name">Template Language Reference</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_builtins.html"><span itemprop="name">Built-in Reference</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_builtins_sequence.html"><span itemprop="name">Built-ins for sequences</span></a></li></ul> </div> |
| <div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul><li><a href="alphaidx.html">Alpha. index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="dgui_template_exp.html#exp_cheatsheet">Expressions</a></li><li><a href="ref_builtins_alphaidx.html">?builtins</a></li><li><a href="ref_directive_alphaidx.html">#directives</a></li><li><a href="ref_specvar.html">.spec_vars</a></li><li><a href="app_faq.html">FAQ</a></li></ul></div></div></div> <div class="main-content site-width"> |
| <div class="content-wrapper"> |
| <div id="table-of-contents-wrapper" class="col-left"> |
| <script>var breadcrumb = ["Apache FreeMarker Manual","Template Language Reference","Built-in Reference","Built-ins for sequences"];</script> |
| <script src="toc.js?1707770044859"></script> |
| <script src="docgen-resources/main.min.js?1707770044859"></script> |
| </div> |
| <div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="ref_builtins_boolean.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_builtins_hash.html"><span>Next</span></a></div><div class="title-wrapper"> |
| <h1 class="content-header header-section1" id="ref_builtins_sequence" itemprop="headline">Built-ins for sequences</h1> |
| </div></div><div class="page-menu"> |
| <div class="page-menu-title">Page Contents</div> |
| <ul><li><a class="page-menu-link" href="#ref_builtin_chunk" data-menu-target="ref_builtin_chunk">chunk</a></li><li><a class="page-menu-link" href="#ref_builtin_drop_while" data-menu-target="ref_builtin_drop_while">drop_while</a></li><li><a class="page-menu-link" href="#ref_builtin_filter" data-menu-target="ref_builtin_filter">filter</a></li><li><a class="page-menu-link" href="#ref_builtin_first" data-menu-target="ref_builtin_first">first</a></li><li><a class="page-menu-link" href="#ref_builtin_join" data-menu-target="ref_builtin_join">join</a></li><li><a class="page-menu-link" href="#ref_builtin_last" data-menu-target="ref_builtin_last">last</a></li><li><a class="page-menu-link" href="#ref_builtin_map" data-menu-target="ref_builtin_map">map</a></li><li><a class="page-menu-link" href="#ref_builtin_min_max" data-menu-target="ref_builtin_min_max">min, max</a></li><li><a class="page-menu-link" href="#ref_builtin_reverse" data-menu-target="ref_builtin_reverse">reverse</a></li><li><a class="page-menu-link" href="#ref_builtin_seq_contains" data-menu-target="ref_builtin_seq_contains">seq_contains</a></li><li><a class="page-menu-link" href="#ref_builtin_seq_index_of" data-menu-target="ref_builtin_seq_index_of">seq_index_of</a></li><li><a class="page-menu-link" href="#ref_builtin_seq_last_index_of" data-menu-target="ref_builtin_seq_last_index_of">seq_last_index_of</a></li><li><a class="page-menu-link" href="#ref_builtin_size" data-menu-target="ref_builtin_size">size</a></li><li><a class="page-menu-link" href="#ref_builtin_sort" data-menu-target="ref_builtin_sort">sort</a></li><li><a class="page-menu-link" href="#ref_builtin_sort_by" data-menu-target="ref_builtin_sort_by">sort_by</a></li><li><a class="page-menu-link" href="#ref_builtin_take_while" data-menu-target="ref_builtin_take_while">take_while</a></li></ul> </div> |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_chunk">chunk</h2> |
| |
| |
| |
| |
| |
| |
| |
| |
| <p>This built-in splits a sequence into multiple sequences of the |
| size given with the 1st parameter to the built-in (like |
| <code class="inline-code">mySeq?chunk(3)</code>). The result is the sequence of |
| these sequences. The last sequence is possibly shorter than the |
| given size, unless the 2nd parameter is given (like |
| <code class="inline-code">mySeq?chunk(3, '-')</code>), that is the item used to |
| make up the size of the last sequence to the given size. |
| Example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']> |
| |
| <#list seq?chunk(4) as row> |
| <#list row as cell>${cell} </#list> |
| </#list> |
| |
| <#list seq?chunk(4, '-') as row> |
| <#list row as cell>${cell} </#list> |
| </#list></pre> </div> |
| |
| |
| <p>The output will be:</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body"> |
| a b c d |
| e f g h |
| i j |
| |
| a b c d |
| e f g h |
| i j - - |
| </pre> </div> |
| |
| |
| <p>This built in is mostly for outputting sequnces in |
| tabular/columnar format. When used with HTML tables, the 2nd |
| parameter is often <code class="inline-code">"\xA0"</code> (that is the code of |
| the no-break space character, also known as "nbsp"), so |
| the border of the empty TD-s will not be missing.</p> |
| |
| <p>The 1st parameter must be a number that is at least 1. If the |
| number is not integer, it will be silently rounded down to integer |
| (i.e. both 3.1 and 3.9 will be rounded to 3). The 2nd parameter can |
| be of any type and value.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_drop_while">drop_while</h2> |
| |
| |
| |
| |
| |
| |
| <p>Returns a new sequence that contains the elements from the |
| input sequence starting with from the first element that does |
| <em>not</em> match the parameter predicate (condition). |
| After that, all elements are included, regardless if they match the |
| predicate or not. See the <a href="#ref_builtin_filter"><code>filter</code> |
| built-in</a> for more details about parameters, and other |
| details, but note that the condition in <code class="inline-code">filter</code> |
| has opposite meaning (what to keep, instead of what to drop).</p> |
| |
| <p>Example and comparison with <code class="inline-code">filter</code>:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign xs = [1, 2, -3, 4, -5, 6]> |
| |
| Drop while positive: |
| <#list xs?drop_while(x -> x > 0) as x>${x} </#list> |
| |
| Filer for positives: |
| <#list xs?filter(x -> x > 0) as x>${x} </#list></pre> </div> |
| |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">Drop while positive: |
| -3 4 -5 6 |
| |
| Filer for positives: |
| 1 2 4 6 </pre> </div> |
| |
| |
| <p>As you can see, <code class="inline-code">drop_while</code> has stopped |
| dropping the elements once it ran into the first element that didn't |
| match the predicate (<code class="inline-code">x > 0</code>). On the other |
| hand, <code class="inline-code">filter</code> keeps the elements that match the |
| same predicate, and it doesn't stop.</p> |
| |
| <p>See also: <a href="#ref_builtin_take_while"><code>take_while</code> |
| built-in</a></p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_filter">filter</h2> |
| |
| |
| |
| |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>This built-in is available since 2.3.29</p> |
| </div> |
| |
| |
| <p>Returns a new sequence that only contains the elements for |
| which the parameter condition (the predicate) returns |
| <code class="inline-code">true</code>. For example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign xs = [1, -2, 3, 4, -5]> |
| Positives: |
| <#list xs?<strong>filter(x -> x > 0)</strong> as x>${x} </#list> |
| Negatives: |
| <#list xs?<strong>filter(x -> x < 0)</strong> as x>${x} </#list></pre> </div> |
| |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">Positives: |
| 1 3 4 |
| Negatives: |
| -2 -5 </pre> </div> |
| |
| |
| <p>This built-in has a single required parameter, the predicate |
| (filter condition, what to keep). The predicate can be specified in |
| 3 ways:</p> |
| |
| <ul> |
| <li> |
| <p>As a single argument <a href="dgui_template_exp.html#dgui_template_exp_lambda">lambda expression</a>: |
| <code class="inline-code"><em class="code-color">element</em> -> |
| <em class="code-color">predicate</em></code>. In that, |
| <code class="inline-code"><em class="code-color">element</em></code> is the |
| variable name with which you can refer to the current element in |
| the <code class="inline-code"><em class="code-color">predicate</em></code>, and |
| the <code class="inline-code"><em class="code-color">predicate</em></code> is |
| an arbitrarily complex <a href="dgui_template_exp.html">expression</a> that must return a |
| boolean value (<code class="inline-code">true</code> or |
| <code class="inline-code">false</code>). An example this was shown above. Note |
| again the predicates can be arbitrarily complex, like the |
| predicate in <code class="inline-code">products?filter(product -> |
| product.discounted && |
| !user.hasBought(product))</code>.</p> |
| </li> |
| |
| <li> |
| <p>As a <a href="ref_directive_function.html">function</a> or method that |
| has a single argument, and returns boolean. For example, the |
| "Negatives" example above could be implemented like |
| this:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#function negative(x)> |
| <#return x < 0> |
| </#function> |
| |
| <em>...</em> |
| |
| Negatives: |
| <#list xs<strong>?filter(negative)</strong> as x>${x} </#list></pre> </div> |
| |
| |
| <p>Note how we just referred to the function by name, and did |
| not call it. Similarly, if you have a Java object called |
| <code class="inline-code">utils</code> in the data-model, and it has a |
| <code class="inline-code">boolean isNegative(Number n)</code> method, then you |
| could use that like |
| <code class="inline-code">xs?filter(utils.isNegative)</code>.</p> |
| </li> |
| </ul> |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>Remember, the condition (predicate) that you specify tells |
| <em>what to keep</em>, not what to filter out! That |
| is, the element will be in the result sequence when you return |
| <code class="inline-code">true</code>, not when you return |
| <code class="inline-code">false</code>. (It's like the <code class="inline-code">WHERE</code> |
| condition in SQL, if you know that.)</p> |
| </div> |
| |
| |
| <p>While <code class="inline-code">filter</code> is most often used in the |
| <a href="ref_directive_list.html#ref.directive.list"><code>list</code> |
| directive</a>, naturally it can be used anywhere where a filtered |
| sequence is needed, and so this works as well:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign negatives = xs?filter(x -> x < 0)> |
| Negatives: |
| <#list negatives as x>${x} </#list></pre> </div> |
| |
| |
| <p>Note however, that for a very long sequences, the above |
| solution can consume significantly more memory. That's because |
| <code class="inline-code"><list |
| <em class="code-color">seq</em>?filter(<em class="code-color">pred</em>) |
| <em class="code-color">...</em>></code> is optimized to do |
| filtering without building an intermediate filtered sequence, while |
| the n above example, <code class="inline-code">assign</code> will first build the |
| whole filtered sequence in memory, and we pass that filtered |
| sequence later to <code class="inline-code">list</code>. But again, this only |
| matters for very long sequences.</p> |
| |
| <p>See also: <a href="#ref_builtin_take_while"><code>take_while</code> |
| built-in</a>, <a href="#ref_builtin_drop_while"><code>drop_while</code> |
| built-in</a></p> |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-simplesect" id="topic.filterLazyEval">Lazy evaluation and its consequences</h3> |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>Identical rules apply to these built-ins as well: <a href="#ref_builtin_map"><code>map(<em>mapper</em>)</code></a>, |
| <a href="#ref_builtin_take_while"><code>take_while(<em>predicate</em>)</code></a>, |
| <a href="#ref_builtin_drop_while"><code>drop_while(<em>predicate</em>)</code></a>.</p> |
| </div> |
| |
| |
| <p>To optimize processing, <code class="inline-code">filter</code> might |
| delays fetching the elements of the input sequence, and applying |
| the predicate on them. But it's guaranteed that those operations |
| are not delayed past the point where the execution of the |
| directive or interpolation, whose parameter contains the |
| <code class="inline-code"><em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>)</code>, |
| is finished. Some examples:</p> |
| |
| <ul> |
| <li> |
| <p>In the case of <code class="inline-code"><list |
| <em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>) |
| <em class="code-color">...</em>><em class="code-color">nested |
| content</em></#list></code>, when the |
| execution enters the <code class="inline-code"><em class="code-color">nested |
| content</em></code>, it's not true that all |
| elements of <code class="inline-code"><em class="code-color">seq</em></code> |
| was already consumed and filtered. Consuming and filtering |
| <code class="inline-code"><em class="code-color">seq</em></code> is instead |
| done bit by bit as <code class="inline-code">list</code> repeats the nested |
| content. But it's guaranteed that past the |
| <code class="inline-code"></#list></code> tag (the end of the |
| execution of the <code class="inline-code">list</code> directive), there are |
| no delayed readings of |
| <code class="inline-code"><em class="code-color">seq</em></code>, or delayed |
| evaluation of the |
| <code class="inline-code"><em class="code-color">predicate</em></code>. So |
| avoid changing a such variable (or other system state) in the |
| nested content of <code class="inline-code">list</code>, which influences |
| the result of the |
| <code class="inline-code"><em class="code-color">predicate</em></code>. Doing |
| so could change the filtering for the rest of the |
| <code class="inline-code"><em class="code-color">seq</em></code>.</p> |
| </li> |
| |
| <li> |
| <p>In the case of <code class="inline-code"><#assign |
| <em class="code-color">filteredSeq</em> = |
| <em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>)></code> |
| it's guaranteed that all elements of |
| <code class="inline-code"><em class="code-color">seq</em></code> were |
| processed, and thus |
| <code class="inline-code"><em class="code-color">predicate</em></code> won't |
| be evaluated after the <code class="inline-code">assign</code> |
| directive.</p> |
| </li> |
| |
| <li> |
| <p>In the case of |
| <code class="inline-code">${<em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>)?join(', |
| ')}</code> it's guaranteed that all elements of |
| <code class="inline-code"><em class="code-color">seq</em></code> were |
| processed, and thus |
| <code class="inline-code"><em class="code-color">predicate</em></code> won't |
| be evaluated after the <code class="inline-code">assign</code> |
| directive.</p> |
| </li> |
| </ul> |
| |
| <p>Inside <a href="dgui_template_exp.html">expressions</a> |
| however, there's no promise regarding when the elements are |
| consumed and when the predicate is evaluated. Like in the case of |
| <code class="inline-code"><em class="code-color">seq</em>?filter(<em class="code-color">predicate1</em>)?filter(<em class="code-color">predicate2</em>)</code>, |
| it's not guaranteed that |
| <code class="inline-code"><em class="code-color">predicate1</em></code> will only |
| be evaluated before |
| <code class="inline-code"><em class="code-color">predicate2</em></code>. (Most |
| likely they will be called alternately: |
| <code class="inline-code"><em class="code-color">predicate1</em></code> for the |
| 1st element, then |
| <code class="inline-code"><em class="code-color">predicate2</em></code> for the |
| 1st element, then |
| <code class="inline-code"><em class="code-color">predicate1</em></code> for the |
| 2nd element, then |
| <code class="inline-code"><em class="code-color">predicate2</em></code> for the |
| 2nd element, and so on.)</p> |
| |
| <p>If you pass a filtered sequence to a |
| <em>custom</em> directive (a macro) or function or |
| method, as in <code class="inline-code"><@<em class="code-color">myMacro</em> |
| <em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>) |
| /></code> or |
| <code class="inline-code"><em class="code-color">myFunction</em>(<em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>))</code>, |
| then it's guaranteed that the filtering is not delayed past the |
| point when the custom directive/function/method is invoked. That |
| is, your macro/function/method will aways receive a fully |
| constructed filtered sequence.</p> |
| |
| <p>Also note that in it's <em>not</em> guaranteed |
| that all elements of the input sequence will be read, and |
| therefore that the predicate will be evaluated for all elements. |
| Some examples of such cases:</p> |
| |
| <ul> |
| <li> |
| <p>You may <a href="ref_directive_list.html#ref.directive.list.break"><code>break</code></a> |
| out from <code class="inline-code"><list |
| <em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>) |
| <em class="code-color">...</em>></code> before it reaches |
| the last element, in which case the rest of the |
| <code class="inline-code"><em class="code-color">seq</em></code> elements |
| won't be fetched and filtered.</p> |
| </li> |
| |
| <li> |
| <p>In the case of |
| <code class="inline-code"><em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>)[2]</code>, |
| which reads the 3rd element of the filtered sequence, |
| FreeMarker stops fetching and filtering the elements of |
| <code class="inline-code"><em class="code-color">seq</em></code> when we have |
| found the 3rd element that matches the |
| <code class="inline-code"><em class="code-color">predicate</em></code>.</p> |
| </li> |
| |
| <li> |
| <p>In the case of |
| <code class="inline-code"><em class="code-color">seq</em>?filter(<em class="code-color">predicate</em>)?size |
| != 0</code>, which tells whether the filtered sequence is |
| non-empty, we stop fetching and filtering the elements of |
| <code class="inline-code"><em class="code-color">seq</em></code> when we have |
| found the 1st element that matches the |
| <code class="inline-code"><em class="code-color">predicate</em></code>. |
| (That's certainly surprising as <code class="inline-code">?size</code> needs |
| to process the whole sequence to tell the size. But in this |
| case FreeMarker notices that we don't really need the exact |
| size.)</p> |
| </li> |
| </ul> |
| |
| <p>If you are a Java programmer, note how the |
| <code class="inline-code">filter</code> built-in differs from Java |
| <code class="inline-code">Stream.filter</code>. <code class="inline-code">Stream.filter</code> |
| is "lazy", while FreeMarker <code class="inline-code">filter</code> |
| is basically "eager", and is only "lazy" |
| in special cases, and within a limited scope. Thus, unlike in |
| Java, calling <code class="inline-code">filter</code> is not always free. In |
| particular, if you assign a filtered sequence to a variable, or |
| pass it to a custom directive/function/method, the filtered |
| sequence will be created eagerly.</p> |
| |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-simplesect" id="topic.filterLongInput">Filtering very long input that you don't hold in |
| memory</h3> |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>Identical rules apply to these built-ins as well: <a href="#ref_builtin_map"><code>map(<em>mapper</em>)</code></a>, |
| <a href="#ref_builtin_take_while"><code>take_while(<em>predicate</em>)</code></a>, |
| <a href="#ref_builtin_drop_while"><code>drop_while(<em>predicate</em>)</code></a>.</p> |
| </div> |
| |
| |
| <p>Some applications, particularly those that render huge |
| tables, use <a href="dgui_datamodel_types.html#dgui_datamodel_container">sequence-like |
| values</a> in the data-model that are not held in memory at |
| once, instead they are like a stream of elements that you can only |
| read in the order as they are given to you (on the Java side these |
| are <code class="inline-code">java.util.Iterator</code>-s, or |
| <code class="inline-code">java.util.Iterables</code>, or the like). These will |
| have "collection" type in the template language, |
| which is like a restricted sequence.</p> |
| |
| <p><code class="inline-code">filter</code> works with collection input too. |
| As you have seen earlier, <code class="inline-code">filter</code> might stores |
| the entire filtered sequence in the memory, which in this case |
| sounds concerning, because if the input was too big to fit into |
| the memory (hence it wasn't exposed as a sequence), then the |
| filtered collection can be too big as well. For that reason, if |
| the input is not a sequence (but a collection), |
| <code class="inline-code">filter</code> never collects its result into the |
| memory, and never fetches and processes the input elements until |
| they are really needed ("lazy" behavior). Furthermore |
| the result of <code class="inline-code">filter</code> is then a collection, not |
| a sequence, therefor sequence operations (like |
| <code class="inline-code"><em class="code-color">seq</em>[<em class="code-color">index</em>]</code>) |
| will not work on it.</p> |
| |
| <p>Unlike with sequence input, any operation that would cause |
| collecting the whole filtered result into the memory will now |
| fail. Let's see that through examples. Let's say we have |
| <code class="inline-code">hugeTable</code> in the data-model, which is not a |
| sequence, but still a collection (probably an |
| <code class="inline-code">Iterator</code> in Java). Then, consider:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#-- Works: --> |
| <#list hugeTable?filter(<em>predicate</em>) as row><em>nested content</em></#list></pre> </div> |
| |
| |
| <p>This works fine, since <code class="inline-code">list</code> doesn't |
| require collecting the result into the memory</p> |
| |
| <p>Consider this:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#-- Fails if hugeTable is not a sequence, just a collection: --> |
| <#assign filteredHugeTable = hugeTable?filter(<em>predicate</em>)></pre> </div> |
| |
| |
| <p>This fails, as filtering can't be postponed beyond the |
| containing directive (<code class="inline-code">assign</code>), so FreeMareker |
| had to put the entire filtered result into |
| <code class="inline-code">filteredHugeTable</code>. If, however, you know that |
| <code class="inline-code">filteredHugeTable</code> won't be too big, you can |
| explicitly collect the result into a sequence via the <a href="ref_builtins_expert.html#ref_builtin_sequence"><code>sequence</code> |
| built-in</a>:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#-- Works, but be sure filtredHugeTable fits into the memory: --> |
| <#assign filteredHugeTable = hugeTable?filter(predicate)<strong>?sequence</strong>></pre> </div> |
| |
| |
| <p>Naturally, applying the <code class="inline-code">sequence</code> built-in |
| allows all sequence operations, such as |
| <code class="inline-code"><em class="code-color">seq</em>[<em class="code-color">index</em>]</code>, |
| <code class="inline-code"><em class="code-color">seq</em>[<em class="code-color">range</em>]</code>, |
| or |
| <code class="inline-code"><em class="code-color">seq</em>?<em class="code-color">size</em></code>. |
| If these operations are directly applied on a sequence that was |
| converted from a collection, then FreeMarker optimizes out |
| actually creating the sequence in memory. So these won't consume |
| much memory regardless of the size of the filtered |
| <code class="inline-code">hugeTable</code>:</p> |
| |
| <ul> |
| <li> |
| <p><code class="inline-code">hugeTable?filter(<em class="code-color">predicate</em>)?sequence[index]</code>: |
| FreeMarker will just fetch and drop the elements till it |
| reaches the element at the desired position.</p> |
| </li> |
| |
| <li> |
| <p><code class="inline-code">hugeTable?filter(<em class="code-color">predicate</em>)?sequence[0..9]</code>: |
| FreeMarker will just collect the first 10 elements.</p> |
| </li> |
| |
| <li> |
| <p><code class="inline-code">hugeTable?filter(<em class="code-color">predicate</em>)?sequence?size</code>: |
| In this case the whole <code class="inline-code">hugeTable</code> will be |
| fetched, which is possibly slow, but the fetched elements are |
| still not collected into the memory, as they only need to be |
| counted.</p> |
| </li> |
| </ul> |
| |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-simplesect" id="topic.filterMissing">Filtering missing (null) values</h3> |
| |
| |
| <p>The argument to a lambda expression can hold the missing |
| (Java <code class="inline-code">null</code>) value, and reading such value will |
| not fall back to a higher scope. Thus, something like |
| <code class="inline-code">seq?filter(it -> it??)</code>, which filters out |
| missing element from the sequence, will work reliably.</p> |
| |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_first">first</h2> |
| |
| |
| |
| |
| <p>Returns the first item of the sequence. Thus |
| <code class="inline-code"><em class="code-color">value</em>?first</code> is the |
| same as <code class="inline-code"><em class="code-color">value</em>[0]</code>, |
| except that, since FreeMarker 2.3.26, |
| <code class="inline-code"><em class="code-color">value</em>?first</code> also works |
| if <code class="inline-code"><em class="code-color">value</em></code> doesn't |
| support getting items with numerical index, but still supports to be |
| listed (i.e., with FTL collection values).</p> |
| |
| <p>If the sequence or collection is empty, the result will be a |
| missing value (as in |
| <code class="inline-code"><em class="code-color">empty</em>?first!'No item was |
| found'</code>).</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_join">join</h2> |
| |
| |
| |
| |
| <p>Concatenates the items of a sequence to a single string, with |
| the given separator. For example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign colors = ["red", "green", "blue"]> |
| ${colors?join(", ")}</pre> </div> |
| |
| |
| <p>will output:</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">red, green, blue</pre> </div> |
| |
| |
| <p>Sequence items that are not strings will be converted to |
| string with the same conversion rules as of |
| <code class="inline-code">${<em class="code-color">...</em>}</code> (except, of |
| course, no automatic escaping is applied at this stage).</p> |
| |
| <p><code class="inline-code">?join(<em class="code-color">...</em>)</code> can |
| have up to 3 parameters:</p> |
| |
| <div class="orderedlist"><ol type="1"> |
| <li> |
| <p>Separator, required: The string that is inserted between |
| items</p> |
| </li> |
| |
| <li> |
| <p>Empty value, defaults to <code class="inline-code">""</code> (empty |
| string): The value used if the sequence contains no |
| items.</p> |
| </li> |
| |
| <li> |
| <p>List ending, defaults to <code class="inline-code">""</code> (empty |
| string): The value printed after the last value, if the list |
| sequence wasn't empty.</p> |
| </li> |
| </ol></div> |
| |
| <p>So this (where <code class="inline-code">[]</code> means an empty |
| sequence):</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body">${colors?join(", ", "-")} |
| ${[]?join(", ", "-")} |
| |
| ${colors?join(", ", "-", ".")} |
| ${[]?join(", ", "-", ".")}</pre> </div> |
| |
| |
| <p>will output:</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">red, green, blue |
| - |
| |
| red, green, blue. |
| -</pre> </div> |
| |
| |
| <p class="programmers-note">Sequences coming from Java might contain |
| <code class="inline-code">null</code> values. Those values will be ignored by this |
| built-in, exactly like if they were removed from the list.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_last">last</h2> |
| |
| |
| |
| |
| <p>The last subvariable of the sequence. Template processing will |
| die with error if the sequence is empty.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_map">map</h2> |
| |
| |
| |
| |
| |
| |
| <p>Returns an new sequence where all elements are replaced with |
| the result of the parameter lambda, function, or method. For |
| example, you have a list of user objects in |
| <code class="inline-code">users</code>, but instead you need a list of user names |
| in variable, then you could do this:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign userNames = users?map(user -> user.name)></pre> </div> |
| |
| |
| <p>The parameter work like the parameter of the with <a href="#ref_builtin_filter"><code>filter</code> |
| built-in</a> (so see there), except that the |
| lambda/function/method you specify can return values of any |
| type.</p> |
| |
| <p>Regarding lazy evaluation, and handling of very long inputs, |
| it also <a href="#topic.filterLazyEval">works on the same |
| way</a> as the <a href="#ref_builtin_filter"><code>filter</code> |
| built-in</a>.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_min_max">min, max</h2> |
| |
| |
| |
| |
| |
| |
| <p>Returns the smaller (<code class="inline-code">min</code>) or greatest |
| (<code class="inline-code">max</code>) item of the sequence (or collection). The |
| items must be either all numbers, or all date/time values of the |
| same kind (date-only, time-only, date-time), or else a comparison |
| error will occur. These are the same restrictions as for the <a href="dgui_template_exp.html#dgui_template_exp_comparison"><code><</code> and |
| <code>></code> operators</a>.</p> |
| |
| <p>Missing items (i.e., Java <code class="inline-code">null</code>-s) will be |
| silently ignored. If the sequence is empty or it only contains |
| missing (Java <code class="inline-code">null</code>) items, the result itself will |
| be missing.</p> |
| |
| <p>Example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body">${[1, 2, 3]?min} |
| ${[1, 2, 3]?max} |
| ${[]?min!'-'}</pre> </div> |
| |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">1 |
| 3 |
| -</pre> </div> |
| |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_reverse">reverse</h2> |
| |
| |
| |
| |
| <p>The sequence with reversed order.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_seq_contains">seq_contains</h2> |
| |
| |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>The <code class="inline-code">seq_</code> prefix is required in the |
| built-in name to differentiate it from the <a href="ref_builtins_string.html#ref_builtin_contains"><code>contains</code> |
| built-in</a> that searches a substring in a string (since a |
| variable can be both string and sequence on the same time).</p> |
| </div> |
| |
| |
| <p>Tells if the sequence contains the specified value (according |
| the <a href="dgui_template_exp.html#dgui_template_exp_comparison"><code>==</code> |
| operator</a> of the template language, not according Java's |
| <code class="inline-code">Object.equals</code>). It has 1 parameter, the value to |
| find. Example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign x = ["red", 16, "blue", "cyan"]> |
| "blue": ${x?seq_contains("blue")?string("yes", "no")} |
| "yellow": ${x?seq_contains("yellow")?string("yes", "no")} |
| 16: ${x?seq_contains(16)?string("yes", "no")} |
| "16": ${x?seq_contains("16")?string("yes", "no")}</pre> </div> |
| |
| |
| <p>The output will be:</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">"blue": yes |
| "yellow": no |
| 16: yes |
| "16": no</pre> </div> |
| |
| |
| <p>To find the value the built-in uses FreeMarker's comparison |
| rules (as if you was using <a href="dgui_template_exp.html#dgui_template_exp_comparison"><code>==</code> |
| operator</a>), except that comparing two values of different |
| types or of types for which FreeMarker doesn't support comparison |
| will not cause error, just will be evaluated as the two values are |
| not equal. Thus, you can use it only to find scalar values (i.e. |
| string, number, boolean or date/time values). For other types the |
| result will be always <code class="inline-code">false</code>.</p> |
| |
| <p>For fault tolerance, this built-in also works with |
| collections.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_seq_index_of">seq_index_of</h2> |
| |
| |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>This built-in is available since FreeMarker 2.3.1. It |
| doesn't exist in 2.3.</p> |
| </div> |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>The <code class="inline-code">seq_</code> prefix is required in the |
| built-in name to differentiate it from the <a href="ref_builtins_string.html#ref_builtin_index_of"><code>index_of</code> |
| built-in</a> that searches a substring in a string (since a |
| variable can be both string and sequence on the same time).</p> |
| </div> |
| |
| |
| <p>Returns the index of the first occurrence of a value in the |
| sequence, or <code class="inline-code">-1</code> if the sequence doesn't contain |
| the specified value. The value to find is specified as the first |
| parameter. For example this template:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign colors = ["red", "green", "blue"]> |
| ${colors?seq_index_of("blue")} |
| ${colors?seq_index_of("red")} |
| ${colors?seq_index_of("purple")}</pre> </div> |
| |
| |
| <p>will output this:</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">2 |
| 0 |
| -1</pre> </div> |
| |
| |
| <p>To find the value the built-in uses FreeMarker's comparison |
| rules (as if you was using <a href="dgui_template_exp.html#dgui_template_exp_comparison"><code>==</code> |
| operator</a>), except that comparing two values of different |
| types or of types for which FreeMarker doesn't support comparison |
| will not cause error, just will be evaluated as the two values are |
| not equal. Thus, you can use it only to find scalar values (i.e. |
| string, number, boolean or date/time values). For other types the |
| result will be always <code class="inline-code">-1</code>.</p> |
| |
| <p>The index where the searching is started can be optionally |
| given as the 2nd parameter. This may be useful if the same item can |
| occur for multiple times in the same sequence. There is no |
| restriction on the numerical value of the second parameter: if it is |
| negative, it has the same effect as if it were zero, and if it is |
| greater than the length of the sequence, it has the same effect as |
| if it were equal to the length of the sequence. Decimal values will |
| be truncated to integers. For example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign names = ["Joe", "Fred", "Joe", "Susan"]> |
| No 2nd param: ${names?seq_index_of("Joe")} |
| -2: ${names?seq_index_of("Joe", -2)} |
| -1: ${names?seq_index_of("Joe", -1)} |
| 0: ${names?seq_index_of("Joe", 0)} |
| 1: ${names?seq_index_of("Joe", 1)} |
| 2: ${names?seq_index_of("Joe", 2)} |
| 3: ${names?seq_index_of("Joe", 3)} |
| 4: ${names?seq_index_of("Joe", 4)}</pre> </div> |
| |
| |
| <p>will output this:</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">No 2nd param: 0 |
| -2: 0 |
| -1: 0 |
| 0: 0 |
| 1: 2 |
| 2: 2 |
| 3: -1 |
| 4: -1</pre> </div> |
| |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_seq_last_index_of">seq_last_index_of</h2> |
| |
| |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>This built-in is available since FreeMarker 2.3.1. It |
| doesn't exist in 2.3.</p> |
| </div> |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>The <code class="inline-code">seq_</code> prefix is required in the |
| built-in name to differentiate it from the <a href="ref_builtins_string.html#ref_builtin_last_index_of"><code>last_index_of</code> |
| built-in</a> that searches a substring in a string (since a |
| variable can be both string and sequence on the same time).</p> |
| </div> |
| |
| |
| <p>Returns the index of the last occurrence of a value in the |
| sequence, or <code class="inline-code">-1</code> if the sequence doesn't contain |
| the specified value. That is, it is the same as <a href="#ref_builtin_seq_index_of"><code>seq_index_of</code></a>, |
| just it searches backward starting from the last item of the |
| sequence. It also supports the optional 2nd parameter that specifies |
| the index where the searching is started. For example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign names = ["Joe", "Fred", "Joe", "Susan"]> |
| No 2nd param: ${names?seq_last_index_of("Joe")} |
| -2: ${names?seq_last_index_of("Joe", -2)} |
| -1: ${names?seq_last_index_of("Joe", -1)} |
| 0: ${names?seq_last_index_of("Joe", 0)} |
| 1: ${names?seq_last_index_of("Joe", 1)} |
| 2: ${names?seq_last_index_of("Joe", 2)} |
| 3: ${names?seq_last_index_of("Joe", 3)} |
| 4: ${names?seq_last_index_of("Joe", 4)}</pre> </div> |
| |
| |
| <p>will output this:</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">No 2nd param: 2 |
| -2: -1 |
| -1: -1 |
| 0: 0 |
| 1: 0 |
| 2: 2 |
| 3: 2 |
| 4: 2</pre> </div> |
| |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_size">size</h2> |
| |
| |
| |
| |
| <p>The number of sub variables in sequence (as a numerical |
| value). The highest possible index in sequence <code class="inline-code">s</code> |
| is <code class="inline-code">s?size - 1</code> (since the index of the first |
| subvariable is 0) assuming that the sequence has at least one |
| subvariable.</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_sort">sort</h2> |
| |
| |
| |
| |
| |
| |
| <p>Returns the sequence sorted in ascending order. (For |
| descending order use this and then the <a href="#ref_builtin_reverse"><code>reverse</code> built |
| in</a>.) This will work only if all sub variables are strings, or |
| if all sub variables are numbers, or if all sub variables are date |
| values (date, time, or date+time), or if all sub variables are |
| booleans (since 2.3.17). If the sub variables are strings, it uses |
| locale (language) specific lexical sorting (which is usually not |
| case sensitive). For example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign ls = ["whale", "Barbara", "zeppelin", "aardvark", "beetroot"]?sort> |
| <#list ls as i>${i} </#list></pre> </div> |
| |
| |
| <p>will print (with US locale at least):</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">aardvark Barbara beetroot whale zeppelin</pre> </div> |
| |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_sort_by">sort_by</h2> |
| |
| |
| |
| |
| |
| |
| <p>Returns the sequence of hashes sorted by the given hash |
| subvariable in ascending order. (For descending order use this and |
| then the <a href="#ref_builtin_reverse"><code>reverse</code> built |
| in</a>.) The rules are the same as with the <a href="#ref_builtin_sort"><code>sort</code> built-in</a>, |
| except that the sub variables of the sequence must be hashes, and |
| you have to give the name of a hash subvariable that will decide the |
| order. For example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign ls = [ |
| {"name":"whale", "weight":2000}, |
| {"name":"Barbara", "weight":53}, |
| {"name":"zeppelin", "weight":-200}, |
| {"name":"aardvark", "weight":30}, |
| {"name":"beetroot", "weight":0.3} |
| ]> |
| Order by name: |
| <#list ls?sort_by("name") as i> |
| - ${i.name}: ${i.weight} |
| </#list> |
| |
| Order by weight: |
| <#list ls?sort_by("weight") as i> |
| - ${i.name}: ${i.weight} |
| </#list></pre> </div> |
| |
| |
| <p>will print (with US locale at least):</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">Order by name: |
| - aardvark: 30 |
| - Barbara: 53 |
| - beetroot: 0.3 |
| - whale: 2000 |
| - zeppelin: -200 |
| |
| Order by weight: |
| - zeppelin: -200 |
| - beetroot: 0.3 |
| - aardvark: 30 |
| - Barbara: 53 |
| - whale: 2000</pre> </div> |
| |
| |
| <p>If the subvariable that you want to use for the sorting is on |
| a deeper level (that is, if it is a subvariable of a subvariable and |
| so on), then you can use a sequence as parameter, that specifies the |
| names of the sub variables that lead down to the desired |
| subvariable. For example:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign members = [ |
| {"name": {"first": "Joe", "last": "Smith"}, "age": 40}, |
| {"name": {"first": "Fred", "last": "Crooger"}, "age": 35}, |
| {"name": {"first": "Amanda", "last": "Fox"}, "age": 25}]> |
| Sorted by name.last: |
| <#list members?sort_by(['name', 'last']) as m> |
| - ${m.name.last}, ${m.name.first}: ${m.age} years old |
| </#list></pre> </div> |
| |
| |
| <p>will print (with US locale at least):</p> |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">Sorted by name.last: |
| - Crooger, Fred: 35 years old |
| - Fox, Amanda: 25 years old |
| - Smith, Joe: 40 years old</pre> </div> |
| |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="ref_builtin_take_while">take_while</h2> |
| |
| |
| |
| |
| |
| |
| <p>Returns a sequence that only contains the elements of the |
| input sequence which are before the first element that doesn't match |
| the parameter predicate (filter condition). This is very similar to |
| the <a href="#ref_builtin_filter"><code>filter</code> |
| built-in</a>, so see further details there.</p> |
| |
| <p>Example and comparison with <code class="inline-code">filter</code>:</p> |
| |
| |
| |
| <div class="code-block role-template"> |
| <div class="code-block-label">Template</div><pre class="code-block-body"><#assign xs = [1, 2, -3, 4, -5, 6]> |
| |
| Take while positive: |
| <#list xs<strong>?take_while</strong>(x -> x > 0) as x>${x} </#list> |
| |
| Filer for positives: |
| <#list xs?filter(x -> x > 0) as x>${x} </#list></pre> </div> |
| |
| |
| |
| |
| <div class="code-block role-output"> |
| <div class="code-block-label">Output</div><pre class="code-block-body">Take while positive: |
| 1 2 |
| |
| Filer for positives: |
| 1 2 4 6 </pre> </div> |
| |
| |
| <p>As you can see, <code class="inline-code">take_while</code> has stopped at |
| the first number that didn't match the predicate (<code class="inline-code">x > |
| 0</code>), while <code class="inline-code">filter</code> has continued finding |
| further matches.</p> |
| |
| <p>See also: <a href="#ref_builtin_drop_while"><code>drop_while</code> |
| built-in</a></p> |
| <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="ref_builtins_boolean.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_builtins_hash.html"><span>Next</span></a></div></div></div></div> </div> |
| </div> |
| <div class="site-footer"><div class="site-width"><div class="footer-top"><div class="col-left sitemap"><div class="column"><h3 class="column-header">Overview</h3><ul><li><a href="https://freemarker.apache.org/">What is FreeMarker?</a></li><li><a href="https://freemarker.apache.org/freemarkerdownload.html">Download</a></li><li><a href="app_versions.html">Version history</a></li><li><a href="app_faq.html">FAQ</a></li><li><a itemprop="license" href="app_license.html">License</a></li><li><a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy policy</a></li></ul></div><div class="column"><h3 class="column-header">Often used / Reference</h3><ul><li><a href="https://try.freemarker.apache.org/">Try template online</a></li><li><a href="dgui_template_exp.html#exp_cheatsheet">Expressions cheatsheet</a></li><li><a href="ref_directive_alphaidx.html">#directives</a></li><li><a href="ref_builtins_alphaidx.html">?built_ins</a></li><li><a href="ref_specvar.html">.special_vars</a></li><li><a href="api/freemarker/core/Configurable.html#setSetting-java.lang.String-java.lang.String-">Configuration settings</a></li></ul></div><div class="column"><h3 class="column-header">Community</h3><ul><li><a href="https://github.com/apache/freemarker">Github project page</a></li><li><a href="https://issues.apache.org/jira/projects/FREEMARKER">Report a bug</a></li><li><a href="https://freemarker.apache.org/report-security-vulnerabilities.html">Report security vulnerability</a></li><li><a href="https://stackoverflow.com/questions/ask?tags=freemarker">Get help on StackOverflow</a></li><li><a href="https://twitter.com/freemarker">Announcements on Twitter</a></li><li><a href="https://freemarker.apache.org/mailing-lists.html">Discuss on mailing lists</a></li></ul></div></div><div class="col-right"><ul class="social-icons"><li><a class="github" href="https://github.com/apache/freemarker">Github</a></li><li><a class="twitter" href="https://twitter.com/freemarker">Twitter</a></li><li><a class="stack-overflow" href="https://stackoverflow.com/questions/ask?tags=freemarker">Stack Overflow</a></li></ul><a class="xxe" href="http://www.xmlmind.com/xmleditor/" rel="nofollow" title="Edited with XMLMind XML Editor"><span>Edited with XMLMind XML Editor</span></a></div></div><div class="footer-bottom"> <p class="last-generated"> |
| Last generated: |
| <time itemprop="dateModified" datetime="2024-02-12T20:34:04Z" title="Monday, February 12, 2024 at 8:34:04 PM Greenwich Mean Time">2024-02-12 20:34:04 GMT</time>, for Freemarker 2.3.32 </p> |
| <p class="copyright"> |
| © <span itemprop="copyrightYear">1999</span>–2024 |
| <a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a>. Apache FreeMarker, FreeMarker, Apache Incubator, Apache, the Apache FreeMarker logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners. </p> |
| </div></div></div></body> |
| </html> |