Nice example. I also like the phrase "the idea of a functor" as
I'm not sure that we can claim these J cases are formally
identical in the absence of a static type system.

With that said, we can simplify things a bit to make Aai's
approach even clearer:

Consider the task of squaring "some" integers. First of all, the
obvious case where the integers are a simple list/vector --
implicit map does the job nicely,

       f =: *:
       f 1 2 3
    1 4 9

but obviously this won't work with boxed values unless we use
'each',

       f 1 2 3
    1 4 9
       f 1;2;3
    |domain error: f
    |       f 1;2;3
       f each 1;2;3
    +-+-+-+
    |1|4|9|
    +-+-+-+

Context preservation (as Aai pointed out) can be obtained through
use of "level at":

       f L: 0 (1 2 3)
    1 4 9
       f L: 0 (1; 2; 3)
    +-+-+-+
    |1|4|9|
    +-+-+-+
       f L: 0 ((<1); (<2); <<3)
    +---+---+---+
    |+-+|+-+|+-+|
    ||1|||4|||9||
    |+-+|+-+|+-+|
    +---+---+---+

Finally, let's rename "F" to "fmap":

       fmap =: 1 : 'u L: 0 y'
       f fmap 1 2 3
    1 4 9
       f fmap 1; 2; 3
    +-+-+-+
    |1|4|9|
    +-+-+-+
       f fmap ((<1); (<2); <<3)
    +---+---+---+
    |+-+|+-+|+-+|
    ||1|||4|||9||
    |+-+|+-+|+-+|
    +---+---+---+

Best,
M

On Tue, Mar 27, 2012 at 10:11 AM, Aai <agroeneveld...@gmail.com> wrote:
> Another nice example of the idea of functor in Haskell is illustrated
> with a binary tree (type) with e.g. string leaves:
>
>    ]tree=. 'appel';<'peer';'pruim'
> ┌─────┬────────────┐
> │appel│┌────┬─────┐│
> │     ││peer│pruim││
> │     │└────┴─────┘│
> └─────┴────────────┘
>
> In Haskeel you have to define a functor (mapping a function) over trees.
> Doing the same in J:
>
> F=: 1 :'u L:0 y'
>
> We want to know the length of the content of each leaf:
>
>     # F tree
> ┌─┬─────┐
> │5│┌─┬─┐│
> │ ││4│5││
> │ │└─┴─┘│
> └─┴─────┘
>
> As you can see the context (tree type) is preserved.
>
>
>
> On 26-03-12 19:20, Raul Miller wrote:
>> http://blog.0branch.com/implementing-functor-in-ocaml
>>
>> "A Functor is (somewhat informally): a structure that provides a
>> mapping operation that applies to a value in a given context,
>> preserving that context."
>>
>> In other words, this is probably an example of a functor:
>>
>>     A=: 2 3 5 7 11 13
>>     F=: *:
>>
>>     (0 0 1 0 0 0 * F A) + A * 1 1 0 1 1 1
>> 2 3 25 7 11 13
>>
>> We could also use merge, and specify the target to be updated using indices:
>>
>>     2 F@{`[`]} A
>> 2 3 25 7 11 13
>>
>> (A meaningful J expression with both curly braces and square brackets
>> being "balanced"... this seems almost misleading.)
>>
>> Also, hypothetically, if we have names for our data elements, we can
>> specify the elements to be updated by name.  Names might be nouns
>> (roughly equivalent to selection vectors or indices) or verbs pairs
>> (one to extract the value and a related name to put the result back in
>> its place -- bonus points if these are obverses of each other) or
>> adverbs (modifying a verb so that it gets applied to the right data
>> and leaves the rest alone).  Adverbs are probably closest to
>> "functors" in the sense of the reference I posted at the top, though
>> they are not always the most useful way of encapsulating this
>> information.
>>
>> Anyways, this struck me as related to an ongoing pattern I have seen
>> discussed in these forums, so I thought I'd post about it and invite
>> you all to post your favorite related concepts.
>>
>> Thanks,
>>
>
> --
> Met vriendelijke groet,
> @@i = Arie Groeneveld
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to