-
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 all 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,10 @@ | ||
| module MutBox | ||
|
|
||
| local class MutBox[t] | ||
|
|
||
| var value : t | ||
|
|
||
| def init( v : t) : unit | ||
| this.value = v | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module Functor | ||
| import Collections.Mutable.Collection | ||
|
|
||
| local trait Functor[t] | ||
| require def map[u](f : local ((t) -> u)) : Functor[u] | ||
| require def flatMap[u](f : local ((t) -> Functor[u])) : Collection[u] | ||
| require def foreach(f : local ((t) -> unit)) : unit | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ module Array | |
|
|
||
| -- new_with_default :: (int, a) -> [a] | ||
| -- new_with_default(size, default) creates an array of size default | ||
| -- with each element | ||
| -- with each element | ||
| fun new_with_default[a](size : int, default : a) : [a] | ||
| val arr = new [a](size) | ||
| for i <- [0 .. size - 1] do | ||
|
|
@@ -72,8 +72,8 @@ end | |
| -- count(pred, arr) counts the number of elements of arr satisfying predicate pred | ||
| fun count[a](pred : a -> bool, arr : [a]) : int | ||
| var count = 0 | ||
| for a <- arr do | ||
| if pred(a) then | ||
| for x <- arr do | ||
| if pred(x) then | ||
| count += 1 | ||
| end | ||
| end | ||
|
|
@@ -83,14 +83,76 @@ end | |
| -- map :: (a -> b, [a]) -> [b] | ||
| -- map(f, arr) produces a new array containing the results of applying f to the | ||
| -- elements of arr | ||
| fun map[a,b](f : a -> b, arr : [a]) : [b] | ||
| fun map[a,b](f : local ((a) -> b), arr : [a]) : [b] | ||
| val ret = new [b](|arr|) | ||
| for i <- [0 .. |arr|-1] 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. can you make this work with for-comprehensions?
Author
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 would rather avoid using for-comprehension inside of map, flatMap and foreach, due to the potential endless compilation loop it could result in. Unless I used it with a range or some other kind of data strucutre. But even then, for performance sake, I would rather use a while, or repeat loop inside of these methods. |
||
| var i = 0 | ||
| while (i <= |arr|-1) do | ||
| ret(i) = f((arr)(i)) | ||
| i += 1 | ||
| end | ||
| ret | ||
| end | ||
|
|
||
| -- flatMap :: (a -> [b], [a]) -> [b] | ||
| -- flatMap(f, arr) produces a new array containeing the flatened results of applying f to | ||
| -- the elements of arr | ||
| fun flatMap[a, b](f : local ((a) -> [b]), arr : [a]) : [b] | ||
| val size = |arr| | ||
| var result = new [b](0) | ||
| var i = 0 | ||
|
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. can you make this work with for-comprehensions? |
||
| while (i < size) do | ||
| var ret = f(arr(i)) | ||
| result = concat[b](result, ret) | ||
| i += 1 | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| -- foreach(a -> unit, [a]) -> unit | ||
| -- forach(f, arr) applied f to all elements of an array, resulting in unit. | ||
| fun foreach[a](f : local ((a) -> unit), arr : [a]) : unit | ||
| val size = |arr| | ||
| var i = 0 | ||
| while i < size do | ||
| f(arr(i)) | ||
| i += 1 | ||
| end | ||
| end | ||
|
|
||
|
|
||
| fun maybeForeach[a](f : local ((a) -> Maybe[unit]), arr : [a]) : Maybe[unit] | ||
| val size = |arr| | ||
| var i = 0 | ||
| while i < size do | ||
| var res = f(arr(i)) | ||
| if res == Nothing then | ||
| return Nothing | ||
| end | ||
| i += 1 | ||
| end | ||
| return Just(()) | ||
| end | ||
|
|
||
| -- concat :: ([a], [a]) -> [a] | ||
| -- concat(firstArr, secondArr) produces a new array that is the concatination of | ||
| -- the two input arrays. | ||
| fun concat[a](firstArr : [a], secondArr : [a]) : [a] | ||
| val firstSize = |firstArr| | ||
| val secondSize = |secondArr| | ||
| val result = new [a](firstSize + secondSize) | ||
| var i = 0 | ||
| while i < firstSize do | ||
| result(i) = firstArr(i) | ||
| i += 1 | ||
| end | ||
| var j = 0 | ||
| while j < secondSize do | ||
| result(j+firstSize) = secondArr(j) | ||
| j += 1 | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| -- show :: (a -> unit, [a]) -> unit | ||
| -- show(showEl, arr) prints out array arr using function showEl to print the elements | ||
| -- of the array | ||
|
|
@@ -123,7 +185,7 @@ fun contains[t](arr : [t], to_find : t) : bool | |
| end | ||
|
|
||
| -- contains_str :: ([String], String) -> bool | ||
| -- contains_str(arr, elem) is true if and only if elem appears in arr | ||
| -- contains_str(arr, elem) is true if and only if elem appears in arr | ||
| -- tested using String.compare | ||
| fun contains_str(arr : [String], to_find : String) : bool | ||
| var retval = false | ||
|
|
@@ -147,7 +209,7 @@ fun clone[t](src : [t]) : [t] | |
| end | ||
|
|
||
| -- nclone :: ([t], int) -> Maybe[[t]] | ||
| -- nclone(arr,n) results in Just(arr') where arr' is a new array containing | ||
| -- nclone(arr,n) results in Just(arr') where arr' is a new array containing | ||
| -- the first n elements of arr, in the case where n < |arr|, otherwise Nothing | ||
| -- a new array containing the same contents as arr | ||
| fun nclone[t](src : [t], n : uint) : Maybe[[t]] | ||
|
|
@@ -160,5 +222,4 @@ fun nclone[t](src : [t], n : uint) : Maybe[[t]] | |
| end | ||
| Just(new_arr) | ||
| end | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| module RRange | ||
|
|
||
| read class RRange | ||
| val start : int | ||
| val stop : int | ||
| val step : int | ||
|
|
||
| def init(start : int, stop : int, step : int) : unit | ||
| this.start = start | ||
| this.stop = stop | ||
| this.step = step | ||
| end | ||
|
|
||
| def foreach(f : local ((int) -> unit)) : unit | ||
| var current = this.start | ||
| while (current <= this.stop) do | ||
| f(current) | ||
| current += this.step | ||
| end | ||
| end | ||
|
|
||
| def maybeForeach(f : local ((int) -> Maybe[unit])) : Maybe[unit] | ||
| var current = this.start | ||
| while (current <= this.stop) do | ||
| var ret = f(current) | ||
| if ret == Nothing then | ||
| return Nothing | ||
| end | ||
| current += this.step | ||
| end | ||
| return Just(()) | ||
| end | ||
| 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.
A functor, as defined in Haskell, is only your
mapoperation.We are not using Haskell concepts, though, so feel free to keep it as is.
This is just a FYI :)