Memory layout of a data structure: Difference between revisions

→‎{{header|J}}: define helpers, show an example
No edit summary
(→‎{{header|J}}: define helpers, show an example)
Line 419:
=={{header|J}}==
J does not support "structures", nor "fields in a structure". Instead, J supports arrays. And, of course, J could have labels corresponding to the elements of an array representing the state (voltage, current, logical bit value, whatever) of each pin of a 9-pin RS-232 plug:
<lang j>default=: 0#~#|:'labels comments'=:|:(4 <({.@;:@{._2 ; }.)]0);._2 :0{{)n
CD Carrier detect
RD Received data
TD Transmitted data
Line 429 ⟶ 428:
CTS Clear to send
RI Ring indicator
}}
)</lang>
 
indices=: labels (i. ;: ::]) ]
ndx=: [ {~ [ indices ]
asgn=: {{ y (x indices m)} x }}</lang>
 
Example use:
 
<lang J> example=: default NB. new instance
example ndx 'RI CTS'
0 0
example=: example 'RI RTS TD' asgn 1 2 3
example ndx 'RI CTS'
)1 0</lang>
 
=={{header|Julia}}==
6,951

edits