Memory layout of a data structure: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|J}}: define helpers, show an example)
Line 419: Line 419:
=={{header|J}}==
=={{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:
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>labels=: <;._2]0 :0
<lang j>default=: 0#~#|:'labels comments'=:|:(4 ({.@;:@{. ; }.)]);._2 {{)n
CD Carrier detect
RD Received data
RD Received data
TD Transmitted data
TD Transmitted data
Line 429: Line 428:
CTS Clear to send
CTS Clear to send
RI Ring indicator
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}}==
=={{header|Julia}}==