langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,4 @@
module type Eatable = sig
type t
val eat : t -> unit
end

View file

@ -0,0 +1,5 @@
module MakeFoodBox(A : Eatable) = struct
type elt = A.t
type t = F of elt list
let make_box_from_list xs = F xs
end

View file

@ -0,0 +1,6 @@
type banana = Foo (* a dummy type *)
module Banana : Eatable with type t = banana = struct
type t = banana
let eat _ = print_endline "I'm eating a banana"
end

View file

@ -0,0 +1,4 @@
module EatFloat : Eatable with type t = float = struct
type t = float
let eat f = Printf.printf "I'm eating %f\n%!" f
end

View file

@ -0,0 +1,5 @@
module BananaBox = MakeFoodBox (Banana)
module FloatBox = MakeFoodBox (EatFloat)
let my_box = BananaBox.make_box_from_list [Foo]
let your_box = FloatBox.make_box_from_list [2.3; 4.5]

View file

@ -0,0 +1,85 @@
macro Gluttony(vartype, capacity, foodlist)
'==========================================
typedef vartype physical
enum food foodlist
type ActualFood
sys name
physical size
physical quantity
end type
Class foodbox
'============
has ActualFood Item[capacity]
sys max
method put(sys f, physical s,q)
max++
Item[max]<=f,s,q
end method
method GetNext(ActualFood *Stuff)
if max then
copy @stuff,@Item[max], sizeof Item
max--
end if
end method
end class
Class Gourmand
'=============
physical WeightGain, SleepTime
method eats(ActualFood *stuff)
WeightGain+=stuff.size*stuff.quantity*0.75
stuff.size=0
stuff.quantity=0
end method
end class
end macro
'IMPLEMENTATION
'==============
Gluttony (
double,100,{
oyster,trout,bloater,
chocolate,truffles,
cheesecake,cream,pudding,pie
})
% small 1
% medium 2
% large 3
% huge 7
% none 0
% single 1
% few 3
% several 7
% many 12
'INSTANCE
'========
FoodBox Hamper
Gourmand MrG
'TEST
'====
Hamper.put food.pudding,large,several
Hamper.put food.pie,huge,few
ActualFood Course
Hamper.GetNext Course
MrG.eats Course
print MrG.WeightGain 'result 15.75

View file

@ -0,0 +1,13 @@
subset Eatable of Any where { .^can('eat') };
class Cake { method eat() {...} }
role FoodBox[Eatable ::T] {
has T %.foodbox;
}
class Yummy does FoodBox[Cake] { } # composes correctly
# class Yucky does FoodBox[Int] { } # fails to compose
my Yummy $foodbox .= new;
say $foodbox.perl;

View file

@ -0,0 +1 @@
Yummy.new(foodbox => {})