Compound data type: Difference between revisions

Content added Content deleted
m (→‎{{header|J}}: Add lang tags)
m (Fixed lang tags.)
Line 5: Line 5:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<lang actionscript>
<lang actionscript>package
package
{
{
public class Point
public class Point
Line 19: Line 18:
}
}
}
}
}</lang>
}
</lang>


=={{header|Ada}}==
=={{header|Ada}}==
Line 54: Line 52:
===Tagged Type===
===Tagged Type===
ALGOL 68 has only tagged-union/discriminants. And the tagging was strictly done by the ''type'' (MODE) of the members.
ALGOL 68 has only tagged-union/discriminants. And the tagging was strictly done by the ''type'' (MODE) of the members.
<pre>MODE UNIONX = UNION(
<lang algol68>MODE UNIONX = UNION(
STRUCT(REAL r, INT i),
STRUCT(REAL r, INT i),
INT,
INT,
Line 61: Line 59:
STRUCT(REAL rr),
STRUCT(REAL rr),
STRUCT([]REAL r)
STRUCT([]REAL r)
);</pre>
);</lang>
To extract the apropriate member of a UNION a '''conformity-clause''' has to be used.
To extract the apropriate member of a UNION a '''conformity-clause''' has to be used.
<lang algol68>UNIONX data := 6.6;
<pre>
UNIONX data := 6.6;
CASE data IN
CASE data IN
(INT i): printf(($"r: "gl$,i)),
(INT i): printf(($"r: "gl$,i)),
Line 72: Line 69:
OUT
OUT
printf($"Other cases"l$)
printf($"Other cases"l$)
ESAC; </pre>
ESAC;</lang>
The '''conformity-clause''' does mean that ALGOL 68 avoids the need for
The '''conformity-clause''' does mean that ALGOL 68 avoids the need for
[[duck typing]], but it also makes the tagged-union kinda tough to use,
[[duck typing]], but it also makes the tagged-union kinda tough to use,
Line 79: Line 76:
ALGOL 68 record types are not extensible through inheritance but they
ALGOL 68 record types are not extensible through inheritance but they
may be part of a larger STRUCT composition.
may be part of a larger STRUCT composition.
<pre>MODE POINT = STRUCT(
<lang algol68>MODE POINT = STRUCT(
INT x,
INT x,
INT y
INT y
);</pre>
);</lang>
====Parameterized Types====
====Parameterized Types====
An ALGOL 68 record type can contain a tagged-union/discriminant. The
An ALGOL 68 record type can contain a tagged-union/discriminant. The
tagged-union/discriminant is used to choose between internal structural
tagged-union/discriminant is used to choose between internal structural
representations.
representations.
<pre>MODE PERSON = STRUCT(
<lang algol68>MODE PERSON = STRUCT(
STRING name,
STRING name,
REAL age,
REAL age,
Line 95: Line 92:
VOID
VOID
) gender details
) gender details
);</pre>
);</lang>
In this case every PERSON will have the attributes of gender details, name, age,
In this case every PERSON will have the attributes of gender details, name, age,
and weight. A PERSON may or may not have a beard. The sex is implied by the tagging.
and weight. A PERSON may or may not have a beard. The sex is implied by the tagging.
Line 115: Line 112:
=={{header|AWK}}==
=={{header|AWK}}==
As usual, arrays are the only data type more complex than a number or a string. Having to use quotes around constant strings as element selectors:
As usual, arrays are the only data type more complex than a number or a string. Having to use quotes around constant strings as element selectors:
<lang awk> p["x"]=10
<lang awk>p["x"]=10
p["y"]=42</lang>
p["y"]=42</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 163: Line 160:
Of course, a constructor can be added in this case as well.
Of course, a constructor can be added in this case as well.


