Anonymous user
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
{
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.
<
STRUCT(REAL r, INT i),
INT,
Line 61 ⟶ 59:
STRUCT(REAL rr),
STRUCT([]REAL r)
);</
To extract the apropriate member of a UNION a '''conformity-clause''' has to be used.
<lang algol68>UNIONX data := 6.6;▼
▲UNIONX data := 6.6;
CASE data IN
(INT i): printf(($"r: "gl$,i)),
Line 72 ⟶ 69:
OUT
printf($"Other cases"l$)
ESAC;
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.
<
INT x,
INT y
);</
====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.
<
STRING name,
REAL age,
Line 95 ⟶ 92:
VOID
) gender details
);</
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>
=={{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===
===Parameterized Algebraic type===
===Synonym type===
Line 186 ⟶ 183:
A struct, it can be inizialized with a Point(x, y) syntax:
It can also be parametrized on the coordinate type:
There are also other ways to initialize them. The D language also supports tuples.
Line 206 ⟶ 203:
=={{header|E}}==
}</lang>▼
=={{header|Forth}}==
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures.
{{works with|GNU Forth|0.6.2}}
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax:
=={{header|Fortran}}==
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax:
<lang fortran>
=={{header|F_Sharp|F#}}==
See the OCaml section as well. Here we create a list of points and print them out.
<
let points = [
Line 266 ⟶ 263:
{x = 5; y = 5} ]
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</
=={{header|Haskell}}==
Line 321 ⟶ 318:
=={{header|IDL}}==
Line 347 ⟶ 344:
10
Y__P
20</lang>
▲</lang>
=={{header|Java}}==
Line 375 ⟶ 371:
=={{header|JSON}}==
<lang
};</lang>▼
=={{header|Logo}}==
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle:
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:
=={{header|MAXScript}}==
Point is a built-in object type in MAX, so...
In practice however, you'd use MAX's built in Point2 type
=={{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
Another option would be a simple class.
Line 508 ⟶ 504:
=={{header|Pop11}}==
=={{header|Python}}==
Line 535 ⟶ 531:
One could also simply instantiate a generic object and "monkeypatch" it:
<lang python>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)
>>>
=={{header|R}}==
R uses the list data type for compound data.
<lang R>mypoint <- list(x=3.4, y=6.7)
▲ # $x
▲ # [1] 3.4
▲ # $y
▲ # [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▼
=={{header|Ruby}}==
Line 676 ⟶ 667:
This shows a structure in its simpest form.
=== 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.
Line 713 ⟶ 704:
===Attributes===
Attributes are often used for simple values. This is how a point might be represented in SVG, for example.
===Children===
More complex, multivariate, and nested data structures can be represented using child nodes.
<fo:block>Circle center = <xsl:value-of select="point/x"/>, <xsl:value-of select="point/y"/></fo:block>
|