# Paste: clay assorted

Author: ceninan factor 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 factor 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));

String(trimSlice(pred, seq));

String(trimBeginSlice(pred, seq));

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 factor 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));

String(trimSlice(x, seq));

String(trimBeginSlice(x, seq));

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;
}
```