## Article: Exploring Cocoa with F-Script

I've made available a new article that shows how it is possible to interactively explore and script Objective-C objects on Mac OS X, using F-Script, an open source scripting language. The article describes the object browser and other interactive modules tailored for the exploration of objects at runtime.

## Comment viewing options

### Interesting

That's pretty interesting. The implicit map-like feature seems somewhat deceptive, though. On that subject, how exactly would you get the length of an array of strings, rather than the length of each element?

edit: Also, the images appear to be zoomed in on firefox and are difficult to read. To get around this I had to view each one separately.

### If an array gets an message

If an array gets an message it don't understands, it sends them to all it's elements. So "array lenght" gets the length of the array.

If you want to force this behaviour (for example to send a 'length' message to all elements of the array) you use a '@' i.e. "array @ length". Same thing works for arguments and you can use as many @ as you want.

edit: There'e also a 'fold' mechanism. For example to caluculate the scalar product of two arrays a1 and a2 you can simply write:

a1 * a2 \ #+

#+ is similar to (+) in many fpls.

### ok

Oh, ok. So does this mean the code example in the article is wrong?

strings := {'Hi', 'mom', 'I am playing', 'with', 'F-Script'}

strings length

{2, 3, 12, 4, 8}


### My fault, for arrays it's

My fault, for arrays it's not 'length', it's 'count' (IMO a bit confusing). So the example works as descibed. To get the size of the array, you have to use 'strings count'.

### Ok. That seems kind of

Ok. That seems kind of inconsistent, considering that strings basically are arrays.

### not F-Script's fault

I don't think you can fault F-Script for that, since it gets the method names from Cocoa.

### I suppose

No, I suppose I can't, can I? I certainly can fault Cocoa for it, though ;)

### Adding a length method on the fly

BTW, just for showing some cool stuff: you can use F-Script to add a length method to Cocoa arrays on the fly. Just type:

 NSArray setFSBlock:[:self| self count] asInstanceMethod:#length

Now you can send the length message to arrays, from both F-Script and Objective-C.

### rank of operators in F-Script

OK, I read the paper on F-Script and OOPAL, but I don't see how to perform operations at specific depths as you can in J or APL. It seems there is just top level or recursive.

I'll use my own language's syntax because I don't recall J's offhand. Hopefully it will be self explanatory.

// z is a 3 x 3 x 3 array
z = [[[ 0,  1,  2], [ 3,  4,  5], [ 6,  7,  8]],
[[ 9, 10, 11], [12, 13, 14], [15, 16, 17]],
[[18, 19, 20], [21, 22, 23], [24, 25, 26]]];
y = [100, 200, 300];

z + y;
[[[100, 101, 102], [103, 104, 105], [106, 107, 108]],
[[209, 210, 211], [212, 213, 214], [215, 216, 217]],
[[318, 319, 320], [321, 322, 323], [324, 325, 326]]]

z +.0 y; // add y to planes of z. same as above.
[[[100, 101, 102], [103, 104, 105], [106, 107, 108]],
[[209, 210, 211], [212, 213, 214], [215, 216, 217]],
[[318, 319, 320], [321, 322, 323], [324, 325, 326]]]

z +.1 y;  // add y to rows of z
[[[100, 101, 102], [203, 204, 205], [306, 307, 308]],
[[109, 110, 111], [212, 213, 214], [315, 316, 317]],
[[118, 119, 120], [221, 222, 223], [324, 325, 326]]]

z +.2 y;  // add y to columns of z
[[[100, 201, 302], [103, 204, 305], [106, 207, 308]],
[[109, 210, 311], [112, 213, 314], [115, 216, 317]],
[[118, 219, 320], [121, 222, 323], [124, 225, 326]]]



How do you do these in F-Script?

### Use the '@'

Use the '@':

z + y works like your example, z @ + y like your "z +.1 y" and z @@ + y like "z +.2 y"

### @ operator

.. and how do you go deeper than that? Do you keep adding '@' operators for every level of depth? e.g. "z @@@@ + y" if you want to add the bottom level of a 5 dimensional array?

 OK I've answered my own question: yes. From the docs: "You can string as many @ as you want. The leftmost @ is said to be at level 1, the next @ is said to be at level 2 and so on."

thanks.