Paste: Looping with sequences

Author: Loryn Jenkins
Mode: factor
Date: Mon, 22 Apr 2013 13:27:16
Plain Text |
! Copyright (C) 2013 Loryn Jenkins.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences sequences.extras combinators locals namespaces accessors 
math math.functions math.ranges utils ;
IN: lamps

<PRIVATE
SYMBOLS: the-lamps ;
PRIVATE>

: >>the-lamps>> ( seq -- seq )
    the-lamps set 
    the-lamps get ;
    
: new-lamps ( -- seq )
    100 [1,b] [ drop t ] map ;

: lamps ( -- seq )
    the-lamps get [ new-lamps >>the-lamps>> ] unless* ;

: reset ( -- )
    f the-lamps set ;

: people ( -- seq )
    100 [1,b] [ ] map ;

: toggle ( ? -- ? )
    not ;
    
: toggle? ( person-index lamp-index -- ? ) 
    swap divisor? ;
    
: toggle-switches ( n -- )
    lamps [ toggle? [ toggle ] when ] with map-index+! drop ;
    
: do-switching ( -- )
    people [ toggle-switches ] each ;
    
: execute ( -- seq )
    do-switching lamps reset ;

Annotation: Utils

Author: Loryn Jenkins
Mode: factor
Date: Mon, 22 Apr 2013 14:19:00
Plain Text |
! Copyright (C) 2013 Loryn Jenkins.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences ;
IN: utils

: map-index+ ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
    '[ [ 1 + @ ] map-index ] call ; inline

: map-index+! ( ... seq quot: ( ... elt index -- ... newelt ) -- ... seq )
    '[ [ 1 + @ ] map-index! ] call ; inline

Annotation: simpler

Author: mrjbq7
Mode: factor
Date: Mon, 22 Apr 2013 14:49:19
Plain Text |
USING: arrays fry kernel math math.functions math.ranges
sequences sequences.extras ;
IN: lamps

: <lamps> ( -- seq )
    100 t <array> ;

: <people> ( -- seq )
    100 [1,b] >array ;

: toggle ( ? -- ? )
    not ;

: toggle? ( person-index lamp-index -- ? )ยท
    1 + swap divisor? ;

: toggle-switches ( n lamps -- )
    [ toggle? [ toggle ] when ] with map-index! drop ;

: do-switching ( -- lamps )
    <people> <lamps> [ '[ _ toggle-switches ] each ] keep ;

Annotation: quick answer

Author: mrjbq7
Mode: factor
Date: Mon, 22 Apr 2013 14:55:14
Plain Text |
2 18 2 <range> >array [ t <array> ] map
{ f } join { f } { f } surround

Annotation: Even simpler - computation by calculation

Author: Loryn Jenkins
Mode: factor
Date: Mon, 22 Apr 2013 15:46:35
Plain Text |
USING: arrays math math.ranges math.primes.factors sequences ;
IN: lamps

: <people> ( -- seq )
    100 [1,b] >array ;
    
: calc-switching ( -- lamps )
    <people> [ divisors length even? ] map ; 

New Annotation

Summary:
Author:
Mode:
Body: