Paste: max/min/avg

Author: mrjbq7
Mode: factor
Date: Fri, 7 Jan 2011 01:51:12
Plain Text |
: maximum ( seq quot -- elt )
    [ max ] map-reduce ; inline

: minimum ( seq quot -- elt )
    [ min ] map-reduce ; inline

: average ( seq -- n )
    [ sum ] [ length ] bi / ;

Annotation: better min/max

Author: mrjbq7
Mode: factor
Date: Fri, 7 Jan 2011 02:21:56
Plain Text |
: maximum ( seq quot: ( ... elt -- ... x ) -- elt )
    [ dup ] prepose [ 2array ] compose
    [ [ [ second ] bi@ [ max ] keep eq? ] most ]
    map-reduce first ; inline

: minimum ( seq quot: ( ... elt -- ... x ) -- elt )
    [ dup ] prepose [ 2array ] compose
    [ [ [ second ] bi@ [ min ] keep eq? ] most ]
    map-reduce first ; inline

Annotation: whoops, need "eq? not"

Author: mrjbq7
Mode: factor
Date: Fri, 7 Jan 2011 02:26:00
Plain Text |
: maximum ( seq quot: ( ... elt -- ... x ) -- elt )
    [ dup ] prepose [ 2array ] compose
    [ [ [ second ] bi@ [ max ] keep eq? not ] most ]
    map-reduce first ; inline

: minimum ( seq quot: ( ... elt -- ... x ) -- elt )
    [ dup ] prepose [ 2array ] compose
    [ [ [ second ] bi@ [ min ] keep eq? not ] most ]
    map-reduce first ; inline

Annotation: better using curry

Author: mrjbq7
Mode: factor
Date: Fri, 7 Jan 2011 02:39:20
Plain Text |
: maximum ( seq quot: ( ... elt -- ... x ) -- elt )
    [ keep 2array ] curry
    [ [ [ first ] bi@ [ max ] keep eq? not ] most ]
    map-reduce second ; inline

: minimum ( seq quot: ( ... elt -- ... x ) -- elt )
    [ keep 2array ] curry
    [ [ [ first ] bi@ [ min ] keep eq? not ] most ]
    map-reduce second ; inline

New Annotation

Summary:
Author:
Mode:
Body: