Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Abstract-type/Mathematica/abstract-type.math
Normal file
26
Task/Abstract-type/Mathematica/abstract-type.math
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(* Define an interface, Foo, which requires that the functions Foo, Bar, and Baz be defined *)
|
||||
InterfaceFooQ[obj_] := ValueQ[Foo[obj]] && ValueQ[Bar[obj]] && ValueQ[Baz[obj]];
|
||||
PrintFoo[obj_] := Print["Object ", obj, " does not implement interface Foo."];
|
||||
PrintFoo[obj_?InterfaceFooQ] := Print[
|
||||
"Foo: ", Foo[obj], "\n",
|
||||
"Bar: ", Bar[obj], "\n",
|
||||
"Baz: ", Baz[obj], "\n"];
|
||||
|
||||
(* Extend all integers with Interface Foo *)
|
||||
Foo[x_Integer] := Mod[x, 2];
|
||||
Bar[x_Integer] := Mod[x, 3];
|
||||
Baz[x_Integer] := Mod[x, 5];
|
||||
|
||||
(* Extend a particular string with Interface Foo *)
|
||||
Foo["Qux"] = "foo";
|
||||
Bar["Qux"] = "bar";
|
||||
Baz["Qux"] = "baz";
|
||||
|
||||
(* Print a non-interface object *)
|
||||
PrintFoo[{"Some", "List"}];
|
||||
(* And for an integer *)
|
||||
PrintFoo[8];
|
||||
(* And for the specific string *)
|
||||
PrintFoo["Qux"];
|
||||
(* And finally a non-specific string *)
|
||||
PrintFoo["foobarbaz"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue