All Downloads are FREE. Search and download functionalities are using the official Maven repository.

e.aviator.aviator.5.3.0-beta.source-code.aviator.av Maven / Gradle / Ivy

## Author:       dennis
## Email:        [email protected]
## License:      Apache License 2.0
## Description:  more seuence functions implemented in aviatorscript

fn group_by(seq, f) {
  let m = seq.map();

  for e in seq {
    let k = f(e);
    let l = seq.get(m, k);
    if l == nil {
      l = seq.list();
      seq.put(m, k, l);
    }
    seq.add(l, e);
  }

  return m;
}

fn distinct(seq) {
  if seq == nil {
    return nil;
  }

  let seen = seq.set();
  let result = seq.collector(seq);
  for x in seq {
    if !include(seen, x) {
      seq.add(result, x);
      seq.add(seen, x);
    }
  }

  return seq.raw(result);
}

fn is_distinct(seq) {
  if seq == nil {
    return true;
  }

  let seen = seq.set();
  for x in seq {
    if !include(seen, x) {
      seq.add(seen, x);
    } else {
      return false;
    }
  }

  return true;
}

fn repeat(n, x) {
  let result = seq.list();

  for i in range(0, n) {
    seq.add(result, x);
  }

  return result;
}

fn repeatedly(n, f) {
  let result = seq.list();

  for i in range(0, n) {
    seq.add(result, f());
  }

  return result;
}

fn take_while(seq, pred) {
  if seq == nil {
    return nil;
  }

  let result = seq.collector(seq);

  for x in seq {
    if pred(x) {
      seq.add(result, x);
    }
  }

  return seq.raw(result);
}

fn drop_while(seq, pred) {
  if seq == nil {
    return nil;
  }

  let result = seq.collector(seq);

  for x in seq {
    if !pred(x) {
      seq.add(result, x);
    }
  }

  return seq.raw(result);
}

fn concat(seq1, seq2) {
  if seq1 == nil && seq2 == nil {
    return nil;
  }

  let result = seq.list();

  for x in seq1 {
    seq.add(result, x);
  }
  for x in seq2 {
    seq.add(result, x);
  }

  return result;
}

fn constantly(x) {
  lambda(&args) -> x end
}

fn is_empty(s) {
  count(s) == 0
}

fn partial(f, &args) {
  return fn(&remaining) {
    return f(*args, *remaining);
  };
}

exports.take_while = take_while;
exports.drop_while = drop_while;
exports.repeat = repeat;
exports.repeatedly = repeatedly;
exports.group_by = group_by;
exports.distinct = distinct;
exports.concat = concat;
exports.constantly = constantly;
exports.is_empty = is_empty;
exports.is_distinct = is_distinct;
exports.partial = partial;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy