Difference b/w apply(),applyComponent,For Each loop

Hi ,

 

Can anyone share some good example to differentiate b/w apply(),applyComponent,For Each loop.

 

Please don't share any documentation link as i am not getting clarity by seeing that.

 

If possible plz use any small dataset to give an example. This always confuse which one i should use on what scenario.

 

  Discussion posts and replies are publicly visible

  • apply(): Call a function or a rule for each time which you have specified in a list.
    ex:

    apply(
    sum(1,_),
    {1,2,3,4}
    )

    function will execute 4 times because length of list is 4,in place of sum() you can cal your own rules.

    apply() cannot be used with rules or functions that store local data, including rules using load() and certain SAIL components.
    In these cases, we have to use a!applyComponents().


    a!foreach() is also a looping function
    in previous versions it was not their so we have to go with apply() and a!applyComponents(),
    from 17.2 version we have a!foreach() ,it will do every thing which apply() and a!applyComponents() does.
  • Hello sauravk,

    Even though you said "don't share documentation links" I still think this can be really easy to understand with this example.

    Before we had the forEach we needed to use the applyComponents which helped us to repeat some interface. Something similar to the "apply" function. applyComponents requires you to create a rule the forEach gives you more control over the loop and in a more generic loop.

    To give you the example how to use each the recipe of the editable grid is the perfect example

    editable with ApplyComponents (old version)
    docs.appian.com/.../recipe_add_edit_and_remove_data_in_an_inline_editable_grid.html

    editable grid with forEach (newer version)
    docs.appian.com/.../recipe_add_edit_and_remove_data_in_an_inline_editable_grid.html

    Hope this helps

    Jose
  • Hi sauravk,

    apply( )-Calls a rule or function for each item in a list, and provides any contexts specified.

    Ex: apply(fn!sum, {1, 2, 3}, 2) returns 3; 4; 5 This is the simple example for apply function.

    forEach( )- Evaluates the provided expression once for every item and returns an array of the results.
    Ex:
    a!forEach(
    items: {"January", "February", "March"},
    expression: with(
    local!month: fv!item,
    a!forEach(
    items: {1, 2},
    expression: local!month & " " & fv!item
    )
    )
    )

    output: {"January 1", "January 15"}, {"February 1", "February 15"}, and {"March 1", "March 15"}

    applyComponents: calls a rule for each item in an array and returns an array of the results.
    a!applyComponents() is applicable when you're iterating over a list of components or iterating over a rule that has a load() within it,

    Ex: a!applyComponents(
    function: fn!isnull,
    array: {
    1,
    5,
    null,
    6,
    null,
    8
    }
    )

    O/p:
    false false true false true false

    Thanks,
    ravalik
  • Hi,

    apply() :
    Calls a rule or function for each item in a list, and provides any contexts specified.
    apply(fn!sumsq, {1,2,3},10)
    It returns the output as
    101
    104
    109
    apply() cannot be used with rules or functions that store local data, including rules using load() and certain interface components. In these cases, apply() will return an error. In these cases, use a!applyComponents().


    a!applyComponents() :
    Calls a rule or function for each item in a list and supports the preservation of the local state on interfaces.
    load(
    local!stateTokens,
    a!applyComponents(
    function: fn!sumsq,
    array: ri!num,
    arrayVariable: local!stateTokens
    )
    )
    It returns the output as
    1
    4
    9
    applyComponents() preserves the local state while apply cannot.

    a!foreach() :
    a!foreach() is also a looping function
    In previous versions it was not their so we have to go with apply() and a!applyComponents(),
    from 17.2 version we have a!foreach() ,it will do every thing which apply() and a!applyComponents() does but with easier syntax, better null handling, and streamlined support of SAIL components.

    a!forEach(items: {1, 2, 3}, expression: fv!item + 10)
    It returns the output as
    11
    12
    13
    Once go through this link for more information about it.
    docs.appian.com/.../fnc_looping_a_foreach.html

    and check this previous discussion
    community.appian.com/.../what-is-the-difference-between-apply-applycomponents-and-foreach

    I hope , it will help you..

    Regards
    Aswini