[squeak-dev] String >> #numArgs
Thiede, Christoph
Christoph.Thiede at student.hpi.uni-potsdam.de
Mon Dec 16 12:18:07 UTC 2019
Hi all! :-)
> Woah! This works? When and why is this useful? Does anybody recall the prime example? :-) #inject:into:?
Yes, I believe I once used it with #inject:into:, but I don't remember. What prime example are you referring to? :)
My personal favorite is:
Matrix rows: 5 columns: 6 tabulate: #@ "for quickly setting up an example matrix :-)"
I find it rather confusing that the following does not work:
#raisedTo: value: 2 value: 3. "works"
#raisedTo:modulo: value: 2 value: 3 value: 4. "does not understand"
This is an inconsistency.
@Eliot:
> Can you give some examples of where you find you are needing to do this?
Actually, I was trying something like this:
SequenceableCollection >> #collectX: aBlock
^ aBlock numArgs = 2
ifTrue: [self withIndexCollect: aBlock]
ifFalse: [self collect: aBlock]].
Now you can say:
#(1 2 3) collectX: [:x | x squared].
#(1 2 3) collectX: #squared.
#(1 2 3) collectX: [:x :i | x + i].
But the following does not work:
#(1 2 3) collectX: #+.
Maybe I would rather need something like #value:cull: for an optional second argument?
In general, from the point of scripting convenience, I would love Symbol to support many more protocols of BlockClosure. What about:
Symbol >> #asBlock
^ self numArgs caseOf: {
[0] -> [[:rcvr | rcvr perform: self]].
[1] -> [[:rcvr :arg | rcvr perform: self with: arg]].
[2] -> [[:rcrv :arg1 :arg2 | rcrv perform: self with: arg1 with: arg2]].
... }
Then I could say at least:
#(1 2 3) collectX: #+ asBlock.
And also:
#raisedTo:modulo: asBlock value: 2 value: 3 value: 4. "does not understand"
Best,
Christoph
________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Taeumel, Marcel
Gesendet: Montag, 16. Dezember 2019 09:22:42
An: John Pfersich via Squeak-dev
Betreff: Re: [squeak-dev] String >> #numArgs
> bar := #raisedTo:.
> bar value: 2 value: 3.
Woah! This works? When and why is this useful? Does anybody recall the prime example? :-) #inject:into:?
I mean, "bar numArgs" works as expected and so does "foo numArgs". That #value:value: on Symbol is surprising for me here.
Best,
Marcel
Am 16.12.2019 04:57:56 schrieb John Pfersich via Squeak-dev <squeak-dev at lists.squeakfoundation.org>:
Besides 2 raisedTo: 3 should answer 8, not 5.
/————————————————————/
For encrypted mail use jgpfersich at protonmail.com
Get a free account at ProtonMail.com
Web: www.objectnets.net and www.objectnets.org
On Dec 15, 2019, at 03:15, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:
Hi all!
Consider the following:
foo := [:a :b | a raisedTo: b].
bar := #raisedTo:.
foo value: 2 value: 3. "5"
bar value: 2 value: 3. "5"
foo numArgs. "2"
bar numArgs. "1"
How can I overcome this inconsistency?
In situations where a symbol acts as "evaluable", quaking like a block, it is confusing to get these different results
Does there already exist a unified protocol? Or am I doing something wrong in general when I need this?
Best,
Christoph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20191216/780d765d/attachment.html>
More information about the Squeak-dev
mailing list
|