new files

This commit is contained in:
Ingy döt Net 2013-04-10 12:38:42 -07:00
parent 3af7344581
commit 86c034bb8b
1364 changed files with 21352 additions and 0 deletions

View file

@ -0,0 +1,9 @@
package My_Package is
type My_Type is tagged private;
procedure Some_Procedure(Item : out My_Type);
function Set(Value : in Integer) return My_Type;
private
type My_Type is tagged record
Variable : Integer := -12;
end record;
end My_Package;

View file

@ -0,0 +1,13 @@
package body My_Package is
procedure Some_Procedure(Item : out My_Type) is
begin
Item := 2 * Item;
end Some_Procedure;
function Set(Value : Integer) return My_Type is
Temp : My_Type;
begin
Temp.Variable := Value;
return Temp;
end Set;
end My_Package;

View file

@ -0,0 +1,8 @@
with My_Package; use My_Package;
procedure Main is
Foo : My_Type; -- Foo is created and initialized to -12
begin
Some_Procedure(Foo); -- Foo is doubled
Foo := Set(2007); -- Foo.Variable is set to 2007
end Main;

View file

@ -0,0 +1,14 @@
:class MyClass <super Object
int memvar
:m ClassInit: ( -- )
ClassInit: super
1 to memvar ;m
:m ~: ( -- ) ." Final " show: [ Self ] ;m
:m set: ( n -- ) to memvar ;m
:m show: ( -- ) ." Memvar = " memvar . ;m
;class

View file

@ -0,0 +1,50 @@
package main
import "fmt"
// a basic "class."
// In quotes because Go does not use that term or have that exact concept.
// Go simply has types that can have methods.
type picnicBasket struct {
nServings int // "instance variables"
corkscrew bool
}
// a method (yes, Go uses the word method!)
func (b *picnicBasket) happy() bool {
return b.nServings > 1 && b.corkscrew
}
// a "constructor."
// Also in quotes as Go does not have that exact mechanism as part of the
// language. A common idiom however, is a function with the name new<Type>,
// that returns a new object of the type, fully initialized as needed and
// ready to use. It makes sense to use this kind of constructor function when
// non-trivial initialization is needed. In cases where the concise syntax
// shown is sufficient however, it is not idiomatic to define the function.
// Rather, code that needs a new object would simply contain &picnicBasket{...
func newPicnicBasket(nPeople int) *picnicBasket {
// arbitrary code to interpret arguments, check resources, etc.
// ...
// return data new object.
// this is the concise syntax. there are other ways of doing it.
return &picnicBasket{nPeople, nPeople > 0}
}
// how to instantiate it.
func main() {
var pb picnicBasket // create on stack (probably)
pbl := picnicBasket{} // equivalent to above
pbp := &picnicBasket{} // create on heap. pbp is pointer to object.
pbn := new(picnicBasket) // equivalent to above
forTwo := newPicnicBasket(2) // using constructor
// equivalent to above. field names, called keys, are optional.
forToo := &picnicBasket{nServings: 2, corkscrew: true}
fmt.Println(pb.nServings, pb.corkscrew)
fmt.Println(pbl.nServings, pbl.corkscrew)
fmt.Println(pbp)
fmt.Println(pbn)
fmt.Println(forTwo)
fmt.Println(forToo)
}

View file

@ -0,0 +1,33 @@
class Shape a where
perimeter :: a -> Double
area :: a -> Double
{- A type class Shape. Types belonging to Shape must support two
methods, perimeter and area. -}
data Rectangle = Rectangle Double Double
{- A new type with a single constructor. In the case of data types
which have only one constructor, we conventionally give the
constructor the same name as the type, though this isn't mandatory. -}
data Circle = Circle Double
instance Shape Rectangle where
perimeter (Rectangle width height) = 2 * width + 2 * height
area (Rectangle width height) = width * height
{- We made Rectangle an instance of the Shape class by
implementing perimeter, area :: Rectangle -> Int. -}
instance Shape Circle where
perimeter (Circle radius) = 2 * pi * radius
area (Circle radius) = pi * radius^2
apRatio :: Shape a => a -> Double
{- A simple polymorphic function. -}
apRatio shape = area shape / perimeter shape
main = do
print $ apRatio $ Circle 5
print $ apRatio $ Rectangle 5 5
{- The correct version of apRatio (and hence the correct
implementations of perimeter and area) is chosen based on the type
of the argument. -}

View file

@ -0,0 +1,19 @@
{
# a class is a package (i.e. a namespace) with methods in it
package MyClass;
# a constructor is a function that returns a blessed reference
sub new {
my $class = shift;
bless {variable => 0}, $class;
# the instance object is a hashref in disguise.
# (it can be a ref to anything.)
}
# an instance method is a function that takes an object as first argument.
# the -> invocation syntax takes care of that nicely, see Usage paragraph below.
sub some_method {
my $self = shift;
$self->{variable} = 1;
}
}

View file

@ -0,0 +1,38 @@
class MyClass:
name2 = 2 # Class attribute
def __init__(self):
"""
Constructor (Technically an initializer rather than a true "constructor")
"""
self.name1 = 0 # Instance attribute
def someMethod(self):
"""
Method
"""
self.name1 = 1
MyClass.name2 = 3
myclass = MyClass() # class name, invoked as a function is the constructor syntax.
class MyOtherClass:
count = 0 # Population of "MyOtherClass" objects
def __init__(self, name, gender="Male", age=None):
"""
One initializer required, others are optional (with different defaults)
"""
MyOtherClass.count += 1
self.name = name
self.gender = gender
if age is not None:
self.age = age
def __del__(self):
MyOtherClass.count -= 1
person1 = MyOtherClass("John")
print person1.name, person1.gender # "John Male"
print person1.age # Raises AttributeError exception!
person2 = MyOtherClass("Jane", "Female", 23)
print person2.name, person2.gender, person2.age # "Jane Female 23"

View file

@ -0,0 +1,13 @@
#You define a class simply by setting the class attribute of an object
circS3 <- list(radius=5.5, centre=c(3, 4.2))
class(circS3) <- "circle"
#plot is a generic function, so we can define a class specific method by naming it plot.classname
plot.circle <- function(x, ...)
{
t <- seq(0, 2*pi, length.out=200)
plot(x$centre[1] + x$radius*cos(t),
x$centre[2] + x$radius*sin(t),
type="l", ...)
}
plot(circS3)

View file

@ -0,0 +1,21 @@
class CLASSTEST is
readonly attr x:INT; -- give a public getter, not a setter
private attr y:INT; -- no getter, no setter
attr z:INT; -- getter and setter
-- constructor
create(x, y, z:INT):CLASSTEST is
res :CLASSTEST := new; -- or res ::= new
res.x := x;
res.y := y;
res.z := z;
return res;
end;
-- a getter for the private y summed to s
getPrivateY(s:INT):INT is
-- y is not shadowed so we can write y instead of
-- self.y
return y + s;
end;
end;