September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,94 @@
|
|||
# define the MODE that will be sorted #
|
||||
MODE SITEM = STRING;
|
||||
#--- Swap function ---#
|
||||
PROC swap = (REF[]SITEM array, INT first, INT second) VOID:
|
||||
(
|
||||
SITEM temp := array[first];
|
||||
array[first] := array[second];
|
||||
array[second]:= temp
|
||||
);
|
||||
#--- Quick sort partition arg function with custom comparision function ---#
|
||||
PROC quick = (REF[]SITEM array, INT first, INT last, PROC(SITEM,SITEM)INT compare) VOID:
|
||||
(
|
||||
INT smaller := first + 1,
|
||||
larger := last;
|
||||
SITEM pivot := array[first];
|
||||
WHILE smaller <= larger DO
|
||||
WHILE compare(array[smaller], pivot) < 0 AND smaller < last DO
|
||||
smaller +:= 1
|
||||
OD;
|
||||
WHILE compare( array[larger], pivot) > 0 AND larger > first DO
|
||||
larger -:= 1
|
||||
OD;
|
||||
IF smaller < larger THEN
|
||||
swap(array, smaller, larger);
|
||||
smaller +:= 1;
|
||||
larger -:= 1
|
||||
ELSE
|
||||
smaller +:= 1
|
||||
FI
|
||||
OD;
|
||||
swap(array, first, larger);
|
||||
IF first < larger-1 THEN
|
||||
quick(array, first, larger-1, compare)
|
||||
FI;
|
||||
IF last > larger +1 THEN
|
||||
quick(array, larger+1, last, compare)
|
||||
FI
|
||||
);
|
||||
#--- Quick sort array function with custom comparison function ---#
|
||||
PROC quicksort = (REF[]SITEM array, PROC(SITEM,SITEM)INT compare) VOID:
|
||||
(
|
||||
IF UPB array > LWB array THEN
|
||||
quick(array, LWB array, UPB array, compare)
|
||||
FI
|
||||
);
|
||||
#***************************************************************#
|
||||
main:
|
||||
(
|
||||
OP LENGTH = (STRING a)INT: ( UPB a + 1 ) - LWB a;
|
||||
OP TOLOWER = (STRING a)STRING:
|
||||
BEGIN
|
||||
STRING result := a;
|
||||
FOR i FROM LWB result TO UPB result DO
|
||||
CHAR c = a[i];
|
||||
IF c >= "A" AND c <= "Z" THEN result[i] := REPR ( ( ABS c - ABS "A" ) + ABS "a" ) FI
|
||||
OD;
|
||||
result
|
||||
END # TOLOWER # ;
|
||||
# custom comparison, descending sort on length #
|
||||
# if lengths are equal, sort lexicographically #
|
||||
PROC compare = (SITEM a, b)INT:
|
||||
IF INT a length = LENGTH a;
|
||||
INT b length = LENGTH b;
|
||||
a length < b length
|
||||
THEN
|
||||
# a is shorter than b # 1
|
||||
ELIF a length > b length
|
||||
THEN
|
||||
# a is longer than b # -1
|
||||
ELIF STRING lower a = TOLOWER a;
|
||||
STRING lower b = TOLOWER b;
|
||||
lower a < lower b
|
||||
THEN
|
||||
# lowercase a is before lowercase b # -1
|
||||
ELIF lower a > lower b
|
||||
THEN
|
||||
# lowercase a is after lowercase b # 1
|
||||
ELIF a > b
|
||||
THEN
|
||||
# a and b are equal ignoring case, #
|
||||
# but a is after b considering case # 1
|
||||
ELIF a < b
|
||||
THEN
|
||||
# a and b are equal ignoring case, #
|
||||
# but a is before b considering case # -1
|
||||
ELSE
|
||||
# the strings are equal # 0
|
||||
FI # compare # ;
|
||||
[]SITEM orig = ("Here", "are", "some", "sample", "strings", "to", "be", "sorted");
|
||||
[LWB orig : UPB orig]SITEM a := orig;
|
||||
print(("Before:"));FOR i FROM LWB a TO UPB a DO print((" ",a[i])) OD; print((newline));
|
||||
quicksort(a, compare);
|
||||
print(("After :"));FOR i FROM LWB a TO UPB a DO print((" ",a[i])) OD; print((newline))
|
||||
)
|
||||
|
|
@ -16,7 +16,7 @@ BEGIN {
|
|||
print("\nsorted:")
|
||||
PROCINFO["sorted_in"] = "@ind_num_desc" ; SORTTYPE = 9
|
||||
for (i in arr) {
|
||||
PROCINFO["sorted_in"] = "@ind_str_asc" ; SORTTYPE = 2
|
||||
PROCINFO["sorted_in"] = "caselessCompare" ; SORTTYPE = 2 # possibly 14?
|
||||
for (j in arr[i]) {
|
||||
for (k=1; k<=arr[i][j]; k++) {
|
||||
print(j)
|
||||
|
|
@ -25,3 +25,9 @@ BEGIN {
|
|||
}
|
||||
exit(0)
|
||||
}
|
||||
function caselessCompare( i1, v1, i2, v2, l1, l2, result )
|
||||
{
|
||||
l1 = tolower( i1 );
|
||||
l2 = tolower( i2 );
|
||||
return ( ( l1 < l2 ) ? -1 : ( ( l1 == l2 ) ? 0 : 1 ) );
|
||||
} # caselessCompare
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
import extensions.
|
||||
import system'routines.
|
||||
import system'culture.
|
||||
import extensions;
|
||||
import system'routines;
|
||||
import system'culture;
|
||||
|
||||
program =
|
||||
[
|
||||
var items := ( "Here", "are", "some", "sample", "strings", "to", "be", "sorted" ).
|
||||
public program()
|
||||
{
|
||||
var items := new string[]{ "Here", "are", "some", "sample", "strings", "to", "be", "sorted" };
|
||||
|
||||
console printLine("Unsorted: ", items).
|
||||
console.printLine("Unsorted: ", items.asEnumerable());
|
||||
|
||||
console printLine("Descending length: ", items clone;
|
||||
sort(:p:n)(p length > n length) ).
|
||||
console.printLine("Descending length: ", items.clone()
|
||||
.sort:(p,n => p.Length > n.Length).asEnumerable());
|
||||
|
||||
console printLine("Ascending order: ", items clone;
|
||||
sort(:p:n)(p toUpper(invariantLocale) < n toUpper(invariantLocale)) ).
|
||||
].
|
||||
console.printLine("Ascending order: ", items.clone()
|
||||
.sort:(p,n => p.toUpper(invariantLocale) < n.toUpper(invariantLocale)).asEnumerable())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
import Data.Ord (comparing)
|
||||
import Data.Char (toLower)
|
||||
import Data.List (sortBy)
|
||||
|
||||
lengthThenAZ :: String -> String -> Ordering
|
||||
lengthThenAZ = comparing length `mappend` comparing (fmap toLower)
|
||||
|
||||
descLengthThenAZ :: String -> String -> Ordering
|
||||
descLengthThenAZ = flip (comparing length) `mappend` comparing (fmap toLower)
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
putStrLn
|
||||
(fmap
|
||||
unlines
|
||||
([sortBy] <*> [lengthThenAZ, descLengthThenAZ] <*>
|
||||
[["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]]))
|
||||
|
|
@ -1,97 +1,76 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// GENERIC FUNCTIONS FOR COMPARISONS
|
||||
// main :: IO ()
|
||||
const main = () => {
|
||||
const
|
||||
lengthThenAZ = mappendOrd(
|
||||
comparing(length),
|
||||
comparing(toLower)
|
||||
),
|
||||
descLengthThenAZ = mappendOrd(
|
||||
flip(comparing(length)),
|
||||
comparing(toLower)
|
||||
);
|
||||
|
||||
// Ordering :: ( LT | EQ | GT ) | ( -1 | 0 | 1 )
|
||||
// compare :: a -> a -> Ordering
|
||||
const compare = (a, b) => a < b ? -1 : (a > b ? 1 : 0);
|
||||
console.log(
|
||||
apList(apList([sortBy])([
|
||||
lengthThenAZ,
|
||||
descLengthThenAZ
|
||||
]))([
|
||||
[
|
||||
"Here", "are", "some", "sample",
|
||||
"strings", "to", "be", "sorted"
|
||||
]
|
||||
]).map(unlines).join('\n\n')
|
||||
);
|
||||
};
|
||||
|
||||
// mappendOrdering :: Ordering -> Ordering -> Ordering
|
||||
const mappendOrdering = (a, b) => a !== 0 ? a : b;
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
|
||||
const on = (f, g) => (a, b) => f(g(a), g(b));
|
||||
// apList (<*>) :: [a -> b] -> [a] -> [b]
|
||||
const apList = fs => xs =>
|
||||
// The application of each of a list of functions,
|
||||
// to each of a list of values.
|
||||
fs.flatMap(
|
||||
f => xs.flatMap(x => [f(x)])
|
||||
);
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
(x, y) => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : (a > b ? 1 : 0);
|
||||
};
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
const flip = f => (a, b) => f.apply(null, [b, a]);
|
||||
|
||||
// arrayCopy :: [a] -> [a]
|
||||
const arrayCopy = (xs) => xs.slice(0);
|
||||
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x, null, 2);
|
||||
|
||||
|
||||
// TEST
|
||||
const xs = ['Shanghai', 'Karachi', 'Beijing', 'Sao Paulo', 'Dhaka', 'Delhi', 'Lagos'];
|
||||
|
||||
const rs = [{
|
||||
name: 'Shanghai',
|
||||
pop: 24.2
|
||||
}, {
|
||||
name: 'Karachi',
|
||||
pop: 23.5
|
||||
}, {
|
||||
name: 'Beijing',
|
||||
pop: 21.5
|
||||
}, {
|
||||
name: 'Sao Paulo',
|
||||
pop: 24.2
|
||||
}, {
|
||||
name: 'Dhaka',
|
||||
pop: 17.0
|
||||
}, {
|
||||
name: 'Delhi',
|
||||
pop: 16.8
|
||||
}, {
|
||||
name: 'Lagos',
|
||||
pop: 16.1
|
||||
}]
|
||||
|
||||
// population :: Dictionary -> Num
|
||||
const population = x => x.pop;
|
||||
const flip = f =>
|
||||
1 < f.length ? (
|
||||
(a, b) => f(b, a)
|
||||
) : (x => y => f(y)(x));
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs => xs.length;
|
||||
const length = xs =>
|
||||
(Array.isArray(xs) || 'string' === typeof xs) ? (
|
||||
xs.length
|
||||
) : Infinity;
|
||||
|
||||
// mappendOrd (<>) :: Ordering -> Ordering -> Ordering
|
||||
const mappendOrd = (a, b) => a !== 0 ? a : b;
|
||||
|
||||
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
|
||||
const sortBy = f => xs =>
|
||||
xs.slice()
|
||||
.sort(f);
|
||||
|
||||
// toLower :: String -> String
|
||||
const toLower = s => s.toLowerCase();
|
||||
const toLower = s => s.toLocaleLowerCase();
|
||||
|
||||
// lengthThenAZ :: String -> String -> ( -1 | 0 | 1)
|
||||
const lengthThenAZ = (a, b) =>
|
||||
mappendOrdering(
|
||||
on(compare, length)(a, b),
|
||||
on(compare, toLower)(a, b)
|
||||
);
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// descLengthThenAZ :: String -> String -> ( -1 | 0 | 1)
|
||||
const descLengthThenAZ = (a, b) =>
|
||||
mappendOrdering(
|
||||
on(flip(compare), length)(a, b),
|
||||
on(compare, toLower)(a, b)
|
||||
);
|
||||
|
||||
return show({
|
||||
default: arrayCopy(xs)
|
||||
.sort(compare),
|
||||
|
||||
descendingDefault: arrayCopy(xs)
|
||||
.sort(flip(compare)),
|
||||
|
||||
byLengthThenAZ: arrayCopy(xs)
|
||||
.sort(lengthThenAZ),
|
||||
|
||||
byDescendingLengthThenZA: arrayCopy(xs)
|
||||
.sort(flip(lengthThenAZ)),
|
||||
|
||||
byDescendingLengthThenAZ: arrayCopy(xs)
|
||||
.sort(descLengthThenAZ),
|
||||
|
||||
byPopulation: arrayCopy(rs)
|
||||
.sort(on(compare, population)),
|
||||
|
||||
byDescendingPopulation: arrayCopy(rs)
|
||||
.sort(on(flip(compare), population))
|
||||
});
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
function my_compare(sequence a, sequence b)
|
||||
if length(a)!=length(b) then
|
||||
return -compare(length(a),length(b))
|
||||
else
|
||||
return compare(lower(a),lower(b))
|
||||
function my_compare(sequence a, b)
|
||||
integer c = -compare(length(a),length(b)) -- descending length
|
||||
if c=0 then
|
||||
c = compare(lower(a),lower(b)) -- ascending lexical within == length
|
||||
end if
|
||||
return c
|
||||
end function
|
||||
?custom_sort(routine_id("my_compare"),{"Here", "are", "some", "sample", "strings", "to", "be", "sorted"})
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
fn main() {
|
||||
let mut words = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"];
|
||||
words.sort_by(|l, r| if l.len() == r.len() {
|
||||
l.cmp(&r)
|
||||
} else {
|
||||
r.len().cmp(&l.len())
|
||||
});
|
||||
words.sort_by(|l, r| Ord::cmp(&r.len(), &l.len()).then(Ord::cmp(l, r)));
|
||||
println!("{:?}", words);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue