pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

Iterating over a Dictionary

SR
sergio ruiz
Tue, Jan 23, 2024 4:24 PM

I need to iterate over a dictionary.

I asked the AI buddy for a little help, and he says:

| myDictionary |

"Create a dictionary"
myDictionary := Dictionary new.
myDictionary at: 'one' put: 1.
myDictionary at: 'two' put: 2.
myDictionary at: 'three' put: 3.

"Iterate over the dictionary"
myDictionary do: [ :key :value |
Transcript show: key , ' -> ', value printString ; nl.
].

but when i try this, I get:

ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument.

Is the AI using a different dialect of smalltalk?

how would I go about acting on each pair?

Thanks!


peace,
sergio
photographer, journalist, visionary

Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
@sergio_101@mastodon.social
https://sergio101.com
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

I need to iterate over a dictionary. I asked the AI buddy for a little help, and he says: | myDictionary | "Create a dictionary" myDictionary := Dictionary new. myDictionary at: 'one' put: 1. myDictionary at: 'two' put: 2. myDictionary at: 'three' put: 3. "Iterate over the dictionary" myDictionary do: [ :key :value | Transcript show: key , ' -> ', value printString ; nl. ]. but when i try this, I get: ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument. Is the AI using a different dialect of smalltalk? how would I go about acting on each pair? Thanks! ---- peace, sergio photographer, journalist, visionary Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV @sergio_101@mastodon.social https://sergio101.com http://www.codeandmusic.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101
JT
Joachim Tuchel
Tue, Jan 23, 2024 4:27 PM

AI knows little about Smalltalk ;-)

You can use do: with each Association as a parameter or you can use
keysAndValuesDo:

myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...].

myDic keysAndValuesDo: [:key :value| ...]

Joachim

Am 23.01.24 um 17:24 schrieb sergio ruiz:

I need to iterate over a dictionary.

I asked the AI buddy for a little help, and he says:

| myDictionary |

"Create a dictionary"
myDictionary := Dictionary new.
myDictionary at: 'one' put: 1.
myDictionary at: 'two' put: 2.
myDictionary at: 'three' put: 3.

"Iterate over the dictionary"
myDictionary do: [ :key :value |
    Transcript show: key , ' -> ', value printString ; nl.
].

but when i try this, I get:

ArgumentsCountMismatch: This block accepts 2 arguments, but was called
with 1 argument.

Is the AI using a different dialect of smalltalk?

how would I go about acting on each pair?

Thanks!


peace,
sergio
photographer, journalist, visionary

Public Key:
https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
@sergio_101@mastodon.social
https://sergio101.com
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

--


Objektfabrik Joachim Tuchelmailto:jtuchel@objektfabrik.de
Fliederweg 1http://www.objektfabrik.de
D-71640 Ludwigsburghttp://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0                    Fax: +49 7141 56 10 86 1

AI knows little about Smalltalk ;-) You can use do: with each Association as a parameter or you can use keysAndValuesDo: myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...]. myDic keysAndValuesDo: [:key :value| ...] Joachim Am 23.01.24 um 17:24 schrieb sergio ruiz: > I need to iterate over a dictionary. > > I asked the AI buddy for a little help, and he says: > > | myDictionary | > > "Create a dictionary" > myDictionary := Dictionary new. > myDictionary at: 'one' put: 1. > myDictionary at: 'two' put: 2. > myDictionary at: 'three' put: 3. > > "Iterate over the dictionary" > myDictionary do: [ :key :value | >     Transcript show: key , ' -> ', value printString ; nl. > ]. > > but when i try this, I get: > > ArgumentsCountMismatch: This block accepts 2 arguments, but was called > with 1 argument. > > Is the AI using a different dialect of smalltalk? > > how would I go about acting on each pair? > > Thanks! > > ---- > peace, > sergio > photographer, journalist, visionary > > Public Key: > https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 > #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV > @sergio_101@mastodon.social > https://sergio101.com > http://www.codeandmusic.com > http://www.twitter.com/sergio_101 > http://www.facebook.com/sergio101 > -- ----------------------------------------------------------------------- Objektfabrik Joachim Tuchelmailto:jtuchel@objektfabrik.de Fliederweg 1http://www.objektfabrik.de D-71640 Ludwigsburghttp://joachimtuchel.wordpress.com Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1
JF
James Foster
Tue, Jan 23, 2024 4:31 PM

myDict associationsDo: [: anAssociation | ].
myDict keysDo: [:aKey | ].
myDict valuesDo: [:aValue | ].
myDict do: [:aValue | ]. “An alias for #valuesDo:”

James Foster

On Jan 23, 2024, at 8:27 AM, Joachim Tuchel jtuchel@objektfabrik.de wrote:

AI knows little about Smalltalk ;-)

You can use do: with each Association as a parameter or you can use keysAndValuesDo:

myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...].
myDic keysAndValuesDo: [:key :value| ...]

Joachim

Am 23.01.24 um 17:24 schrieb sergio ruiz:

I need to iterate over a dictionary.

I asked the AI buddy for a little help, and he says:

| myDictionary |

"Create a dictionary"
myDictionary := Dictionary new.
myDictionary at: 'one' put: 1.
myDictionary at: 'two' put: 2.
myDictionary at: 'three' put: 3.

"Iterate over the dictionary"
myDictionary do: [ :key :value |
Transcript show: key , ' -> ', value printString ; nl.
].

but when i try this, I get:

ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument.

Is the AI using a different dialect of smalltalk?

how would I go about acting on each pair?

Thanks!


peace,
sergio
photographer, journalist, visionary

Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
@sergio_101@mastodon.social
https://sergio101.com https://sergio101.com/
http://www.codeandmusic.com http://www.codeandmusic.com/
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

--


Objektfabrik Joachim Tuchel              mailto:jtuchel@objektfabrik.de
Fliederweg 1                                http://www.objektfabrik.de http://www.objektfabrik.de/
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com http://joachimtuchel.wordpress.com/
Telefon: +49 7141 56 10 86 0                    Fax: +49 7141 56 10 86 1

myDict associationsDo: [: anAssociation | ]. myDict keysDo: [:aKey | ]. myDict valuesDo: [:aValue | ]. myDict do: [:aValue | ]. “An alias for #valuesDo:” James Foster > On Jan 23, 2024, at 8:27 AM, Joachim Tuchel <jtuchel@objektfabrik.de> wrote: > > AI knows little about Smalltalk ;-) > > You can use do: with each Association as a parameter or you can use keysAndValuesDo: > > myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...]. > myDic keysAndValuesDo: [:key :value| ...] > > > Joachim > > > > > > Am 23.01.24 um 17:24 schrieb sergio ruiz: >> I need to iterate over a dictionary. >> >> I asked the AI buddy for a little help, and he says: >> >> | myDictionary | >> >> "Create a dictionary" >> myDictionary := Dictionary new. >> myDictionary at: 'one' put: 1. >> myDictionary at: 'two' put: 2. >> myDictionary at: 'three' put: 3. >> >> "Iterate over the dictionary" >> myDictionary do: [ :key :value | >> Transcript show: key , ' -> ', value printString ; nl. >> ]. >> >> but when i try this, I get: >> >> ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument. >> >> Is the AI using a different dialect of smalltalk? >> >> how would I go about acting on each pair? >> >> Thanks! >> >> ---- >> peace, >> sergio >> photographer, journalist, visionary >> >> Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 >> #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV >> @sergio_101@mastodon.social >> https://sergio101.com <https://sergio101.com/> >> http://www.codeandmusic.com <http://www.codeandmusic.com/> >> http://www.twitter.com/sergio_101 >> http://www.facebook.com/sergio101 >> > -- > > ----------------------------------------------------------------------- > Objektfabrik Joachim Tuchel mailto:jtuchel@objektfabrik.de > Fliederweg 1 http://www.objektfabrik.de <http://www.objektfabrik.de/> > D-71640 Ludwigsburg http://joachimtuchel.wordpress.com <http://joachimtuchel.wordpress.com/> > Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1 >
JT
Joachim Tuchel
Tue, Jan 23, 2024 4:33 PM

So there we already have dialect differences …

Am 23.01.2024 um 17:32 schrieb James Foster via Pharo-users <pharo-users@lists.pharo.org>:

myDict associationsDo: [: anAssociation | ].myDict keysDo: [:aKey | ].

myDict valuesDo: [:aValue | ].

myDict do: [:aValue | ]. “An alias for #valuesDo:”

James Foster

On Jan 23, 2024, at 8:27 AM, Joachim Tuchel <jtuchel@objektfabrik.de> wrote:

AI knows little about Smalltalk ;-)

You can use do: with each Association as a parameter or you can use keysAndValuesDo:

myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...].
myDic keysAndValuesDo: [:key :value| ...]

Joachim

Am 23.01.24 um 17:24 schrieb sergio ruiz:

I need to iterate over a dictionary.

I asked the AI buddy for a little help, and he says:

| myDictionary |

"Create a dictionary"

myDictionary := Dictionary new.

myDictionary at: 'one' put: 1.

myDictionary at: 'two' put: 2.

myDictionary at: 'three' put: 3.

"Iterate over the dictionary"

myDictionary do: [ :key :value |

Transcript show: key , ' -> ', value printString ; nl.

].

but when i try this, I get:

ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument.

Is the AI using a different dialect of smalltalk?

how would I go about acting on each pair?

Thanks!


peace,
sergio
photographer, journalist, visionary

Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV

@sergio_101@mastodon.social

https://sergio101.com
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

<pre class="moz-signature" cols="72">-- 

----------------------------------------------------------------------- 
Objektfabrik Joachim Tuchel              <a class="moz-txt-link-freetext" href="mailto:jtuchel@objektfabrik.de">mailto:jtuchel@objektfabrik.de</a> 
Fliederweg 1                                 <a class="moz-txt-link-freetext" href="http://www.objektfabrik.de/">http://www.objektfabrik.de</a>
D-71640 Ludwigsburg                  <a class="moz-txt-link-freetext" href="http://joachimtuchel.wordpress.com/">http://joachimtuchel.wordpress.com</a>
Telefon: +49 7141 56 10 86 0                    Fax: +49 7141 56 10 86 1

SR
sergio ruiz
Tue, Jan 23, 2024 5:04 PM

oh! this makes sense..

let me try this.

The trick is, i have used the AI system from JetBrains (in my day job IDE).. and it has figured out a bunch of smalltalk questions i had. Even questions about the pharo ecosystem.

It hasn’t always been 100%, but it got me to look in the right place quickly.

This was the first time it came up with something that looked right (i have seen this pattern everywhere).. but it was not correct..

Thanks!

On Jan 23, 2024, at 11:27 AM, Joachim Tuchel jtuchel@objektfabrik.de wrote:

AI knows little about Smalltalk ;-)

oh! this makes sense.. let me try this. The trick is, i have used the AI system from JetBrains (in my day job IDE).. and it has figured out a bunch of smalltalk questions i had. Even questions about the pharo ecosystem. It hasn’t always been 100%, but it got me to look in the right place quickly. This was the first time it came up with something that looked right (i have seen this pattern everywhere).. but it was not correct.. Thanks! > On Jan 23, 2024, at 11:27 AM, Joachim Tuchel <jtuchel@objektfabrik.de> wrote: > > AI knows little about Smalltalk ;-) ---- peace, sergio photographer, journalist, visionary Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV @sergio_101@mastodon.social https://sergio101.com http://www.codeandmusic.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101
RS
Richard Sargent
Tue, Jan 23, 2024 6:29 PM

In the dialects I am familiar with, James' answer is correct. Some dialects
implement #keysAndValuesDo: on sequenceable collections, in which case, the
keys are the indexes of the values.

On Tue, Jan 23, 2024, 12:05 sergio ruiz sergio.rrd@gmail.com wrote:

oh! this makes sense..

let me try this.

The trick is, i have used the AI system from JetBrains (in my day job
IDE).. and it has figured out a bunch of smalltalk questions i had. Even
questions about the pharo ecosystem.

It hasn’t always been 100%, but it got me to look in the right place
quickly.

This was the first time it came up with something that looked right (i
have seen this pattern everywhere).. but it was not correct..

Thanks!

On Jan 23, 2024, at 11:27 AM, Joachim Tuchel jtuchel@objektfabrik.de
wrote:

AI knows little about Smalltalk ;-)


peace,
sergio
photographer, journalist, visionary

Public Key:
https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
@sergio_101@mastodon.social
https://sergio101.com
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

In the dialects I am familiar with, James' answer is correct. Some dialects implement #keysAndValuesDo: on sequenceable collections, in which case, the keys are the indexes of the values. On Tue, Jan 23, 2024, 12:05 sergio ruiz <sergio.rrd@gmail.com> wrote: > oh! this makes sense.. > > let me try this. > > The trick is, i have used the AI system from JetBrains (in my day job > IDE).. and it has figured out a bunch of smalltalk questions i had. Even > questions about the pharo ecosystem. > > It hasn’t always been 100%, but it got me to look in the right place > quickly. > > This was the first time it came up with something that looked right (i > have seen this pattern everywhere).. but it was not correct.. > > Thanks! > > > On Jan 23, 2024, at 11:27 AM, Joachim Tuchel <jtuchel@objektfabrik.de> > wrote: > > AI knows little about Smalltalk ;-) > > > ---- > peace, > sergio > photographer, journalist, visionary > > Public Key: > https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 > #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV > @sergio_101@mastodon.social > https://sergio101.com > http://www.codeandmusic.com > http://www.twitter.com/sergio_101 > http://www.facebook.com/sergio101 > >
JT
Joachim Tuchel
Tue, Jan 23, 2024 6:30 PM

James is of course right.

do: iterates over the values, if you want the whole associations, you
use associationsDo:

At least I was right about keysAndValuesDo: ;-)

Joachim

myDict do: [:aValue | ]. “An alias for #valuesDo:”

James Foster

On Jan 23, 2024, at 8:27 AM, Joachim Tuchel jtuchel@objektfabrik.de
wrote:

AI knows little about Smalltalk ;-)

You can use do: with each Association as a parameter or you can use
keysAndValuesDo:

myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...].
myDic keysAndValuesDo: [:key :value| ...]

Joachim

Am 23.01.24 um 17:24 schrieb sergio ruiz:

I need to iterate over a dictionary.

I asked the AI buddy for a little help, and he says:

| myDictionary |

"Create a dictionary"
myDictionary := Dictionary new.
myDictionary at: 'one' put: 1.
myDictionary at: 'two' put: 2.
myDictionary at: 'three' put: 3.

"Iterate over the dictionary"
myDictionary do: [ :key :value |
    Transcript show: key , ' -> ', value printString ; nl.
].

but when i try this, I get:

ArgumentsCountMismatch: This block accepts 2 arguments, but was
called with 1 argument.

Is the AI using a different dialect of smalltalk?

how would I go about acting on each pair?

Thanks!


peace,
sergio
photographer, journalist, visionary

Public Key:
https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
@sergio_101@mastodon.social
https://sergio101.com
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

--


Objektfabrik Joachim Tuchelmailto:jtuchel@objektfabrik.de
Fliederweg 1http://www.objektfabrik.de
D-71640 Ludwigsburghttp://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0                    Fax: +49 7141 56 10 86 1

--


Objektfabrik Joachim Tuchelmailto:jtuchel@objektfabrik.de
Fliederweg 1http://www.objektfabrik.de
D-71640 Ludwigsburghttp://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0                    Fax: +49 7141 56 10 86 1

James is of course right. do: iterates over the values, if you want the whole associations, you use associationsDo: At least I was right about keysAndValuesDo: ;-) Joachim > myDict do: [:aValue | ]. “An alias for #valuesDo:” > > James Foster > >> On Jan 23, 2024, at 8:27 AM, Joachim Tuchel <jtuchel@objektfabrik.de> >> wrote: >> >> AI knows little about Smalltalk ;-) >> >> You can use do: with each Association as a parameter or you can use >> keysAndValuesDo: >> >> myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...]. >> myDic keysAndValuesDo: [:key :value| ...] >> >> >> Joachim >> >> >> >> Am 23.01.24 um 17:24 schrieb sergio ruiz: >>> I need to iterate over a dictionary. >>> >>> I asked the AI buddy for a little help, and he says: >>> >>> | myDictionary | >>> >>> "Create a dictionary" >>> myDictionary := Dictionary new. >>> myDictionary at: 'one' put: 1. >>> myDictionary at: 'two' put: 2. >>> myDictionary at: 'three' put: 3. >>> >>> "Iterate over the dictionary" >>> myDictionary do: [ :key :value | >>>     Transcript show: key , ' -> ', value printString ; nl. >>> ]. >>> >>> but when i try this, I get: >>> >>> ArgumentsCountMismatch: This block accepts 2 arguments, but was >>> called with 1 argument. >>> >>> Is the AI using a different dialect of smalltalk? >>> >>> how would I go about acting on each pair? >>> >>> Thanks! >>> >>> ---- >>> peace, >>> sergio >>> photographer, journalist, visionary >>> >>> Public Key: >>> https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 >>> #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV >>> @sergio_101@mastodon.social >>> https://sergio101.com >>> http://www.codeandmusic.com >>> http://www.twitter.com/sergio_101 >>> http://www.facebook.com/sergio101 >>> >> -- >> >> ----------------------------------------------------------------------- >> Objektfabrik Joachim Tuchelmailto:jtuchel@objektfabrik.de >> Fliederweg 1http://www.objektfabrik.de >> D-71640 Ludwigsburghttp://joachimtuchel.wordpress.com >> Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1 >> > -- ----------------------------------------------------------------------- Objektfabrik Joachim Tuchelmailto:jtuchel@objektfabrik.de Fliederweg 1http://www.objektfabrik.de D-71640 Ludwigsburghttp://joachimtuchel.wordpress.com Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1
TB
Todd Blanchard
Tue, Jan 23, 2024 8:10 PM

Hi Sergio,

Keep in mind, you have the source code to everything.

So you can just browse Dictionary and when you find #do: it says right in the code what it does.

On Jan 23, 2024, at 10:04 AM, sergio ruiz sergio.rrd@gmail.com wrote:

oh! this makes sense..

let me try this.

The trick is, i have used the AI system from JetBrains (in my day job IDE).. and it has figured out a bunch of smalltalk questions i had. Even questions about the pharo ecosystem.

It hasn’t always been 100%, but it got me to look in the right place quickly.

This was the first time it came up with something that looked right (i have seen this pattern everywhere).. but it was not correct..

Thanks!

On Jan 23, 2024, at 11:27 AM, Joachim Tuchel jtuchel@objektfabrik.de wrote:

AI knows little about Smalltalk ;-)

Hi Sergio, Keep in mind, you have the source code to everything. So you can just browse Dictionary and when you find #do: it says right in the code what it does.  > On Jan 23, 2024, at 10:04 AM, sergio ruiz <sergio.rrd@gmail.com> wrote: > > oh! this makes sense.. > > let me try this. > > The trick is, i have used the AI system from JetBrains (in my day job IDE).. and it has figured out a bunch of smalltalk questions i had. Even questions about the pharo ecosystem. > > It hasn’t always been 100%, but it got me to look in the right place quickly. > > This was the first time it came up with something that looked right (i have seen this pattern everywhere).. but it was not correct.. > > Thanks! > > >> On Jan 23, 2024, at 11:27 AM, Joachim Tuchel <jtuchel@objektfabrik.de> wrote: >> >> AI knows little about Smalltalk ;-) > > ---- > peace, > sergio > photographer, journalist, visionary > > Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 > #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV > @sergio_101@mastodon.social > https://sergio101.com > http://www.codeandmusic.com > http://www.twitter.com/sergio_101 > http://www.facebook.com/sergio101 >
RO
Richard O'Keefe
Wed, Jan 24, 2024 9:59 AM

There are many books about Smalltalk.  Stephane Ducasse has made many
of them available as FREE pdfs.
Of course Pharo by Example would be a good start.  The latest edition
of Pharo by Example I have ready to hand is 8.0, 2020, where what you
want is chapter 13, Collections.

aCollection do: [:element | ...]

aKeyedCollection keysAndValuesDo: [:key :element | ...]

dictionaries are keyed and so are sequences.  You can iterate over the
elements of any collection using #do: with a one-argument block.  The
elements will be passed to the block and the keys will NOT.  If you
want the keys, must use #keysAndValuesDo: with a two-argument block.
The keys and corresponding elements will be passed to the block.

You "AI buddy" is a treacherous lying ignorant bastard.  Such is the
start of AI.

  1. Open a browser on Dictionary.

  2. Select "enumerating" the the method category panel (top row, third
    from left).

  3. In the method panel (top row, rightmost) you will see do:
    keysAndValuesDo: keysDo: valuesDo:

  4. Selecting them one at a time you will see

    do: aBlock
    ^self valuesDo: aBlock
    keysAndValuesDo: aBlock
    ^self associationsDo: [:assoc |
    aBlock value: assoc key value: assoc value]
    keysDo: aBlock
    ^self associationsDo:: [:association |
    aBlock value: associatoin key]
    valuesDo: aBlock
    tally = 0 ifTrue: [^self].
    1 to: array size do: [:eachIndex |
    |eachAssociation|
    eachAssociation := array at: eachIndex.
    nil == eachAssociation ifFalse: [
    aBlock value: eachAssociation value]].

Now to fully make sense of this, you'd have to understand how a
Dictionary is implemented in Pharo.  (An Array whose elements are
reach either nil or an Association holding a key and a value.  This is
a traditional Smalltalk implementation of dictionaries, but others
except and some are better.)  But you CAN see quite easily from this
code that #do:, #keysDo:, and #valuesDo: pass just one value to their
block argument, while #keysAndValuesDo: passes TWO values.  And this
is typicalk of trying to glean understanding by looking at the system
source code:  it is easy, you won't undersand everything, but you'll
learn something.

On Wed, 24 Jan 2024 at 05:24, sergio ruiz sergio.rrd@gmail.com wrote:

I need to iterate over a dictionary.

I asked the AI buddy for a little help, and he says:

| myDictionary |

"Create a dictionary"
myDictionary := Dictionary new.
myDictionary at: 'one' put: 1.
myDictionary at: 'two' put: 2.
myDictionary at: 'three' put: 3.

"Iterate over the dictionary"
myDictionary do: [ :key :value |
Transcript show: key , ' -> ', value printString ; nl.
].

but when i try this, I get:

ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument.

Is the AI using a different dialect of smalltalk?

how would I go about acting on each pair?

Thanks!


peace,
sergio
photographer, journalist, visionary

Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
@sergio_101@mastodon.social
https://sergio101.com
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

There are many books about Smalltalk. Stephane Ducasse has made many of them available as FREE pdfs. Of course Pharo by Example would be a good start. The latest edition of Pharo by Example I have ready to hand is 8.0, 2020, where what you want is chapter 13, Collections. aCollection do: [:element | ...] aKeyedCollection keysAndValuesDo: [:key :element | ...] dictionaries are keyed and so are sequences. You can iterate over the elements of any collection using #do: with a one-argument block. The elements will be passed to the block and the keys will NOT. If you want the keys, must use #keysAndValuesDo: with a two-argument block. The keys and corresponding elements will be passed to the block. You "AI buddy" is a treacherous lying ignorant bastard. Such is the start of AI. 1. Open a browser on Dictionary. 2. Select "enumerating" the the method category panel (top row, third from left). 3. In the method panel (top row, rightmost) you will see do: keysAndValuesDo: keysDo: valuesDo: 4. Selecting them one at a time you will see do: aBlock ^self valuesDo: aBlock keysAndValuesDo: aBlock ^self associationsDo: [:assoc | aBlock value: assoc key value: assoc value] keysDo: aBlock ^self associationsDo:: [:association | aBlock value: associatoin key] valuesDo: aBlock tally = 0 ifTrue: [^self]. 1 to: array size do: [:eachIndex | |eachAssociation| eachAssociation := array at: eachIndex. nil == eachAssociation ifFalse: [ aBlock value: eachAssociation value]]. Now to fully make sense of this, you'd have to understand how a Dictionary is implemented in Pharo. (An Array whose elements are reach either nil or an Association holding a key and a value. This is a traditional Smalltalk implementation of dictionaries, but others except and some are better.) But you CAN see quite easily from this code that #do:, #keysDo:, and #valuesDo: pass just one value to their block argument, while #keysAndValuesDo: passes TWO values. And this is typicalk of trying to glean understanding by looking at the system source code: it is easy, you won't undersand everything, but you'll learn *something*. On Wed, 24 Jan 2024 at 05:24, sergio ruiz <sergio.rrd@gmail.com> wrote: > > I need to iterate over a dictionary. > > I asked the AI buddy for a little help, and he says: > > | myDictionary | > > "Create a dictionary" > myDictionary := Dictionary new. > myDictionary at: 'one' put: 1. > myDictionary at: 'two' put: 2. > myDictionary at: 'three' put: 3. > > "Iterate over the dictionary" > myDictionary do: [ :key :value | > Transcript show: key , ' -> ', value printString ; nl. > ]. > > but when i try this, I get: > > ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument. > > Is the AI using a different dialect of smalltalk? > > how would I go about acting on each pair? > > Thanks! > > ---- > peace, > sergio > photographer, journalist, visionary > > Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 > #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV > @sergio_101@mastodon.social > https://sergio101.com > http://www.codeandmusic.com > http://www.twitter.com/sergio_101 > http://www.facebook.com/sergio101 >
RO
Richard O'Keefe
Wed, Jan 24, 2024 10:02 AM

Wrong.
If you want to iterate of the associations of a dictionary, you need
#associationsDo:.
#do: passes the values to the block, NOT the associations.

On Wed, 24 Jan 2024 at 05:28, Joachim Tuchel jtuchel@objektfabrik.de wrote:

AI knows little about Smalltalk ;-)

You can use do: with each Association as a parameter or you can use keysAndValuesDo:

myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...].

myDic keysAndValuesDo: [:key :value| ...]

Joachim

Am 23.01.24 um 17:24 schrieb sergio ruiz:

I need to iterate over a dictionary.

I asked the AI buddy for a little help, and he says:

| myDictionary |

"Create a dictionary"
myDictionary := Dictionary new.
myDictionary at: 'one' put: 1.
myDictionary at: 'two' put: 2.
myDictionary at: 'three' put: 3.

"Iterate over the dictionary"
myDictionary do: [ :key :value |
Transcript show: key , ' -> ', value printString ; nl.
].

but when i try this, I get:

ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument.

Is the AI using a different dialect of smalltalk?

how would I go about acting on each pair?

Thanks!


peace,
sergio
photographer, journalist, visionary

Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
@sergio_101@mastodon.social
https://sergio101.com
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

--


Objektfabrik Joachim Tuchel              mailto:jtuchel@objektfabrik.de
Fliederweg 1                                http://www.objektfabrik.de
D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
Telefon: +49 7141 56 10 86 0                    Fax: +49 7141 56 10 86 1

Wrong. If you want to iterate of the associations of a dictionary, you need #associationsDo:. #do: passes the *values* to the block, *NOT* the associations. On Wed, 24 Jan 2024 at 05:28, Joachim Tuchel <jtuchel@objektfabrik.de> wrote: > > AI knows little about Smalltalk ;-) > > You can use do: with each Association as a parameter or you can use keysAndValuesDo: > > myDic do: [:assoc| | key value| key := assoc key. value := assoc value. ...]. > > myDic keysAndValuesDo: [:key :value| ...] > > > Joachim > > > > Am 23.01.24 um 17:24 schrieb sergio ruiz: > > I need to iterate over a dictionary. > > I asked the AI buddy for a little help, and he says: > > | myDictionary | > > "Create a dictionary" > myDictionary := Dictionary new. > myDictionary at: 'one' put: 1. > myDictionary at: 'two' put: 2. > myDictionary at: 'three' put: 3. > > "Iterate over the dictionary" > myDictionary do: [ :key :value | > Transcript show: key , ' -> ', value printString ; nl. > ]. > > but when i try this, I get: > > ArgumentsCountMismatch: This block accepts 2 arguments, but was called with 1 argument. > > Is the AI using a different dialect of smalltalk? > > how would I go about acting on each pair? > > Thanks! > > ---- > peace, > sergio > photographer, journalist, visionary > > Public Key: https://pgp.key-server.io/pks/lookup?op=get&search=0x69B08F58923AB3A2 > #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV > @sergio_101@mastodon.social > https://sergio101.com > http://www.codeandmusic.com > http://www.twitter.com/sergio_101 > http://www.facebook.com/sergio101 > > -- > > ----------------------------------------------------------------------- > Objektfabrik Joachim Tuchel mailto:jtuchel@objektfabrik.de > Fliederweg 1 http://www.objektfabrik.de > D-71640 Ludwigsburg http://joachimtuchel.wordpress.com > Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1 >