Latest news, food, business, travel, sport, Tips and Tricks...

Blogger Lambda Operator

Lambda or Arrow => It is used in lambda expressions treated as object to separate the input body from the lambda body on the right side. In Blogger, lambda expression is a nested operation to allow us to apply rules to whole sets of data (labels, posts, or comments, etc), rather than just individual instances.

Lambda Syntax on Blogger Language

THE LAMBDA SYNTAX

array {lambda expression}

THE LAMBDA EXPRESSION SYNTAX

{lambda operator} (variable => expression)

DOUBLE LAMBDA EXPRESSION SYNTAX

{lambda operator} (variable => expression) (lambda operator) (variable => expression)

NESTED SYNTAX

{lambda operator} (variable => expression lambda operator* (variable => expression))

  • array is operand with data structure that contains a group of elements.
  • lambda operator is Operator that can be used with Lambda operation.
  • variable is a  variable name can be any string of letters. Generally, a relevant letter, like P for Post, L for Label, etc, this to make code look shorter in the left body of lambda.
  • Lambda expression can be nested.
  • When nested, first lambda operator can be any of lambda operator, while the second lambda operator* is any, all or none.

Lambda Blogger Operator Syntax

OperatorMeaningSyntax
anyAny of the items is match lambda expression.array any (var => boolean)
allAll of the items are match lambda expression.array all (var => boolean)
noneNone of the items in the set matches.array none (var => boolean)
whereReturns a set of items for which the lambda expression is true.array where (var => boolean)
firstReturns the first item which the lambda expression is true.array first (var => boolean)
mapReturns a set containing each of the results of the lambda expression.array map (var => operand)
countReturns the count of the items in the set, which return true for the lambda expression.array count (var => boolean)

Blogger Lambda Expression Example

For better understanding how Lambda expression work in Blogger, we must have knowledge of data:posts layout and knowledge of other operator.

Blogger Lambda Operator [ANY], [ALL], [NONE]

THE CODE SAMPLE FOR OPERATOR [ANY] [ALL] [NONE]

<b:with value='["Foo","Bar"]' var='mainList'>

    <b:if cond='data:post.labels lambda operator (baz => baz.name in data:mainList)'>

      <h1>Hello World</h1>

    </b:if>

</b:with>

In above code sample, we can see lambda operation in <b:if> tag, the detail are:

  • <b:with> tag is an Array String containing value Foo and Bar. We give him mainList as variable name. DoubleQuote indicate its String type and separated by a Comma
  • Inside of <b:if> tag:
    • data:post.labels is array from variable named post, hold value from its parent. One of them is 'name'
    • lambda operator is the operator.
    • baz is variable name that hold value from data:post.labels.
    • baz.name in data:mainList is the Expression, has detail:
      • baz.name the name part is from baz where it is known that it has a 'name' value which is an array containing data 'label name' of post.
      • in is one of the membership operator.
      • data:mainList is data set in the <b:with> tag.
    • <h1>Hello World</h1> is is the part that will be displayed when the condition is fulfilled.

Try it

  1. Put the above code right below <b:includable id='post' var='post'> of XML template in HTML Editor.
  2. Make edit String value in <b:with> tag with our own label name.
  3. Replace the lambda operator with one of the three operator mentioned above.

The Result:

- ANY -

When post has Label Foo or Bar, or both, "Hello World" will be displayed.

- ALL -

 "Hello World" will be displayed if post contain both label only.

- NONE -

The word "Hello World" not be displayed if the post contain any of them.

Blogger Lambda Operator [WHERE] aka [FILTER] and, [FIRST]

THE CODE SAMPLE FOR OPERATOR [WHERE/FILTER] [FIRST]

<ul>

<b:loop values='data:posts lambda operator (foo => foo.title contains "Foo")' var='item'>

    <li><data:item.title/></li>

</b:loop>

</ul>

Above code example is a function to show list of Post Title that contain a word "Foo"

Operator WHERE is also known as FILTER operator, also we can write where or filter as an operator.

Try it

