fmap=. L:0
-----Original Message----- From: Marc Simpson <m...@0branch.com> Sent: Tuesday, March 27, 2012 6:08 AM To: Programming forum <programming@jsoftware.com> Subject: Re: [Jprogramming] J functors 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 [BEST LLC] Bayesian Efficient Strategic Trading LLC The information in this communication and any attachment is confidential and intended solely for the attention and use of the named addressee(s). Any views or opinions presented are solely those of the author and do not necessarily represent those of BEAM Bayesian Efficient Asset Management, LLC (BEAM), Bayesian Efficient Strategic Trading, LLC (BEST) and/or their affiliates unless otherwise specifically stated. All information and opinions expressed herein are subject to change without notice. This communication is not to be construed as an offer to sell or the solicitation of an offer to buy any security. Any reliance one may place on the accuracy or validity of this information is at their own risk. Past performance is not necessarily indicative of the future results of an investment. If you are not the intended recipient, or a person responsible for delivering this to the intended recipient, you are not authorized to and must not disclose, copy, distribute, or retain this message or any part of it. If you are not the intended recipient, please permanently delete all copies of this communication and any attachments from your computer system, destroy any hard copies, and immediately notify the sender or BEAM/BEST at either i...@2bestsystems.com, i...@beamstrategy.com or (201) 792-1002. No waiver of confidentiality or privilege is made by mistransmission. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm