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

Reply via email to