September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,108 +0,0 @@
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Gnat.Heap_Sort_G;
procedure Custom_Compare is
type StringArrayType is array (Natural range <>) of Unbounded_String;
Strings : StringArrayType := (Null_Unbounded_String,
To_Unbounded_String("this"),
To_Unbounded_String("is"),
To_Unbounded_String("a"),
To_Unbounded_String("set"),
To_Unbounded_String("of"),
To_Unbounded_String("strings"),
To_Unbounded_String("to"),
To_Unbounded_String("sort"),
To_Unbounded_String("This"),
To_Unbounded_String("Is"),
To_Unbounded_String("A"),
To_Unbounded_String("Set"),
To_Unbounded_String("Of"),
To_Unbounded_String("Strings"),
To_Unbounded_String("To"),
To_Unbounded_String("Sort"));
procedure Move (From, To : in Natural) is
begin
Strings(To) := Strings(From);
end Move;
function UpCase (Char : in Character) return Character is
Temp : Character;
begin
if Char >= 'a' and Char <= 'z' then
Temp := Character'Val(Character'Pos(Char)
- Character'Pos('a')
+ Character'Pos('A'));
else
Temp := Char;
end if;
return Temp;
end UpCase;
function Lt (Op1, Op2 : Natural)
return Boolean is
Temp, Len : Natural;
begin
Len := Length(Strings(Op1));
Temp := Length(Strings(Op2));
if Len < Temp then
return False;
elsif Len > Temp then
return True;
end if;
declare
S1, S2 : String(1..Len);
begin
S1 := To_String(Strings(Op1));
S2 := To_String(Strings(Op2));
Put("Same size: ");
Put(S1);
Put(" ");
Put(S2);
Put(" ");
for I in S1'Range loop
Put(UpCase(S1(I)));
Put(UpCase(S2(I)));
if UpCase(S1(I)) = UpCase(S2(I)) then
null;
elsif UpCase(S1(I)) < UpCase(S2(I)) then
Put(" LT");
New_Line;
return True;
else
return False;
end if;
end loop;
Put(" GTE");
New_Line;
return False;
end;
end Lt;
procedure Put (Arr : in StringArrayType) is
begin
for I in 1..Arr'Length-1 loop
Put(To_String(Arr(I)));
New_Line;
end loop;
end Put;
package Heap is new Gnat.Heap_Sort_G(Move,
Lt);
use Heap;
begin
Put_Line("Unsorted list:");
Put(Strings);
New_Line;
Sort(16);
New_Line;
Put_Line("Sorted list:");
Put(Strings);
end Custom_Compare;

View file

@ -1,35 +0,0 @@
Unsorted list:
this
is
a
set
of
strings
to
sort
This
Is
A
Set
Of
Strings
To
Sort
Sorted list:
strings
Strings
sort
Sort
this
This
Set
set
is
Is
Of
of
to
To
a
A

View file

@ -0,0 +1,97 @@
use framework "Foundation"
-- SORTING LISTS OF ATOMIC (NON-RECORD) DATA WITH A CUSTOM SORT FUNCTION
-- In sortBy, f is a function from () to a tuple of two parts:
-- 1. a function from any value to a record derived from (and containing) that value
-- The base value should be present in the record under the key 'value'
-- additional derivative keys (and optionally the 'value' key) should be included in 2:
-- 2. a list of (record key, boolean) tuples, in the order of sort comparison,
-- where the value *true* selects ascending order for the paired key
-- and the value *false* selects descending order for that key
-- sortBy :: (() -> ((a -> Record), [(String, Bool)])) -> [a] -> [a]
on sortBy(f, xs)
set {fn, keyBools} to mReturn(f)'s |λ|()
script unWrap
on |λ|(x)
value of x
end |λ|
end script
map(unWrap, sortByComparing(keyBools, map(fn, xs)))
end sortBy
-- SORTING APPLESCRIPT RECORDS BY PRIMARY AND N-ARY SORT KEYS
-- sortByComparing :: [(String, Bool)] -> [Records] -> [Records]
on sortByComparing(keyDirections, xs)
set ca to current application
script recDict
on |λ|(x)
ca's NSDictionary's dictionaryWithDictionary:x
end |λ|
end script
set dcts to map(recDict, xs)
script asDescriptor
on |λ|(kd)
set {k, d} to kd
ca's NSSortDescriptor's sortDescriptorWithKey:k ascending:d selector:dcts
end |λ|
end script
((ca's NSArray's arrayWithArray:dcts)'s ¬
sortedArrayUsingDescriptors:map(asDescriptor, keyDirections)) as list
end sortByComparing
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- TEST ----------------------------------------------------------------------
on run
set xs to ["Shanghai", "Karachi", "Beijing", "Sao Paulo", "Dhaka", "Delhi", "Lagos"]
-- Custom comparator:
-- Returns a lifting function and a sequence of {key, bool} pairs
-- Strings in order of descending length,
-- and ascending lexicographic order
script lengthDownAZup
on |λ|()
script
on |λ|(x)
{value:x, n:length of x}
end |λ|
end script
{result, {{"n", false}, {"value", true}}}
end |λ|
end script
sortBy(lengthDownAZup, xs)
end run

View file

@ -12,6 +12,10 @@ begin
TArray.Sort<string>(lArray , TDelegatedComparer<string>.Construct(
function(const Left, Right: string): Integer
begin
Result := Length(Right) - Length(Left);
//Returns ('Here', 'are', 'be', 'sample', 'some', 'sorted', 'strings', 'to')
//Result := CompareStr(Left, Right);
//Returns ('are', 'be', 'Here', 'sample', 'some', 'sorted', 'strings', 'to')
Result := CompareText(Left, Right);
end));
end.

View file

@ -1,9 +1,26 @@
import List
import Char
import Data.List (sortBy)
import Data.Function (on)
import Data.Char (toLower)
mycmp s1 s2 = case compare (length s2) (length s1) of
EQ -> compare (map toLower s1) (map toLower s2)
x -> x
lengthThenAZ :: String -> String -> Ordering
lengthThenAZ a b
| d == EQ = on compare (toLower <$>) a b
| otherwise = d
where
d = on compare length a b
strings = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
sorted = sortBy mycmp strings
descLengthThenAZ :: String -> String -> Ordering
descLengthThenAZ a b
| d == EQ = on compare (toLower <$>) a b
| otherwise = d
where
d = on (flip compare) length a b
xs :: [String]
xs = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
main :: IO ()
main =
mapM_
putStrLn
[unlines $ sortBy lengthThenAZ xs, unlines $ sortBy descLengthThenAZ xs]

View file

@ -1,3 +1,26 @@
import Data.List (sortBy)
import Data.Char (toLower)
import Data.Ord (comparing)
import Data.Monoid
mycmp s1 s2 = mappend (compare (length s2) (length s1))
(compare (map toLower s1) (map toLower s2))
-- the Ordering instance of mappend is defined to yield
-- lexicographical ordering.
-- instance Monoid Ordering where
-- mempty = EQ
-- LT `mappend` _ = LT
-- EQ `mappend` y = y
-- GT `mappend` _ = GT
xs :: [String]
xs = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
lowerCase :: String -> String
lowerCase = (toLower <$>)
main :: IO ()
main =
mapM_ putStrLn $
(unlines . flip sortBy xs) <$>
[ comparing length <> comparing lowerCase -- Ascending length
, flip (comparing length) <> comparing lowerCase -- Descending length
]

View file

@ -0,0 +1,121 @@
(function () {
'use strict';
// GENERIC FUNCTIONS FOR COMPARISONS
// Ordering :: ( LT | EQ | GT ) | ( -1 | 0 | 1 )
// compare :: a -> a -> Ordering
var compare = function (a, b) {
return a < b ? -1 : a > b ? 1 : 0;
};
// mappendOrdering :: Ordering -> Ordering -> Ordering
var mappendOrdering = function (a, b) {
return a !== 0 ? a : b;
};
// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
var on = function (f, g) {
return function (a, b) {
return f(g(a), g(b));
};
};
// flip :: (a -> b -> c) -> b -> a -> c
var flip = function (f) {
return function (a, b) {
return f.apply(null, [b, a]);
};
};
// arrayCopy :: [a] -> [a]
var arrayCopy = function (xs) {
return xs.slice(0);
};
// show :: a -> String
var show = function (x) {
return JSON.stringify(x, null, 2);
};
// TEST
var xs = ['Shanghai', 'Karachi', 'Beijing', 'Sao Paulo', 'Dhaka', 'Delhi', 'Lagos'];
var 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
var population = function (x) {
return x.pop;
};
// length :: [a] -> Int
var length = function (xs) {
return xs.length;
};
// toLower :: String -> String
var toLower = function (s) {
return s.toLowerCase();
};
// lengthThenAZ :: String -> String -> ( -1 | 0 | 1)
var lengthThenAZ = function (a, b) {
return mappendOrdering(
on(compare, length)(a, b),
on(compare, toLower)(a, b)
);
};
// descLengthThenAZ :: String -> String -> ( -1 | 0 | 1)
var descLengthThenAZ = function (a, b) {
return 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))
});
})();

View file

@ -0,0 +1,97 @@
(() => {
'use strict';
// GENERIC FUNCTIONS FOR COMPARISONS
// Ordering :: ( LT | EQ | GT ) | ( -1 | 0 | 1 )
// compare :: a -> a -> Ordering
const compare = (a, b) => a < b ? -1 : (a > b ? 1 : 0);
// mappendOrdering :: Ordering -> Ordering -> Ordering
const mappendOrdering = (a, b) => a !== 0 ? a : b;
// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
const on = (f, g) => (a, b) => f(g(a), g(b));
// 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;
// length :: [a] -> Int
const length = xs => xs.length;
// toLower :: String -> String
const toLower = s => s.toLowerCase();
// lengthThenAZ :: String -> String -> ( -1 | 0 | 1)
const lengthThenAZ = (a, b) =>
mappendOrdering(
on(compare, length)(a, b),
on(compare, toLower)(a, b)
);
// 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))
});
})();

View file

@ -0,0 +1,57 @@
use Collection;
class Test {
function : Main(args : String[]) ~ Nil {
v := CreateHolders(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]);
"unsorted: "->Print(); Show(v);
v->Sort();
"sorted: "->Print(); Show(v);
}
function : CreateHolders(strings : String[]) ~ CompareVector {
vector := CompareVector->New();
each(i : strings) {
vector->AddBack(StringHolder->New(strings[i]));
};
return vector;
}
function : Show(v : CompareVector) ~ Nil {
each(i : v) {
s := v->Get(i)->As(StringHolder);
s->ToString()->Print();
if(i + 1 < v->Size()) {
','->Print();
};
};
'\n'->Print();
}
}
class StringHolder implements Compare {
@s : String;
New(s : String) {
@s := s;
}
method : public : Compare(c : Compare) ~ Int {
h := c->As(StringHolder);
r := h->ToString();
size := r->Size() - @s->Size();
if(size = 0) {
size := @s->ToUpper()->Compare(r->ToUpper());
};
return size;
}
method : public : HashID() ~ Int {
return @s->HashID();
}
method : public : ToString() ~ String {
return @s;
}
}

View file

@ -0,0 +1,10 @@
A=.array~of('The seven deadly sins','Pride','avarice','Wrath','envy','gluttony','sloth','Lust')
say 'Sorted in order of descending length, and in ascending lexicographic order'
say A~sortWith(.DescLengthAscLexical~new)~makeString
::class DescLengthAscLexical mixinclass Comparator
::method compare
use strict arg left, right
if left~length==right~length
then return left~caselessCompareTo(right)
else return right~length-left~length

View file

@ -0,0 +1,34 @@
load "stdlib.ring"
sList = newlist(8, 2)
aList = ["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]
ind = len(aList)
for n = 1 to ind
sList[n] [1] = aList[n]
sList[n] [2] = len(aList[n])
next
nList = sortFirstSecond(sList, 2)
oList = newlist(8, 2)
count = 0
for n = len(nList) to 1 step -1
count = count + 1
oList[count] [1] = nList[n] [1]
oList[count] [2] = nList[n] [2]
next
for n = 1 to len(oList) - 1
temp1 = oList[n] [1]
temp2 = oList[n+1] [1]
if (oList[n] [2] = oList[n+1] [2]) and (strcmp(temp1, temp2) > 0)
temp = oList[n] [1]
oList[n] [1] = oList[n+1] [1]
oList[n+1] [1] = temp
ok
next
for n = 1 to len(oList)
see oList[n] [1] + nl
next

View file

@ -0,0 +1,9 @@
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())
});
println!("{:?}", words);
}

View file

@ -1,5 +1,5 @@
(use srfi-13);;Syntax for module inclusion depends on implementation,
;;as does the presence of a sort function.
;;a sort function may be predefined, or available through srfi 95
(define (mypred? a b)
(let ((len-a (string-length a))
(len-b (string-length b)))

View file

@ -0,0 +1,6 @@
s:=T("Cat","apple","Adam","zero","Xmas","quit","Level","add","Actor","base","butter");
r:=s.sort(fcn(a,b){
an,bn := a.len(),b.len();
if(an==bn)(a.toLower() < b.toLower()) else (an > bn)
});
r.pump(Console.println);