diff --git a/config.json b/config.json index 98569ec9..6db735b9 100644 --- a/config.json +++ b/config.json @@ -1020,7 +1020,8 @@ ], "prerequisites": [ "conditionals", - "booleans" + "booleans", + "combinators" ], "difficulty": 3 }, diff --git a/exercises/practice/armstrong-numbers/.meta/example.factor b/exercises/practice/armstrong-numbers/.meta/example.factor index de61d2ee..ce2ef7bc 100644 --- a/exercises/practice/armstrong-numbers/.meta/example.factor +++ b/exercises/practice/armstrong-numbers/.meta/example.factor @@ -1,24 +1,7 @@ -USING: assocs kernel locals math math.functions math.order sequences vectors ; +USING: kernel locals math math.functions math.parser sequences ; IN: armstrong-numbers -:: digits ( x -- digits ) - x 10 /mod :> x! :> xs! - V{ } clone :> digits - x digits push - - [ xs 0 = not ] - [ - xs 10 /mod x! xs! - x digits push - ] while - - digits reverse ; - -:: powsum ( x -- powsum ) - x digits :> digs - digs length :> len - - digs [ len ^ ] map sum ; - -: armstrong? ( n -- ? ) - [ ] [ powsum ] bi = ; +:: armstrong? ( n -- ? ) + n number>string [ CHAR: 0 - ] map :> digits + digits length :> len + digits [ len ^ ] map-sum n = ; diff --git a/exercises/practice/gigasecond/.meta/example.factor b/exercises/practice/gigasecond/.meta/example.factor index 4b2b1adc..42409311 100644 --- a/exercises/practice/gigasecond/.meta/example.factor +++ b/exercises/practice/gigasecond/.meta/example.factor @@ -1,28 +1,11 @@ -USING: accessors calendar kernel math math.parser sequences -splitting ; +USING: calendar combinators formatting kernel math.parser sequences splitting ; IN: gigasecond -string 2 CHAR: 0 pad-head ; - -! Parse "YYYY-MM-DD" or "YYYY-MM-DDTHH:MM:SS" into a timestamp. -:: parse-moment ( str -- timestamp ) - str "T" split1 :> ( datepart timepart ) - datepart "-" split [ string>number ] map first3 :> ( y mo d ) - timepart [ "00:00:00" ] unless* ":" split [ string>number ] map - first3 :> ( h mi s ) - y mo d h mi s instant ; - -:: format-moment ( ts -- str ) - ts year>> number>string - ts month>> pad2 "-" glue - ts day>> pad2 "-" glue - ts hour>> pad2 "T" glue - ts minute>> pad2 ":" glue - ts second>> >integer pad2 ":" glue ; - -PRIVATE> - : gigasecond-after ( moment -- moment ) - parse-moment 1000000000 seconds time+ format-moment ; + "T" split1 + [ "-" split [ string>number ] map first3 ] + [ "00:00:00" or ":" split [ string>number ] map first3 ] + bi* + instant 1000000000 seconds time+ + [ >date< ] [ >time< ] bi + "%04d-%02d-%02dT%02d:%02d:%02d" sprintf ; diff --git a/exercises/practice/leap/.meta/example.factor b/exercises/practice/leap/.meta/example.factor index 736e7912..48d8023a 100644 --- a/exercises/practice/leap/.meta/example.factor +++ b/exercises/practice/leap/.meta/example.factor @@ -1,10 +1,5 @@ -USING: combinators kernel math ; +USING: combinators kernel math.functions ; IN: leap : leap-year? ( year -- ? ) - { - { [ dup 400 mod zero? ] [ drop t ] } - { [ dup 100 mod zero? ] [ drop f ] } - { [ dup 4 mod zero? ] [ drop t ] } - [ drop f ] - } cond ; + [ 4 divisor? ] [ 100 divisor? not ] [ 400 divisor? ] tri or and ; diff --git a/exercises/practice/word-count/.meta/example.factor b/exercises/practice/word-count/.meta/example.factor index 38117ebe..03648074 100644 --- a/exercises/practice/word-count/.meta/example.factor +++ b/exercises/practice/word-count/.meta/example.factor @@ -1,24 +1,10 @@ -USING: assocs hashtables kernel locals math sequences splitting -strings unicode ; +USING: assocs fry kernel sequences splitting unicode ; IN: word-count -: word-char? ( ch -- ? ) - dup Letter? over digit? or swap CHAR: ' = or ; - -:: strip-apostrophes ( str -- str' ) - str dup empty? not [ - dup first CHAR: ' = [ rest ] when - dup empty? not [ dup last CHAR: ' = [ but-last ] when ] when - ] when ; - -:: count-words ( sentence -- counts ) - H{ } clone :> result - sentence >lower - [ word-char? not ] split-when - [ empty? not ] filter - [ strip-apostrophes ] map - [ empty? not ] filter - [| word | - word result at 0 or 1 + word result set-at - ] each - result ; +: count-words ( sentence -- counts ) + >lower + [ [ CHAR: ' = ] [ digit? ] [ Letter? ] tri or or not ] split-when + [ [ CHAR: ' = ] trim ] map + harvest + H{ } clone + [ '[ _ inc-at ] each ] keep ;