SugarCpp
SugarCpp copied to clipboard
introducing python-style list generation, and more
how about adding something like:
// list generation
list := (i * i for i <- 1 to 10)
printf("%d\n", i) for i <- list
// NASTY case, complicated one
// note: two i's should be distinguished by compiler
printf("%d\n", i) for i <- (i * i if i % 3 is 0 for i <- 1 to 100, i & 1 is 0)
// or if two types of `if` reduced the consistency, you just specify that, `if` prior to `for` is not valid.
printf("%d\n", i) for i <- (i * i for i <- 1 to 100, i & 1 is 0, i % 3 == 0)
func = (a, b) -> a + b
// or even more complicated but expressive grammar:
printf("%d\n", i) for i <- (((x) -> x * x * func(x * x, x / 2))(i) for i <- 1 to 100, i & 1 is 0, (i * i + i) % 3 == 0, func(i, i / 2) % 2 == 1)
I think maybe what you want is something like "map" in functional programming. I introduced a new syntax "=>" during for loop. See examples below.
Hint: Although SugarCpp support mixed usage of for and if, I suggest use just one for. Because this can both improve the fluency of coding and make code more readable.
import "iostream"
"cstdio"
"vector"
"cstdlib"
using namespace std
int main()
// due to the return value type is unknown
// list generation maybe difficult
// This is an alternative
list: vector<int>
list.push_back(i) for i <- 1 to 10
// i => i * i
// means map i into i*i
printf("%d\n", i) for i <- 1 to 100, (i & 1) is 0, i % 3 == 0, i => i * i
func := (a: int, b: int) -> a + b
printf("%d\n", i) for i <- 1 to 100, (i & 1) is 0, (i * i + i) % 3 == 0, func(i, i / 2) % 2 == 1, i => ((x:int) -> x * x * func(x * x, x / 2))(i)
// notice the type of i can be also changed
printf("%d\n", i) for i <- ["12", "21"], i => atoi(i), i => i * 2
Your idea seems good in my point of view.
I love the substitution chain in printf("%d\n", i) for i <- ["12", "21"], i => atoi(i), i => i * 2
Type in list generation is still a problem: in list := i * i for i <- 1 to 10
, list`` can be either a
int *or a
vector
I add keyword ':' after "list generation" to specify the type of list generation.
Type supports:
vector
list
forward_list
queue
deque
priority_queue
stack
set
multiset
unordered_set
unordered_multiset
map
multimap
unordered_map
unordered_multimap
But some types are not supported now:
array
bitset
int*
import "iostream"
"cstdio"
"vector"
"list"
"forward_list"
"queue"
"stack"
"set"
"unordered_set"
"map"
"unordered_map"
"tuple"
using namespace std
int main()
a := [i for i <- 1 to 10] : vector<int>
b := [i for i <- 1 to 10] : list<int>
c := [i for i <- 1 to 10] : forward_list<int>
d := [i for i <- 1 to 10] : queue<int>
e := [i for i <- 1 to 10] : deque<int>
f := [i for i <- 1 to 10] : priority_queue<int>
g := [i for i <- 1 to 10] : stack<int>
h := [i for i <- 1 to 10] : set<int>
i := [i for i <- 1 to 10] : multiset<int>
j := [i for i <- 1 to 10] : unordered_set<int>
k := [i for i <- 1 to 10] : unordered_multiset<int>
l := [[i, i*i] for i <- 1 to 10] : map<int, int>
m := [[i, i*i] for i <- 1 to 10] : multimap<int, int>
n := [[i, i*i] for i <- 1 to 10] : unordered_map<int, int>
o := [[i, i*i] for i <- 1 to 10] : unordered_multimap<int, int>
// combine list generation with for loop
printf("%d\n", i) for i <- [i * i for i <- 1 to 100, (i & 1) is 0, (i % 3) is 0] : vector<int>
It seems that I can add more syntax like OrderBy
Distinct
GroupBy
Foldl
/Flodr
in list generations....
Oh great! That's pretty cool, I can not wait to give it a try!
I have pulled to up-to-date version, but it does not work....
What is your input?