Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,22 @@
( ( sortTable
= table ordering column reverse
. !arg
: ( ?table
. ( ? (ordering.?ordering) ?
| ?&lexicographic:?ordering
)
: ( ? (column.?column) ?
| ?&1:?column
)
: ( ? (reverse.?reverse) ?
| ?&no:?reverse
)
)
& (...)
)
& (12.Claes.left)
(11.Otto.right)
(8.Frederikke.middle)
: ?table
& sortTable$(!table.(column.2) (reverse.yes))
);

View file

@ -0,0 +1,3 @@
def orderedSort(Collection table, column = 0, reverse = false, ordering = {x, y -> x <=> y } as Comparator) {
table.sort(false) { x, y -> (reverse ? -1 : 1) * ordering.compare(x[column], y[column])}
}

View file

@ -0,0 +1,7 @@
def table = [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
assert orderedSort(table) == [['', 'q', 'z'], ['a', 'b', 'c'], ['zap', 'zip', 'Zot']]
assert orderedSort(table, 2) == [['zap', 'zip', 'Zot'], ['a', 'b', 'c'], ['', 'q', 'z']]
assert orderedSort(table, 1) == [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
assert orderedSort(table, 1, true) == [['zap', 'zip', 'Zot'],['', 'q', 'z'],['a', 'b', 'c']]
assert orderedSort(table, 0, false, {x, y -> y?.size() <=> x?.size()} as Comparator) == [['zap', 'zip', 'Zot'],['a', 'b', 'c'],['', 'q', 'z']]

View file

@ -0,0 +1,7 @@
Collection.metaClass.orderedSort = { params ->
def column = params?.column ?: 0
def reverse = params?.reverse ?: false
def ordering = params?.ordering ?: {x, y -> x <=> y } as Comparator
table.sort(false) { x, y -> (reverse ? -1 : 1) * ordering.compare(x[column], y[column])}
}

View file

@ -0,0 +1,7 @@
def table = [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
assert table.orderedSort() == [['', 'q', 'z'], ['a', 'b', 'c'], ['zap', 'zip', 'Zot']]
assert table.orderedSort(column: 2) == [['zap', 'zip', 'Zot'], ['a', 'b', 'c'], ['', 'q', 'z']]
assert table.orderedSort(column: 1) == [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
assert table.orderedSort(column: 1, reverse: true) == [['zap', 'zip', 'Zot'],['', 'q', 'z'],['a', 'b', 'c']]
assert table.orderedSort(ordering: {x, y -> y?.size() <=> x?.size()} as Comparator) == [['zap', 'zip', 'Zot'],['a', 'b', 'c'],['', 'q', 'z']]

View file

@ -0,0 +1,15 @@
{-# LANGUAGE RecordWildCards #-}
data SorterArgs = SorterArgs { cmp :: String, col :: Int, rev :: Bool } deriving Show
defSortArgs = SorterArgs "lex" 0 False
sorter :: SorterArgs -> [[String]] -> [[String]]
sorter (SorterArgs{..}) = case cmp of
_ -> undefined
main = do
sorter defSortArgs{cmp = "foo", col=1, rev=True} [[]]
sorter defSortArgs{cmp = "foo"} [[]]
sorter defSortArgs [[]]
return ()

View file

@ -0,0 +1,12 @@
import Data.Maybe (fromMaybe)
-- Use fromMaybe as an operator because its prettier
(//) = flip fromMaybe
sorter :: Maybe String -> Maybe Int -> Maybe Bool -> [[String]] -> [[String]]
sorter ((// "lex") -> cmp)
((// 0) -> col)
((// False) -> rev) = undefined
main = do
sorter (Just "foo") (Just 1) (Just True)
sorter Nothing Nothing Nothing

View file

@ -0,0 +1,13 @@
OptionalSort := proc(input, {
ordering :: Or(procedures,identical("lexicographic")) := "lexicographic",
column :: posint := 1,
reverse :: truefalse := false
} )
local compare;
if ordering = "lexicographic" then
compare := (x,y)->evalb(`if`(reverse,x[column]>=y[column],x[column]<=y[column]));
else
compare := (x,y)->`if`(reverse,ordering(x[column],y),ordering(y,x));
end if;
sort( input, compare );
end proc:

View file

@ -0,0 +1,7 @@
> L := [[1, 2], [3, 4], [-5, 7]]:
> OptionalSort(L);
[[-5, 7], [1, 2], [3, 4]]
> OptionalSort(L, reverse);
[[3, 4], [1, 2], [-5, 7]]
> OptionalSort(L, reverse, column = 2);
[[-5, 7], [3, 4], [1, 2]]

View file

@ -1,19 +1,18 @@
sortStrings: procedure expose @. /*stemmed array is named: @. */
col=1; reverse='NO'; order='LEXICOGRAPHIC' /*set the defaults.*/
col=1; reverse='NO'; order='LEXICOGRAPHIC' /*set some defaults.*/
arg options
do j=1 for words(options); x=word(options,j)
do j=1 for words(options); x=word(options,j)
select
when datatype(x,'W') then col=x/1
when pos('=',x)==0 then order=x
otherwise parse var x nam '=' value
when datatype(x, 'W') then col=x/1
when pos('=', x)==0 then order=x
otherwise parse var x nam '=' value
end /*select*/
end /*j*/
/*check for errors here: COL isn't positive, */
/* REVERSE value isn't NO or YES, */
/* ORDER value is recognized ... */
... main body of string sort here ...
return
/*╔═══════════════════════════════════════════════════════════╗
check for errors here: COL isn't a positive integer ···,
REVERSE value isn't NO or YES,
ORDER value is recognized ···
*/
... main body of string sort here ...
return /*stick a fork in it, we're done.*/

View file

@ -1,4 +1,4 @@
/*REXX example to use the SortStrings subroutine with optional args. */
/*...define array (@.nnn) of strings here...*/
/*REXX example uses the SortStrings subroutine with optional args. */
/*···define array (@.nnn) of strings here···*/
call sortStrings 'Reverse=no' 3
/*stick a fork in it, we're done.*/