How to take care that the + before a number is ignored
Hello, I have a collection that looks like this : sampleData1    "comment stating purpose of message"    ^ #( -8    +7) I want to add those numbers up but the code chokes at the + so I did this : FrequencyFinderData new class sampleData1  inject: 0 into: [:sum :each | (each ~= $+) ifTrue: [sum + each asInteger] ] so when the each is not a + it must count it but still the code chokes at the + Where do I think wrong ? Roelof
Hi Roelof, Two problems: 1. + is a symbol so compare it to #+ 2. A false case is needed to return the untouched sum, otherwise it truns to nil #(+1 -8) inject: 0 into: [:sum :each | each ~= #+ ifTrue: [sum +Â each] ifFalse: [sum]] . Hilaire -- Dr. Geo http://drgeo.eu
You're giving yourself *A LOT* of trouble by not simply reading a file! Besides, you'll have the same problem for every problem, twice per problem! ----------------- Benoît St-Jean Yahoo! Messenger: bstjean Twitter: @BenLeChialeux Pinterest: benoitstjean Instagram: Chef_Benito IRC: lamneth Blogue: endormitoire.wordpress.com "A standpoint is an intellectual horizon of radius zero". (A. Einstein) On Sunday, December 2, 2018, 5:46:21 a.m. EST, Roelof Wobben <r.wobben@home.nl> wrote: Op 2-12-2018 om 11:15 schreef Hilaire:
#(+1 -8) inject: 0 into: [:sum :each | each ~= #+ ifTrue: [sum +Â each] ifFalse: [sum]] .
Thanks, Now to stretch my mind I will try to make one with double-dispatch. Roelof
Thinking about this functionally, you want to sum the array elements that are not + . So, (array select: [:each | each ~~ #+]) sum The *best* approach is not to put the + symbols into the array in the first place. On Sun, 2 Dec 2018 at 23:46, Roelof Wobben <r.wobben@home.nl> wrote:
Op 2-12-2018 om 11:15 schreef Hilaire:
#(+1 -8) inject: 0 into: [:sum :each | each ~= #+ ifTrue: [sum + each] ifFalse: [sum]] .
Thanks,
Now to stretch my mind I will try to make one with double-dispatch.
Roelof
Who said anything about deleting any numbers? The code fragment I posted did not delete any numbers; it merely made a new array with everything that was not #+. In the original post there were no strings. (By the way, you wrote "trailing" where you meant "leading".) On Mon, 3 Dec 2018 at 08:40, Roelof Wobben <r.wobben@home.nl> wrote:
and I do not want to delete all numbers that begin with a + but remove the trailing + from a string which look like this +15
Op 2-12-2018 om 20:36 schreef Richard O'Keefe:
Thinking about this functionally, you want to sum the array elements that are not + . So, (array select: [:each | each ~~ #+]) sum The *best* approach is not to put the + symbols into the array in the first place.
On Sun, 2 Dec 2018 at 23:46, Roelof Wobben <r.wobben@home.nl> wrote:
Op 2-12-2018 om 11:15 schreef Hilaire:
#(+1 -8) inject: 0 into: [:sum :each | each ~= #+ ifTrue: [sum + each] ifFalse: [sum]] .
Thanks,
Now to stretch my mind I will try to make one with double-dispatch.
Roelof
Roeloff, Is this for the Advent of Code 2018? The easiest way is to read the input file... Then, I can guide you from there! ;) ----------------- Benoît St-Jean Yahoo! Messenger: bstjean Twitter: @BenLeChialeux Pinterest: benoitstjean Instagram: Chef_Benito IRC: lamneth Blogue: endormitoire.wordpress.com "A standpoint is an intellectual horizon of radius zero". (A. Einstein) On Sunday, December 2, 2018, 4:49:06 a.m. EST, Roelof Wobben <r.wobben@home.nl> wrote: Hello, I have a collection that looks like this : sampleData1    "comment stating purpose of message"    ^ #( -8    +7) I want to add those numbers up but the code chokes at the + so I did this : FrequencyFinderData new class sampleData1  inject: 0 into: [:sum :each | (each ~= $+) ifTrue: [sum + each asInteger] ] so when the each is not a + it must count it but still the code chokes at the + Where do I think wrong ? Roelof
On Sun, 2 Dec 2018 at 17:49, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I have a collection that looks like this :
sampleData1 "comment stating purpose of message"
^ #( -8 +7)
I want to add those numbers up but the code chokes at the +
To get some insight, do.... #( -8 +7) inspect You will notice three elements when I think you expected two. So perhaps you need... x := #( -8 +7) reject: [:y | y = #+]. and then #inject:into: "x" cheers -ben
so I did this :
FrequencyFinderData new class sampleData1 inject: 0 into: [:sum :each | (each ~= $+) ifTrue: [sum + each asInteger] ]
so when the each is not a + it must count it but still the code chokes at the +
Where do I think wrong ?
Roelof
participants (5)
-
Ben Coman -
Benoit St-Jean -
Hilaire -
Richard O'Keefe -
Roelof Wobben