Put the above code right under <b:includable id='main'> inside Blog1 widget of XML template in HTML Editor.

Replace the lambda operator with one of the two operator mentioned above code sample.

The Result:

- WHERE -

Based on Max number of post in Blog1 widget setting, x Number of Post will in Loop and x Number Post Title has contain a word "Foo" will be printed.

- FIRST -

With the first operator, the result only return ONE Post Title, because First operator meaning to take only the first result.

Blogger Lambda Operator [MAP] aka [SELECT]

THE CODE SAMPLE FOR OPERATOR [MAP/SELECT]

<ul>

<b:loop values='data:posts where (foo => foo.title contains "Foo") map (f => f.title)' var='item'>

    <li><data:item/></li>

</b:loop>

</ul>

Result

  - Foo title text

  - Title Foo text

  - title text Foo

Similar to the example above, but this time we add operator [map] into it.

As seen above, we display <data:item/> which is different from operator [where] and [first]. This is because after the operator [map] is given, <data:item/> has no other derived data. Meaning, he only has an array that he gets from an expression.

As the result, of the seven posts that have been set in Blog1 widget settings, only the title containing the word 'Foo' will be displayed, otherwise it will be eliminated. This means, if from 7 posts there are 3 titles that have the word 'Foo' then it will only display the 3 items.

THE CODE SAMPLE 2 FOR OPERATOR [MAP/SELECT]

<ul>

<b:loop values='data:posts map (foo => foo.title contains "Foo")' var='item'>

    <li><data:item/></li>

</b:loop>

</ul>

Result

  - true

  - false

  - true

  - true

  - false

  - false

  - false

The output is like above result, from the seven post, he will mapping the post title and replaced by true or false.

THE CODE SAMPLE 3 FOR OPERATOR [MAP/SELECT]

<ul>

<b:loop values='data:posts map (foo => foo.title + " Bar")' var='item'>

    <li><data:item/></li>

</b:loop>

</ul>

Result

  - Title text Bar

This time we give and addition word " Bar", the result will be giving word " Bar" on the tail of each title.

Blogger Lambda Operator [COUNT]

THE CODE SAMPLE FOR OPERATOR [COUNT]

<b:with value='data:posts count (foo => foo.title contains "Foo")' var='item'>

    <data:item/>

</b:with>

Operator count return number as result. In sample code above, number will shown depending how much Post does contain word "Foo" in the post title.

The Experiment

FILTERING POST IN THE HOMEPAGE

<b:if cond='data:view.isHomepage'>

<b:with value='data:posts where (w => w.title contains "The") map (m => m.id)' var='list'> 

<b:with value='data:posts where (post => post.id in data:list)' var='foo'>

  <b:loop values='data:foo' var='post'>

    <div>

      <h1>Post Title Contain word "The"</h1>

      <b:include data='post' name='post'/>

    </div>

    </b:loop>

</b:with>

<b:with value='data:posts where (post => post.id not in data:list)' var='bar'>

  <b:loop values='data:bar' var='post'>

    <div>

      <h1>Post Title Not Contain word "The"</h1>

      <b:include data='post' name='post'/>

    </div>

    </b:loop>

</b:with>         

</b:with>

</b:if>

Code above work in homepage, it does filtering the post positioning by title has word "The". If he has, all posts that have the word "The" will be placed above the post that does not have that word. Think about Popular post.

Nesting Lambda

The rule is, the first lambda can be any of the seven lambda operators, while the second lambda, only any , all, and none can be used.

Sometimes, when we targeting some nested data, we must nest a lambda inside lambda too. For example we want to show post title by filtering with label that he has. label name is nested under labels then data:posts so we write like below example.

DATA:POSTS.LABELS.NAME

<b:loop values='data:posts where (p => p.labels any (l => l.name == "Foo"))' var='bar'>

  <data:bar.title/>

</b:loop>

WRONG IMPLEMENTATION

<b:loop values='data:posts.labels where (l => l.name == "Foo")' var='bar'>

  <data:bar.title/>

</b:loop>

,
//