Talk:Function composition: Difference between revisions

functional composition in J
mNo edit summary
(functional composition in J)
Line 19:
 
Spoon, Shintakezou; I think the C solution should stay, as it does show the kind of hoops you would have to go through to implement this task in C. It also helps to explain the second paragraph in [[wp:First-class_function#Availability|First-class function: Availability]], on why they don't normally include C in the list of FP languages and mention the limitations of function pointers. --[[User:Paddy3118|Paddy3118]] 23:53, 4 March 2009 (UTC)
 
==J==
Function composition is fundamental in J. J has a rich set of composition primitives and syntax to combine functions in multiple, interesting ways.
 
In the following examples, <tt>'''f'''</tt> and <tt>'''g'''</tt> are functions, <tt>''x''</tt> and <tt>''y''</tt> are variables (data). The syntax <tt>'''f''' ''y''</tt> means the function <tt>'''f'''</tt> applied to one argument, <tt>''y''</tt> (unary), whereas <tt>''x'' '''f''' ''y''</tt> means the function <tt>'''f'''</tt> applied to (between) two arguments, <tt>''x''</tt> and <tt>''y''</tt> (binary).
 
Here's a selection of J's composition options:
 
{| class="wikitable"
|-
! Composition: <tt>∘</tt>
! Unary interpretation: <tt>'''f'''∘'''g''' y</tt>
! Binary interpretation: <tt>x '''f'''∘'''g''' y</tt>
! Notes
|-
|<tt>@</tt>
|<tt>'''f'''('''g''' ''y'')</tt>
|<tt>'''f'''(''x'' '''g''' ''y'')</tt>
|<tt>'''f'''</tt> applied to each output of <tt>'''g'''</tt> independently
|-
|<tt>@:</tt>
|<tt>'''f'''('''g''' ''y'')</tt>
|<tt>'''f'''(''x'' '''g''' ''y'')</tt>
|<tt>'''f'''</tt> applied to all outputs of <tt>'''g'''</tt> simultaneously
|-
|<tt>&</tt>
|<tt>'''f'''('''g''' ''y'')</tt>
|<tt>('''g''' ''x'')'''f'''('''g''' ''y'')</tt>
|<tt>'''f'''</tt> applied between each output of <tt>'''g'''</tt> on <tt>''x''</tt> and <tt>''y''</tt> pairwise
|-
|<tt>&:</tt>
|<tt>'''f'''('''g''' ''y'')</tt>
|<tt>('''g''' ''x'')'''f'''('''g''' ''y'')</tt>
|<tt>'''f'''</tt> applied between all outputs of <tt>'''g'''</tt> on <tt>''x''</tt> and <tt>''y''</tt> ''in toto''
|-
|<tt>..</tt>
|<tt>(('''f''' y)+ ('''g''' ''x'')'''f'''('''g''' ''y''))/2</tt>
|<tt>N/A</tt>
|Given <tt>'''h'''←'''f'''..'''g'''</tt>, the resulting function, <tt>'''h'''</tt>, is ''even'' in the sense that <tt>('''h''' ''y'') = ('''h''' -''y'')</tt> for any <tt>''y''</tt> ; its graph is reflected in the vertical axis.
|-
|<tt>.:</tt>
|<tt>(('''f''' y) -('''g''' ''x'')'''f'''('''g''' ''y''))/2</tt>
|<tt>N/A</tt>
|Given <tt>'''h'''←'''f'''.:'''g'''</tt>, the resulting function, <tt>'''h'''</tt>, is ''odd'' in the sense that <tt>('''h''' ''y'') = (-'''h''' -''y'')</tt> for any <tt>''y''</tt> ; its graph is reflected in the origin.
|-
|<tt>:</tt>
|<tt>'''f''' ''y''</tt>
|<tt>''x'' '''g''' ''y''</tt>
| Allows the unary and binary definitions of a function to be specified independently.
|-
|<tt>::</tt>
|<tt>try { '''f''' ''y'' } catch { '''g''' ''y'' } </tt>
|<tt>try { ''x'' '''f''' ''y'' } catch { ''x'' '''g''' ''y'' } </tt>
| Given <tt>'''h'''←'''f'''::'''g'''</tt>, if <tt>'''f'''</tt> returns a valid value without error, then the result of <tt>'''h'''</tt> is the result of <tt>'''f'''</tt>; else, the result of <tt>'''h'''</tt> is the result of <tt>'''g'''</tt>. These succinct, functional exception handlers can be chained.
|-
|<tt>::</tt>
|<tt>try { '''f''' ''y'' } catch { '''g''' ''y'' } </tt>
|<tt>try { ''x'' '''f''' ''y'' } catch { ''x'' '''g''' ''y'' } </tt>
| Given <tt>'''h'''←'''f'''::'''g'''</tt>, if <tt>'''f'''</tt> returns a valid value without error, then the result of <tt>'''h'''</tt> is the result of <tt>'''f'''</tt>; else, the result of <tt>'''h'''</tt> is the result of <tt>'''g'''</tt>. These succinct, functional exception handlers can be chained.
|-
|}
 
 
Discuss
@. (<tt>'''f'''`'''g'''@.'''h'''</tt>)
&.
&.:
. (<tt>'''f'''.'''g'''</tt> is defined in terms of a recursive expansion by minors along the first column when unary, and as a generalized inner product when binary
::)
:. (obverse)
fork
hook
Anonymous user