summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-25 14:51:35 +0200
committerTyge Løvset <[email protected]>2022-09-25 15:09:23 +0200
commit2093b18689383286915df511167a12dc5d7bc75f (patch)
tree174779ae7e7b4f6d10ec8be300ea9574e2395729 /examples
parent756120e349a892ef1b7655fc777f28ec13845300 (diff)
downloadSTC-modified-2093b18689383286915df511167a12dc5d7bc75f.tar.gz
STC-modified-2093b18689383286915df511167a12dc5d7bc75f.zip
Added: crange number generator type. (similar to c++ std::iota). Fixed c_forfilter again. Cleanups. Docs added.
Diffstat (limited to 'examples')
-rw-r--r--examples/city.c8
-rw-r--r--examples/forfilter.c63
-rw-r--r--examples/new_deq.c2
-rw-r--r--examples/new_vec.c2
4 files changed, 49 insertions, 26 deletions
diff --git a/examples/city.c b/examples/city.c
index c98d3098..80bfbb12 100644
--- a/examples/city.c
+++ b/examples/city.c
@@ -52,10 +52,10 @@ int main(void)
c_auto (CityMap, map)
{
c_forlist (i, City, {
- {cstr_new("New York"), cstr_new("US"), 4.3, 23.2, 9000000},
- {cstr_new("Paris"), cstr_new("France"), 4.3, 23.2, 9000000},
- {cstr_new("Berlin"), cstr_new("Germany"), 4.3, 23.2, 9000000},
- {cstr_new("London"), cstr_new("UK"), 4.3, 23.2, 9000000},
+ {cstr_new("New York"), cstr_new("US"), 4.3f, 23.2f, 9000000},
+ {cstr_new("Paris"), cstr_new("France"), 4.3f, 23.2f, 9000000},
+ {cstr_new("Berlin"), cstr_new("Germany"), 4.3f, 23.2f, 9000000},
+ {cstr_new("London"), cstr_new("UK"), 4.3f, 23.2f, 9000000},
}) Cities_emplace(&cities, *i.ref);
copy = Cities_clone(cities); // share each element!
diff --git a/examples/forfilter.c b/examples/forfilter.c
index 46e59562..29128836 100644
--- a/examples/forfilter.c
+++ b/examples/forfilter.c
@@ -14,6 +14,7 @@
// filters and transforms:
#define flt_remove(i, x) (*i.ref != (x))
#define flt_even(i) ((*i.ref & 1) == 0)
+#define flt_odd(i) (*i.ref & 1)
#define trf_square(i) (*i.ref * *i.ref)
void demo1(void)
@@ -22,21 +23,24 @@ void demo1(void)
c_forlist (i, int, {0, 1, 2, 3, 4, 5, 80, 6, 7, 80, 8, 9, 80, 10, 11, 12, 13, 14, 15, 80, 16, 17})
IVec_push(&vec, *i.ref);
+ puts("demo1:");
c_forfilter (i, IVec, vec, flt_remove(i, 80))
printf(" %d", *i.ref);
puts("");
int res, sum = 0;
- c_forfilter (i, IVec, vec, c_flt_drop(i, 3)
- && flt_even(i)
- && flt_remove(i, 80)
- //&& c_flt_take(i, 5)
- , c_flt_take(i, 5)
+ c_forfilter (i, IVec, vec,
+ c_flt_dropwhile(i, *i.ref != 80)
+ && c_flt_drop(i, 1)
+ && c_flt_dropwhile(i, *i.ref != 80)
+ && flt_even(i)
+ && flt_remove(i, 80)
+ , c_flt_take(i, 5) // takewhile
){
sum += res = trf_square(i);
- printf(" %d:%d", *i.ref, res);
+ printf(" %d", res);
}
- printf("\nsum: %d\n", sum);
+ printf("\n sum: %d\n", sum);
}
}
@@ -55,11 +59,13 @@ fn main() {
void demo2(void)
{
c_auto (IVec, vector) {
- int n = 0;
- for (size_t x=1;; ++x)
- if (n == 5) break;
- else if (x & 1) ++n, IVec_push(&vector, x*x);
+ crange rv = crange_from(1);
+ c_forfilter (x, crange, rv,
+ flt_odd(x)
+ , c_flt_take(x, 5))
+ IVec_push(&vector, trf_square(x));
+ puts("demo2:");
c_foreach (i, IVec, vector) printf(" %d", *i.ref);
puts("");
}
@@ -89,6 +95,7 @@ void demo3(void)
csview_contains(*w.ref, c_sv("i")))
SVec_push(&words_containing_i, *w.ref);
+ puts("demo3:");
c_foreach (w, SVec, words_containing_i)
printf(" %.*s", c_ARGsv(*w.ref));
puts("");
@@ -97,18 +104,33 @@ void demo3(void)
void demo4(void)
{
- csview s = c_sv("ab123cReAghNGnΩoEp");
- cstr out = cstr_null;
-
- c_forfilter (i, csview, s, utf8_isupper(utf8_peek(i.ref))) {
- char buf[4];
- utf8_encode(buf, utf8_tolower(utf8_peek(i.ref)));
- cstr_push(&out, buf);
+ csview s = c_sv("ab123cReAghNGnΩoEp"); // Ω = multi-byte
+ c_auto (cstr, out) {
+ c_forfilter (i, csview, s, utf8_isupper(utf8_peek(i.ref))) {
+ char chr[4];
+ utf8_encode(chr, utf8_tolower(utf8_peek(i.ref)));
+ cstr_push(&out, chr);
+ }
+ puts("demo4:");
+ printf(" %s\n", cstr_str(&out));
}
-
- printf("%s\n", cstr_str(&out));
}
+void demo5(void)
+{
+ #define flt_even(i) ((*i.ref & 1) == 0)
+ #define flt_mid_decade(i) ((*i.ref % 10) != 0)
+ puts("demo5:");
+ crange r1 = crange_from(1963);
+ c_forfilter (i, crange, r1,
+ c_flt_drop(i,15)
+ && c_flt_dropwhile(i, flt_mid_decade(i))
+ && c_flt_drop(i,30)
+ && flt_even(i)
+ , c_flt_take(i,10))
+ printf(" %lld", *i.ref);
+ puts("");
+}
int main(void)
{
@@ -116,6 +138,7 @@ int main(void)
demo2();
demo3();
demo4();
+ demo5();
}
#include "../src/utf8code.c"
diff --git a/examples/new_deq.c b/examples/new_deq.c
index 5f7c357f..dd6f04c0 100644
--- a/examples/new_deq.c
+++ b/examples/new_deq.c
@@ -42,7 +42,7 @@ int main()
}
c_auto (cdeq_float, fvec)
{
- cdeq_float_push_back(&fvec, 123.3);
+ cdeq_float_push_back(&fvec, 123.3f);
}
c_auto (cdeq_pnt, pvec)
{
diff --git a/examples/new_vec.c b/examples/new_vec.c
index 9ae2ccf1..e0fa79ae 100644
--- a/examples/new_vec.c
+++ b/examples/new_vec.c
@@ -41,7 +41,7 @@ int main()
c_auto (cvec_str, svec)
{
cvec_i32_push_back(&vec, 123);
- cvec_float_push_back(&fvec, 123.3);
+ cvec_float_push_back(&fvec, 123.3f);
cvec_pnt_push_back(&pvec, (Point){42, 14});
cvec_pnt_push_back(&pvec, (Point){32, 94});