=={{header|C sharp|C #}}==
=={{header|C sharp|C#}}==


<lang csharp>struct Point
<lang csharp>struct Point
Line 176: Line 173:
=={{header|Clean}}==
=={{header|Clean}}==
===Record type===
===Record type===
:: Point = { x :: Int, y :: Int }
<lang clean>:: Point = { x :: Int, y :: Int }</lang>
===Parameterized Algebraic type===
===Parameterized Algebraic type===
:: Point a = Point a a // usage: (Point Int)
<lang clean>:: Point a = Point a a // usage: (Point Int)</lang>
===Synonym type===
===Synonym type===
:: Point :== (Int, Int)
<lang clean>:: Point :== (Int, Int)</lang>




Line 186: Line 183:
A struct, it can be inizialized with a Point(x, y) syntax:
A struct, it can be inizialized with a Point(x, y) syntax:


struct Point { int x, y; }
<lang d>struct Point { int x, y; }</lang>


It can also be parametrized on the coordinate type:
It can also be parametrized on the coordinate type:


struct Point(T) { T x, y; }
<lang d>struct Point(T) { T x, y; }

// A point with integer coordinates
// A point with integer coordinates
auto p1 = Point!(int)(3, 5);
auto p1 = Point!(int)(3, 5);

// a point with floating point coordinates
// a point with floating point coordinates
auto p1 = Point!(float)(3, 5);
auto p1 = Point!(float)(3, 5);</lang>


There are also other ways to initialize them. The D language also supports tuples.
There are also other ways to initialize them. The D language also supports tuples.
Line 206: Line 203:
=={{header|E}}==
=={{header|E}}==


def makePoint(x, y) {
<lang e>def makePoint(x, y) {
def point {
def point {
to getX() { return x }
to getX() { return x }
to getY() { return y }
to getY() { return y }
}
}
return point
return point
}</lang>
}


=={{header|Forth}}==
=={{header|Forth}}==
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures.
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures.


: pt>x ( point -- x ) ;
<lang forth>: pt>x ( point -- x ) ;
: pt>y ( point -- y ) CELL+ ;
: pt>y ( point -- y ) CELL+ ;
: .pt ( point -- ) dup pt>x @ . pt>y @ . ; \ or for this simple structure, 2@ . .
: .pt ( point -- ) dup pt>x @ . pt>y @ . ; \ or for this simple structure, 2@ . .

create point 6 , 0 ,
create point 6 , 0 ,
7 point pt>y !
7 point pt>y !
.pt \ 6 7
.pt \ 6 7</lang>


{{works with|GNU Forth|0.6.2}}
{{works with|GNU Forth|0.6.2}}
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax:
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax:


struct
<lang forth>struct
cell% field pt>x
cell% field pt>x
cell% field pt>y
cell% field pt>y
end-struct point%
end-struct point%</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax:
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax:
<lang fortran> program typedemo
<lang fortran>program typedemo
type rational ! Type declaration
type rational ! Type declaration
integer :: numerator
integer :: numerator
integer :: denominator
integer :: denominator
end type rational
end type rational
type( rational ), parameter :: zero = rational( 0, 1 ) ! Variables initialized
type( rational ), parameter :: zero = rational( 0, 1 ) ! Variables initialized
type( rational ), parameter :: one = rational( 1, 1 ) ! by constructor syntax
type( rational ), parameter :: one = rational( 1, 1 ) ! by constructor syntax
type( rational ), parameter :: half = rational( 1, 2 )
type( rational ), parameter :: half = rational( 1, 2 )
integer :: n, halfd, halfn
integer :: n, halfd, halfn
type( rational ) :: &
type( rational ) :: &
one_over_n(20) = (/ (rational( 1, n ), n = 1, 20) /) ! Array initialized with
one_over_n(20) = (/ (rational( 1, n ), n = 1, 20) /) ! Array initialized with
! constructor inside
! constructor inside
! implied-do array initializer
! implied-do array initializer
integer :: oon_denoms(20)
integer :: oon_denoms(20)
halfd = half%denominator ! field access with "%" delimiter
halfd = half%denominator ! field access with "%" delimiter
halfn = half%numerator
halfn = half%numerator
oon_denoms = one_over_n%denominator ! Access denominator field in every
oon_denoms = one_over_n%denominator ! Access denominator field in every
! rational array element & store
! rational array element & store
end program typedemo ! as integer array</lang>
end program typedemo ! as integer array</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
See the OCaml section as well. Here we create a list of points and print them out.
See the OCaml section as well. Here we create a list of points and print them out.
<pre>type Point = { x : int; y : int }
<lang fsharp>type Point = { x : int; y : int }


let points = [
let points = [
Line 266: Line 263:
{x = 5; y = 5} ]
{x = 5; y = 5} ]
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</pre>
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</lang>
=={{header|Haskell}}==
=={{header|Haskell}}==


Line 321: Line 318:
=={{header|IDL}}==
=={{header|IDL}}==


point = {x: 6 , y: 0 }
<lang idl>point = {x: 6 , y: 0 }
point.y = 7
point.y = 7
print, point
print, point
;=> { 6 7}
;=> { 6 7}</lang>




Line 347: Line 344:
10
10
Y__P
Y__P
20
20</lang>
</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 375: Line 371:
=={{header|JSON}}==
=={{header|JSON}}==


var point = {
<lang json>var point = {
x:1,
x:1,
y:2
y:2
};</lang>
};


=={{header|Logo}}==
=={{header|Logo}}==
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle:
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle:
setpos [100 100] setpos [100 0] setpos [0 0]
<lang logo>setpos [100 100] setpos [100 0] setpos [0 0]
show pos ; [0 0]
show pos ; [0 0]</lang>
Access is via normal list operations like FIRST and BUTFIRST (BF). X is FIRST point, Y is LAST point. For example, a simple drawing program which exits if mouse X is negative:
Access is via normal list operations like FIRST and BUTFIRST (BF). X is FIRST point, Y is LAST point. For example, a simple drawing program which exits if mouse X is negative:
until [(first mousepos) < 0] [ifelse button? [pendown] [penup] setpos mousepos]
<lang logo>until [(first mousepos) < 0] [ifelse button? [pendown] [penup] setpos mousepos]</lang>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Point is a built-in object type in MAX, so...
Point is a built-in object type in MAX, so...
struct myPoint (x, y)
<lang maxscript>struct myPoint (x, y)
newPoint = myPoint x:3 y:4
newPoint = myPoint x:3 y:4</lang>
In practice however, you'd use MAX's built in Point2 type
In practice however, you'd use MAX's built in Point2 type
newPoint = Point2 3 4
<lang maxscript>newPoint = Point2 3 4</lang>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 456: Line 452:
The temp-table is a in memory database table. So you can query sort and iterate it, but is the data structure that comes closest.
The temp-table is a in memory database table. So you can query sort and iterate it, but is the data structure that comes closest.


def temp-table point
<lang openedge>def temp-table point
field x as int
field x as int
field y as int
field y as int
.
.</lang>


Another option would be a simple class.
Another option would be a simple class.
Line 508: Line 504:
=={{header|Pop11}}==
=={{header|Pop11}}==


uses objectclass;
<lang pop11>uses objectclass;
define :class Point;
define :class Point;
slot x = 0;
slot x = 0;
slot y = 0;
slot y = 0;
enddefine;
enddefine;</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 535: Line 531:
One could also simply instantiate a generic object and "monkeypatch" it:
One could also simply instantiate a generic object and "monkeypatch" it:


<lang python>
<lang python>class MyObject(object): pass
class MyObject(object): pass
point = MyObject()
point = MyObject()
point.x, point.y = 0, 1
point.x, point.y = 0, 1
# objects directly instantiated from "object()" cannot be "monkey patched"
# objects directly instantiated from "object()" cannot be "monkey patched"
# however this can generally be done to it's subclasses
# however this can generally be done to it's subclasses</lang>
</lang>


=== Named Tuples ===
=== Named Tuples ===
Line 573: Line 567:
Point(x=100, y=22)
Point(x=100, y=22)


>>> </lang>
>>></lang>


=={{header|R}}==
=={{header|R}}==
R uses the list data type for compound data.
R uses the list data type for compound data.
<lang R>
<lang R>mypoint <- list(x=3.4, y=6.7)
# $x
mypoint <- list(x=3.4, y=6.7)
# [1] 3.4
# $x
# $y
# [1] 3.4
# [1] 6.7
# $y
mypoint$x # 3.4
# [1] 6.7
mypoint$x # 3.4
list(a=1:10, b="abc", c=runif(10), d=list(e=1L, f=TRUE))
# $a
# [1] 1 2 3 4 5 6 7 8 9 10
# $b
# [1] "abc"
# $c
# [1] 0.64862897 0.73669435 0.11138945 0.10408015 0.46843836 0.32351247
# [7] 0.20528914 0.78512472 0.06139691 0.76937113
# $d
# $d$e
# [1] 1
# $d$f
# [1] TRUE


list(a=1:10, b="abc", c=runif(10), d=list(e=1L, f=TRUE))
</lang>
# $a
# [1] 1 2 3 4 5 6 7 8 9 10
# $b
# [1] "abc"
# $c
# [1] 0.64862897 0.73669435 0.11138945 0.10408015 0.46843836 0.32351247
# [7] 0.20528914 0.78512472 0.06139691 0.76937113
# $d
# $d$e
# [1] 1
# $d$f
# [1] TRUE</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 676: Line 667:
This shows a structure in its simpest form.
This shows a structure in its simpest form.


Structure Simple_Point
<lang vbnet>Structure Simple_Point
Public X, Y As Integer
Public X, Y As Integer
End Structure
End Structure</lang>


=== Immutable Structures ===
=== Immutable Structures ===
Line 684: Line 675:
In Visual Basic, mutable strucutures are difficult to use properly and should only be used when performance measurements warrant it. The rest of the time, immutable structures should be used. Below is the same structure seen before, but in an immutable form.
In Visual Basic, mutable strucutures are difficult to use properly and should only be used when performance measurements warrant it. The rest of the time, immutable structures should be used. Below is the same structure seen before, but in an immutable form.


Structure Immutable_Point
<lang vbnet>Structure Immutable_Point
Private m_X As Integer
Private m_X As Integer
Private m_Y As Integer
Private m_Y As Integer

Public Sub New(ByVal x As Integer, ByVal y As Integer)
Public Sub New(ByVal x As Integer, ByVal y As Integer)
m_X = x
m_X = x
m_Y = y
m_Y = y
End Sub
End Sub

Public ReadOnly Property X() As Integer
Public ReadOnly Property X() As Integer
Get
Get
Return m_X
Return m_X
End Get
End Get
End Property
End Property

Public ReadOnly Property Y() As Integer
Public ReadOnly Property Y() As Integer
Get
Get
Return m_Y
Return m_Y
End Get
End Get
End Property
End Property

End Structure
End Structure</lang>




Line 713: Line 704:
===Attributes===
===Attributes===
Attributes are often used for simple values. This is how a point might be represented in SVG, for example.
Attributes are often used for simple values. This is how a point might be represented in SVG, for example.
<point x="20" y="30"/>
<lang xml><point x="20" y="30"/>


&lt;!-- context is a point node. The '@' prefix selects named attributes of the current node. -->
<!-- context is a point node. The '@' prefix selects named attributes of the current node. -->
<fo:block>Point = <xsl:value-of select="@x"/>, <xsl:value-of select="@y"/></fo:block>
<fo:block>Point = <xsl:value-of select="@x"/>, <xsl:value-of select="@y"/></fo:block></lang>


===Children===
===Children===
More complex, multivariate, and nested data structures can be represented using child nodes.
More complex, multivariate, and nested data structures can be represented using child nodes.
<circle>
<lang xml><circle>
<point>
<point>
<x>20</x>
<x>20</x>
<y>30</y>
<y>30</y>
</point>
</point>
<radius>10</radius>
<radius>10</radius>
</circle>
</circle>


&lt;!-- context is a circle node. Children are accessed using a path-like notation (hence the name "XPath"). -->
<!-- context is a circle node. Children are accessed using a path-like notation (hence the name "XPath"). --></lang>
<fo:block>Circle center = <xsl:value-of select="point/x"/>, <xsl:value-of select="point/y"/></fo:block>
<fo:block>Circle center = <xsl:value-of select="point/x"/>, <xsl:value-of select="point/y"/></fo:block>