2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -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:
|
||||
* [[Enumeration]]
|
||||
|
||||
|
||||
{{Template:See also lists}}
|
||||
<br><br>
|
||||
|
|
|
|||
18
Task/Compound-data-type/Elixir/compound-data-type.elixir
Normal file
18
Task/Compound-data-type/Elixir/compound-data-type.elixir
Normal 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
|
||||
18
Task/Compound-data-type/PowerShell/compound-data-type.psh
Normal file
18
Task/Compound-data-type/PowerShell/compound-data-type.psh
Normal 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()
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Structure MyPoint
|
||||
x.i
|
||||
y.i
|
||||
EndStructure
|
||||
9
Task/Compound-data-type/Rust/compound-data-type-1.rust
Normal file
9
Task/Compound-data-type/Rust/compound-data-type-1.rust
Normal 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);
|
||||
}
|
||||
5
Task/Compound-data-type/Rust/compound-data-type-2.rust
Normal file
5
Task/Compound-data-type/Rust/compound-data-type-2.rust
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
struct Point<T>(T, T);
|
||||
fn main() {
|
||||
let p = Point(1.0, 2.5);
|
||||
println!("{},{}", p.0, p.1);
|
||||
}
|
||||
4
Task/Compound-data-type/Rust/compound-data-type-3.rust
Normal file
4
Task/Compound-data-type/Rust/compound-data-type-3.rust
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fn main() {
|
||||
let p = (0.0, 2.4);
|
||||
println!("{},{}", p.0, p.1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue