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
15
Task/Classes/Coco/classes.coco
Normal file
15
Task/Classes/Coco/classes.coco
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class Rectangle
|
||||
# The constructor is defined as a bare function. This
|
||||
# constructor accepts one argument and automatically assigns it
|
||||
# to an instance variable.
|
||||
(@width) ->
|
||||
|
||||
# Another instance variable.
|
||||
length: 10
|
||||
|
||||
# A method.
|
||||
area: ->
|
||||
@width * @length
|
||||
|
||||
# Instantiate the class using the 'new' operator.
|
||||
rect = new Rectangle 2
|
||||
30
Task/Classes/ERRE/classes.erre
Normal file
30
Task/Classes/ERRE/classes.erre
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
PROGRAM CLASS2_DEMO
|
||||
|
||||
CLASS QUADRATO
|
||||
|
||||
LOCAL LATO
|
||||
|
||||
PROCEDURE GETLATO(L)
|
||||
LATO=L
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE AREA(->A)
|
||||
A=LATO*LATO
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE PERIMETRO(->P)
|
||||
P=4*LATO
|
||||
END PROCEDURE
|
||||
|
||||
END CLASS
|
||||
|
||||
NEW P:QUADRATO,Q:QUADRATO
|
||||
|
||||
BEGIN
|
||||
P_GETLATO(10)
|
||||
P_AREA(->AREAP)
|
||||
PRINT(AREAP)
|
||||
Q_GETLATO(20)
|
||||
Q_PERIMETRO(->PERIMETROQ)
|
||||
PRINT(PERIMETROQ)
|
||||
END PROGRAM
|
||||
11
Task/Classes/EchoLisp/classes-1.echolisp
Normal file
11
Task/Classes/EchoLisp/classes-1.echolisp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(lib 'gloops) ; load oo library
|
||||
|
||||
(define-class Person null (name (age :initform 66)))
|
||||
(define-method tostring (Person) (lambda (p) ( format "🚶 %a " p.name)))
|
||||
(define-method mailto (Person Person) (lambda( p o) (printf "From %a to️ %a : ..." p o)))
|
||||
|
||||
;; define a sub-class of Person with same methods
|
||||
(define-class Writer (Person) (books))
|
||||
(define-method tostring (Writer) (lambda (w)( format "🎩 %a" w.name)))
|
||||
(define-method mailto (Person Writer)
|
||||
(lambda (p w) (printf " From %a (age %d). Dear writer of %a ..." p p.age w.books )))
|
||||
16
Task/Classes/EchoLisp/classes-2.echolisp
Normal file
16
Task/Classes/EchoLisp/classes-2.echolisp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
;; instantiate
|
||||
(define simone (make-instance Person :name 'simone :age 42)) ;; slots values by name
|
||||
(define antoinette (make-instance Person :name 'antoinette :age 37))
|
||||
(define albert (Person "albert" 33)) ;; quick way : slots values in order
|
||||
(define simon (make-instance Writer :name "simon" :books '(my-life my-bike)))
|
||||
|
||||
|
||||
(mailto simone simon) ;; method Person-Writer
|
||||
(mailto simone antoinette) ;; method Person-Person
|
||||
(mailto simon albert) ;; no method Writer-Person : call 'super' Person-Person
|
||||
(mailto simon simon) ;; no mehod Writer-Writer : call 'super' Person-Writer
|
||||
→
|
||||
From 🚶 simone (age 42). Dear writer of (my-life my-bike) ...
|
||||
From 🚶 simone to️ 🚶 antoinette : ...
|
||||
From 🎩 simon to️ 🚶 albert : ...
|
||||
From 🎩 simon (age 66). Dear writer of (my-life my-bike) ...
|
||||
27
Task/Classes/FreeBASIC/classes.freebasic
Normal file
27
Task/Classes/FreeBASIC/classes.freebasic
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type MyClass
|
||||
Private:
|
||||
myInt_ As Integer
|
||||
Public:
|
||||
Declare Constructor(myInt_ As Integer)
|
||||
Declare Property MyInt() As Integer
|
||||
Declare Function Treble() As Integer
|
||||
End Type
|
||||
|
||||
Constructor MyClass(myInt_ As Integer)
|
||||
This.myInt_ = myInt_
|
||||
End Constructor
|
||||
|
||||
Property MyClass.MyInt() As Integer
|
||||
Return myInt_
|
||||
End Property
|
||||
|
||||
Function MyClass.Treble() As Integer
|
||||
Return 3 * myInt_
|
||||
End Function
|
||||
|
||||
Dim mc As MyClass = MyClass(24)
|
||||
Print mc.MyInt, mc.Treble()
|
||||
Print "Press any key to quit the program"
|
||||
Sleep
|
||||
31
Task/Classes/LFE/classes-1.lfe
Normal file
31
Task/Classes/LFE/classes-1.lfe
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
(defmodule simple-object
|
||||
(export all))
|
||||
|
||||
(defun fish-class (species)
|
||||
"
|
||||
This is the constructor used internally, once the children and fish id are
|
||||
known.
|
||||
"
|
||||
(let ((habitat '"water"))
|
||||
(lambda (method-name)
|
||||
(case method-name
|
||||
('habitat
|
||||
(lambda (self) habitat))
|
||||
('species
|
||||
(lambda (self) species))))))
|
||||
|
||||
(defun get-method (object method-name)
|
||||
"
|
||||
This is a generic function, used to call into the given object (class
|
||||
instance).
|
||||
"
|
||||
(funcall object method-name))
|
||||
|
||||
; define object methods
|
||||
(defun get-habitat (object)
|
||||
"Get a variable set in the class."
|
||||
(funcall (get-method object 'habitat) object))
|
||||
|
||||
(defun get-species (object)
|
||||
"Get a variable passed when constructing the object."
|
||||
(funcall (get-method object 'species) object))
|
||||
8
Task/Classes/LFE/classes-2.lfe
Normal file
8
Task/Classes/LFE/classes-2.lfe
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
> (slurp '"simple-object.lfe")
|
||||
#(ok simple-object)
|
||||
> (set my-fish (fish-class '"Carp"))
|
||||
#Fun<lfe_eval.10.91765564>
|
||||
> (get-habitat my-fish)
|
||||
"water"
|
||||
> (get-species my-fish)
|
||||
"Carp"
|
||||
19
Task/Classes/Lasso/classes.lasso
Normal file
19
Task/Classes/Lasso/classes.lasso
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
define mytype => type {
|
||||
data
|
||||
public id::integer = 0,
|
||||
public val::string = '',
|
||||
public rand::integer = 0
|
||||
|
||||
public onCreate() => {
|
||||
// "onCreate" runs when instance created, populates .rand
|
||||
.rand = math_random(50,1)
|
||||
}
|
||||
public asString() => {
|
||||
return 'has a value of: "'+.val+'" and a rand number of "'+.rand+'"'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
local(x = mytype)
|
||||
#x->val = '99 Bottles of beer'
|
||||
#x->asString // outputs 'has a value of: "99 Bottles of beer" and a rand number of "48"'
|
||||
19
Task/Classes/Lingo/classes-1.lingo
Normal file
19
Task/Classes/Lingo/classes-1.lingo
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
----------------------------------------
|
||||
-- @desc Class "MyClass"
|
||||
-- @file parent script "MyClass"
|
||||
----------------------------------------
|
||||
|
||||
-- instance variable
|
||||
property _myvar
|
||||
|
||||
-- constructor
|
||||
on new (me)
|
||||
me._myvar = 23
|
||||
return me
|
||||
end
|
||||
|
||||
-- a method
|
||||
on doubleAndPrint (me)
|
||||
me._myvar = me._myvar * 2
|
||||
put me._myvar
|
||||
end
|
||||
3
Task/Classes/Lingo/classes-2.lingo
Normal file
3
Task/Classes/Lingo/classes-2.lingo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
foo = script("MyClass").new()
|
||||
foo.doubleAndPrint()
|
||||
-- 46
|
||||
27
Task/Classes/Nim/classes.nim
Normal file
27
Task/Classes/Nim/classes.nim
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
type MyClass = object
|
||||
name: int
|
||||
|
||||
proc initMyClass(): MyClass =
|
||||
result.name = 2
|
||||
|
||||
proc someMethod(m: var MyClass) =
|
||||
m.name = 1
|
||||
|
||||
var mc = initMyClass()
|
||||
mc.someMethod()
|
||||
|
||||
type
|
||||
Gender = enum male, female, other
|
||||
|
||||
MyOtherClass = object
|
||||
name: string
|
||||
gender: Gender
|
||||
age: Natural
|
||||
|
||||
proc initMyOtherClass(name; gender = female; age = 50): auto =
|
||||
MyOtherClass(name: name, gender: gender, age: age)
|
||||
|
||||
var person1 = initMyOtherClass("Jane")
|
||||
echo person1.name, " ", person1.gender, " ", person1.age # Jane female 50
|
||||
var person2 = initMyOtherClass("John", male, 23)
|
||||
echo person2.name, " ", person2.gender, " ", person2.age # John male 23
|
||||
2
Task/Classes/Oforth/classes-1.oforth
Normal file
2
Task/Classes/Oforth/classes-1.oforth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Object Class new: MyClass(att)
|
||||
MyClass method: initialize(v) v := att ;
|
||||
1
Task/Classes/Oforth/classes-2.oforth
Normal file
1
Task/Classes/Oforth/classes-2.oforth
Normal file
|
|
@ -0,0 +1 @@
|
|||
MyClass new("some value")
|
||||
23
Task/Classes/PHL/classes.phl
Normal file
23
Task/Classes/PHL/classes.phl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
module classes;
|
||||
|
||||
extern printf;
|
||||
|
||||
class @MyClass {
|
||||
field @Integer myField { get:get_myField, set:set_myField };
|
||||
|
||||
new [
|
||||
this.set_myField(2);
|
||||
]
|
||||
|
||||
@Void method [
|
||||
this.set_myField(this::get_myField + 1);
|
||||
]
|
||||
};
|
||||
|
||||
@Integer main [
|
||||
var obj = new @MyClass;
|
||||
printf("obj.myField: %i\n", obj::get_myField);
|
||||
obj::method;
|
||||
printf("obj.myField: %i\n", obj::get_myField);
|
||||
return 0;
|
||||
]
|
||||
2
Task/Classes/Ring/classes-1.ring
Normal file
2
Task/Classes/Ring/classes-1.ring
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
New point { x=10 y=20 z=30 print() }
|
||||
Class Point x y z func print see x + nl + y + nl + z + nl
|
||||
15
Task/Classes/Ring/classes-2.ring
Normal file
15
Task/Classes/Ring/classes-2.ring
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
New point # create new object using the point class
|
||||
{ # access the new object attributes and methods
|
||||
x = 10 # set the x attribute to 10
|
||||
y = 20 # set the y attribute to 20
|
||||
z = 30 # set the z attribute to 30
|
||||
print() # call the print method
|
||||
} # end of object access
|
||||
|
||||
|
||||
Class Point # define the Point class
|
||||
x y z # the class contains three attributes x, y & z
|
||||
func print # define the print method
|
||||
see x + nl + # print the x attribute
|
||||
y + nl + # print the y attribute
|
||||
z + nl # print the z attribute
|
||||
9
Task/Classes/Sidef/classes.sidef
Normal file
9
Task/Classes/Sidef/classes.sidef
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
class MyClass(instance_var) {
|
||||
method add(num) {
|
||||
instance_var += num;
|
||||
}
|
||||
}
|
||||
|
||||
var obj = MyClass(3); # `instance_var` will be set to 3
|
||||
obj.add(5); # calls the add() method
|
||||
say obj.instance_var; # prints the value of `instance_var`: 8
|
||||
19
Task/Classes/Swift/classes-1.swift
Normal file
19
Task/Classes/Swift/classes-1.swift
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class MyClass{
|
||||
|
||||
// stored property
|
||||
var variable : Int
|
||||
|
||||
/**
|
||||
* The constructor
|
||||
*/
|
||||
init() {
|
||||
self.variable = 42
|
||||
}
|
||||
|
||||
/**
|
||||
* A method
|
||||
*/
|
||||
func someMethod() {
|
||||
self.variable = 1
|
||||
}
|
||||
}
|
||||
1
Task/Classes/Swift/classes-2.swift
Normal file
1
Task/Classes/Swift/classes-2.swift
Normal file
|
|
@ -0,0 +1 @@
|
|||
MyClass()
|
||||
28
Task/Classes/Visual-FoxPro/classes.visual
Normal file
28
Task/Classes/Visual-FoxPro/classes.visual
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
LOCAL o1 As MyClass, o2 As MyClass
|
||||
*!* Instantiate o1
|
||||
o1 = NEWOBJECT("MyClass")
|
||||
o1.ShowInstance()
|
||||
*!* Instantiate o2
|
||||
o2 = CREATEOBJECT("MyClass", 2)
|
||||
o2.ShowInstance()
|
||||
|
||||
|
||||
DEFINE CLASS MyClass As Session
|
||||
*!* Custom property (protected)
|
||||
PROTECTED nInstance
|
||||
nInstance = 0
|
||||
|
||||
*!* Constructor
|
||||
PROCEDURE Init(tnInstance As Integer)
|
||||
IF VARTYPE(tnInstance) = "N"
|
||||
THIS.nInstance = tnInstance
|
||||
ELSE
|
||||
THIS.nInstance = THIS.nInstance + 1
|
||||
ENDIF
|
||||
ENDPROC
|
||||
|
||||
*!* Custom Method
|
||||
PROCEDURE ShowInstance
|
||||
? "Instance", THIS.nInstance
|
||||
ENDPROC
|
||||
ENDDEFINE
|
||||
19
Task/Classes/XLISP/classes.xlisp
Normal file
19
Task/Classes/XLISP/classes.xlisp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(DEFINE-CLASS PROGRAMMING-LANGUAGE
|
||||
(INSTANCE-VARIABLES NAME YEAR))
|
||||
|
||||
(DEFINE-METHOD (PROGRAMMING-LANGUAGE 'INITIALIZE X)
|
||||
(SETQ NAME X)
|
||||
SELF)
|
||||
|
||||
(DEFINE-METHOD (PROGRAMMING-LANGUAGE 'WAS-CREATED-IN X)
|
||||
(SETQ YEAR X))
|
||||
|
||||
(DEFINE-METHOD (PROGRAMMING-LANGUAGE 'DESCRIBE)
|
||||
`(THE PROGRAMMING LANGUAGE ,NAME WAS CREATED IN ,YEAR))
|
||||
|
||||
(DEFINE LISP (PROGRAMMING-LANGUAGE 'NEW 'LISP))
|
||||
|
||||
(LISP 'WAS-CREATED-IN 1958)
|
||||
|
||||
(DISPLAY (LISP 'DESCRIBE))
|
||||
(NEWLINE)
|
||||
Loading…
Add table
Add a link
Reference in a new issue