Πέμπτη 10 Αυγούστου 2023

Some examples from kotlinlang.org as M2000 programs

Asynchronous




Its not the same type under the hood, so we have to adjust it:
Using stack of values (each trhead has one)
module threads1 {
profiler
for i=1 to 10
thread {
print "i="; number, "time=";timecount
refresh
thread this erase
} as k interval 3000-i*200
thread k execute push i
next
wait 4000
}
threads1

Using static variables (different set for each thread)

module threads2 {
profiler
for i=1 to 10
thread {
print "i="; j, "time=";timecount
refresh
thread this erase
} as k interval 3000-i*200
thread k execute static j=i
next
wait 4000
}
threads2

Object-Oriented




module ObjectOriented {
class Person {
private:
name$
public:
module greed {
error "abstract"
}
class:
module Person (.name$){
}
}

class FoodConsumer {
module eat {
error "abstract"
}
module pay (amount as currency) {
print format$("Delicious! Here's {0} bucks!", amount)
}
}
class RestaurantCustomer as Person as FoodConsumer {
private:
dish$=""
public:
module order {
print format$("{0}, please!", .dish$)
}
module final eat {
print format$("*Eats {0}*", .dish$)
}
module final greed {
print format$("It's me, {0}", .name$)
}
class:
module RestaurantCustomer (.name$, .dish$) {
}
}
sam = RestaurantCustomer("Sam", "Mixed salad")
sam.greed
sam.order
sam.eat
sam.pay 10
}
ObjectOriented


Functional

The Kotlin Example



The M2000 example using tuple.

Module Functional {
function message(sender as string, body as string, isRead as boolean=false) {
=((Sender, body, isRead),) // array in an array - need for using append (an array to an array)
}
// define messages as a pointer to an array with zero items:
messages=(,)
Append messages, message("Ma", "Hey! Where are you?")
Append messages, message("Adam", "Everything going according to plan today?")
Append messages, message("Ma", "Please reply. I've lost you!")
// filter items by the Sender name to be unique
distinct =lambda ->{
mylist=list
=lambda myList ( k as array)->{
=true
if not exist(mylist, k#val$(0)) then append mylist, k#val$(0) else =false
}
}
// return the Sender with most messages on a fold function
MostMessages =lambda ->{
mylist=list
=lambda myList (k as array, which$)->{
if not exist(mylist, k#val$(0)) then
append mylist, k#val$(0):=1
else
return mylist, k#val$(0):=mylist(k#val$(0))+1
end if
if len(which$)>1 then
if mylist(which$)<mylist(k#val$(0)) then which$=k#val$(0)
else
which$=k#val$(0)
end if
push which$
}
}
// get an array and extract only the Sender
map1 =lambda (k as array)->{
push k#val$(0)
}
print "Sender with most messages:";messages#fold$(MostMessages(), "")
print "Senders:";"["+messages#filter(distinct(),(,))#map(map1)#Sort()#str$(", ")+"]"
}
Functional


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.