Some My Experiences

Header Ads

Showing posts with label Blogger. Show all posts
Showing posts with label Blogger. Show all posts

Sunday 16 December 2018

Lambda Expression in Blogger

Lambda expression is an expression that allow to apply one or more expression to a data. Lambda expression use fat arrow (=>) token in an expression. Lambda expression will work on posts, comments, or labels.
Lambda expression pattern:
[variable name] => [expression]
Example:
label => label.name == "Technology"

In order to use lambda expression, we need to use available aggregate operator that can take lambda expression as an operand (lambda operator). Lambda operators is set of aggregate operations that allow to pass lambda expression as an operand. Lambda operator take two operand. Left operand is a set of data that can be posts, comments, or labels. Right operand is lambda expression.
Lambda operation pattern:
[set of data] [lambda operator] ([lambda expression])
Example:
data:post.labels any (label => label.name == "Blogger")
data:post.labels is a set of data,
any is lambda operator,
(label => label.name == "Blogger") is lambda expression.

Below is list of available aggregate operators that can be used to operates lambda expression:

Operator Description
any Returns true if any item matches the rules on lambda expression
(boolean)
all Returns true if all of the items in the set matches the rules on lambda expression
(boolean)
none Returns true if none of the items in the set matches lambda expression
(boolean)
count Returns the count of the items in the set which matches rules on lambda expression
(number)
first Returns the first item which match lambda expression
(data)
filter, where Returns all of items that matches rules on lambda expression
(set of data)
map, select Returns a set containing each of the results in a new form applied by lambda expression
(new set of data)

Operators in Blogger

Arithmetic Operators
Blogger provides arithmetic operator to perform addition (+), subtraction (-), multiplication (*), division (/) and modulo/remainder (%). Additive operator (+) also used for string concatenation.

Operator Description
+ Adds Left-Operand and Right-Operand, also use for concatenate strings
- Subtracts Right-Operand from Left-Operand
* Multiplies values on either side of the operator
/ Divides Left-Operand by Right-Operand
% Computes the remainder of dividing Left-Operand by Right-Operand


Example:
<b:with var='totalComments' value='9'>
    data:totalComments = <b:eval expr='data:totalComments' /><br/>
    data:totalComments + 2 = <b:eval expr='data:totalComments + 2' /><br/>
    data:totalComments - 2 = <b:eval expr='data:totalComments - 2' /><br/>
    data:totalComments * 2 = <b:eval expr='data:totalComments * 2' /><br/>
    data:totalComments / 2 = <b:eval expr='data:totalComments / 2' /><br/>
    data:totalComments % 2 = <b:eval expr='data:totalComments % 2' /><br/>
    "Hello" + " World" = <b:eval expr='"Hello" + " World"' /><br/>
</b:with>

Result:
data:totalComments = 9
data:totalComments + 2 = 11
data:totalComments - 2 = 7
data:totalComments * 2 = 18
data:totalComments / 2 = 4.5
data:totalComments % 2 = 1
"Hello" + " World" = Hello World


Relational Operators
The relational operator determines the relation between two operand. Relational operator compare two operand and determine if one operand is equal, not equal, greater than, less than, greater than or equal, less than or equal to another operand.

Operator Description
== Equal to. True if two operands are equal
!= Not equal to. True if two operands are not equal
> Greater than. True if Left-Operand is greater than Right-Operand
< Less than. True if Left-Operand is less than Right-Operand
>= Greater than or equal to. True if Left-Operand is greater than or equal to Right-Operand
<= Less than or equal to. True if Left-Operand is less than or equal to Right-Operand

Example:
5 == 7 = <b:eval expr='5 == 7' />
5 != 7 = <b:eval expr='5 != 7' />
5 > 7 = <b:eval expr='5 > 7' />
5 < 7 = <b:eval expr='5 < 7' />
5 >= 7 = <b:eval expr='5 >= 7' />
5 <= 7 = <b:eval expr='5 <= 7' />

Result:
5 == 7 = false
5 != 7 = true
5 > 7 = false
5 < 7 = true
5 >= 7 = false
5 <= 7 = true


Conditional or Logical Operator
The logical operators || (OR) and && (AND) operates two boolean expression and return boolean value.
There is logical NOT/complement operator, which take one operand, unary operator category. This operator used to inverses boolean value of its operand.
There are another conditional operator called Ternary Operator. Called ternary operator because it take 3 (Three) operand, and only this operator that take 3 operand. Ternary operator is like simplified b:if, b:else statement, but with return value. Format: operand1 ? operand2 : operand3
Operand1 must be boolean expression, operand2 and operand3 can be any data or expression that will be return value. Ternary operator will return operand2 value if operand1 is true, otherwise return value of operand3.

Operator Description
&&, AND conditional-AND; true if all operand are true.
||, OR conditional-OR; true if either of the operand is true
! logical NOT, return negation of its boolean operand
?: Ternary Operator, return operand2 value if operand1 is true, otherwise return value of operand3.

Example:
9 == 7 AND 9 > 7 returns <b:eval expr='9 == 7 AND 9 > 7' /><br/>
9 == 7 OR 9 > 7 returns <b:eval expr='9 == 7 OR 9 > 7' /><br/>
!(9 == 7) returns <b:eval expr='(9 == 9)' />
9 == 9 ? "Equal" returns "Not equal" : <b:eval expr='9 == 9 ? "Equal" : "Not equal"' /><br/>
Result:
9 == 7 AND 9 > 7 returns false
9 == 7 OR 9 > 7 returns true
!(9 == 7) returns true
9 == 9 ? "Equal" : "Not equal" returns Equal

Control Flow in Blogger

This section describes the control flow in blogger. Control flow is used to alter the flow of statement execution with decision making and looping that will enabling execute particular blocks of code/show content conditionally.

Decision Making

b:if, b:elseif, & b:else Statement
We can use the b:if, b:elseif and b:else tags to execute particular statement in different cases.
Format:
<b:if cond='condition 1'>
  [SHOW CONTENT IF MEET `condition 1`]
<b:elseif cond='condition 2'/>
  [SHOW CONTENT IF MEET `condition 2` and NOT MEET `condition 1`]
<b:else/>
  [SHOW CONTENT IF `condition 1` and `condition 2` evaluates to false]
</b:if>
The b:elseif and b:else tags are optional. So we can use only just b:if, or b:if with b:elseif, or b:if with b:else as needed. For `condition`, we can put anything that will be evaluates to boolean value (true or false).
Example:
<b:if cond='data:post.allowComments'>
<b:if cond='data:blog.pageType != "static_page"'>
<b:if cond='data:blog.pageType == "static_page" AND data:blog.pageName == "About Us"'>
<b:if cond='data:post.labels any (l => l.name == "Tutorial")'>
<b:if cond='data:blog.pageType not in {"static_page", "index"}'>
<b:if cond='data:post.labels any (l => l.name in {"Technology", "Sport"})'>

b:switch statement
The use of b:switch statement is best in case when we need to show content based on same variable.
Format:
<b:switch var='[data]'>
  <b:case value="[value 1]" />
    [SHOW CONTENT IF data value is `value 1]
  <b:case value="[value 2]" />
   [SHOW CONTENT IF data value is `value 1]
  <b:default />
   [SHOW CONTENT IF data value is not equal to any case value above]
</b:switch>
Example:
<b:switch var='data:blog.pageType'>
  <b:case value="static_page" />
    <title>Static Page</title>
  <b:case value="item" />
    <title>Blog Post</title>
  <b:default />
    <h2>Other Page Type</h2>
</b:switch>

LOOPING

b:loop
The b:loop will execute statements or block code for each data of items individually.
Format:
<b:loop var='identifier' index='index' values='set-of-data'>
  [PUT CONTENT LOOP HERE]
</b:loop>
identifier will hold value of each item in loop. index attribut is optional, which is zero-based index. This index will save state current iteration number and is incremented.
Example:
<b:loop index='i' values='data:posts' var='post'>
  <b:if cond='data:i == 3'> 
    <div>Show ads when index number is 3</div>
  </b:if> 
  <div><data:post.title/></div>
</b:loop>
b:loop Allow us to loop between two number, in case we need loop with no set of data relation. Two number that define start and end can be increment (lower to higher) or decrement (higher to lower). example we loop from 1 to 3 that show div element with different class:
<b:loop var='number' values='1 to 3'>
  <div expr:id='"layoutId_" + data:number'></div>
</b:loop>