CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
4
Task/Compound-data-type/0DESCRIPTION
Normal file
4
Task/Compound-data-type/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{{Data structure}}
|
||||
Create a compound data type Point(x,y).
|
||||
|
||||
A compound data type is one that holds multiple independent values. See also [[Enumeration]].
|
||||
2
Task/Compound-data-type/1META.yaml
Normal file
2
Task/Compound-data-type/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic language learning
|
||||
9
Task/Compound-data-type/ACL2/compound-data-type.acl2
Normal file
9
Task/Compound-data-type/ACL2/compound-data-type.acl2
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defstructure point
|
||||
(x (:assert (rationalp x)))
|
||||
(y (:assert (rationalp y))))
|
||||
|
||||
(assign p1 (make-point :x 1 :y 2))
|
||||
(point-x (@ p1)) ; Access the x value of the point
|
||||
(assign p1 (update-point (@ p1) :x 3)) ; Update the x value
|
||||
(point-x (@ p1))
|
||||
(point-p (@ p1)) ; Recognizer for points
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
MODE UNIONX = UNION(
|
||||
STRUCT(REAL r, INT i),
|
||||
INT,
|
||||
REAL,
|
||||
STRUCT(INT ii),
|
||||
STRUCT(REAL rr),
|
||||
STRUCT([]REAL r)
|
||||
);
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
UNIONX data := 6.6;
|
||||
CASE data IN
|
||||
(INT i): printf(($"r: "gl$,i)),
|
||||
(REAL r): printf(($"r: "gl$,r)),
|
||||
(STRUCT(REAL r, INT i) s): printf(($"r&i: "2(g)l$,s)),
|
||||
(STRUCT([]REAL r) s): printf(($"r: "n(UPB r OF s)(g)l$,s))
|
||||
OUT
|
||||
printf($"Other cases"l$)
|
||||
ESAC;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
MODE POINT = STRUCT(
|
||||
INT x,
|
||||
INT y
|
||||
);
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
MODE PERSON = STRUCT(
|
||||
STRING name,
|
||||
REAL age,
|
||||
REAL weight,
|
||||
UNION (
|
||||
STRUCT (REAL beard length),
|
||||
VOID
|
||||
) gender details
|
||||
);
|
||||
2
Task/Compound-data-type/AWK/compound-data-type.awk
Normal file
2
Task/Compound-data-type/AWK/compound-data-type.awk
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
p["x"]=10
|
||||
p["y"]=42
|
||||
14
Task/Compound-data-type/ActionScript/compound-data-type.as
Normal file
14
Task/Compound-data-type/ActionScript/compound-data-type.as
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package
|
||||
{
|
||||
public class Point
|
||||
{
|
||||
public var x:Number;
|
||||
public var y:Number;
|
||||
|
||||
public function Point(x:Number, y:Number)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
4
Task/Compound-data-type/Ada/compound-data-type-1.ada
Normal file
4
Task/Compound-data-type/Ada/compound-data-type-1.ada
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
type Point is tagged record
|
||||
X : Integer := 0;
|
||||
Y : Integer := 0;
|
||||
end record;
|
||||
4
Task/Compound-data-type/Ada/compound-data-type-2.ada
Normal file
4
Task/Compound-data-type/Ada/compound-data-type-2.ada
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
type Point is record
|
||||
X : Integer := 0;
|
||||
Y : Integer := 0;
|
||||
end record;
|
||||
11
Task/Compound-data-type/Ada/compound-data-type-3.ada
Normal file
11
Task/Compound-data-type/Ada/compound-data-type-3.ada
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
type Person (Gender : Gender_Type) is record
|
||||
Name : Name_String;
|
||||
Age : Natural;
|
||||
Weight : Float;
|
||||
Case Gender is
|
||||
when Male =>
|
||||
Beard_Length : Float;
|
||||
when Female =>
|
||||
null;
|
||||
end case;
|
||||
end record;
|
||||
12
Task/Compound-data-type/AmigaE/compound-data-type.amiga
Normal file
12
Task/Compound-data-type/AmigaE/compound-data-type.amiga
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
OBJECT point
|
||||
x, y
|
||||
ENDOBJECT
|
||||
|
||||
PROC main()
|
||||
DEF pt:PTR TO point,
|
||||
|
||||
NEW pt
|
||||
pt.x := 10.4
|
||||
pt.y := 3.14
|
||||
END pt
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
point := Object()
|
||||
point.x := 1
|
||||
point.y := 0
|
||||
4
Task/Compound-data-type/BASIC/compound-data-type.bas
Normal file
4
Task/Compound-data-type/BASIC/compound-data-type.bas
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TYPE Point
|
||||
x AS INTEGER
|
||||
y AS INTEGER
|
||||
END TYPE
|
||||
1
Task/Compound-data-type/BBC-BASIC/compound-data-type.bbc
Normal file
1
Task/Compound-data-type/BBC-BASIC/compound-data-type.bbc
Normal file
|
|
@ -0,0 +1 @@
|
|||
DIM Point{x%, y%}
|
||||
11
Task/Compound-data-type/Bracmat/compound-data-type.bracmat
Normal file
11
Task/Compound-data-type/Bracmat/compound-data-type.bracmat
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
( ( Point
|
||||
= (x=)
|
||||
(y=)
|
||||
(new=.!arg:(?(its.x).?(its.y)))
|
||||
)
|
||||
& new$(Point,(3.4)):?pt
|
||||
& out$(!(pt..x) !(pt..y))
|
||||
{ Show independcy by changing x, but not y }
|
||||
& 7:?(pt..x)
|
||||
& out$(!(pt..x) !(pt..y))
|
||||
);
|
||||
5
Task/Compound-data-type/C++/compound-data-type-1.cpp
Normal file
5
Task/Compound-data-type/C++/compound-data-type-1.cpp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
struct Point
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
6
Task/Compound-data-type/C++/compound-data-type-2.cpp
Normal file
6
Task/Compound-data-type/C++/compound-data-type-2.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
struct Point
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
Point(int ax, int ay): x(ax), y(ax) {}
|
||||
};
|
||||
10
Task/Compound-data-type/C++/compound-data-type-3.cpp
Normal file
10
Task/Compound-data-type/C++/compound-data-type-3.cpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
template<typename Coordinate> struct point
|
||||
{
|
||||
Coordinate x, y;
|
||||
};
|
||||
|
||||
// A point with integer coordinates
|
||||
Point<int> point1 = { 3, 5 };
|
||||
|
||||
// a point with floating point coordinates
|
||||
Point<float> point2 = { 1.7, 3.6 };
|
||||
5
Task/Compound-data-type/C/compound-data-type.c
Normal file
5
Task/Compound-data-type/C/compound-data-type.c
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
typedef struct Point
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
} Point;
|
||||
1
Task/Compound-data-type/Clean/compound-data-type-1.clean
Normal file
1
Task/Compound-data-type/Clean/compound-data-type-1.clean
Normal file
|
|
@ -0,0 +1 @@
|
|||
:: Point = { x :: Int, y :: Int }
|
||||
1
Task/Compound-data-type/Clean/compound-data-type-2.clean
Normal file
1
Task/Compound-data-type/Clean/compound-data-type-2.clean
Normal file
|
|
@ -0,0 +1 @@
|
|||
:: Point a = Point a a // usage: (Point Int)
|
||||
1
Task/Compound-data-type/Clean/compound-data-type-3.clean
Normal file
1
Task/Compound-data-type/Clean/compound-data-type-3.clean
Normal file
|
|
@ -0,0 +1 @@
|
|||
:: Point :== (Int, Int)
|
||||
1
Task/Compound-data-type/Clojure/compound-data-type-1.clj
Normal file
1
Task/Compound-data-type/Clojure/compound-data-type-1.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
(defrecord Point [x y])
|
||||
3
Task/Compound-data-type/Clojure/compound-data-type-2.clj
Normal file
3
Task/Compound-data-type/Clojure/compound-data-type-2.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(def p (Point. 0 1))
|
||||
(assert (= 0 (:x p)))
|
||||
(assert (= 1 (:y p)))
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
# Lightweight JS objects (with CS sugar).
|
||||
point =
|
||||
x: 5
|
||||
y: 3
|
||||
|
||||
console.log point.x, point.y # 5 3
|
||||
|
||||
# Heavier OO style
|
||||
class Point
|
||||
constructor: (@x, @y) ->
|
||||
distance_from: (p2) ->
|
||||
dx = p2.x - @x
|
||||
dy = p2.y - @y
|
||||
Math.sqrt dx*dx + dy*dy
|
||||
|
||||
p1 = new Point(1, 6)
|
||||
p2 = new Point(6, 18)
|
||||
console.log p1 # { x: 1, y: 6 }
|
||||
console.log p1.distance_from # [Function]
|
||||
console.log p1.distance_from p2 # 13
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
CL-USER> (defstruct point (x 0) (y 0)) ;If not provided, x or y default to 0
|
||||
POINT
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
CL-USER> (setf a (make-point)) ;The default constructor using the default values for x and y
|
||||
#S(POINT :X 0 :Y 0)
|
||||
CL-USER> (setf b (make-point :x 5.5 :y #C(0 1))) ;Dynamic datatypes are the default
|
||||
#S(POINT :X 5.5 :Y #C(0 1)) ;y has been set to the imaginary number i (using the Common Lisp complex number data type)
|
||||
CL-USER> (point-x b) ;The default name for the accessor functions is structname-slotname
|
||||
5.5
|
||||
CL-USER> (point-y b)
|
||||
#C(0 1)
|
||||
CL-USER> (setf (point-y b) 3) ;The accessor is setfable
|
||||
3
|
||||
CL-USER> (point-y b)
|
||||
3
|
||||
47
Task/Compound-data-type/D/compound-data-type.d
Normal file
47
Task/Compound-data-type/D/compound-data-type.d
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
void main() {
|
||||
// A normal POD struct
|
||||
// (if it's nested and it's not static then it has a hidden
|
||||
// field that points to the enclosing function):
|
||||
static struct Point {
|
||||
int x, y;
|
||||
}
|
||||
|
||||
auto p1 = Point(10, 20);
|
||||
|
||||
// It can also be parametrized on the coordinate type:
|
||||
static struct Pair(T) {
|
||||
T x, y;
|
||||
}
|
||||
|
||||
// A pair with integer coordinates:
|
||||
auto p2 = Pair!int(3, 5);
|
||||
|
||||
// A pair with floating point coordinates:
|
||||
auto p3 = Pair!double(3, 5);
|
||||
|
||||
// Classes (static inner):
|
||||
static class PointClass {
|
||||
int x, y;
|
||||
this(int x_, int y_) {
|
||||
this.x = x_;
|
||||
this.y = y_;
|
||||
}
|
||||
}
|
||||
|
||||
auto p4 = new PointClass(1, 2);
|
||||
|
||||
// There are also library-defined tuples:
|
||||
import std.typecons;
|
||||
|
||||
alias Tuple!(int,"x", int,"y") PointXY;
|
||||
|
||||
auto p5 = PointXY(3, 5);
|
||||
|
||||
// And even built-in "type tuples":
|
||||
import std.typetuple;
|
||||
|
||||
alias TypeTuple!(int, 5) p6;
|
||||
|
||||
static assert(is(p6[0] == int));
|
||||
static assert(p6[1] == 5);
|
||||
}
|
||||
4
Task/Compound-data-type/Delphi/compound-data-type.delphi
Normal file
4
Task/Compound-data-type/Delphi/compound-data-type.delphi
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TPoint = record
|
||||
X: Longint;
|
||||
Y: Longint;
|
||||
end;
|
||||
7
Task/Compound-data-type/E/compound-data-type.e
Normal file
7
Task/Compound-data-type/E/compound-data-type.e
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def makePoint(x, y) {
|
||||
def point {
|
||||
to getX() { return x }
|
||||
to getY() { return y }
|
||||
}
|
||||
return point
|
||||
}
|
||||
1
Task/Compound-data-type/Ela/compound-data-type-1.ela
Normal file
1
Task/Compound-data-type/Ela/compound-data-type-1.ela
Normal file
|
|
@ -0,0 +1 @@
|
|||
type Maybe = None | Some a
|
||||
4
Task/Compound-data-type/Ela/compound-data-type-2.ela
Normal file
4
Task/Compound-data-type/Ela/compound-data-type-2.ela
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
opentype Several = One | Two | Three
|
||||
|
||||
//Add new constructor to an existing type
|
||||
data Several = Four
|
||||
10
Task/Compound-data-type/Erlang/compound-data-type.erl
Normal file
10
Task/Compound-data-type/Erlang/compound-data-type.erl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
-module(records_test).
|
||||
-compile(export_all).
|
||||
|
||||
-record(point,{x,y}).
|
||||
|
||||
test() ->
|
||||
P1 = #point{x=1.0,y=2.0}, % creates a new point record
|
||||
io:fwrite("X: ~f, Y: ~f~n",[P1#point.x,P1#point.y]),
|
||||
P2 = P1#point{x=3.0}, % creates a new point record with x set to 3.0, y is copied from P1
|
||||
io:fwrite("X: ~f, Y: ~f~n",[P2#point.x,P2#point.y]).
|
||||
7
Task/Compound-data-type/Forth/compound-data-type-1.fth
Normal file
7
Task/Compound-data-type/Forth/compound-data-type-1.fth
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
: 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
|
||||
4
Task/Compound-data-type/Forth/compound-data-type-2.fth
Normal file
4
Task/Compound-data-type/Forth/compound-data-type-2.fth
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
struct
|
||||
cell% field pt>x
|
||||
cell% field pt>y
|
||||
end-struct point%
|
||||
22
Task/Compound-data-type/Fortran/compound-data-type.f
Normal file
22
Task/Compound-data-type/Fortran/compound-data-type.f
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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
|
||||
11
Task/Compound-data-type/Go/compound-data-type.go
Normal file
11
Task/Compound-data-type/Go/compound-data-type.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type point struct {
|
||||
x, y float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(point{3, 4})
|
||||
}
|
||||
1
Task/Compound-data-type/Haskell/compound-data-type.hs
Normal file
1
Task/Compound-data-type/Haskell/compound-data-type.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
p = (2,3)
|
||||
14
Task/Compound-data-type/Java/compound-data-type.java
Normal file
14
Task/Compound-data-type/Java/compound-data-type.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
public class Point
|
||||
{
|
||||
public int x, y;
|
||||
public Point() { this(0); }
|
||||
public Point(int x0) { this(x0,0); }
|
||||
public Point(int x0, int y0) { x = x0; y = y0; }
|
||||
|
||||
public static void main(String args[])
|
||||
{
|
||||
Point point = new Point(1,2);
|
||||
System.out.println("x = " + point.x );
|
||||
System.out.println("y = " + point.y );
|
||||
}
|
||||
}
|
||||
1
Task/Compound-data-type/JavaScript/compound-data-type.js
Normal file
1
Task/Compound-data-type/JavaScript/compound-data-type.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
var point = {x : 1, y : 2};
|
||||
8
Task/Compound-data-type/Lua/compound-data-type-1.lua
Normal file
8
Task/Compound-data-type/Lua/compound-data-type-1.lua
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
a = {x = 1; y = 2}
|
||||
b = {x = 3; y = 4}
|
||||
c = {
|
||||
x = a.x + b.x;
|
||||
y = a.y + b.y
|
||||
}
|
||||
print(a.x, a.y) --> 1 2
|
||||
print(c.x, c.y) --> 4 6
|
||||
9
Task/Compound-data-type/Lua/compound-data-type-2.lua
Normal file
9
Task/Compound-data-type/Lua/compound-data-type-2.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
cPoint = {} -- metatable (behaviour table)
|
||||
function newPoint(x, y) -- constructor
|
||||
local pointPrototype = {} -- prototype declaration
|
||||
function pointPrototype:getX() return x end -- public method
|
||||
function pointPrototype:getY() return y end -- public method
|
||||
function pointPrototype:getXY() return x, y end -- public method
|
||||
function pointPrototype:type() return "point" end -- public method
|
||||
return setmetatable(pointPrototype, cPoint) -- set behaviour and return the pointPrototype
|
||||
end--newPoint
|
||||
9
Task/Compound-data-type/Lua/compound-data-type-3.lua
Normal file
9
Task/Compound-data-type/Lua/compound-data-type-3.lua
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
local oldtype = type; -- store original type function
|
||||
function type(v)
|
||||
local vType = oldtype(v)
|
||||
if (vType=="table" and v.type) then
|
||||
return v:type() -- bypass original type function if possible
|
||||
else
|
||||
return vType
|
||||
end--if vType=="table"
|
||||
end--type
|
||||
14
Task/Compound-data-type/Lua/compound-data-type-4.lua
Normal file
14
Task/Compound-data-type/Lua/compound-data-type-4.lua
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function cPoint.__add(op1, op2) -- add the x and y components
|
||||
if type(op1)=="point" and type(op2)=="point" then
|
||||
return newPoint(
|
||||
op1:getX()+op2:getX(),
|
||||
op1:getY()+op2:getY())
|
||||
end--if type(op1)
|
||||
end--cPoint.__add
|
||||
function cPoint.__sub(op1, op2) -- subtract the x and y components
|
||||
if (type(op1)=="point" and type(op2)=="point") then
|
||||
return newPoint(
|
||||
op1:getX()-op2:getX(),
|
||||
op1:getY()-op2:getY())
|
||||
end--if type(op1)
|
||||
end--cPoint.__sub
|
||||
7
Task/Compound-data-type/Lua/compound-data-type-5.lua
Normal file
7
Task/Compound-data-type/Lua/compound-data-type-5.lua
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
a = newPoint(1, 2)
|
||||
b = newPoint(3, 4)
|
||||
c = a + b -- using __add behaviour
|
||||
print(a:getXY()) --> 1 2
|
||||
print(type(a)) --> point
|
||||
print(c:getXY()) --> 4 6
|
||||
print((a-b):getXY()) --> -2 -2 -- using __sub behaviour
|
||||
10
Task/Compound-data-type/PHP/compound-data-type-1.php
Normal file
10
Task/Compound-data-type/PHP/compound-data-type-1.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Using pack/unpack
|
||||
$point = pack("ii", 1, 2);
|
||||
|
||||
$u = unpack("ix/iy", $point);
|
||||
echo $x;
|
||||
echo $y;
|
||||
|
||||
list($x,$y) = unpack("ii", $point);
|
||||
echo $x;
|
||||
echo $y;
|
||||
8
Task/Compound-data-type/PHP/compound-data-type-2.php
Normal file
8
Task/Compound-data-type/PHP/compound-data-type-2.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Using array
|
||||
$point = array('x' => 1, 'y' => 2);
|
||||
|
||||
list($x, $y) = $point;
|
||||
echo $x, ' ', $y, "\n";
|
||||
|
||||
# or simply:
|
||||
echo $point['x'], ' ', $point['y'], "\n";
|
||||
7
Task/Compound-data-type/PHP/compound-data-type-3.php
Normal file
7
Task/Compound-data-type/PHP/compound-data-type-3.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Using class
|
||||
class Point {
|
||||
function __construct($x, $y) { $this->x = $x; $this->y = $y; }
|
||||
function __tostring() { return $this->x . ' ' . $this->y . "\n"; }
|
||||
}
|
||||
$point = new Point(1, 2);
|
||||
echo $point; # will call __tostring() in later releases of PHP 5.2; before that, it won't work so good.
|
||||
1
Task/Compound-data-type/Perl/compound-data-type-1.pl
Normal file
1
Task/Compound-data-type/Perl/compound-data-type-1.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
my @point = (3, 8);
|
||||
4
Task/Compound-data-type/Perl/compound-data-type-2.pl
Normal file
4
Task/Compound-data-type/Perl/compound-data-type-2.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
my %point = (
|
||||
x => 3,
|
||||
y => 8
|
||||
);
|
||||
9
Task/Compound-data-type/Perl/compound-data-type-3.pl
Normal file
9
Task/Compound-data-type/Perl/compound-data-type-3.pl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package Point;
|
||||
|
||||
use strict;
|
||||
use base 'Class::Struct'
|
||||
x => '$',
|
||||
y => '$',
|
||||
;
|
||||
|
||||
my $point = Point->new(x => 3, y => 8);
|
||||
9
Task/Compound-data-type/PicoLisp/compound-data-type.l
Normal file
9
Task/Compound-data-type/PicoLisp/compound-data-type.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(class +Point)
|
||||
|
||||
(dm T (X Y)
|
||||
(=: x X)
|
||||
(=: y Y) )
|
||||
|
||||
(setq P (new '(+Point) 3 4))
|
||||
|
||||
(show P)
|
||||
5
Task/Compound-data-type/Python/compound-data-type-1.py
Normal file
5
Task/Compound-data-type/Python/compound-data-type-1.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
X, Y = 0, 1
|
||||
p = (3, 4)
|
||||
p = [3, 4]
|
||||
|
||||
print p[X]
|
||||
7
Task/Compound-data-type/Python/compound-data-type-2.py
Normal file
7
Task/Compound-data-type/Python/compound-data-type-2.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class Point:
|
||||
def __init__(self, x=0, y=0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
p = Point()
|
||||
print p.x
|
||||
5
Task/Compound-data-type/Python/compound-data-type-3.py
Normal file
5
Task/Compound-data-type/Python/compound-data-type-3.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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
|
||||
27
Task/Compound-data-type/Python/compound-data-type-4.py
Normal file
27
Task/Compound-data-type/Python/compound-data-type-4.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
>>> from collections import namedtuple
|
||||
>>> help(namedtuple)
|
||||
Help on function namedtuple in module collections:
|
||||
|
||||
namedtuple(typename, field_names, verbose=False)
|
||||
Returns a new subclass of tuple with named fields.
|
||||
|
||||
>>> Point = namedtuple('Point', 'x y')
|
||||
>>> Point.__doc__ # docstring for the new class
|
||||
'Point(x, y)'
|
||||
>>> p = Point(11, y=22) # instantiate with positional args or keywords
|
||||
>>> p[0] + p[1] # indexable like a plain tuple
|
||||
33
|
||||
>>> x, y = p # unpack like a regular tuple
|
||||
>>> x, y
|
||||
(11, 22)
|
||||
>>> p.x + p.y # fields also accessable by name
|
||||
33
|
||||
>>> d = p._asdict() # convert to a dictionary
|
||||
>>> d['x']
|
||||
11
|
||||
>>> Point(**d) # convert from a dictionary
|
||||
Point(x=11, y=22)
|
||||
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
|
||||
Point(x=100, y=22)
|
||||
|
||||
>>>
|
||||
20
Task/Compound-data-type/R/compound-data-type.r
Normal file
20
Task/Compound-data-type/R/compound-data-type.r
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
4
Task/Compound-data-type/REXX/compound-data-type-1.rexx
Normal file
4
Task/Compound-data-type/REXX/compound-data-type-1.rexx
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
x= -4.9
|
||||
y= 1.7
|
||||
|
||||
point=x y
|
||||
8
Task/Compound-data-type/REXX/compound-data-type-2.rexx
Normal file
8
Task/Compound-data-type/REXX/compound-data-type-2.rexx
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
x= -4.1
|
||||
y= 1/4e21
|
||||
|
||||
point=x y
|
||||
|
||||
bpoint=point
|
||||
|
||||
gpoint=5.6 7.3e-12
|
||||
2
Task/Compound-data-type/Racket/compound-data-type-1.rkt
Normal file
2
Task/Compound-data-type/Racket/compound-data-type-1.rkt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#lang racket
|
||||
(struct point (x y))
|
||||
5
Task/Compound-data-type/Racket/compound-data-type-2.rkt
Normal file
5
Task/Compound-data-type/Racket/compound-data-type-2.rkt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#lang racket
|
||||
(define point% ; classes are suffixed with % by convention
|
||||
(class object%
|
||||
(super-new)
|
||||
(init-field x y)))
|
||||
5
Task/Compound-data-type/Ruby/compound-data-type.rb
Normal file
5
Task/Compound-data-type/Ruby/compound-data-type.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Point = Struct.new(:x,:y)
|
||||
p = Point.new(6,7)
|
||||
p.y=3
|
||||
puts p
|
||||
=> #<struct Point x=6, y=3>
|
||||
4
Task/Compound-data-type/Scala/compound-data-type.scala
Normal file
4
Task/Compound-data-type/Scala/compound-data-type.scala
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
case class Point(x:Int=0, y:Int=0)
|
||||
|
||||
val p=Point(1,2)
|
||||
println(p.y) //=> 2
|
||||
5
Task/Compound-data-type/Scheme/compound-data-type.ss
Normal file
5
Task/Compound-data-type/Scheme/compound-data-type.ss
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(define-record-type point
|
||||
(make-point x y)
|
||||
point?
|
||||
(x point-x)
|
||||
(y point-y))
|
||||
4
Task/Compound-data-type/Tcl/compound-data-type-1.tcl
Normal file
4
Task/Compound-data-type/Tcl/compound-data-type-1.tcl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
array set point {x 4 y 5}
|
||||
set point(y) 7
|
||||
puts "Point is {$point(x),$point(y)}"
|
||||
# => Point is {4,7}
|
||||
3
Task/Compound-data-type/Tcl/compound-data-type-2.tcl
Normal file
3
Task/Compound-data-type/Tcl/compound-data-type-2.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
set point [dict create x 4 y 5]
|
||||
dict set point y 7
|
||||
puts "Point is {[dict get $point x],[dict get $point y]}"
|
||||
10
Task/Compound-data-type/Tcl/compound-data-type-3.tcl
Normal file
10
Task/Compound-data-type/Tcl/compound-data-type-3.tcl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
oo::class create Point {
|
||||
variable x y
|
||||
constructor {X Y} {set x $X;set y $Y}
|
||||
method x {args} {set x {*}$args}
|
||||
method y {args} {set y {*}$args}
|
||||
method show {} {return "{$x,$y}"}
|
||||
}
|
||||
Point create point 4 5
|
||||
point y 7
|
||||
puts "Point is [point show]"
|
||||
Loading…
Add table
Add a link
Reference in a new issue