Paste: First factor application

Author: ben_m
Mode: factor
Date: Fri, 29 Jan 2010 01:35:19
Plain Text |
! Copyright (C) 2010 Benjamin Meinl.
! See http://factorcode.org/license.txt for BSD license.

! Library to calculate your average grade

USING: kernel io prettyprint namespaces math.parser sequences
    math ;
IN: average-grade

SYMBOL: grades

: init ( -- )
    V{ } clone grades set ;

: read-number ( -- number/f )
    readln string>number ;

: read-grades ( -- )
    [
        read-number dup [ grades get push ] when*
    ] loop ;

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

: average-grade ( -- avg )
    grades get average ;

: main ( -- )
    "Enter grades, or nothing to stop:" print
    init read-grades average-grade . ;

MAIN: main

Annotation: improvements done

Author: ben_m
Mode: factor
Date: Fri, 29 Jan 2010 01:46:39
Plain Text |
! Copyright (C) 2010 Benjamin Meinl.
! See http://factorcode.org/license.txt for BSD license.

! Library to calculate your average grade

USING: kernel io prettyprint namespaces math.parser sequences
    math ;
IN: average-grade

SYMBOL: grades

: read-number ( -- number/f )
    readln string>number ;

: read-grades ( -- )
    [
        read-number dup [ grades get push ] when*
    ] loop ;

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

: average-grade ( -- avg )
    grades get average ;

: (main) ( -- )
    "Enter grades, or nothing to stop:" print
    read-grades average-grade number>string print ;

: main ( -- )
    V{ } clone grades [ (main) ] with-variable ;

MAIN: main

Annotation: shorter

Author: meh
Mode: factor
Date: Fri, 29 Jan 2010 12:30:34
Plain Text |
! Copyright (C) 2010 Benjamin Meinl.
! See http://factorcode.org/license.txt for BSD license.

! Library to calculate your average grade

USING: kernel io prettyprint math.parser
    math math.statistics make ;
IN: grades

: read-number ( -- number/f )
    readln string>number ;

: read-grades ( -- grades ) [ [
            read-number [ [ , ] when* ] keep
        ] loop
    ] { } make ;

: main ( -- )
    "Enter grades, or nothing to stop:" print
    read-grades mean >float . ;

MAIN: main

New Annotation

Summary:
Author:
Mode:
Body: