String multiplication idiom in apex?
Does apex include any programming idioms to quickly and clearly initialise or pad strings with a specific character, for example 'string multiplication' such as '-'*40 to easily create a rule, or '.'*(size-str.length() to pad to length?
I new to apex, though experienced developer; I want to produce a quick and simple bar graph of a specific dataset to highlight any anomalies. This code will render a bar-graph to the logs, seems very 'old school', surely there is better way to do this in apex? I want something that is clear and concise. Surely there is a way to do without the nested loop in the produce bar graph part of the code?
// retrieve distribution data
Integer distribution = new Integer[100];
for(Integer i=0; i<1000 ; i++) {
// the real data actual comes from elsewhere;
// this just simulates it with random numbers for this example.
Integer num = (math.random() * 100).intValue();
System.assert(num >= 0 && num < 100, 'Value out of expected range : ' + num);
if (distribution[num] == null) {
distribution[num] = 1;
} else {
distribution[num]++;
}
}
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') ';
// Surely there is better way to do this bit?
for(integer i=0 ; i<distribution[num] ; i++) { barGraph += '.'; }
barGraph += 'n';
}
System.Debug( barGraph );
apex
add a comment |
Does apex include any programming idioms to quickly and clearly initialise or pad strings with a specific character, for example 'string multiplication' such as '-'*40 to easily create a rule, or '.'*(size-str.length() to pad to length?
I new to apex, though experienced developer; I want to produce a quick and simple bar graph of a specific dataset to highlight any anomalies. This code will render a bar-graph to the logs, seems very 'old school', surely there is better way to do this in apex? I want something that is clear and concise. Surely there is a way to do without the nested loop in the produce bar graph part of the code?
// retrieve distribution data
Integer distribution = new Integer[100];
for(Integer i=0; i<1000 ; i++) {
// the real data actual comes from elsewhere;
// this just simulates it with random numbers for this example.
Integer num = (math.random() * 100).intValue();
System.assert(num >= 0 && num < 100, 'Value out of expected range : ' + num);
if (distribution[num] == null) {
distribution[num] = 1;
} else {
distribution[num]++;
}
}
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') ';
// Surely there is better way to do this bit?
for(integer i=0 ; i<distribution[num] ; i++) { barGraph += '.'; }
barGraph += 'n';
}
System.Debug( barGraph );
apex
1
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago
1
So you're rendering a bar graph in the debug log, eh? Is this data ever displayed on a Visualforce page / Lightning component? If so, you may consider using the charting built in to Visualforce or a javascript library like D3.js or chart.js.
– Derek F
13 mins ago
@DerekF in this case the code will be used in testing only, it is not part of the core functional requirements, so the test logs will be fine for this output. If you want to suggest wider solution for others to benefit go right ahead.
– Martin of Hessle
7 mins ago
add a comment |
Does apex include any programming idioms to quickly and clearly initialise or pad strings with a specific character, for example 'string multiplication' such as '-'*40 to easily create a rule, or '.'*(size-str.length() to pad to length?
I new to apex, though experienced developer; I want to produce a quick and simple bar graph of a specific dataset to highlight any anomalies. This code will render a bar-graph to the logs, seems very 'old school', surely there is better way to do this in apex? I want something that is clear and concise. Surely there is a way to do without the nested loop in the produce bar graph part of the code?
// retrieve distribution data
Integer distribution = new Integer[100];
for(Integer i=0; i<1000 ; i++) {
// the real data actual comes from elsewhere;
// this just simulates it with random numbers for this example.
Integer num = (math.random() * 100).intValue();
System.assert(num >= 0 && num < 100, 'Value out of expected range : ' + num);
if (distribution[num] == null) {
distribution[num] = 1;
} else {
distribution[num]++;
}
}
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') ';
// Surely there is better way to do this bit?
for(integer i=0 ; i<distribution[num] ; i++) { barGraph += '.'; }
barGraph += 'n';
}
System.Debug( barGraph );
apex
Does apex include any programming idioms to quickly and clearly initialise or pad strings with a specific character, for example 'string multiplication' such as '-'*40 to easily create a rule, or '.'*(size-str.length() to pad to length?
I new to apex, though experienced developer; I want to produce a quick and simple bar graph of a specific dataset to highlight any anomalies. This code will render a bar-graph to the logs, seems very 'old school', surely there is better way to do this in apex? I want something that is clear and concise. Surely there is a way to do without the nested loop in the produce bar graph part of the code?
// retrieve distribution data
Integer distribution = new Integer[100];
for(Integer i=0; i<1000 ; i++) {
// the real data actual comes from elsewhere;
// this just simulates it with random numbers for this example.
Integer num = (math.random() * 100).intValue();
System.assert(num >= 0 && num < 100, 'Value out of expected range : ' + num);
if (distribution[num] == null) {
distribution[num] = 1;
} else {
distribution[num]++;
}
}
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') ';
// Surely there is better way to do this bit?
for(integer i=0 ; i<distribution[num] ; i++) { barGraph += '.'; }
barGraph += 'n';
}
System.Debug( barGraph );
apex
apex
asked 3 hours ago
Martin of HessleMartin of Hessle
586
586
1
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago
1
So you're rendering a bar graph in the debug log, eh? Is this data ever displayed on a Visualforce page / Lightning component? If so, you may consider using the charting built in to Visualforce or a javascript library like D3.js or chart.js.
– Derek F
13 mins ago
@DerekF in this case the code will be used in testing only, it is not part of the core functional requirements, so the test logs will be fine for this output. If you want to suggest wider solution for others to benefit go right ahead.
– Martin of Hessle
7 mins ago
add a comment |
1
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago
1
So you're rendering a bar graph in the debug log, eh? Is this data ever displayed on a Visualforce page / Lightning component? If so, you may consider using the charting built in to Visualforce or a javascript library like D3.js or chart.js.
– Derek F
13 mins ago
@DerekF in this case the code will be used in testing only, it is not part of the core functional requirements, so the test logs will be fine for this output. If you want to suggest wider solution for others to benefit go right ahead.
– Martin of Hessle
7 mins ago
1
1
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago
1
1
So you're rendering a bar graph in the debug log, eh? Is this data ever displayed on a Visualforce page / Lightning component? If so, you may consider using the charting built in to Visualforce or a javascript library like D3.js or chart.js.
– Derek F
13 mins ago
So you're rendering a bar graph in the debug log, eh? Is this data ever displayed on a Visualforce page / Lightning component? If so, you may consider using the charting built in to Visualforce or a javascript library like D3.js or chart.js.
– Derek F
13 mins ago
@DerekF in this case the code will be used in testing only, it is not part of the core functional requirements, so the test logs will be fine for this output. If you want to suggest wider solution for others to benefit go right ahead.
– Martin of Hessle
7 mins ago
@DerekF in this case the code will be used in testing only, it is not part of the core functional requirements, so the test logs will be fine for this output. If you want to suggest wider solution for others to benefit go right ahead.
– Martin of Hessle
7 mins ago
add a comment |
1 Answer
1
active
oldest
votes
Using the comment by @kurunve for direction, and for the benefit of others, this is the improved code using rightPad.
// single line rule.
System.Debug( ''.rightPad(40, '-' ));
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') '.rightPad(distribution[num], '+') + 'n';
}
System.Debug( barGraph );
// produce double line rule.
System.Debug( ''.rightPad(40, '=' ));
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago
2
@pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.
– Adrian Larson♦
1 hour ago
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f246322%2fstring-multiplication-idiom-in-apex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using the comment by @kurunve for direction, and for the benefit of others, this is the improved code using rightPad.
// single line rule.
System.Debug( ''.rightPad(40, '-' ));
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') '.rightPad(distribution[num], '+') + 'n';
}
System.Debug( barGraph );
// produce double line rule.
System.Debug( ''.rightPad(40, '=' ));
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago
2
@pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.
– Adrian Larson♦
1 hour ago
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago
add a comment |
Using the comment by @kurunve for direction, and for the benefit of others, this is the improved code using rightPad.
// single line rule.
System.Debug( ''.rightPad(40, '-' ));
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') '.rightPad(distribution[num], '+') + 'n';
}
System.Debug( barGraph );
// produce double line rule.
System.Debug( ''.rightPad(40, '=' ));
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago
2
@pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.
– Adrian Larson♦
1 hour ago
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago
add a comment |
Using the comment by @kurunve for direction, and for the benefit of others, this is the improved code using rightPad.
// single line rule.
System.Debug( ''.rightPad(40, '-' ));
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') '.rightPad(distribution[num], '+') + 'n';
}
System.Debug( barGraph );
// produce double line rule.
System.Debug( ''.rightPad(40, '=' ));
Using the comment by @kurunve for direction, and for the benefit of others, this is the improved code using rightPad.
// single line rule.
System.Debug( ''.rightPad(40, '-' ));
// produce bar graph
String barGraph = 'n';
for(Integer num=0; num<100 ; num++) {
barGraph += '' + num + ') '.rightPad(distribution[num], '+') + 'n';
}
System.Debug( barGraph );
// produce double line rule.
System.Debug( ''.rightPad(40, '=' ));
edited 13 mins ago
answered 2 hours ago
Martin of HessleMartin of Hessle
586
586
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago
2
@pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.
– Adrian Larson♦
1 hour ago
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago
add a comment |
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago
2
@pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.
– Adrian Larson♦
1 hour ago
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago
2
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago
2
2
@pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.
– Adrian Larson♦
1 hour ago
@pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.
– Adrian Larson♦
1 hour ago
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f246322%2fstring-multiplication-idiom-in-apex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago
1
So you're rendering a bar graph in the debug log, eh? Is this data ever displayed on a Visualforce page / Lightning component? If so, you may consider using the charting built in to Visualforce or a javascript library like D3.js or chart.js.
– Derek F
13 mins ago
@DerekF in this case the code will be used in testing only, it is not part of the core functional requirements, so the test logs will be fine for this output. If you want to suggest wider solution for others to benefit go right ahead.
– Martin of Hessle
7 mins ago