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 |
In reply to this post by 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 <[hidden email]> 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 |
In reply to this post by HilaireFernandes
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 |
In reply to this post by Pharo Smalltalk Users mailing list
Op 2-12-2018 om 11:43 schreef Benoit St-Jean via Pharo-users:
if you can learn me that. Right now., the input is a method. I think I can do the rest from there |
In reply to this post by Roelof
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 <[hidden email]> 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 |
In reply to this post by Roelof
Do you want the solution for the first one to get you going? The part2 of problem 1 is somewhat a little more complex but once you get the idea, the rest shouldn't be that hard! ----------------- 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:50:30 a.m. EST, Roelof Wobben <[hidden email]> wrote:
Op 2-12-2018 om 11:43 schreef Benoit St-Jean via Pharo-users: if you can learn me that. Right now., the input is a method. I think I can do the rest from there |
Op 2-12-2018 om 11:55 schreef Benoit St-Jean via Pharo-users:
Nope, only the part how I can read the file. Roelof |
You can do solve the problem without the "lines" variable but, believe me, you want to keep those lines in a collection. It's gonna be a lot easier down the road! Hint (to solve the problem) : look at what "lines" contain (instances of which class). That class has everything you need to parse those '+2' and '-8' ... ;) | file | lines := OrderedCollection new. file := StandardFileStream readOnlyFileNamed: 'day.1.input'. [file atEnd] whileFalse: [lines add: file nextLine]. file close. ----------------- 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, 6:09:42 a.m. EST, Roelof Wobben <[hidden email]> wrote:
Op 2-12-2018 om 11:55 schreef Benoit St-Jean via Pharo-users: Nope, only the part how I can read the file. Roelof |
In reply to this post by Roelof
Oups! Usually, it's way nicer/better/suggested to declare all variables... Forgot "lines" ! . Nicer version: | file lines | lines := OrderedCollection new. file := StandardFileStream readOnlyFileNamed: 'day.1.input'. [file atEnd] whileFalse: [lines add: file nextLine]. file close. ----------------- 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, 6:09:42 a.m. EST, Roelof Wobben <[hidden email]> wrote:
Op 2-12-2018 om 11:55 schreef Benoit St-Jean via Pharo-users: Nope, only the part how I can read the file. Roelof |
In reply to this post by Roelof
> On 2 Dec 2018, at 12:07, Roelof Wobben <[hidden email]> wrote: > > Nope, only the part how I can read the file. For scripting (non-production) code, you can read the whole file (and split it into lines) using 'file.log' asFileReference contents lines. |
In reply to this post by Pharo Smalltalk Users mailing list
> On 2 Dec 2018, at 12:14, Benoit St-Jean via Pharo-users <[hidden email]> wrote: > > > From: Benoit St-Jean <[hidden email]> > Subject: Re: [Pharo-users] How to take care that the + before a number is ignored > Date: 2 December 2018 at 12:14:10 GMT+1 > To: [hidden email] > Reply-To: Benoit St-Jean <[hidden email]> > > > You can do solve the problem without the "lines" variable but, believe me, you want to keep those lines in a collection. It's gonna be a lot easier down the road! > > Hint (to solve the problem) : look at what "lines" contain (instances of which class). That class has everything you need to parse those '+2' and '-8' ... ;) > > > | file | > > lines := OrderedCollection new. > > file := StandardFileStream readOnlyFileNamed: 'day.1.input'. That is a deprecated class in Pharo 7. > [file atEnd] whileFalse: [lines add: file nextLine]. > file close. Here is a better way: Array streamContents: [ :lines | 'file.log' asFileReference readStreamDo: [ :in | [ in atEnd ] whileFalse: [ lines nextPut: in nextLine ] ] ] > ----------------- > 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, 6:09:42 a.m. EST, Roelof Wobben <[hidden email]> wrote: > > > Op 2-12-2018 om 11:55 schreef Benoit St-Jean via Pharo-users: > > Nope, only the part how I can read the file. > > > Roelof > > > > |
In reply to this post by Pharo Smalltalk Users mailing list
Op 2-12-2018 om 12:19 schreef Benoit St-Jean via Pharo-users:
oke, and if I understand your code files is then the variable that holds the file. Roelof |
Exactly! It's easier to manipulate that OrderedCollection that to open/reopen the file. It'll be VERY handy for problem #2 where you will need to iterate multiple times on the data! ;) ----------------- 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, 7:15:06 a.m. EST, Roelof Wobben <[hidden email]> wrote:
Op 2-12-2018 om 12:19 schreef Benoit St-Jean via Pharo-users: oke, and if I understand your code files is then the variable that holds the file. Roelof |
In reply to this post by Sven Van Caekenberghe-2
Wasn't sure, I'm on Squeak right now!! loll ----------------- 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, 6:26:12 a.m. EST, Sven Van Caekenberghe <[hidden email]> wrote:
> On 2 Dec 2018, at 12:14, Benoit St-Jean via Pharo-users <[hidden email]> wrote: > > > From: Benoit St-Jean <[hidden email]> > Subject: Re: [Pharo-users] How to take care that the + before a number is ignored > Date: 2 December 2018 at 12:14:10 GMT+1 > To: [hidden email] > Reply-To: Benoit St-Jean <[hidden email]> > > > You can do solve the problem without the "lines" variable but, believe me, you want to keep those lines in a collection. It's gonna be a lot easier down the road! > > Hint (to solve the problem) : look at what "lines" contain (instances of which class). That class has everything you need to parse those '+2' and '-8' ... ;) > > > | file | > > lines := OrderedCollection new. > > file := StandardFileStream readOnlyFileNamed: 'day.1.input'. That is a deprecated class in Pharo 7. > [file atEnd] whileFalse: [lines add: file nextLine]. > file close. Here is a better way: Array streamContents: [ :lines | 'file.log' asFileReference readStreamDo: [ :in | [ in atEnd ] whileFalse: [ lines nextPut: in nextLine ] ] ] > ----------------- > 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, 6:09:42 a.m. EST, Roelof Wobben <[hidden email]> wrote: > > > Op 2-12-2018 om 11:55 schreef Benoit St-Jean via Pharo-users: > > Nope, only the part how I can read the file. > > > Roelof > > > > |
In reply to this post by Roelof
On Sun, 2 Dec 2018 at 17:49, Roelof Wobben <[hidden email]> wrote: Hello, 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
|
In reply to this post by Pharo Smalltalk Users mailing list
I am not using a file, but copy/paste my puzzleInput into a puzzleInput method. Then I use lines from there. e.g. shifts ^ self puzzleInput lines collect: [ :each | ((each beginsWith: '+') ifTrue: [ each allButFirst ] ifFalse: [ each ]) asNumber ] Because the challenge is personalized and it is annoying to have to commit a supplementary file in the github repository. Phil On Sun, Dec 2, 2018 at 3:27 PM Benoit St-Jean via Pharo-users <[hidden email]> wrote:
|
Again a memory message? Op 2 december 2018 om 17:06 schreef phil--- via Pharo-users <[hidden email]>: |
In reply to this post by Pharo Smalltalk Users mailing list
Just realized that String>>#asNumber works differently in Pharo and in Squeak. '+3' asNumber "Works in Squeak, not in Pharo" BUT '+3' asInteger "Works in both Squeak and Pharo" ----------------- 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, 11:06:56 a.m. EST, [hidden email] <[hidden email]> wrote:
I am not using a file, but copy/paste my puzzleInput into a puzzleInput method. Then I use lines from there. e.g. shifts ^ self puzzleInput lines collect: [ :each | ((each beginsWith: '+') ifTrue: [ each allButFirst ] ifFalse: [ each ]) asNumber ] Because the challenge is personalized and it is annoying to have to commit a supplementary file in the github repository. Phil On Sun, Dec 2, 2018 at 3:27 PM Benoit St-Jean via Pharo-users <[hidden email]> wrote:
|
In reply to this post by 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 <[hidden email]> wrote: Op 2-12-2018 om 11:15 schreef Hilaire: |
Free forum by Nabble | Edit this page |