Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
42
Task/Polymorphism/EchoLisp/polymorphism.echolisp
Normal file
42
Task/Polymorphism/EchoLisp/polymorphism.echolisp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
(struct Point ((real:x 0) (real:y 0)))
|
||||
(struct Circle ((real:x 0) (real:y 0) (real:r 1)))
|
||||
|
||||
(define-method (print Point:p) (printf "📌 [%d %d]" p.x p.y))
|
||||
(define-method (print Circle:c) (printf "⭕️ center:[%d %d] radius:%d" c.x c.y c.r))
|
||||
|
||||
(print (Point 5 6))
|
||||
→ 📌 [5 6]
|
||||
(print (Circle 2 3 4))
|
||||
→ ⭕️ center:[2 3] radius:4
|
||||
|
||||
;; Accessors :
|
||||
;; (Point-x p), (Point-y p) or p.x, p.y
|
||||
;; (Circle-x c), c.x , etc.
|
||||
;; Setters :
|
||||
;; (set-Point-x! p value), (set-Circle-r! c value) etc.
|
||||
|
||||
;; Constructors
|
||||
;; (Point) (Point x) (Point x y)
|
||||
;; (Circle) (circle x) (Circle x y) (Circle x y r)
|
||||
|
||||
;;Copy
|
||||
(print (copy (Circle 3 3 )))
|
||||
→ ⭕️ center:[3 3] radius:1
|
||||
|
||||
;;Assignment (to a variable)
|
||||
(define my-point (Point 7 8))
|
||||
|
||||
;;Destructor : none. Points and Circles are garbage collected.
|
||||
|
||||
;;Type checking
|
||||
(Point "here" "there")
|
||||
💣 error: Real : type-check failure : here → 'Point:x'
|
||||
|
||||
;;Initializer procedure
|
||||
(struct Circle ((x 0) (y 0) (r 1) d) #:initialize circle-init)
|
||||
(define (circle-init Circle:c) (set-Circle-d! c (* 2 PI c.r)))
|
||||
(define-method (print Circle:c)
|
||||
(printf "⭕️ center:[%d %d] radius:%d diameter:%d" c.x c.y c.r c.d))
|
||||
|
||||
(print (Circle 0 0 10))
|
||||
→ ⭕️ center:[0 0] radius:10 diameter:62.83185307179586
|
||||
35
Task/Polymorphism/Nim/polymorphism.nim
Normal file
35
Task/Polymorphism/Nim/polymorphism.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
type
|
||||
Point = object
|
||||
x, y: float
|
||||
|
||||
Circle = object
|
||||
center: Point
|
||||
radius: float
|
||||
|
||||
# Constructors
|
||||
proc createPoint(x, y = 0.0): Point =
|
||||
result.x = x
|
||||
result.y = y
|
||||
|
||||
proc createCircle(x, y = 0.0, radius = 1.0): Circle =
|
||||
result.center.x = x
|
||||
result.center.y = y
|
||||
result.radius = radius
|
||||
|
||||
var p1 = createPoint()
|
||||
echo "p1: ", p1 # We use the default $ operator for printing
|
||||
var p2 = createPoint(3, 4.2)
|
||||
var p3 = createPoint(x = 2)
|
||||
var p4 = createPoint(y = 2.5)
|
||||
|
||||
p2 = p4
|
||||
p3 = createPoint()
|
||||
|
||||
var c1 = createCircle()
|
||||
echo "c1: ", c1
|
||||
var c2 = createCircle(2, 0.5, 4.2)
|
||||
var c3 = createCircle(x = 2.1, y = 2)
|
||||
var c4 = createCircle(radius = 10)
|
||||
|
||||
c1.center.x = 12
|
||||
c1.radius = 5.2
|
||||
14
Task/Polymorphism/Oforth/polymorphism-1.oforth
Normal file
14
Task/Polymorphism/Oforth/polymorphism-1.oforth
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
Object Class new: Point(x, y)
|
||||
Point method: initialize(x, y) x := x y := y ;
|
||||
Point method: _x @x ;
|
||||
Point method: _y @y ;
|
||||
Point method: << "(" << @x << ", " << @y << ")" << ;
|
||||
|
||||
Object Class new: Circle(x, y, r)
|
||||
Circle method: initialize(x, y, r) x := x y := y r := r ;
|
||||
Circle method: _x @x ;
|
||||
Circle method: _y @y ;
|
||||
Circle method: _r @r ;
|
||||
Circle method: << "(" << @x << ", " << @y << ", " << @r << ")" << ;
|
||||
|
||||
Circle classMethod: newFromPoint(aPoint, r) self new(aPoint _x, aPoint _y, r) ;
|
||||
9
Task/Polymorphism/Oforth/polymorphism-2.oforth
Normal file
9
Task/Polymorphism/Oforth/polymorphism-2.oforth
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
: testPoly
|
||||
| p c |
|
||||
Point new(3, 4) ->p
|
||||
p println
|
||||
System.Out "Attributes of this point are : " << p _x << " and " << p _y << cr
|
||||
Circle new(5, 6, 7.1) ->c
|
||||
c println
|
||||
System.Out "Attributes of this circle are : " << c _x << ", " << c _y << " and " << c _r << cr
|
||||
Circle newFromPoint(p, 2) println ;
|
||||
24
Task/Polymorphism/Phix/polymorphism.phix
Normal file
24
Task/Polymorphism/Phix/polymorphism.phix
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
type point(object o)
|
||||
return sequence(o) and length(o)=2 and atom(o[1]) and atom(o[2])
|
||||
end type
|
||||
|
||||
function new_point(atom x=0, atom y=0)
|
||||
return {x,y}
|
||||
end function
|
||||
|
||||
type circle(object o)
|
||||
return sequence(o) and length(o)=2 and point(o[1]) and atom(o[2])
|
||||
end type
|
||||
|
||||
function new_circle(object x=0, atom y=0, atom r=0)
|
||||
if point(x) then
|
||||
r = y -- assume r got passed in y
|
||||
return {x,r} -- {point,r}
|
||||
end if
|
||||
return {{x,y},r}
|
||||
end function
|
||||
|
||||
point p = new_point(4,5)
|
||||
circle c = new_circle(p,6)
|
||||
?p
|
||||
?c
|
||||
15
Task/Polymorphism/Sidef/polymorphism-1.sidef
Normal file
15
Task/Polymorphism/Sidef/polymorphism-1.sidef
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class Point(x=0, y=0) {
|
||||
|
||||
}
|
||||
|
||||
class Circle(x=0, y=0, r=0) {
|
||||
|
||||
}
|
||||
|
||||
func pp(Point obj) {
|
||||
say "Point at #{obj.x},#{obj.y}";
|
||||
}
|
||||
|
||||
func pp(Circle obj) {
|
||||
say "Circle at #{obj.x},#{obj.y} with radius #{obj.r}";
|
||||
}
|
||||
12
Task/Polymorphism/Sidef/polymorphism-2.sidef
Normal file
12
Task/Polymorphism/Sidef/polymorphism-2.sidef
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
pp(Point.new); # => Point at 0,0
|
||||
var p = Point(1, 2); # create a point
|
||||
pp(p); # => Point at 1,2
|
||||
say p.x; # => 1
|
||||
p.y += 1; # add one to y
|
||||
pp(p); # => Point at 1,3
|
||||
|
||||
var c = Circle(4,5,6); # create a circle
|
||||
var d = c.clone; # make a clone of it
|
||||
d.r = 7.5; # and change the radius to 7.5
|
||||
pp(c); # => Circle at 4,5 with radius 6
|
||||
pp(d); # => Circle at 4,5 with radius 7.5
|
||||
46
Task/Polymorphism/Swift/polymorphism.swift
Normal file
46
Task/Polymorphism/Swift/polymorphism.swift
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
class RCPoint : Printable {
|
||||
var x: Int
|
||||
var y: Int
|
||||
init(x: Int = 0, y: Int = 0) {
|
||||
self.x = x
|
||||
self.y = y
|
||||
}
|
||||
convenience init(p: RCPoint) {
|
||||
self.init(x:p.x, y:p.y)
|
||||
}
|
||||
var description: String {
|
||||
return "<RCPoint x: \(self.x) y: \(self.y)>"
|
||||
}
|
||||
}
|
||||
|
||||
class RCCircle : RCPoint {
|
||||
var r: Int
|
||||
init(p: RCPoint, r: Int = 0) {
|
||||
self.r = r
|
||||
super.init(x:p.x, y:p.y)
|
||||
}
|
||||
init(x: Int = 0, y: Int = 0, r: Int = 0) {
|
||||
self.r = r
|
||||
super.init(x:x, y:y)
|
||||
}
|
||||
convenience init(c: RCCircle) {
|
||||
self.init(x:c.x, y:c.y, r:c.r)
|
||||
}
|
||||
override var description: String {
|
||||
return "<RCCircle x: \(x) y: \(y) r: \(r)>"
|
||||
}
|
||||
}
|
||||
|
||||
println(RCPoint())
|
||||
println(RCPoint(x:3))
|
||||
println(RCPoint(x:3, y:4))
|
||||
println(RCCircle())
|
||||
println(RCCircle(x:3))
|
||||
println(RCCircle(x:3, y:4))
|
||||
println(RCCircle(x:3, y:4, r:7))
|
||||
let p = RCPoint(x:1, y:2)
|
||||
println(RCCircle(p:p))
|
||||
println(RCCircle(p:p, r:7))
|
||||
println(p.x) // 1
|
||||
p.x = 8
|
||||
println(p.x) // 8
|
||||
43
Task/Polymorphism/Wollok/polymorphism.wollok
Normal file
43
Task/Polymorphism/Wollok/polymorphism.wollok
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
class Point {
|
||||
var x
|
||||
var y
|
||||
new(ax, ay) {
|
||||
this.x = ax
|
||||
this.y = ay
|
||||
}
|
||||
new(point) {
|
||||
this(point.x, point.y)
|
||||
}
|
||||
method getX() { return x }
|
||||
method setX(newX) { x = newX }
|
||||
|
||||
method getY() { return y }
|
||||
method setY(newY) { y = newY }
|
||||
|
||||
method print() {
|
||||
console.println("Point")
|
||||
}
|
||||
}
|
||||
|
||||
class Circle extends Point {
|
||||
var r
|
||||
|
||||
new() { this(0,0,0) }
|
||||
new(point, aR) { super(point) ; r = aR }
|
||||
new(aX, aY, aR) { super(aX, aY); r = aR }
|
||||
|
||||
method getR() { return r }
|
||||
method setR(newR) { r = newR }
|
||||
|
||||
method print() {
|
||||
console.println("Circle")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
program polymorphism {
|
||||
val p = new Point()
|
||||
val c = new Circle()
|
||||
p.print()
|
||||
c.print()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue