"Pipes" for fast and simple functional programming ================================================== Functional programming is filled with non-intuitive terminology and performance becomes really bad when function are passed across modules (and cannot be inlined). "Pipes" are functions that take N elements and yield M output elements. This could be implemented internally with batch calls, so a such function might internally take an "input chunk" and an "output chunk", and get called repeatedly until there is no more input and there is free space in the output chunk. Definition syntax ----------------- The definition syntax could be very simple, just like a function except that it uses the "pipe" keyword: # ...but max(0, x) is a bit non-intuitive, because it # actually means that 0 is the minimum value! pipe max(pipe int a, pipe int b) -> int { if a > b return a else return b } pipe at_least(int x, pipe int b) -> int { if a > b return a else return b } pipe is_even(pipe int a) -> bool { return (a mod 2) == 0 } # filter(). but should it exist as a pipe, # or should it somehow be builtin? pipe where(pipe bool condition, pipe T value) -> T { if condition return value else noreturn # <--- clever "overload" of the noreturn keyword ;) } It would be nice if (some of) the following things could be merged (see unified_lists_arrays.txt): * arrays: int[...] * lists: List * pipes: pipe int - but unbounded, unlike a list. * iterators: Iter - perhaps pipes and iterators could be merged. Usage syntax 1 -------------- func example( List a, usize b_len, ref int[b_len] b) { # "map" (decide on a keyword) List even = a to is_even List even = a convert is_even List bounded = a ??? at_least(0) # "filter" List even = a where is_even # "reduce" int largest = a the(max) int largest = a ??? max int largest = a ??? add # "findAny" bool has_even = a contains is_even # "all" bool all_even = a all is_even } Usage syntax 2 -------------- func example( List a, usize b_len, ref int[b_len] b) { # "map" List even = .map(a, is_even) List even = .from_pipe(a, is_even) List bounded = .map(a, at_least(0)) ... } Usage syntax 3 -------------- func example( List a, usize b_len, ref int[b_len] b) { # "map" List even = a | is_even() List bounded = a | at_least(0) # "filter" ("where" keyword) List even = a | where is_even() # "findFirst" int firstEven = a | where is_even() | first() # "reduce" int largest = a | of_all max() int largest = a | of_all max() int largest = a | of_all add() # "exists" bool has_even = a | is_even() | exists() # "all" bool all_even = a | is_even() | all() }