Paste: clay assorted

Author: ceninan
Mode: factor
Date: Thu, 5 Aug 2010 19:10:04
Plain Text |
//
// in?
//

in?(elem, seq) = find(elem, seq) != end(seq);



//
// trim, trimBeginning, trimEnd
//

trim(trim, seq) = String(trimBeginningSlice(trim, trimEndSlice(trim, seq)));

trimBeginning(trim, seq) = String(trimBeginningSlice(trim, seq));

trimEnd(trim, seq) = String(trimEndSlice(trim, seq));



//
// trimSlice, trimBeginningSlice, trimEndSlice
//

trimSlice(trim, seq) = trimBeginningSlice(trim, trimEndSlice(trim, seq));

trimBeginningSlice(trim, seq) {
    var i = 0;
    for (x in seq) {
        if (in?(x, trim)) inc(i);
        else break;
    }
    return sliceFrom(seq, i);
}

trimEndSlice(trim, seq) {
    var i = 0;
    for (x in reverse(seq)) {
        if (in?(x,trim)) inc(i);
        else break;
    }
    return sliceUpto(seq, size(seq) - i);
}



//
// beginsWith?, endsWith?
//

beginsWith?(x, seq) = sliceUpto(seq, size(x)) == x;

endsWith?(x, seq) {
    if (size(seq) < size(x))
        return false;
    return sliceFrom(seq, size(seq) - size(x)) == x;
}

Annotation: .

Author: ceninan
Mode: factor
Date: Thu, 5 Aug 2010 21:27:34
Plain Text |
//
// in?
//

in?(elem, seq) = find(elem, seq) != end(seq);



//
// trim, trimBegin, trimEnd
//

[T | Sequence?(T)]
trim(pred, seq:T) = T(trimSlice(pred, seq));

[T | Sequence?(T)]
trimBegin(pred, seq:T) = T(trimBeginSlice(pred, seq));

[T | Sequence?(T)]
trimEnd(pred, seq:T) = T(trimEndSlice(pred, seq));



overload trim(pred, seq:StringConstant) =
    String(trimSlice(pred, seq));

overload trimBegin(pred, seq:StringConstant) =
    String(trimBeginSlice(pred, seq));

overload trimEnd(pred, seq:StringConstant) =
    String(trimEndSlice(pred, seq));



[T | String?(T)]
overload trim(s:T, seq) = trim(lambda(x) { return in?(x,s); }, seq);

[T | String?(T)]
overload trimBegin(s:T, seq) = trimBegin(lambda(x) { return in?(x,s); }, seq);

[T | String?(T)]
overload trimEnd(s:T, seq) = trimEnd(lambda(x) { return in?(x,s); }, seq);



//
// trimSlice, trimBeginSlice, trimEndSlice
//

trimSlice(pred, seq) = trimBeginSlice(pred, trimEndSlice(pred, seq));

trimBeginSlice(pred, seq) {
    var i = 0;
    for (x in seq) {
        if (pred(x)) inc(i);
        else break;
    }
    return sliceFrom(seq, i);
}

trimEndSlice(pred, seq) {
    var i = 0;
    for (x in reverse(seq)) {
        if (pred(x)) inc(i);
        else break;
    }
    return sliceUpto(seq, size(seq) - i);
}



[T | String?(T)]
overload trimSlice(s:T, seq) = trimSlice(lambda(x) { return in?(x,s); }, seq);

[T | String?(T)]
overload trimBeginSlice(s:T, seq) = trimBeginSlice(lambda(x) { return in?(x,s); }, seq);

[T | String?(T)]
overload trimEndSlice(s:T, seq) = trimEndSlice(lambda(x) { return in?(x,s); }, seq);



//
// beginsWith?, endsWith?
//

beginsWith?(x, seq) = sliceUpto(seq, size(x)) == x;

endsWith?(x, seq) {
    if (size(seq) < size(x))
        return false;
    return sliceFrom(seq, size(seq) - size(x)) == x;
}

Annotation: ..

Author: ceninan
Mode: factor
Date: Thu, 5 Aug 2010 21:49:49
Plain Text |
//
// in?
//

in?(elem, seq) = find(elem, seq) != end(seq);



//
// trim, trimBegin, trimEnd
//

[T | Sequence?(T)]
trim(x, seq:T) = T(trimSlice(x, seq));

[T | Sequence?(T)]
trimBegin(x, seq:T) = T(trimBeginSlice(x, seq));

[T | Sequence?(T)]
trimEnd(x, seq:T) = T(trimEndSlice(x, seq));



overload trim(x, seq:StringConstant) =
    String(trimSlice(x, seq));

overload trimBegin(x, seq:StringConstant) =
    String(trimBeginSlice(x, seq));

overload trimEnd(x, seq:StringConstant) =
    String(trimEndSlice(x, seq));



//
// trimSlice, trimBeginSlice, trimEndSlice
//

trimSlice(pred, seq) = trimBeginSlice(pred, trimEndSlice(pred, seq));

trimBeginSlice(pred, seq) {
    var i = 0;
    for (x in seq) {
        if (pred(x)) inc(i);
        else break;
    }
    return sliceFrom(seq, i);
}

trimEndSlice(pred, seq) {
    var i = 0;
    for (x in reverse(seq)) {
        if (pred(x)) inc(i);
        else break;
    }
    return sliceUpto(seq, size(seq) - i);
}



[T | Sequence?(T)]
overload trimSlice(s:T, seq) = trimSlice(lambda(x) { return in?(x,s); }, seq);

[T | Sequence?(T)]
overload trimBeginSlice(s:T, seq) = trimBeginSlice(lambda(x) { return in?(x,s); }, seq);

[T | Sequence?(T)]
overload trimEndSlice(s:T, seq) = trimEndSlice(lambda(x) { return in?(x,s); }, seq);



//
// beginsWith?, endsWith?
//

beginsWith?(x, seq) = sliceUpto(seq, size(x)) == x;

endsWith?(x, seq) {
    if (size(seq) < size(x))
        return false;
    return sliceFrom(seq, size(seq) - size(x)) == x;
}

New Annotation

Summary:
Author:
Mode:
Body: