Skip to content

Instantly share code, notes, and snippets.

@omar-3
Last active April 10, 2020 06:45
Show Gist options
  • Save omar-3/95dcc3c719bcdba8864127e26a8537f1 to your computer and use it in GitHub Desktop.
Save omar-3/95dcc3c719bcdba8864127e26a8537f1 to your computer and use it in GitHub Desktop.
a very very short itertools-ish module
/* Support for functional programming capabilities in Chapel
Provides a different number of generators and iterators inspired
by Python, and other functional programming languages: APL, Haskell, Scala.
*/
module functional {
/* iterator that produces an evenly spaced values
starting with *start* and ends at *end*
with *step* spaces inclusively. Has the same functionality
as ranges.
:arg start: starting point of the iterator
:type start: int
:arg end: ending point of the iterator
:type end: int
:arg step: the space between every two conscutive value
:type step: int
*/
iter count(start: int, step: int, end: int) {
var n = start;
while 1 {
yield n;
n = n + step;
if n == end {
return;
}
}
}
/* iterator returning the first *number* of elements in the container.
:arg list: an iterable container like a list or a string
:arg type: [] T
:arg number: number of elements to be yielded, default is 5
:arg type: int
*/
iter firstElements(list, number=5) {
var iterable = list;
var iterations = number;
for i in iterable {
iterations = iterations - 1;
yield i;
if iterations == 0 {
return;
}
}
}
/* filtering a container by the truth value of *function*
:arg list: an iterable container
:arg type: [] T
:arg function: non generic boolean function
*/
iter filter(list, function) {
var iterable = list;
for i in iterable {
if function(i) {
yield i;
}
}
}
/* trying to concatenate iterable containers with + in Chapel, does element-wise
addition. *chain* function returns an iterable container produced by chaining
all the lists passed to the function
:arg lists: all the containers to be chained together
:arg type: [] T
*/
iter chain(lists...?numOflists) {
var iterables = lists;
var num = numOflists;
for i in 1..num {
var iterable = iterables(i);
for element in iterable {
yield element;
}
}
}
/* returns elements from *list* that have corresponding Truth value
in the *truthList*
:arg list: an iterable container
:arg type: [] T
:arg truthList: an iterable container, whose elements has boolean value
:arg type: preferly [] int
*/
iter compress(list, truthList) {
var iterable = list;
var truth = truthList;
for (i,j) in zip(list, truthList) {
if j {
yield i;
}
}
}
/* drops elements from *list* as long as the predicate *function*
is false, once the predicate evaluate to true, the function returns
every element.
:arg list: an iterable container
:arg type: [] T
:arg function: non generic boolean function
*/
iter dropwhile(list, function) {
var iterable = list;
var barrier = false;
for i in iterable {
if function(i){
barrier = true;
}
if barrier {
yield i;
}
}
}
}
@ben-albrecht
Copy link

@Omar659 - Mason packages can use the UnitTest testing system alternatively. UnitTest does not have a built-in mechanism for performance testing, but that shouldn't stop you from implementing them by adding timers to your tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment