function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jane1234jane1234 

filtering http response lambda expression

 say if we have 100 objects
[
{“ECN”:10001, “PCN”:”2131932199”, “RoleType”=”PNI”},
{“ECN”:10002, “PCN”:”2131932199”, “RoleType”=”PNI”},
.
.
.
{“ECN”:10099, “PCN”:”2131932199”, “RoleType”=”SNI”},
{“ECN”:10100, “PCN”:”2131932199”, “RoleType”=”SNI”}
]
 
If I want to take the object having the ECN value as 10099, how should I take it using single line of code instead of looping all the elements.
 
In JAVA, it can be done through lambda expression in one line of code. Please check the feasibility.
Best Answer chosen by jane1234
Alain CabonAlain Cabon
Hi,

I used R.apex and that works.  I deleted all the loops as you want for your question.

https://github.com/Click-to-Cloud/R.apex
String json_string = '[' +
'{"ECN":10001, "PCN":"2131932199", "RoleType":"PNI"},' +
'{"ECN":10099, "PCN":"2131932200", "RoleType":"PNI"},' +
'{"ECN":10099, "PCN":"2131932201", "RoleType":"SNI"},' +
'{"ECN":10100, "PCN":"2131932202", "RoleType":"SNI"}' +
    ']';

List<Object> lst= (List<Object>) JSON.deserializeUntyped(json_string);

R found = R.of(lst)
    .find(R.whereEq.apply(new Map<String, Object>{
        'ECN' => 10099
    }));

Map<String,String> m = found.toStringMap();
system.debug('ECN: ' + m.get('ECN') + ', PCN:' + m.get('PCN') + ', RoleType:' + m.get('RoleType')) ;


User-added image

I have deployed all these classes but for only R.apex, you could deployed less classes.
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://soap.sforce.com/2006/04/metadata">
    <version>43.0</version>
    <types>
        <members>Flow</members>
	    <members>FlowPackage</members>
		<members>FlowTest</members>
		<members>Func</members>
		<members>Jsep</members>
		<members>JsepTest</members>
		<members>R</members>
		<members>RTest</members>
		<members>ScriptEngine</members>
		<members>ScriptEngineTest</members>
        <name>ApexClass</name>
    </types>
</package>

 

All Answers

Raj VakatiRaj Vakati
No such in Apex .. they only easy way you can do it use the Apex to Json converter into the list of apex class and perform the loop to get the list of values
jane1234jane1234
i need alternative for looping  with out looping can i filter the element
jane1234jane1234
yes i am using apex
Raj VakatiRaj Vakati
That is the only way to do  .. use this tools  to convert apex wrapper class and then do the for loop 

https://json2apex.herokuapp.com/
Alain CabonAlain Cabon
Hi, 

Apex is closed for the "imports" (like the imports of jars in java) but you can create an invocable object camouflaged as a function, and it is referred to as a Func.

R.apex is a functional library based on Apex, inspired by Lodash and Ramda.js.
https://github.com/Click-to-Cloud/R.apex/

It is a recent project.

Apex does not support first class functions, and we have NO WAY to get around it. However, we can create an invocable object camouflaged as a function, and it is referred to as a Func. Or more precisely, it is an instance of class Func. In R.apex, we roughly refer to instances of Func when we mention functions, to make things clear.

Another thing to notice is that in functional programming, we tend to put the data we are manipulating in the last position in the argument list. For example, R.filter.run(R.isNotNull, myList);
Alain CabonAlain Cabon
Hi,

Flow.apex is a library to help you weave functions in a procedural style.
Why Flow.apex? Flow.apex is created to simplify the creation of a Function. It simulates the procedural invocation of Functions, and weave them to build a larger and more complicated Function. Flow.apex acts as a bridge between the small Funcs and big custom Funcs that you create by subclassing Func

Object result = Flow.eval('product(1, 2, 3, 4)');.

Flow f = new Flow() .inputAs('a', 'b').returnInteger() .doIf( 'a == 1', Flow.block().doReturn(1) );

https://github.com/Click-to-Cloud/Flow.apex

https://clicktocloud.com/
Alain CabonAlain Cabon
Hi,

I used R.apex and that works.  I deleted all the loops as you want for your question.

https://github.com/Click-to-Cloud/R.apex
String json_string = '[' +
'{"ECN":10001, "PCN":"2131932199", "RoleType":"PNI"},' +
'{"ECN":10099, "PCN":"2131932200", "RoleType":"PNI"},' +
'{"ECN":10099, "PCN":"2131932201", "RoleType":"SNI"},' +
'{"ECN":10100, "PCN":"2131932202", "RoleType":"SNI"}' +
    ']';

List<Object> lst= (List<Object>) JSON.deserializeUntyped(json_string);

R found = R.of(lst)
    .find(R.whereEq.apply(new Map<String, Object>{
        'ECN' => 10099
    }));

Map<String,String> m = found.toStringMap();
system.debug('ECN: ' + m.get('ECN') + ', PCN:' + m.get('PCN') + ', RoleType:' + m.get('RoleType')) ;


User-added image

I have deployed all these classes but for only R.apex, you could deployed less classes.
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://soap.sforce.com/2006/04/metadata">
    <version>43.0</version>
    <types>
        <members>Flow</members>
	    <members>FlowPackage</members>
		<members>FlowTest</members>
		<members>Func</members>
		<members>Jsep</members>
		<members>JsepTest</members>
		<members>R</members>
		<members>RTest</members>
		<members>ScriptEngine</members>
		<members>ScriptEngineTest</members>
        <name>ApexClass</name>
    </types>
</package>

 
This was selected as the best answer
Alain CabonAlain Cabon
The team of Clicktocloud (Sydney) made an outstanding contribution to the Apex language:

https://www.ctcproperty.com/About.html

Their Github repository is really great:   https://github.com/Click-to-Cloud