Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples to document features #305

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions examples/database.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import immutable/list

interface DataBase[R] {
//will save name to database
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//will save name to database
// will save name to database

why "name"?

def addEntry(entry: R): Unit

//returns list of currently saved names
def getEntries(): List[R]

//prints all names to console
def printEntries(): Unit
}

record Student(
firstName: String,
lastName: String,
id: Int
)



def myApplication(){ db: DataBase[Student] } = {
var currentId = 0;
def nextId():Int = {
val c = currentId;
currentId = currentId +1;
return c;
}
def newStudent(firstName: String, lastName: String): Student = {
Student(firstName, lastName, nextId());
}

db.addEntry(newStudent("Max","Marschall"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
db.addEntry(newStudent("Max","Marschall"));
db.addEntry(newStudent("Max", "Marschall"));

db.addEntry(newStudent("Peter","Lustig"));
db.addEntry(newStudent("Pierre","Drole"));
db.addEntry(newStudent("Airan","Sayüt"));

db.printEntries[Student]()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the type annotation necessary?

println("db current state:")
println(db.getEntries[Student]())
}

def makeNewDB(){ r: Region }:DataBase[Student] at { r } = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def makeNewDB(){ r: Region }:DataBase[Student] at { r } = {
def makeNewDB { r: Region }: DataBase[Student] at { r } = {

//create database instance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//create database instance
// create database instance

var names: List[Student] in r = [];
def firebase = new DataBase[Student] {

def addEntry(entry: Student) = {
names = Cons[Student](entry, names)
}

def getEntries() = {
return names;
}

def printEntries() = {
names.foreach(){ name => println(name); ()}
}
};
return firebase
}

def main() = {

//create database instance
var names: List[Student] = [];
var idCounter = 0;
def firebase = new DataBase[Student] {

def addEntry(entry: Student) = {
names = Cons[Student](entry, names)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, is the type annotation necessary?

}

def getEntries() = {
return names;
}
Comment on lines +74 to +76
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def getEntries() = {
return names;
}
def getEntries() = names


def printEntries() = {
names.foreach(){ name => println(name); ()}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
names.foreach(){ name => println(name); ()}
names.foreach { name => println(name) }

}
};

region r {
def fb2 = makeNewDB(){ r };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def fb2 = makeNewDB(){ r };
def fb2 = makeNewDB {r};

Typically we omit empty value sections

myApplication(){ fb2 }
}

//run application
}
5 changes: 5 additions & 0 deletions examples/timer.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def main() = {
var time = currentTimeNanos();
var timeMillis = time / 1000000;
println("time millis: " ++ show(timeMillis))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printing the time in a "test" is difficult because you don't know what the expected result will be

Loading