[squeak-dev] String >> #numArgs

Tobias Pape Das.Linux at gmx.de
Mon Dec 16 12:41:03 UTC 2019


Hi

> On 16.12.2019, at 13:18, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:
> 
> 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 :-)"

Why not simply 
	Matrix rows: 5 columns: 6 tabulate: [:x :y | x @ y]

What's wrong with blocks?

> 
> 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.

Yes. The first case should also bail.

A symbol answering to #perform: is IMHO borderline.
A symbol's answer to #value should be itself.
A symbol should not answer to an #value: … #value:value:...


> 
> @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].

That's hacky.

I would then rather use withIndexCollect. It does what it says on the tin. 

> But the following does not work:
> #(1 2 3) collectX: #+.
> 
> Maybe I would rather need something like #value:cull: for an optional second argument?
> 

you can use cull:cull: anyways.

> 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]].
> ... }
> 
that's not too bad :D
but there is already perform:withArguments:, so this should be simpler?

Best regards
	-Tobias


More information about the Squeak-dev mailing list