This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View 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]].

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View 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

View file

@ -0,0 +1,8 @@
MODE UNIONX = UNION(
STRUCT(REAL r, INT i),
INT,
REAL,
STRUCT(INT ii),
STRUCT(REAL rr),
STRUCT([]REAL r)
);

View file

@ -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;

View file

@ -0,0 +1,4 @@
MODE POINT = STRUCT(
INT x,
INT y
);

View file

@ -0,0 +1,9 @@
MODE PERSON = STRUCT(
STRING name,
REAL age,
REAL weight,
UNION (
STRUCT (REAL beard length),
VOID
) gender details
);

View file

@ -0,0 +1,2 @@
p["x"]=10
p["y"]=42

View 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;
}
}
}

View file

@ -0,0 +1,4 @@
type Point is tagged record
X : Integer := 0;
Y : Integer := 0;
end record;

View file

@ -0,0 +1,4 @@
type Point is record
X : Integer := 0;
Y : Integer := 0;
end record;

View 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;

View 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

View file

@ -0,0 +1,3 @@
point := Object()
point.x := 1
point.y := 0

View file

@ -0,0 +1,4 @@
TYPE Point
x AS INTEGER
y AS INTEGER
END TYPE

View file

@ -0,0 +1 @@
DIM Point{x%, y%}

View 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))
);

View file

@ -0,0 +1,5 @@
struct Point
{
int x;
int y;
};

View file

@ -0,0 +1,6 @@
struct Point
{
int x;
int y;
Point(int ax, int ay): x(ax), y(ax) {}
};

View 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 };

View file

@ -0,0 +1,5 @@
typedef struct Point
{
int x;
int y;
} Point;

View file

@ -0,0 +1 @@
:: Point = { x :: Int, y :: Int }

View file

@ -0,0 +1 @@
:: Point a = Point a a // usage: (Point Int)

View file

@ -0,0 +1 @@
:: Point :== (Int, Int)

View file

@ -0,0 +1 @@
(defrecord Point [x y])

View file

@ -0,0 +1,3 @@
(def p (Point. 0 1))
(assert (= 0 (:x p)))
(assert (= 1 (:y p)))

View file

@ -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

View file

@ -0,0 +1,2 @@
CL-USER> (defstruct point (x 0) (y 0)) ;If not provided, x or y default to 0
POINT

View file

@ -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

View 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);
}

View file

@ -0,0 +1,4 @@
TPoint = record
X: Longint;
Y: Longint;
end;

View file

@ -0,0 +1,7 @@
def makePoint(x, y) {
def point {
to getX() { return x }
to getY() { return y }
}
return point
}

View file

@ -0,0 +1 @@
type Maybe = None | Some a

View file

@ -0,0 +1,4 @@
opentype Several = One | Two | Three
//Add new constructor to an existing type
data Several = Four

View 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]).

View 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

View file

@ -0,0 +1,4 @@
struct
cell% field pt>x
cell% field pt>y
end-struct point%

View 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

View file

@ -0,0 +1,11 @@
package main
import "fmt"
type point struct {
x, y float64
}
func main() {
fmt.Println(point{3, 4})
}

View file

@ -0,0 +1 @@
p = (2,3)

View 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 );
}
}

View file

@ -0,0 +1 @@
var point = {x : 1, y : 2};

View 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

View 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

View 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

View 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

View 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

View 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;

View 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";

View 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.

View file

@ -0,0 +1 @@
my @point = (3, 8);

View file

@ -0,0 +1,4 @@
my %point = (
x => 3,
y => 8
);

View file

@ -0,0 +1,9 @@
package Point;
use strict;
use base 'Class::Struct'
x => '$',
y => '$',
;
my $point = Point->new(x => 3, y => 8);

View file

@ -0,0 +1,9 @@
(class +Point)
(dm T (X Y)
(=: x X)
(=: y Y) )
(setq P (new '(+Point) 3 4))
(show P)

View file

@ -0,0 +1,5 @@
X, Y = 0, 1
p = (3, 4)
p = [3, 4]
print p[X]

View 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

View 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

View 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)
>>>

View 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

View file

@ -0,0 +1,4 @@
x= -4.9
y= 1.7
point=x y

View file

@ -0,0 +1,8 @@
x= -4.1
y= 1/4e21
point=x y
bpoint=point
gpoint=5.6 7.3e-12

View file

@ -0,0 +1,2 @@
#lang racket
(struct point (x y))

View file

@ -0,0 +1,5 @@
#lang racket
(define point% ; classes are suffixed with % by convention
(class object%
(super-new)
(init-field x y)))

View 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>

View file

@ -0,0 +1,4 @@
case class Point(x:Int=0, y:Int=0)
val p=Point(1,2)
println(p.y) //=> 2

View file

@ -0,0 +1,5 @@
(define-record-type point
(make-point x y)
point?
(x point-x)
(y point-y))

View 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}

View 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]}"

View 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]"