|
yeah yeah.
although your postfix increment inside the expression like that could be more clear by moving it outside the expression.
Adding, in my final code i actually special case for 2 items as well "apple or pear" sans comma, like that.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I find
count++;
if (count > 1)
{
result.Append(", ");
if (count == list.Count)
result.Append("or ");
}
less obvious to read, since if (count > 1) really mean first iteration.... (which would be nicer with a zero)
|
|
|
|
|
A matter of taste I suppose.
I'm trying a little something different with parser generation this go round.
I'm making one that generates a recursive descent parser.
so like, your rules get baked out into recursive descent routines like this, instead of table driven code:
Makes it easy to put code into it.
private static ParseNode _ParseBoolean(ParserContext context) {
ParseNode result = null;
if ((JsonParser.@true == context.SymbolId)) {
ParseNode[] children = new ParseNode[1];
children[0] = new ParseNode(JsonParser.@true, context.Value);
context.Advance();
return new ParseNode(JsonParser.Boolean, children);
}
if ((JsonParser.@false == context.SymbolId)) {
ParseNode[] children = new ParseNode[1];
children[0] = new ParseNode(JsonParser.@false, context.Value);
context.Advance();
return new ParseNode(JsonParser.Boolean, children);
}
context.Error("Expecting true or false");
return null;
}
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Nice, keep it up!
|
|
|
|
|
honey the codewitch wrote: context.Error("Expecting true or false");
Are you already programming with Q-Bits?
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
2nd option, perhaps to your liking, but really slower, using more memory, less readable
but it does avoid the local count variable
var list = new HashSet<object>() { };
var result = new StringBuilder();
foreach (var item in list)
{
if (result.Length > 0)
result.Remove(result.Length - 3, 3);
result.Append(item);
result.Append(",or ");
}
return result.ToString();
|
|
|
|
|
now you're just being a smart ass.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
The real problem is the insistence on infix notation in English. If lists were written as "Apple, pear, banana OR", the meaning would be perfectly clear. It would also lend itself to more complex constructs.
Unfortunately, I know of no human language that uses similar syntax.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
that just gave me a potential idea.
Swap that last word and the notation such that they change places.
Apple, banana, or pear becomes Apple, banana, pear, or
and you have your fix.
Although I'd very much prefer
Or, apple, banana, pear
from a parsing standpoint
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Hahah, only now, reading this last message, I understand how English is making the list confusing, hahah!
|
|
|
|
|
Consider the LDAP way.
|( apple , banana , pear )
|
|
|
|
|
or the programming way
(apple | banana | pear)
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
I should add, no it's humans. Mixtec has constructs in it that would make your head spin. Like what they classify as "round things" is incomprehensible. Laundry detergent is round. AND THEY ALL HAVE PRONOUNS. So you have to know what's what. Also the language has way more verbs than nouns, such that you can't tell what kind of bag someone has except by describing how they're carrying it.
Also they don't have a word for smile.
Humans are ridiculous.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Quote:
else if(0==c) {
result.Append(list[0]);
} An empty list will now produce an ArgumentOutOfRangeException .
This should work with any IEnumerable list:
public static string JoinList<T>(this IEnumerable<T> list, string delimiter = ", ", string finalDelimiter = "or ")
{
int count = list.Count();
switch (count)
{
case 0:
{
return string.Empty;
}
case 1:
{
return Convert.ToString(list.First());
}
default:
{
int index = count;
var result = new StringBuilder();
foreach (var item in list)
{
if (result.Length != 0) result.Append(delimiter);
if (index == 1) result.Append(finalDelimiter);
result.Append(item);
--index;
}
return result.ToString();
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The Oxford comma (i.e. the comma before "and" or "or") is a style choice and optional.
In any event, assuming all elements of the list are unique, the solution is the same:
With comma:
var result = new StringBuilder();
list.ForEach(item => result.Append(item).Append(item == list.Last() ? "" : item == list.Take(list.Count - 1).Last() ? ", or " : ", "));
Or without comma:
var result = new StringBuilder();
list.ForEach(item => result.Append(item).Append(item == list.Last() ? "" : item == list.Take(list.Count - 1).Last() ? " or " : ", "));
|
|
|
|
|
I'd never create that many objects to solve such a mundane problem unless I was doing business dev.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Guess what I do?
Actually, as I wrote it, I did wonder about how efficient it is!
I don't think it creates that many objects; I think Take just builds a new List referencing the original elements. Last definitely just references the last element and doesn't copy it.
|
|
|
|
|
Other than when LINQ can combine them, all enumerations such as foreach, or any operating which takes any subset or combine of an enumeration will create an enumerator object.
You're creating a lot of them there. Well, 3 by my count. Basically every time you use a linq call, minus when linq can combine internally.
Edit: I should add, while it would be acceptable where I'm using it, I'm not currently referencing LINQ in my project and I wouldn't for a single situation like this.
It's easy enough to use LINQless code to achieve the same thing.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Alter conforms to filter (4)
|
|
|
|
|
FITS
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Um, now if you alter that...
|
|
|
|
|
Nature's best science photos of 2019: [^]Quote: He who can no longer pause to wonder and stand rapt in awe, is as good as dead; his eyes are closed. Albert Einstein
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Those are terrific images. I just wish they were higher resolution.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
|
|
|
|
|
Fascinating, thanks for posting!
|
|
|
|
|
Nice selection, thanks Bill!
|
|
|
|