2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,7 +1,16 @@
{{Data structure}}
Create a compound data type Point(x,y).
;Task:
Create a compound data type:
<big> Point(x,y) </big>
A compound data type is one that holds multiple independent values.
See also [[Enumeration]].
;Related task:
* &nbsp; [[Enumeration]]
{{Template:See also lists}}
<br><br>

View file

@ -0,0 +1,18 @@
iex(1)> defmodule Point do
...(1)> defstruct x: 0, y: 0
...(1)> end
{:module, Point, <<70, 79, 82, ...>>, %Point{x: 0, y: 0}}
iex(2)> origin = %Point{}
%Point{x: 0, y: 0}
iex(3)> pa = %Point{x: 10, y: 20}
%Point{x: 10, y: 20}
iex(4)> pa.x
10
iex(5)> %Point{pa | y: 30}
%Point{x: 10, y: 30}
iex(6)> %Point{x: px, y: py} = pa # pattern matching
%Point{x: 10, y: 20}
iex(7)> px
10
iex(8)> py
20

View file

@ -0,0 +1,18 @@
class Point {
[Int]$a
[Int]$b
Point() {
$this.a = 0
$this.b = 0
}
Point([Int]$a, [Int]$b) {
$this.a = $a
$this.b = $b
}
[Int]add() {return $this.a + $this.b}
[Int]mul() {return $this.a * $this.b}
}
$p1 = [Point]::new()
$p2 = [Point]::new(3,2)
$p1.add()
$p2.mul()

View file

@ -0,0 +1,4 @@
Structure MyPoint
x.i
y.i
EndStructure

View file

@ -0,0 +1,9 @@
// Defines a generic struct where x and y can be of any type T
struct Point<T> {
x: T,
y: T,
}
fn main() {
let p = Point { x: 1.0, y: 2.5 }; // p is of type Point<f64>
println!("{}, {}", p.x, p.y);
}

View file

@ -0,0 +1,5 @@
struct Point<T>(T, T);
fn main() {
let p = Point(1.0, 2.5);
println!("{},{}", p.0, p.1);
}

View file

@ -0,0 +1,4 @@
fn main() {
let p = (0.0, 2.4);
println!("{},{}", p.0, p.1);
}