-
Notifications
You must be signed in to change notification settings - Fork 24
For-Comprehension #879
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
base: development
Are you sure you want to change the base?
For-Comprehension #879
Changes from 13 commits
2895d7c
957edcc
ce50299
97e2549
61c15fa
979bcb6
8b61939
067051d
4cb0e56
17a835d
8a365c9
c5204b8
0c100a5
e2b1f0b
af9141b
17f1b6b
499d158
ddce7ab
8a3c951
5dbc4a9
ba10b39
308149e
740946b
2c13220
9edd6c4
b475d04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| The first slide after introduction: Have a picture? | ||
|
|
||
| One of the first slidess: Align "::" | ||
|
|
||
| gotward delegation: picture with printers again? 19 | ||
|
|
||
| 26: Make the chosen example bigger on like the next slide? Hard to see. Especially when using cursive:make bigger or bold or something | ||
| 27: --------""--------- | ||
| 28: Make the examples bigger. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| typeCheckSource sourceType fors@(ForSource{fsTy, collection}) = do | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is Haskell but uses the Encore file extension. |
||
| collectionTyped <- doTypecheck collection | ||
| let collectionType = AST.getType collectionTyped | ||
| formalType <- findFormalRefType collectionType | ||
| let mtyType = return $ getInnerType collectionType | ||
| unless (formalType == sourceType) $ | ||
| pushError collection $ TypeMismatchError formalType sourceType | ||
| return fors{fsTy = mtyType | ||
| ,collection = setType collectionType collectionTyped} | ||
|
|
||
| firstSourceType ForSource{fsTy, collection} = do | ||
| collectionTyped <- doTypecheck collection | ||
| let collectionType = AST.getType collectionTyped | ||
| formal <- findFormalRefType collectionType | ||
| return formal | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| active class Main | ||
| def main() : unit | ||
| var range = new Range(1, 10, 2) | ||
| range.foreach(print_index) | ||
| end | ||
| end | ||
|
|
||
| fun print_index(i: int) : unit | ||
| print("{ }", i) | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --These are preliminary for-comprehension tests | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encore does not yet return multiple errors. |
||
| -- The following assumptions have been made: | ||
| -- 1. It is possible to loop over empty structures | ||
| -- which either does nothing, | ||
| -- or returns a new struct that is empty. | ||
| -- 2. It is possible to loop over different kinds of structures | ||
| -- If using for as a function call, it will return | ||
| -- a similar struct as the first one listed, but perhaps | ||
| -- containing elements of different type. | ||
| -- 3. Looping over multiple structures is done in a nested may: | ||
| -- for x <- [1, 2], y <- [3, 4] | ||
| -- would loop over all the y elements for every x element | ||
| -- 4. Mutable variables can be changed inside a for-loop | ||
| -- 5. To use for over a structure, it must only allow one type of element | ||
| -- i.e. be homogenous, thus for example, it cannot iterate over a tuple | ||
| -- 6. The following tests assume there is a simple Container LinkedList which | ||
| -- has support for map and flatMap. | ||
|
|
||
| --None of these examples should compile at all | ||
|
|
||
| active class Main | ||
|
|
||
| def main() : unit | ||
|
|
||
| -- indentation error | ||
| for x <- [1, 2, 3] do | ||
| end | ||
|
|
||
| -- "<- list" missing | ||
| for x do | ||
| x + 1 | ||
| end | ||
|
|
||
| -- "<-" missing | ||
| for x = [1, 2, 3] do | ||
| x + 1 | ||
| end | ||
|
|
||
| -- [] is not allowed in Encore | ||
| for x <- [] do | ||
| x + 1 | ||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| active class Equal | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure on what is this file testing. |
||
|
|
||
| var num : int | ||
|
|
||
| def init() : unit | ||
| this.num = 3 | ||
| end | ||
|
|
||
| def isEqual(num : int) : bool | ||
| return (this.num == num) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| active class Main | ||
|
|
||
| def main(args : [String]) : unit | ||
|
|
||
| var n = match (args(1)).to_int() with | ||
| case Just(res) => | ||
| res | ||
| end | ||
| case Nothing => | ||
| 0 | ||
| end | ||
| end | ||
| var instance = new Equal() | ||
| var b = instance!isEqual(n) | ||
| print("They are equal? {}\n", get(b)) | ||
|
|
||
| if ([1, 2, 3].isEqual([1, 2, 3])) then | ||
| print("It does not work!") | ||
| end | ||
|
|
||
| end | ||
| end | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import Collections.Mutable.LinkedList | ||
|
|
||
| fun accum(mun : int) : int | ||
| mun + 1 | ||
| end | ||
|
|
||
| fun useList(list : LinkedList[(int, String)]) : unit | ||
| for elem <- list do | ||
| print("{}", elem) | ||
| end | ||
| end | ||
|
|
||
| active class Main | ||
| def main() : unit | ||
|
|
||
| var list = new LinkedList[int]() | ||
| list.append(1) | ||
| list.append(2) | ||
| list.append(3) | ||
|
|
||
| var list2 = new LinkedList[String]() | ||
| list2.append("katt") | ||
| list2.append("katt") | ||
| list2.append("katt") | ||
|
|
||
| var acc = 0 | ||
| useList(for elem <- list, el <- list2 do | ||
| (elem, el) | ||
| end) | ||
|
|
||
|
|
||
| {-var list = new LinkedList[int]() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is these lines are not tested, please remove them |
||
| list.append(1) | ||
| list.append(2) | ||
| list.append(3) | ||
|
|
||
| var list2 = new LinkedList[int] | ||
| list2.append(5) | ||
| list2.append(6) | ||
|
|
||
| var list3 = for x <- list, y <- list2 do | ||
| x + y | ||
| end | ||
|
|
||
| for x <- list do | ||
| print("{}\n", x) | ||
| end | ||
|
|
||
| for e <- list3 do | ||
| print("{}", e) | ||
| end-} | ||
|
|
||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| active class TryInt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you using this file for anything? |
||
|
|
||
| var num : Integer | ||
|
|
||
| def init() : unit | ||
| this.num = 3 | ||
| end | ||
|
|
||
| def add(num : Integer) : unit | ||
| this.num.add(num) | ||
| print("New value: {}", this.num) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| active class Main | ||
|
|
||
| def main(args : [String]) : unit | ||
|
|
||
| var n = 3 : Integer | ||
| var instance = new TryInt() | ||
| instance!add(n) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import Collections.Mutable.LinkedList | ||
|
|
||
| active class Main | ||
| def main() : unit | ||
| var list = new LinkedList() | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module Functor | ||
|
|
||
| local trait Functor[t] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A functor, as defined in Haskell, is only your |
||
| require def map[u](f : t -> u) : Functor[u] | ||
| require def flatMap[u](f : t -> Functor[u]) : Functor[u] | ||
| require def foreach(f : local ((t) -> unit)) : unit | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong file? This file is not needed here.