Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue