25
loading...
This website collects cookies to deliver better user experience
with Ada.Text_IO; use Ada.Text_IO;
procedure Drinking_Venezuelan_Coffee is
type Level_Type is range 0 .. 100;
type Coffee_Origin_Type is (Venezuela, Kenya, Rwanda, Sumatra);
type Coffee_Mug is record
Level : Level_Type;
Coffee_Origin : Coffee_Origin_Type;
end record;
procedure Fill_Mug (M : in out Coffee_Mug) is
begin
case M.Level is
when 0 .. 99 =>
M.Level := Level_Type'Last;
Put_line ("Mug filled!");
when others =>
Put_Line ("Mug is already full!");
end case;
end Fill_Mug;
My_Mug : Coffee_Mug := (Level => 0, Coffee_Origin => Venezuela);
begin
Fill_Mug (My_Mug);
end Drinking_Venezuelan_Coffee;
actor Customer
let name: String
var _coffee: String = ""
new create(name': String) =>
name = name'
be ask_for_coffee(b: Barista) =>
b.prepare_coffee(this, name)
be receive_coffee (coffee': String) =>
_coffee = coffee'
actor Barista
let available_coffee: String
let _out: OutStream
new create(available_coffee': String, out: OutStream) =>
available_coffee = available_coffee'
_out = out
be prepare_coffee(c: Customer, name: String) =>
_out.print("One " + available_coffee + " coffee for " + name)
c.receive_coffee(available_coffee)
actor Main
new create(env: Env) =>
var barista = Barista("Kenyan", env.out)
var customer = Customer("Jane")
customer.ask_for_coffee(barista)
let workingHours = 8;
let workStartsAt = 9;
let myPreferredDrink = "Rwandan coffee";
let translateTime = (hour) =>
hour < 13
? string_of_int(hour) ++ "AM"
: string_of_int(hour - 12) ++ "PM";
let whatToDo = (firstHour, currentHour, drink) => {
let timeOfDay = firstHour + currentHour;
let atTime = (predicate) =>
"At " ++ translateTime(timeOfDay) ++ predicate ++ "!";
switch (timeOfDay, timeOfDay mod 2) {
| (_, 0) => atTime(" drink more " ++ drink)
| (13, _) => atTime(": Lunch break")
| _ => atTime(" drink " ++ drink)
};
};
for (hour in 0 to workingHours) {
Js.log(whatToDo(workStartsAt, hour, myPreferredDrink))
};
concat
function, which would be typed something like List n t -> List m t -> List (n + m) t
. The first argument will be a list of n
elements, the second a list of m
elements and the result will be a list with n + m
elements. How cool is that?! With dependent types, you gain a whole new level of control over what to expect from your code. In my book, that’s always good.isSumatranCoffee : String -> Bool
isSumatranCoffee "Sumatran" = True
isSumatranCoffee _ = False
drinkOrPass : String -> String
drinkOrPass coffee = case isSumatranCoffee coffee of
True => "I'll have some of that Sumatran coffee, please!"
False => "No thanks, I’d rather have something else"
main : IO ()
main = putStrLn (drinkOrPass "Sumatran")
(which is a cool language (based on Lisp (which means (it uses lots of parentheses))))
.25