ipavlic / apex-fp Goto Github PK
View Code? Open in Web Editor NEWFunctional programming for Salesforce Apex
Home Page: https://www.apexfp.org
License: MIT License
Functional programming for Salesforce Apex
Home Page: https://www.apexfp.org
License: MIT License
I have a question, because I'm having trouble with one of my tests because of this.
In the "compare" method of the PrimitiveComparer class, you check if both values are of the same type to do the comparison. But there is one problem for me: What if fieldValue is null? null is not an instance of any type, so when fieldValue is null, and condition.value is not null, the compare method is called anyway and a lambdaException is thrown.
For instance, in the comparisonConditionSatisfied method in FieldsMatch class, if condition.value is null, then you compare fieldValue to null. But why don't you add, if comparison.value != null and fieldValue is null, return false?
I'm not an experienced programmer, and I don't know the repercussions to the rest of the system, so I would appreciate a quick explanation on this.
Am I supposed to skip records with null field values before filtering?
I appreciate the response, and I'm sorry if it is an obvious one.
While it's convenient to be able to just call a method through a class name, like Pluck
, chaining reads in reverse. For example, GroupBy.strings(Filter)
translates to group what you have filtered. A new API would instead be Filter(...).groupBy(...)
, which would translate to filter then group.
On the downside, collections would first need to be wrapped into a new class, which would then provide a starting point. The new API would therefore look something like this.
static Lambda.of(collection) → LambdaList
LambdaList.groupByDates(fieldName) → Map<Date, List<SObject>>
LambdaList.pluckStrings() → List<String>
LambdaList.pick(fields) → LambdaList
LambdaList.match(prototype) → LambdaList
LambdaList.filter(field) → PartialFieldFilterQuery
PartialFieldFilterQuery.equals(value) → FieldFilterQuery
FieldFilterQuery.apply() → LambdaList
LambdaList.map(sobjectFunction) → LambdaList
LambdaList.asList() → List
LamdaList.asSet() → Set
Finally, API use examples would look something like:
List<Opportunity> opps = [SELECT ...];
Lambda.of(opps).filter(Opportunity.Amount).gt(10000).apply().groupByDates(Opportunity.CloseDate)
Opportunity prototype;
SObjectFunction fn;
Lambda.of(opps).match(prototype).map(fn).asList();
I am trying to use pluck to get a list of email. Here is what I tried. Is there any way we can do this? Thanks.
List<PermissionSetAssignment> psa = [SELECT Id, AssigneeId, Assignee.Email
FROM PermissionSetAssignment
];
List<String> emails = Pluck.strings(psa, PermissionSetAssignment.Assignee.Email);
Suppose i am developing a managed package for App Exchange.
How would i include the apex-lambda code with that as a dependency?
Do i need to deploy all the classes to my dev org and then ship inside my package?
nice work.
is there a way to install (managed package?) so that the apex-fp tests are not part of the organization test runs? thinking i may have installed incorrectly as the apex-fp test runs are running with our code deploys.
Hi @ipavlic - thanks for the great library but it's hard to deploy a lot of classes in different environments.
Is it possible to setup a CI to create the packages? You can migrate the source to DX and create an unlocked package. It still will be open source but it will be much easier to install and upgrade on destination org.
I'm happy to help with DX convert and CI setup, let me know if you are looking forward to the pull request.
Cheers!
Hi Ilija,
I have a scenario where I would like to determine the record returned when working out the difference between two lists, i.e the record having the min of a field value compared.
What do you think would be a good approach to solving this or do you think I should compare the lists twice and manage the minimum of the two outside of the compare. An idea I had was perhaps a similar approach to mapAll(SObjectToSObjectFunction fn), or perhaps something entirely different?
Amazing work btw, one of my favorite projects!
Right now the Collection class only caters to a list of sobjects. Are there plans to cater to list of custom classes? My use case right now is to use methods like pluck against wrapper classes that wraps responses from API callouts.
For example, my API response wrapper class contains a List<Shipments>
. I wanted to iterate over that list of Shipment records and pluck the shipment_id
field. Do you foresee that use case in your roadmap?
Filter could have an applyLazy. If I'm looking for the first 10 accounts that match some query filtering the whole list is wasteful. You could return an Iterable instead so I could consume as many or as few as I needed.
CollectionTest.testHasValue Fails with the following exception:
LambdaException: Comparison for provided types is not supported 232
Class.PrimitiveComparer.compare: line 76, column 1
Class.FieldsMatch.comparisonConditionSatisfied: line 72, column 1
Class.FieldsMatch.conditionSatisfied: line 52, column 1
Class.FieldsMatch.apply: line 40, column 1
Class.Collection.filter: line 79, column 1
Class.CollectionTest.testHasValue: line 414, column 1
Looking at the documentation it says
Defines a set membership condition for the current field. value has to be a Set, where T is a Boolean, Date, Datetime, Decimal, Double, Id, Integer, Long or String. Other types are not supported and will throw an exception.
I see in the implementation you just do
public FieldsMatch isIn(Object value) {
return filterWith(BinaryRelation.IS_IN, value);
}
You can overload the definition with other signatures to constraint the type at compile time by replacing it with
public FieldsMatch isIn(Set<Boolean> value) {
return filterWith(BinaryRelation.IS_IN, value);
}
public FieldsMatch isIn(Set<Date> value) {
return filterWith(BinaryRelation.IS_IN, value);
}
//...
Same for isNotIn()
.
Is there a syntax problems which prevents you from doing this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.