Multiton: Difference between revisions
Content added Content deleted
(added Perl programming solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 17: | Line 17: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
A registry (just in memory, not on disk) is used below instead of an enum. The registry is protected by a lock on the Multiton constructor, to prevent two threads creating the same object at the same time. |
A registry (just in memory, not on disk) is used below instead of an enum. The registry is protected by a lock on the Multiton constructor, to prevent two threads creating the same object at the same time. |
||
< |
<syntaxhighlight lang="julia">struct Multiton{T} |
||
data::T |
data::T |
||
function Multiton(registry, refnum, data) |
function Multiton(registry, refnum, data) |
||
Line 64: | Line 64: | ||
# produce error |
# produce error |
||
m5 = Multiton(reg, 5) |
m5 = Multiton(reg, 5) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Multiton is Multiton{String}("zero") |
Multiton is Multiton{String}("zero") |
||
Line 75: | Line 75: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl"># 20211215 Perl programming solution |
||
use strict; |
use strict; |
||
Line 111: | Line 111: | ||
if ($inst0 eq $inst2) { print "Instance0 and Instance2 share the same object\n" }; |
if ($inst0 eq $inst2) { print "Instance0 and Instance2 share the same object\n" }; |
||
if ($inst0 eq $inst3) { print "Instance0 and Instance3 share the same object\n" }; |
if ($inst0 eq $inst3) { print "Instance0 and Instance3 share the same object\n" }; |
||
if ($inst3 eq $inst4) { print "Instance3 and Instance4 share the same object\n" };</ |
if ($inst3 eq $inst4) { print "Instance3 and Instance4 share the same object\n" };</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>We create several instances and compare them to see if multiton is in effect. |
<pre>We create several instances and compare them to see if multiton is in effect. |
||
Line 133: | Line 133: | ||
Put this (up to "end class") in a separate file, to keep allowed and instances private. Classes are not [yet] supported under pwa/p2js.<br> |
Put this (up to "end class") in a separate file, to keep allowed and instances private. Classes are not [yet] supported under pwa/p2js.<br> |
||
Technically I suppose this should really use new_dict()/getd()/setd(), but I'm quite sure you'll cope somehow.. |
Technically I suppose this should really use new_dict()/getd()/setd(), but I'm quite sure you'll cope somehow.. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">allowed</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"zero"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two"</span><span style="color: #0000FF;">},</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">allowed</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"zero"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two"</span><span style="color: #0000FF;">},</span> |
||
Line 166: | Line 166: | ||
<span style="color: #0000FF;">?</span><span style="color: #000000;">e</span><span style="color: #0000FF;">.</span><span style="color: #000000;">id</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">e</span><span style="color: #0000FF;">.</span><span style="color: #000000;">id</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #000000;">f</span><span style="color: #0000FF;">.</span><span style="color: #000000;">id</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">f</span><span style="color: #0000FF;">.</span><span style="color: #000000;">id</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 179: | Line 179: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Tried to translate [https://wikipedia.org/wiki/Multiton_pattern#Implementations the C# example] at WP but not sure if my interpretation/implementation is correct |
Tried to translate [https://wikipedia.org/wiki/Multiton_pattern#Implementations the C# example] at WP but not sure if my interpretation/implementation is correct |
||
<lang |
<syntaxhighlight lang="raku" line># 20211001 Raku programming solution |
||
enum MultitonType < Gold Silver Bronze >; |
enum MultitonType < Gold Silver Bronze >; |
||
Line 200: | Line 200: | ||
} |
} |
||
); |
); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 220: | Line 220: | ||
Although all Wren code runs within the context of a fiber (of which there can be thousands) only one fiber can run at a time and so the language is effectively single threaded. Thread-safety is therefore never an issue. |
Although all Wren code runs within the context of a fiber (of which there can be thousands) only one fiber can run at a time and so the language is effectively single threaded. Thread-safety is therefore never an issue. |
||
< |
<syntaxhighlight lang="ecmascript">import "/dynamic" for Enum |
||
var MultitonType = Enum.create("MultitonType", ["zero", "one", "two"]) |
var MultitonType = Enum.create("MultitonType", ["zero", "one", "two"]) |
||
Line 252: | Line 252: | ||
System.print(m2) |
System.print(m2) |
||
var m3 = Multiton.getInstance(3) // produces an error</ |
var m3 = Multiton.getInstance(3) // produces an error</syntaxhighlight> |
||
{{out}} |
{{out}} |