Optional parameters: Difference between revisions

Add Factor
No edit summary
(Add Factor)
Line 984:
[|"a"; "b"; "c"|]
[|""; "q"; "z"|]</pre>
 
=={{header|Factor}}==
Factor doesn't have special support for optional parameters, so the idiom is to define a tuple with desired initial values, set the desired slots, then pass it to a word.
<lang factor>USING: accessors combinators io kernel math.order prettyprint
sequences sorting ;
 
TUPLE: table-sorter
data
{ column initial: 0 }
reversed?
{ ordering initial: [ ] } ;
 
: <table-sorter> ( -- obj ) table-sorter new ;
 
: sort-table ( table-sorter -- matrix )
{
[ data>> ]
[ column>> [ swap nth ] curry ]
[ ordering>> compose ]
[ reversed?>> [ >=< ] [ <=> ] ? [ bi@ ] prepose curry ]
} cleave [ sort ] curry call( x -- x ) ;
 
 
! ===== Now we can use the interface defined above =====
 
CONSTANT: table
{ { "a" "b" "c" } { "" "q" "z" } { "can" "z" "a" } }
 
"Unsorted" print
table simple-table.
 
"Default sort" print
<table-sorter>
table >>data
sort-table simple-table.
 
"Sorted by col 2" print
<table-sorter>
table >>data
2 >>column
sort-table simple-table.
 
"Sorted by col 1" print
<table-sorter>
table >>data
1 >>column
sort-table simple-table.
 
"Reverse sorted by col 1" print
<table-sorter>
table >>data
1 >>column
t >>reversed?
sort-table simple-table.
 
"Sorted by decreasing length" print
<table-sorter>
table >>data
t >>reversed?
[ length ] >>ordering
sort-table simple-table.</lang>
{{out}}
<pre>
Unsorted
a b c
q z
can z a
Default sort
q z
a b c
can z a
Sorted by col 2
can z a
a b c
q z
Sorted by col 1
a b c
q z
can z a
Reverse sorted by col 1
can z a
q z
a b c
Sorted by decreasing length
can z a
a b c
q z
</pre>
 
=={{header|Fortran}}==
1,820

edits