Expression/Monads

From FlexRule Wiki
(Redirected from Expression/Pipes)
Jump to: navigation, search

Contents

Introduction

Monads

Monads are functions that can be chained together, which means the output of the previous one can be the input of the next one. The binding operator of monads is Pipe (i.e., |)

Fr-pipe-ops.png

Pipe is an operator that enables the chaining of monads' results and produces a new result that may have a new type.

input |MonadName (arg1, arg2, arg3...)

As you can see in the above format, it has a name and a list of arguments which may be either optional or mandatory.

The power of Pipe is in its chaining capability at the expression level.

input |monad1 (a1, a2, a3, ...)
      |monad2 (b1, b2, b3, ...)
      |monad3 (c1, c2, c3, ...)
      ...

This has the following benefits:

  • Eliminates the need for introducing multiple expressions and assignment evaluations
  • Allows the evaluation of complex multi-line expressions in as a single unit of execution (no nested rule commands for the evaluation of complex logic)

As a result, the logic model will be significantly simpler.

For instance, let's say you have an input parameter named input in which you need to:

  1. apply func1 and then
  2. apply func2 with the input of func1's result

So instead of executing these two:

  1. a = func1(input, 1, 2, 3)
  2. b = func2(a, 4, 5, 6)

You can execute this one:

b = input |func1 (1, 2, 3) |func2 (4, 5, 6)

For a real example, have a look at identifying duplicates on this page.

Supporting Platforms

In the front of each Monadic operator, you can see the icons below, each of which each indicates the supported platform of the monad.

  • FlexRule Engine in .Net Platforms: This monad is supported on any .Net platform in which RuntimeEngine is integrated and runs the logic.
  • JavaScript Platforms: This monad is supported on the JavaScript platform.

Sequence Monads

These pipes are applicable on a sequence of elements. Some of these pipes require a local name and predicate.

Local name is a name that will be used to reference an element in the sequence. Predicate is a an Expression based on your parameters and the defined local name.

Let's consider the following business objects:

Name Age Sex
Arash 38 Male
Parsa 6 Male
Pooya 3 Male
Shah 3 Female

These business objects are the basis of the examples provided here. Please note in the example expressions, people is the parameter name that references these business objects.

all JavaScript Platforms FlexRule Engine in .Net Platforms

Determines whether all of the elements of a sequence satisfy a condition.

|all (localName, predicate)
  • localName: Required
  • predicate: Required
  • return: Boolean

Example:

Sample: people |all(x, x.Sex == "Female")
Result: false

any JavaScript Platforms FlexRule Engine in .Net Platforms

Determines whether all of the elements of a sequence satisfy a condition.

|any (localName, predicate)
  • localName: Optional
  • predicate: Optional (must follow localName)
  • return: Boolean

Example: Has any element with Female sex?

Sample: people |any(x, x.Sex == "Female")
Result: true


Example: Has any element at all?

Sample: people |any()
Result: true

contains FlexRule Engine in .Net Platforms

Determines whether a sequence contains a specified element. It checks for collection, sequence, enum flag and string.

|contains (localName, predicate)
  • localName: Required
  • predicate: Required
  • return: Boolean

Example:

Sample: people
        |select (x, x.Name)
        |contains ('arash')
Result: true


Example: String check

Sample: 'test 123'
        |contains ('123')
Result: true

toIndex FlexRule Engine in .Net Platforms

Creates a lookup index based on a key. Use [ and ] to access the data using a key. When key is not found in the index, return null.

|toIndex (localName, predicate)
  • localName: Required
  • predicate: This is used to create a key for the index. The key can be a single property or composite by using { and }.

groupBy JavaScript Platforms FlexRule Engine in .Net Platforms

Groups the elements of a sequence based on a key.

|group (localName, predicate)
  • localName: Required
  • predicate: Required (key builder for groups) or list of properties of an element
  • return: Sequence of IGrouping elements

Example 1: Group people by gender (single property)

Sample: people |groupBy (x, x.Sex)
Result: Sequence of groups based on people gender (Sex property)


Example 2: Group people by gender and Age (by key function)

Sample: people |groupBy (x, key(x.Sex, x.Age))
Result: Sequence of groups based on people gender and age (Sex and Age property)

Example 3: Group people by gender and Age (by list of properties)

Sample: people |groupBy (x, x.Sex, x.Age)
Result: Sequence of groups based on people gender and age (Sex and Age property)

groupIndexBy FlexRule Engine in .Net Platforms

Groups the elements of a sequence based on a key and then creates an index based on the grouping key.

|groupIndexBy (localName, predicate)

select JavaScript Platforms FlexRule Engine in .Net Platforms

Projects each element of a sequence into a new form.

|select (localName, predicate)
  • localName: Required
  • predicate: Required
  • return: Sequence of the projections

Example:

Sample: people |select (x, x.Name)
Result: a list of strings containing all the names ('Name' property of business objects)
Name
Arash
Parsa
Pooya
Shah

selectMany FlexRule Engine in .Net Platforms

Projects each element of a sequence to an list and flattens the resulting sequences into one sequence.

|selectMany (localName, sequence)
  • localName: Required
  • sequence: Required
  • return: flattened sequence of the projections

Example:

Sample: people |selectMany (x, x.Emails)
Result: a list of strings containing all emails of all people

apply FlexRule Engine in .Net Platforms

Executes a set of expressions on all elements of a sequence.

|apply (localName, predicates)
  • localName: mandatory
  • predicates: comma separated list of expressions based on localName

Example:

Sample: people |apply (x, x.Email=null, x.Age=0)
Result: on the list of people it sets Email to null and sets Age to 0.

forEach FlexRule Engine in .Net Platforms

Please check apply

firstOrDefault JavaScript Platforms FlexRule Engine in .Net Platforms

Returns the first item in the sequence or a null value. When a predicate is defined, the first item that matches will be returned.

|firstOrDefault (localName, predicate)
  • localName: Optional
  • predicate: Optional
  • return: an element from sequence or null.
Sample: people |firstOrDefault (x, x.Age lt 3)
Result: The first person has the Age less than 3

first JavaScript Platforms FlexRule Engine in .Net Platforms

Please check firstOrDefault

lastOrDefault JavaScript Platforms FlexRule Engine in .Net Platforms

Returns the last item in the sequence or a null value. When a predicate is defined, the last item that matches will be returned.

|lastOrDefault (localName, predicate)
  • localName: Optional
  • predicate: Optional
  • return: an element from sequence or null.
Sample: people |lastOrDefault (x, x.Age gt 3)
Result: The last person has the Age greater than 3

distinct FlexRule Engine in .Net Platforms

last JavaScript Platforms FlexRule Engine in .Net Platforms

Please check lastOrDefault

where JavaScript Platforms FlexRule Engine in .Net Platforms

Filters sequence based on a condition.

|where (localName, predicate)
  • localName: Required
  • predicate: Required
  • return: Sequence that matches the predicate

Example:

Sample: people |where (x, x.Age == 3)
Result: A sequence of business objects that has all the people with the Age of 3
Name Age Sex
Pooya 3 Male
Shah 3 Female

take JavaScript Platforms FlexRule Engine in .Net Platforms

Returns the first n elements of a sequence.

|take (count)
  • count: Required (Length of the elements to be returned)
  • return: Sequence

Example:

Sample: people |take (2)
Result: A list that contains the first two elements of the business objects list.
Name Age Sex
Arash 38 Male
Parsa 6 Male

skip JavaScript Platforms FlexRule Engine in .Net Platforms

Bypasses a specified number of elements in a sequence and then returns the remaining ones.

|skip (count)
  • count: Required
  • return: Sequence

Example:

Sample: people |skip (2)
Result: Sequence containing 2 elements that is excluding the first two elements of the original list
Name Age Sex
Pooya 3 Male
Shah 3 Female

union FlexRule Engine in .Net Platforms

Produces the set union of two sequences.

|union (second)
  • second: Mandatory (address of the second set)
  • return: Sequence (collection)

Example: Let's consider the following two sets

a = [ 1, 2, 3, 4, 5, 9, 12 ]
b = [ 3, 5, 7, 8, 9 ]
Sample: a |union (b)
Result: [ 1, 2, 4, 5, 7, 8, 9, 12 ]

intersect FlexRule Engine in .Net Platforms

Produces the set intersection of two sequences by using the default equality comparer to compare values.

|intersect (second)
  • second: Mandatory (address of the second set)
  • return: Sequence (collection)

Example: Let's consider the following two sets

a = [ 1, 2, 3, 4, 5, 9, 12 ]
b = [ 3, 5, 7, 8, 9 ]
Sample: a |intersect (b)
Result: [ 3, 5, 9 ]

except FlexRule Engine in .Net Platforms

Produces the set difference of two sequences.

|except (second)
  • second: Mandatory (address of the second set)
  • return: Sequence (collection)

Example: Let's consider the following two sets

a = [ 1, 2, 3, 4, 5, 9, 12 ]
b = [ 3, 5, 7, 8, 9 ]
Sample: a |except (b)
Result: [ 1, 2, 4, 12 ]

toList FlexRule Engine in .Net Platforms

filter JavaScript Platforms FlexRule Engine in .Net Platforms

Look at Expression/Pipes#where

exists JavaScript Platforms FlexRule Engine in .Net Platforms

Look at Expression/Pipes#any

minus FlexRule Engine in .Net Platforms

Look at Expression/Pipes#except

difference FlexRule Engine in .Net Platforms

Look at Expression/Pipes#except

min FlexRule Engine in .Net Platforms

max FlexRule Engine in .Net Platforms

avg FlexRule Engine in .Net Platforms

sum FlexRule Engine in .Net Platforms

Use this monad operator to compute the sum of numeric values in a sequence.

|sum (localName, predicate)
  • localName: optional
  • predicate: optional
  • return: numeric value

Joins

join FlexRule Engine in .Net Platforms

Correlates the elements of two sequences based on matching keys, when a key does not exist in the innerList, it will be filtered out. The default equality comparer is used to compare keys

outerList |join (innerList, 
                 outerLocalVariable, outerSelector, 
                 innerLocalVariable, innerSelector, 
                 resultLocalVariable = null, resultSelector = null)
  • outerList: Required
  • innerList: Required
  • outerLocalVariable: Required
  • outerSelector: Required
  • innerLocalVariable: Required
  • innerSelector: Required
  • resultLocalVariable: Optional (default = null)
  • resultSelector = Optional (default = null)
  • return: a Tuple<object, object> where Item1 is from outer list and Item2 is from inner list when resultLocalVariable and resultSelector are not set. Otherwise returns a projector that is used by |key monad.

Examples: Let's consider the following data structure for Animals and Medications:

Animals:

ID Breed
0 cat
10 dog
22 fish

Medication:

ID Type
10 antibiotic
0 sedative
0 antihistamine

Join with no result selector:

Sample 1: animal |join (medication,
                        a, a.ID,
                        m, m.ID)
Result: a list with 3 Tuples which in each Item1 is an Animal and Item2 is a Medication

Join with the provided result selector to build a projection:

Sample 2: animal |join (medication,
                        a, a.ID,
                        m, m.ID,
                        r, r|key(Breed: a.Breed, Type: m.Type))
Result: a list with 3 objects that each has Breed and Type properties.
Breed Type
cat sedative
cat antihistamine
dog antibiotic

leftJoin FlexRule Engine in .Net Platforms

Correlates the elements of two sequences based on matching keys. When the key does not exist in the innerList it will be null value associated. The result includes all the outer list elements. The default equality comparer is used to compare keys

outerList |leftJoin (innerList, 
                 outerLocalVariable, outerSelector, 
                 innerLocalVariable, innerSelector, 
                 resultLocalVariable = null, resultSelector = null)
  • outerList: Required
  • innerList: Required
  • outerLocalVariable: Required
  • outerSelector: Required
  • innerLocalVariable: Required
  • innerSelector: Required
  • resultLocalVariable: Optional (default = null)
  • resultSelector = Optional (default = null)
  • return: a Tuple<object, object> where Item1 is from outer list and Item2 is from inner list when resultLocalVariable and resultSelector are not set. Otherwise returns a projector that is used by |key monad.

Animals:

ID Breed
0 cat
10 dog
22 fish

Medication:

ID Type
10 antibiotic
0 sedative
0 antihistamine

Left join with provided result selector to build a projection:

Sample 1: animal |join (medication,
                        a, a.ID,
                        m, m.ID,
                        r, r|key(Breed: a.Breed, Type: m==null ? null : m.Type))
Result: a list with 4 objects (all animals are included) in which each has Breed and Type properties. Type is null if no key matches from animals to medications.
Breed Type
cat sedative
cat antihistamine
dog antibiotic
fish null

crossJoin FlexRule Engine in .Net Platforms

groupJoin FlexRule Engine in .Net Platforms

Orders

orderAsc FlexRule Engine in .Net Platforms

orderDesc FlexRule Engine in .Net Platforms

orderByAsc FlexRule Engine in .Net Platforms

orderByDesc FlexRule Engine in .Net Platforms

orderBy FlexRule Engine in .Net Platforms

Sorts the elements of a sequence in ascending order according to a key.

|orderBy (localName, predicate1, predicate2, ... )
predicate: expression asc 
predicate: expression desc
predicate: expression

Predicate is an expression to access members using the local parameters ending with asc or desc. The default order is asc, unless specified.

ID Name Family
3 Clark Kent
2 Peter Parker
4 Tony Stark
5 Helena Wayne
1 Bruce Wayne
Example: list |orderBy(x,x.Family desc, x.Name)

Collection Management

add JavaScript Platforms FlexRule Engine in .Net Platforms

Adds an item to a collection.

|add (item)

addRange FlexRule Engine in .Net Platforms

Adds all items to a collection.

|addRange (item)

insert FlexRule Engine in .Net Platforms

Inserts an item into a collection on a specific index.

|insert (item, index)

remove JavaScript Platforms FlexRule Engine in .Net Platforms

Removes an item from a collection by its reference.

|remove (item)

removeAt JavaScript Platforms FlexRule Engine in .Net Platforms

Removes an item from a collection by its index.

|removeAt (index)

count JavaScript Platforms FlexRule Engine in .Net Platforms

Determines the length of a sequence of elements.

|count (localName, predicate)
  • localName: Optional
  • predicate: Optional (must follow localName)
  • return: Number (Integer)

Example:

Sample: people |count ()
Result: 4

elementAt FlexRule Engine in .Net Platforms

Gets an element by index in a collection.

|elementAt (index)

Parallel operators

any` FlexRule Engine in .Net Platforms

Parallel version of any

splitBy` FlexRule Engine in .Net Platforms

Parallel version of splitBy

all` FlexRule Engine in .Net Platforms

Parallel version of all

filter` FlexRule Engine in .Net Platforms

Parallel version of filter

where` FlexRule Engine in .Net Platforms

Parallel version of where

select` FlexRule Engine in .Net Platforms

Parallel version of select

apply` FlexRule Engine in .Net Platforms

Parallel version of apply

forEach` FlexRule Engine in .Net Platforms

Parallel version of forEach

leftJoin` FlexRule Engine in .Net Platforms

Parallel version of leftJoin

groupJoin` FlexRule Engine in .Net Platforms

Parallel version of groupJoin

toIndex` FlexRule Engine in .Net Platforms

Parallel version of toIndex

project` FlexRule Engine in .Net Platforms

Parallel version of project

Generic Monads

between FlexRule Engine in .Net Platforms

Determines whether a value is within a range.

value |between (lower, upper)
  • lower(Required): Lower bound of a range. Can be partial expression or value. When it is a value it will considered inclusive.
  • upper(Required): Higher bound of a range. Can be partial expression or value. When it is a value it will considered inclusive.
  • value(Required): A value to evaluate range condition

Partial expressions are op value format. i.e. <34

Example: 2 |between (2, <56)
    Result: true. 2 is inclusive. 

in FlexRule Engine in .Net Platforms

Determines whether a sequence contains a specified element.

value |in (sequence)
  • sequence: Required
  • value: Required
Example: "VIC" |in ("VIC","SA","NT")

key FlexRule Engine in .Net Platforms

Key is available as both function and monad. It creates an object to build a key. Key creates a structure of name and value.

Format: |key(expression1, expression2, expression3...)

Note expression1, expression2, expression3... can be simply a valid Expression or these can have the following format:

propertyName: expression

In this case, the object will have the set property names. This option is available only for the key monad, not as a function.

Example 1: Group people by gender and Age and select Sex

Sample: people 
        |groupBy (x, key(x.Sex, x.Age))
        |select (s, s.Key.Sex)
Result: Sequence of groups based on people gender and age (Sex and Age property)

Example 2: key with custom property name

Sample: people 
        |select (x, x)
        |key (Gender: person.Sex, FirstName: person.Name)
Result: Sequence of new objects that each have (Gender and FirstName property)

tuple FlexRule Engine in .Net Platforms

Creates a key based on multiple values combination. i.e. Composite Key.

|tuple(arg1, arg2, arg3...)

Note the maximum number of accepted arguments is 8.

The result of the key is an object with maximum of 8 properties

Item1, Item2, Item3,... Item8 

This corresponds to the argument that was passed to key.

Example 2: Group people by gender and Age and select Sex

Sample: people 
        |groupBy (x, tuple(x.Sex, x.Age))
        |select (s, s.Key.Item1)
Result: Sequence of groups based on people gender and age (Sex and Age property)

In this example, Item1 is referring to x.Sex values and similarly Item2 will refer to x.Age.

String

toLower FlexRule Engine in .Net Platforms

Returns a copy of this string converted to lowercase.

value |toLower ()
  • value: Required

toUpper FlexRule Engine in .Net Platforms

Returns a copy of this string converted to uppercase.

value |toUpper ()
  • value: Required

startsWith FlexRule Engine in .Net Platforms

Determines whether the beginning of this string instance matches a specified string.

value |startsWith (string)
  • string: Required
  • value: Required

endsWith FlexRule Engine in .Net Platforms

Determines whether the end of this string instance matches a specified string.

value |endsWith (string)
  • string: Required
  • value: Required

match FlexRule Engine in .Net Platforms

Determines whether a string matches a specific regular expression pattern.

value |match (pattern, flags)
  • pattern: Required
  • flags: Optional
    • i: Specifies case-insensitive matching.
    • m: Multiline mode. Changes the meaning of ^ and $ so these respectively match at the beginning and end of any line, and not just the beginning and end of the entire string.
    • s: Specifies single-line mode. Changes the meaning of the dot (.), so that it matches every character (instead of every character except \n).
    • c: Specifies that cultural differences in language are ignored.
    • r: Specifies that the search will be from right to left, instead of from left to right.
  • value (Required): the value must be a string value, otherwise the match result is false.
Example: "test@gmail.com" |macth ("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*","i")

replace FlexRule Engine in .Net Platforms

Determines whether a string matches a specific regular expression pattern.

value |replace (pattern, replacement, flags)
  • pattern: Required
  • replacement: New string for the replacement
  • flags: Optional
    • i: Specifies case-insensitive matching.
    • m: Multiline mode. Changes the meaning of ^ and $ so these respectively match at the beginning and end of any line, and not just the beginning and end of the entire string.
    • s: Specifies single-line mode. Changes the meaning of the dot (.), so that it matches every character (instead of every character except \n).
    • c: Specifies that cultural differences in language are ignored.
    • r: Specifies that the search will be from right to left, instead of from left to right.
  • value: Required

Object

hasProperty FlexRule Engine in .Net Platforms

Determines whether or not an object has a property or attribute.

property FlexRule Engine in .Net Platforms

Reads the value of an object's property.

setProperties FlexRule Engine in .Net Platforms

Sets the values of object's properties.

|setProperties (list)
  • list (Required): list of name:value separated with comma.
  • return: the actual object

Example:

person|setProperties(Name:'Arash', Email:'test@test.com')

attribute FlexRule Engine in .Net Platforms

Look at Expression/Pipes#property

setAttributes FlexRule Engine in .Net Platforms

Look at Expression/Pipes#setProperties

maybe FlexRule Engine in .Net Platforms

Allows conditional access to an object's members. If the object is null, the result would be null or a default value that can be set.

Type

is FlexRule Engine in .Net Platforms

Determines whether an object is from a specific type.

|is (typeName)
  • typeName: Required
  • return: Boolean

Example:

Sample: list 
        |any (x, x |is (Car))
Result: true

asString FlexRule Engine in .Net Platforms

Converts the input to string typed value.

|asString (surroundWithQuote)
  • surroundWithQuote: optional (true/false)
  • return: string

Example: Converting numeric to string

Sample: 55
        |asString(true) ()
Result: '55'

Example: Converting an object to JSON string

Sample: car
        |asJson() ()
        |asString() ()
Result: Return JSON string of the car object

asInt FlexRule Engine in .Net Platforms

Converts the input to integer typed value.

|asInt ()
  • return: Integer

Example:

Sample: '34' 
        |asInt ()
Result: 34

asDouble FlexRule Engine in .Net Platforms

Converts the input to double typed value.

|asDouble ()
  • return: Double

Example:

Sample: '2.5' 
        |asDouble ()
Result: 2.5d

asDecimal FlexRule Engine in .Net Platforms

Converts the input to decimal typed value.

|asDecimal ()
  • return: Double

Example:

Sample: '22.34' 
        |asDecimal ()
Result: 22.35m

asBoolean FlexRule Engine in .Net Platforms

Converts the input to a boolean typed value.

|asBoolean ()
  • return: Boolean (true/false)

Example:

Sample: 'false' 
        |asBoolean ()
Result: false

asDate FlexRule Engine in .Net Platforms

Converts the input to DateTime typed value.

|asDate (format)
  • format: Optional. Custom format to convert the input to DateTime.
  • return: DateTime

Example:

Sample: '03/22/2009' 
        |asDate (dd/MM/yyyy)
Result: 03/22/2009

asJson FlexRule Engine in .Net Platforms

Converts the input to a JSON value.

|asJson ()
  • return: JSON

Formatting

format FlexRule Engine in .Net Platforms

Binds values of execution context to a parameterized string value. Parameters in the string should be between { and }.

input |format ()

Example:

Sample: 'hello {name}' |format() 
Result: hello arash
        The assumption is that there is a name parameter in the logic which has a value of arash


formatDate FlexRule Engine in .Net Platforms

Format a date to a string value with a specific display format.

input |formatDate (format)

Below are the list of formatters that can be used:

  • year: y yy yyy yyyy
  • month: M MM MMM MMMM
  • day: d dd ddd dddd
  • hour: h hh H HH
  • minute: m mm
  • second: s ss
  • AM/PM: t tt

Example:

Sample: date |formatDate(dd/MM/yyyy)

formatNumber FlexRule Engine in .Net Platforms

Format a numeric value.

input |formatNumber (format)

Example:

Sample: 12323d |formatNumber(#,##.00) 
Result: 12,323.00

CSV

toCsv FlexRule Engine in .Net Platforms

Creates a CSV document from a source input

source |toCsv (options)
  • source: null to create a new CSV document, otherwise a list of objects
  • options: The monad options is a set of key:value separated by ,
    • header: If header should be part of the content when serialized, then true, otherwise false. Default is true.
    • separator: Specifies the field value separator in each row of CSV. Default is ,
  • return: a CSV document that CSV monads can operate on
Example: null |toCsv (separator:'\t', header: false)
Result: Returns a new CSV document that field values are separated by Tab (\t) and header will not be written.

csvColumn FlexRule Engine in .Net Platforms

Creates or gets column on a CSV document

source |csvColumn (name, options)
  • source: can be an existing column or CSV document
  • name: Property
  • options: The monad options is a set of key:value separated by ,
    • formatter: Allows introducing a function that can format the values when Csv is serialized.
    • trim: True when values should be trimmed. Otherwise false.
    • headerName: name of the header, otherwise the property name is used
  • return: CSV document

Formatter function

The function should be in the following format:

FunctionName(ArgName)=Expression
  • FunctionName: A name for the formatter function
  • ArgName: The argument of the function which will be a place holder for the column value to be formatted. This function must have only one argument.
  • Expression: The behaviour using Expressions to define the formatting logic

Example

Define Name, Date and Country columns on doc. Also set the formatter property for the Date column called fnDate which accepts the value of date as an argument named dt.

doc 
 |csvColumn('Name')
 |csvColumn('Date', 
    formatter: fnDate (dt)= 
            if dt!=null 
            then dt|formatDate(dd/MM/yyyy) 
            else null
    )
 |csvColumn('Country')

csvAdd FlexRule Engine in .Net Platforms

Adds a record or object to a CSV document

source |csvAdd (record)
  • source: can is a CSV document
  • record: can be a single or a collection of objects to be added to the CSV document
  • return: CSV document

XML

toXml FlexRule Engine in .Net Platforms

Creates XML object from a source input

source |toXml (defaultNamespace, autoPrefix)
  • source: a string representation of an XML, or an XML document.
  • return: an XML object that can be manipulated using related monads
  • defaultNamespace: the prefix of default namespace to discover and register namespaces automatically
  • autoPrefix: true to use xpath prefixed with default namespaces, default is false

xmlNamespaces FlexRule Engine in .Net Platforms

Returns all namespaces of an XML object

xml |xmlNamespaces ()
  • return: a collection of string representations of all namespaces of the document

xmlDefaultNamespace FlexRule Engine in .Net Platforms

Returns the default namespace of an XML object

xml |xmlDefaultNamespace ()
  • return: The default namespace of xml document

xmlElements FlexRule Engine in .Net Platforms

Returns list of elements of a node based on an xpath

xmlNode |xmlElements (xpath)
  • xpath: (Mandatory) a valid xpath address in the document

xmlElement FlexRule Engine in .Net Platforms

Returns a single element of a node based on an xpath

xmlNode |xmlElement (xpath)
  • xpath: (Mandatory) a valid xpath address in the document

xmlAttribute FlexRule Engine in .Net Platforms

Returns an attribute element of a node based on an xpath. It also can set value of an attribute.

xmlNode |xmlAttribute (attributeName, attributeValue)
  • attributeName: (Mandatory) name of attribute
  • attributeValue: an [expression]. When this is set, it changes the value of an attribute when it exists, otherwise it adds an attribute with a name and value provided.

xmlAttributes FlexRule Engine in .Net Platforms

Returns all of the attributes of a collection of nodes based on an xpath. It also can set the values of all attributes.

xmlNodes |xmlAttributes (attributeName, attributeValue)
  • attributeName: (Mandatory) name of attribute
  • attributeValue: an [expression]. When this is set, it changes the value of an attribute when it exists, otherwise it adds an attribute with a name and value provided.

xmlValue FlexRule Engine in .Net Platforms

Returns or sets value of element or attributes.

xmlNode |xmlElement (value)

xmlValues FlexRule Engine in .Net Platforms

Returns a list of values of element or attributes.

xmlNodes |xmlValues ()

xmlNodes can be one or a list of nodes. Still the result will be a list with one or many values.

xmlNew FlexRule Engine in .Net Platforms

Creates a new element inside the xml node.

xmlNode |xmlNew (name)
  • name: (Mandatory) This is the name of a new element.

xmlRemove FlexRule Engine in .Net Platforms

Removes the current element and all of its children. If the node is an attribute, it removes the attribute from the node and returns the element. If the current element is a node, it deletes the node and all of its children, and will return the parent of the node.

xmlRemove |xmlRemove ()

xmlParent FlexRule Engine in .Net Platforms

Returns the parent of a node.

xmlNode |xmlParent (level)
  • level: (Optional) Number of levels to navigate up the hierarchy. If not provided, default is one, which means the parent of the current xmlNode.

xmlParents FlexRule Engine in .Net Platforms

Returns all of the parents of a collection of nodes.

xmlNodes |xmlParents ()

Complex Examples

Identifying Duplicates

Let's say we have the following list:

Name Age Sex
Arash 38 Male
Parsa 6 Male
Arash 38 Male
Pooya 3 Male
Shah 3 Female
Shah 31 Male

Example 1: Name Duplication

Sample: people 
        |groupBy(p, p.Name) 
        |where (g, g.Count>1)
        |select (d, d.Key)
Result: List of people names that are duplicated
Name
Arash
Shah


Example 2: Name and Sex Duplication (A person is duplicated when Name and Sex are the same)

Sample: people 
        |groupBy (p, key(p.Name, p.Sex)) 
        |where (g, g.Count>1)
        |select (d, d.Key.Name)
Result: List of names that are duplicated
Name
Arash

Complex Data Processing

If we have the following data and would like to split it into two sections:

Cars-data-question.png

We have other versions of this at this example.

The other way is to use pure monadic expressions and solve it in two steps;

 /* intersections */
 allUniqueCars = cars |filter (thisCar, 
                         cars |count(otherCar, 
                           (otherCar.Make == thisCar.Make) and (otherCar.Model == thisCar.Model)) 
                         == cars|distinct(g, g.Group)|count()
                           )
                    |distinct(x, x.Make, x.Model);
                     
 /* differences */
 commonCars = cars |filter (thisCar, 
                         cars |count(otherCar, 
                           (otherCar.Make == thisCar.Make) and (otherCar.Model == thisCar.Model)) 
                           == 1
                           )
                    |distinct(x, x.Make, x.Model);

Monad101

Do you need more examples and sample expressions? Visit Monad101