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
|
|
@ -0,0 +1,5 @@
|
|||
// Static
|
||||
MyClass.method(someParameter);
|
||||
|
||||
// Instance
|
||||
myInstance.method(someParameter);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
MyClass myClassObject;
|
||||
myClassObject.myFunction(some parameter);
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type MyType
|
||||
Public:
|
||||
Declare Sub InstanceMethod(s As String)
|
||||
Declare Static Sub StaticMethod(s As String)
|
||||
Private:
|
||||
dummy_ As Integer ' types cannot be empty in FB
|
||||
End Type
|
||||
|
||||
Sub MyType.InstanceMethod(s As String)
|
||||
Print s
|
||||
End Sub
|
||||
|
||||
Static Sub MyType.StaticMethod(s As String)
|
||||
Print s
|
||||
End Sub
|
||||
|
||||
Dim t As MyType
|
||||
t.InstanceMethod("Hello world!")
|
||||
MyType.Staticmethod("Hello static world!")
|
||||
Print
|
||||
Print "Press any key to quit the program"
|
||||
Sleep
|
||||
93
Task/Call-an-object-method/LFE/call-an-object-method-1.lfe
Normal file
93
Task/Call-an-object-method/LFE/call-an-object-method-1.lfe
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
(defmodule aquarium
|
||||
(export all))
|
||||
|
||||
(defun fish-class (species)
|
||||
"
|
||||
This is the constructor that will be used most often, only requiring that
|
||||
one pass a 'species' string.
|
||||
|
||||
When the children are not defined, simply use an empty list.
|
||||
"
|
||||
(fish-class species ()))
|
||||
|
||||
(defun fish-class (species children)
|
||||
"
|
||||
This contructor is mostly useful as a way of abstracting out the id
|
||||
generation from the larger constructor. Nothing else uses fish-class/2
|
||||
besides fish-class/1, so it's not strictly necessary.
|
||||
|
||||
When the id isn't know, generate one.
|
||||
"
|
||||
(let* (((binary (id (size 128))) (: crypto rand_bytes 16))
|
||||
(formatted-id (car
|
||||
(: io_lib format
|
||||
'"~32.16.0b" (list id)))))
|
||||
(fish-class species children formatted-id)))
|
||||
|
||||
(defun fish-class (species children id)
|
||||
"
|
||||
This is the constructor used internally, once the children and fish id are
|
||||
known.
|
||||
"
|
||||
(let ((move-verb '"swam"))
|
||||
(lambda (method-name)
|
||||
(case method-name
|
||||
('id
|
||||
(lambda (self) id))
|
||||
('species
|
||||
(lambda (self) species))
|
||||
('children
|
||||
(lambda (self) children))
|
||||
('info
|
||||
(lambda (self)
|
||||
(: io format
|
||||
'"id: ~p~nspecies: ~p~nchildren: ~p~n"
|
||||
(list (get-id self)
|
||||
(get-species self)
|
||||
(get-children self)))))
|
||||
('move
|
||||
(lambda (self distance)
|
||||
(: io format
|
||||
'"The ~s ~s ~p feet!~n"
|
||||
(list species move-verb distance))))
|
||||
('reproduce
|
||||
(lambda (self)
|
||||
(let* ((child (fish-class species))
|
||||
(child-id (get-id child))
|
||||
(children-ids (: lists append
|
||||
(list children (list child-id))))
|
||||
(parent-id (get-id self))
|
||||
(parent (fish-class species children-ids parent-id)))
|
||||
(list parent child))))
|
||||
('children-count
|
||||
(lambda (self)
|
||||
(: erlang length children)))))))
|
||||
|
||||
(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-id (object)
|
||||
(funcall (get-method object 'id) object))
|
||||
|
||||
(defun get-species (object)
|
||||
(funcall (get-method object 'species) object))
|
||||
|
||||
(defun get-info (object)
|
||||
(funcall (get-method object 'info) object))
|
||||
|
||||
(defun move (object distance)
|
||||
(funcall (get-method object 'move) object distance))
|
||||
|
||||
(defun reproduce (object)
|
||||
(funcall (get-method object 'reproduce) object))
|
||||
|
||||
(defun get-children (object)
|
||||
(funcall (get-method object 'children) object))
|
||||
|
||||
(defun get-children-count (object)
|
||||
(funcall (get-method object 'children-count) object))
|
||||
45
Task/Call-an-object-method/LFE/call-an-object-method-2.lfe
Normal file
45
Task/Call-an-object-method/LFE/call-an-object-method-2.lfe
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
; Load the file and create a fish-class instance:
|
||||
|
||||
> (slurp '"object.lfe")
|
||||
#(ok object)
|
||||
> (set mommy-fish (fish-class '"Carp"))
|
||||
#Fun<lfe_eval.10.91765564>
|
||||
|
||||
; Execute some of the basic methods:
|
||||
|
||||
> (get-species mommy-fish)
|
||||
"Carp"
|
||||
> (move mommy-fish 17)
|
||||
The Carp swam 17 feet!
|
||||
ok
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
|
||||
; Now let's look at "modifying" state data (e.g., children counts):
|
||||
|
||||
> (get-children mommy-fish)
|
||||
()
|
||||
> (get-children-count mommy-fish)
|
||||
0
|
||||
> (set (mommy-fish baby-fish-1) (reproduce mommy-fish))
|
||||
(#Fun<lfe_eval.10.91765564> #Fun<lfe_eval.10.91765564>)
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-1)
|
||||
"fdcf35983bb496650e558a82e34c9935"
|
||||
> (get-children-count mommy-fish)
|
||||
1
|
||||
> (set (mommy-fish baby-fish-2) (reproduce mommy-fish))
|
||||
(#Fun<lfe_eval.10.91765564> #Fun<lfe_eval.10.91765564>)
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-2)
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"
|
||||
> (get-children-count mommy-fish)
|
||||
2
|
||||
> (get-info mommy-fish)
|
||||
id: "47eebe91a648f042fc3fb278df663de5"
|
||||
species: "Carp"
|
||||
children: ["fdcf35983bb496650e558a82e34c9935",
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"]
|
||||
ok
|
||||
101
Task/Call-an-object-method/LFE/call-an-object-method-3.lfe
Normal file
101
Task/Call-an-object-method/LFE/call-an-object-method-3.lfe
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
(defmodule object
|
||||
(export all))
|
||||
|
||||
(defun fish-class (species)
|
||||
"
|
||||
This is the constructor that will be used most often, only requiring that
|
||||
one pass a 'species' string.
|
||||
|
||||
When the children are not defined, simply use an empty list.
|
||||
"
|
||||
(fish-class species ()))
|
||||
|
||||
(defun fish-class (species children)
|
||||
"
|
||||
This constructor is useful for two reasons:
|
||||
1) as a way of abstracting out the id generation from the
|
||||
larger constructor, and
|
||||
2) spawning the 'object loop' code (fish-class/3).
|
||||
"
|
||||
(let* (((binary (id (size 128))) (: crypto rand_bytes 16))
|
||||
(formatted-id (car
|
||||
(: io_lib format
|
||||
'"~32.16.0b" (list id)))))
|
||||
(spawn 'object
|
||||
'fish-class
|
||||
(list species children formatted-id))))
|
||||
|
||||
(defun fish-class (species children id)
|
||||
"
|
||||
This function is intended to be spawned as a separate process which is
|
||||
used to track the state of a fish. In particular, fish-class/2 spawns
|
||||
this function (which acts as a loop, pattern matching for messages).
|
||||
"
|
||||
(let ((move-verb '"swam"))
|
||||
(receive
|
||||
((tuple caller 'move distance)
|
||||
(! caller (list species move-verb distance))
|
||||
(fish-class species children id))
|
||||
((tuple caller 'species)
|
||||
(! caller species)
|
||||
(fish-class species children id))
|
||||
((tuple caller 'children)
|
||||
(! caller children)
|
||||
(fish-class species children id))
|
||||
((tuple caller 'children-count)
|
||||
(! caller (length children))
|
||||
(fish-class species children id))
|
||||
((tuple caller 'id)
|
||||
(! caller id)
|
||||
(fish-class species children id))
|
||||
((tuple caller 'info)
|
||||
(! caller (list id species children))
|
||||
(fish-class species children id))
|
||||
((tuple caller 'reproduce)
|
||||
(let* ((child (fish-class species))
|
||||
(child-id (get-id child))
|
||||
(children-ids (: lists append
|
||||
(list children (list child-id)))))
|
||||
(! caller child)
|
||||
(fish-class species children-ids id))))))
|
||||
|
||||
(defun call-method (object method-name)
|
||||
"
|
||||
This is a generic function, used to call into the given object (class
|
||||
instance).
|
||||
"
|
||||
(! object (tuple (self) method-name))
|
||||
(receive
|
||||
(data data)))
|
||||
|
||||
(defun call-method (object method-name arg)
|
||||
"
|
||||
Same as above, but with an additional argument.
|
||||
"
|
||||
(! object (tuple (self) method-name arg))
|
||||
(receive
|
||||
(data data)))
|
||||
|
||||
; define object methods
|
||||
(defun get-id (object)
|
||||
(call-method object 'id))
|
||||
|
||||
(defun get-species (object)
|
||||
(call-method object 'species))
|
||||
|
||||
(defun get-info (object)
|
||||
(let ((data (call-method object 'info)))
|
||||
(: io format '"id: ~s~nspecies: ~s~nchildren: ~p~n" data)))
|
||||
|
||||
(defun move (object distance)
|
||||
(let ((data (call-method object 'move distance)))
|
||||
(: io format '"The ~s ~s ~p feet!~n" data)))
|
||||
|
||||
(defun reproduce (object)
|
||||
(call-method object 'reproduce))
|
||||
|
||||
(defun get-children (object)
|
||||
(call-method object 'children))
|
||||
|
||||
(defun get-children-count (object)
|
||||
(call-method object 'children-count))
|
||||
45
Task/Call-an-object-method/LFE/call-an-object-method-4.lfe
Normal file
45
Task/Call-an-object-method/LFE/call-an-object-method-4.lfe
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
; Load the file and create a fish-class instance:
|
||||
|
||||
> (slurp '"object.lfe")
|
||||
#(ok object)
|
||||
> (set mommy-fish (fish-class '"Carp"))
|
||||
<0.33.0>
|
||||
|
||||
; Execute some of the basic methods:
|
||||
|
||||
> (get-species mommy-fish)
|
||||
"Carp"
|
||||
> (move mommy-fish 17)
|
||||
The Carp swam 17 feet!
|
||||
ok
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
|
||||
; Now let's look at modifying state data:
|
||||
|
||||
> (get-children mommy-fish)
|
||||
()
|
||||
> (get-children-count mommy-fish)
|
||||
0
|
||||
> (set baby-fish-1 (reproduce mommy-fish))
|
||||
<0.34.0>
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-1)
|
||||
"fdcf35983bb496650e558a82e34c9935"
|
||||
> (get-children-count mommy-fish)
|
||||
1
|
||||
> (set baby-fish-2 (reproduce mommy-fish))
|
||||
<0.35.0>
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-2)
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"
|
||||
> (get-children-count mommy-fish)
|
||||
2
|
||||
> (get-info mommy-fish)
|
||||
id: 47eebe91a648f042fc3fb278df663de5
|
||||
species: Carp
|
||||
children: ["fdcf35983bb496650e558a82e34c9935",
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"]
|
||||
ok
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
-- call static method
|
||||
script("MyClass").foo()
|
||||
|
||||
-- call instance method
|
||||
obj = script("MyClass").new()
|
||||
obj.foo()
|
||||
3
Task/Call-an-object-method/Nim/call-an-object-method.nim
Normal file
3
Task/Call-an-object-method/Nim/call-an-object-method.nim
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var x = @[1, 2, 3]
|
||||
add(x, 4)
|
||||
x.add(5)
|
||||
|
|
@ -0,0 +1 @@
|
|||
1.2 sqrt
|
||||
|
|
@ -0,0 +1 @@
|
|||
Date now
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
new point { print() }
|
||||
Class Point
|
||||
x = 10 y = 20 z = 30
|
||||
func print see x + nl + y + nl + z + nl
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
o1 = new System.output.console
|
||||
o1.print("Hello World")
|
||||
|
||||
Package System.Output
|
||||
Class Console
|
||||
Func Print cText
|
||||
see cText + nl
|
||||
23
Task/Call-an-object-method/Sidef/call-an-object-method.sidef
Normal file
23
Task/Call-an-object-method/Sidef/call-an-object-method.sidef
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class MyClass {
|
||||
method foo(arg) { say arg }
|
||||
}
|
||||
|
||||
var arg = 42;
|
||||
|
||||
# Call a class method
|
||||
MyClass.foo(arg);
|
||||
|
||||
# Alternatively, using an expression for the method name
|
||||
MyClass.(:foo)(arg);
|
||||
|
||||
# Create an instance
|
||||
var instance = MyClass();
|
||||
|
||||
# Instance method
|
||||
instance.foo(arg);
|
||||
|
||||
# Alternatively, by using an expression for the method name
|
||||
instance.(:foo)(arg);
|
||||
|
||||
# Alternatively, by asking for a method
|
||||
instance.method(:foo)(arg);
|
||||
11
Task/Call-an-object-method/Swift/call-an-object-method.swift
Normal file
11
Task/Call-an-object-method/Swift/call-an-object-method.swift
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// Class
|
||||
MyClass.method(someParameter)
|
||||
// or equivalently:
|
||||
let foo = MyClass.self
|
||||
foo.method(someParameter)
|
||||
|
||||
// Instance
|
||||
myInstance.method(someParameter)
|
||||
|
||||
// Method with multiple arguments
|
||||
myInstance.method(red:arg1, green:arg2, blue:arg3)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# create an instance of the built-in file class
|
||||
decl file f
|
||||
|
||||
# call the file.open method
|
||||
f.open "filename.txt"
|
||||
23
Task/Call-an-object-method/XLISP/call-an-object-method.xlisp
Normal file
23
Task/Call-an-object-method/XLISP/call-an-object-method.xlisp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(DEFINE-CLASS MY-CLASS)
|
||||
|
||||
(DEFINE-CLASS-METHOD (MY-CLASS 'DO-SOMETHING-WITH SOME-PARAMETER)
|
||||
(DISPLAY "I am the class -- ")
|
||||
(DISPLAY SELF)
|
||||
(NEWLINE)
|
||||
(DISPLAY "You sent me the parameter ")
|
||||
(DISPLAY SOME-PARAMETER)
|
||||
(NEWLINE))
|
||||
|
||||
(DEFINE-METHOD (MY-CLASS 'DO-SOMETHING-WITH SOME-PARAMETER)
|
||||
(DISPLAY "I am an instance of the class -- ")
|
||||
(DISPLAY SELF)
|
||||
(NEWLINE)
|
||||
(DISPLAY "You sent me the parameter ")
|
||||
(DISPLAY SOME-PARAMETER)
|
||||
(NEWLINE))
|
||||
|
||||
(MY-CLASS 'DO-SOMETHING-WITH 'FOO)
|
||||
|
||||
(DEFINE MY-INSTANCE (MY-CLASS 'NEW))
|
||||
|
||||
(MY-INSTANCE 'DO-SOMETHING-WITH 'BAR)
|
||||
Loading…
Add table
Add a link
Reference in a new issue