Paste: sort-args, or one-upping pair-methods ;)

Author: rien
Mode: factor
Date: Fri, 4 Nov 2011 20:05:58
Plain Text |
USE: math.combinatorics

MACRO: sort-args ( stencil -- )
    2 all-combinations
    [ [ first ] [ second ] bi 2dup swap
      '[ 2dup [ @ ] [ @ ] bi* and ] [ 2drop +gt+ ] 2array -rot
      '[ 2dup [ @ ] [ @ ] bi* and ] [ 2drop +lt+ ] 2array 2array
    ] map concat [ '[ [ _ cond ] sort ] ] call ;

{ { } 0 "hey" } { [ fixnum? ] [ string? ] [ array? ] } sort-args .

result:
{ 0 "hey" { } }

Annotation: Version with || and true-quotations indices

Author: Jon
Mode: factor
Date: Sat, 5 Nov 2011 15:12:57
Plain Text |
! Copyright (C) 2011 Jon Harper.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit.smart fry kernel macros
math.order sequences sorting ;
IN: rien

MACRO: first-true-quot-idx ( quots -- idx )
  [ [ and ] curry compose ] map-index [ || ] curry ;
: sort-args ( els quots -- els ) 
  '[ [ _ first-true-quot-idx ] bi@ <=> ] sort ; inline

Annotation: changed Jon's code to accept a simpler stencil

Author: rien
Mode: factor
Date: Sun, 6 Nov 2011 06:11:03
Plain Text |
; and now for a puny enhancement compared to the previous one ;)

MACRO: (sort-args) ( stencil -- thunk )
    [ \ and 3array >quotation ] map-index [ || ] curry ;

: sort-args ( args stencil -- sorted-args )
    '[ [ _ (sort-args) ] bi@ <=> ] sort ; inline

{ { } 0 "hey" } { fixnum? string? array? } sort-args .

--- Data stack:
{ 0 "hey" { } }

New Annotation

Summary:
Author:
Mode:
Body: