Compound data type: Difference between revisions

m
Fixed lang tags.
m (→‎{{header|J}}: Add lang tags)
m (Fixed lang tags.)
Line 5:
 
=={{header|ActionScript}}==
<lang actionscript>package
package
{
public class Point
Line 19 ⟶ 18:
}
}
}</lang>
}
</lang>
 
=={{header|Ada}}==
Line 54 ⟶ 52:
===Tagged Type===
ALGOL 68 has only tagged-union/discriminants. And the tagging was strictly done by the ''type'' (MODE) of the members.
<prelang algol68>MODE UNIONX = UNION(
STRUCT(REAL r, INT i),
INT,
Line 61 ⟶ 59:
STRUCT(REAL rr),
STRUCT([]REAL r)
);</prelang>
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
(INT i): printf(($"r: "gl$,i)),
Line 72 ⟶ 69:
OUT
printf($"Other cases"l$)
ESAC; </prelang>
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,
Line 79 ⟶ 76:
ALGOL 68 record types are not extensible through inheritance but they
may be part of a larger STRUCT composition.
<prelang algol68>MODE POINT = STRUCT(
INT x,
INT y
);</prelang>
====Parameterized Types====
An ALGOL 68 record type can contain a tagged-union/discriminant. The
tagged-union/discriminant is used to choose between internal structural
representations.
<prelang algol68>MODE PERSON = STRUCT(
STRING name,
REAL age,
Line 95 ⟶ 92:
VOID
) gender details
);</prelang>
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.
Line 115 ⟶ 112:
=={{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:
<lang awk> p["x"]=10
p["y"]=42</lang>
 
=={{header|BASIC}}==
Line 163 ⟶ 160:
Of course, a constructor can be added in this case as well.
 
=={{header|C sharp|C #}}==
 
<lang csharp>struct Point
Line 176 ⟶ 173:
=={{header|Clean}}==
===Record type===
<lang clean>:: Point = { x :: Int, y :: Int }</lang>
===Parameterized Algebraic type===
<lang clean>:: Point a = Point a a // usage: (Point Int)</lang>
===Synonym type===
<lang clean>:: Point :== (Int, Int)</lang>
 
 
Line 186 ⟶ 183:
A struct, it can be inizialized with a Point(x, y) syntax:
 
<lang d>struct Point { int x, y; }</lang>
 
It can also be parametrized on the coordinate type:
 
<lang d>struct Point(T) { T x, y; }
 
// A point with integer coordinates
auto p1 = Point!(int)(3, 5);
 
// a point with floating point coordinates
auto p1 = Point!(float)(3, 5);</lang>
 
There are also other ways to initialize them. The D language also supports tuples.
Line 206 ⟶ 203:
=={{header|E}}==
 
<lang e>def makePoint(x, y) {
def point {
to getX() { return x }
to getY() { return y }
}
return point
}</lang>
}
 
=={{header|Forth}}==
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures.
 
<lang forth>: pt>x ( point -- x ) ;
: pt>y ( point -- y ) CELL+ ;
: .pt ( point -- ) dup pt>x @ . pt>y @ . ; \ or for this simple structure, 2@ . .
 
create point 6 , 0 ,
7 point pt>y !
.pt \ 6 7</lang>
 
{{works with|GNU Forth|0.6.2}}
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax:
 
<lang forth>struct
cell% field pt>x
cell% field pt>y
end-struct point%</lang>
 
=={{header|Fortran}}==
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax:
<lang fortran> program typedemo
type rational ! Type declaration
integer :: numerator
integer :: denominator
end type rational
type( rational ), parameter :: zero = rational( 0, 1 ) ! Variables initialized
type( rational ), parameter :: one = rational( 1, 1 ) ! by constructor syntax
type( rational ), parameter :: half = rational( 1, 2 )
integer :: n, halfd, halfn
type( rational ) :: &
one_over_n(20) = (/ (rational( 1, n ), n = 1, 20) /) ! Array initialized with
! constructor inside
! implied-do array initializer
integer :: oon_denoms(20)
halfd = half%denominator ! field access with "%" delimiter
halfn = half%numerator
oon_denoms = one_over_n%denominator ! Access denominator field in every
! rational array element & store
end program typedemo ! as integer array</lang>
 
=={{header|F_Sharp|F#}}==
See the OCaml section as well. Here we create a list of points and print them out.
<prelang fsharp>type Point = { x : int; y : int }
 
let points = [
Line 266 ⟶ 263:
{x = 5; y = 5} ]
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</prelang>
=={{header|Haskell}}==
 
Line 321 ⟶ 318:
=={{header|IDL}}==
 
<lang idl>point = {x: 6 , y: 0 }
point.y = 7
print, point
;=> { 6 7}</lang>
 
 
Line 347 ⟶ 344:
10
Y__P
20</lang>
</lang>
 
=={{header|Java}}==
Line 375 ⟶ 371:
=={{header|JSON}}==
 
<lang json>var point = {
x:1,
y:2
};</lang>
};
 
=={{header|Logo}}==
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle:
<lang logo>setpos [100 100] setpos [100 0] setpos [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:
<lang logo>until [(first mousepos) < 0] [ifelse button? [pendown] [penup] setpos mousepos]</lang>
 
=={{header|MAXScript}}==
Point is a built-in object type in MAX, so...
<lang maxscript>struct myPoint (x, y)
newPoint = myPoint x:3 y:4</lang>
In practice however, you'd use MAX's built in Point2 type
<lang maxscript>newPoint = Point2 3 4</lang>
 
=={{header|Modula-3}}==
Line 456 ⟶ 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.
 
<lang openedge>def temp-table point
field x as int
field y as int
.</lang>
 
Another option would be a simple class.
Line 508 ⟶ 504:
=={{header|Pop11}}==
 
<lang pop11>uses objectclass;
define :class Point;
slot x = 0;
slot y = 0;
enddefine;</lang>
 
=={{header|Python}}==
Line 535 ⟶ 531:
One could also simply instantiate a generic object and "monkeypatch" it:
 
<lang python>class MyObject(object): pass
class MyObject(object): pass
point = MyObject()
point.x, point.y = 0, 1
# objects directly instantiated from "object()" cannot be "monkey patched"
# however this can generally be done to it's subclasses</lang>
</lang>
 
=== Named Tuples ===
Line 573 ⟶ 567:
Point(x=100, y=22)
 
>>> </lang>
 
=={{header|R}}==
R uses the list data type for compound data.
<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}}==
Line 676 ⟶ 667:
This shows a structure in its simpest form.
 
<lang vbnet>Structure Simple_Point
Public X, Y As Integer
End Structure</lang>
 
=== Immutable Structures ===
Line 684 ⟶ 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.
 
<lang vbnet>Structure Immutable_Point
Private m_X As Integer
Private m_Y As Integer
 
Public Sub New(ByVal x As Integer, ByVal y As Integer)
m_X = x
m_Y = y
End Sub
 
Public ReadOnly Property X() As Integer
Get
Return m_X
End Get
End Property
 
Public ReadOnly Property Y() As Integer
Get
Return m_Y
End Get
End Property
 
End Structure</lang>
 
 
Line 713 ⟶ 704:
===Attributes===
Attributes are often used for simple values. This is how a point might be represented in SVG, for example.
<lang xml><point x="20" y="30"/>
 
&lt;<!-- 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></lang>
 
===Children===
More complex, multivariate, and nested data structures can be represented using child nodes.
<lang xml><circle>
<point>
<x>20</x>
<y>30</y>
</point>
<radius>10</radius>
</circle>
 
&lt;<!-- 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>
Anonymous user