diff --git a/config.json b/config.json index a875e7d..00f8385 100644 --- a/config.json +++ b/config.json @@ -1621,7 +1621,8 @@ "recursion", "strings", "errors", - "locals" + "locals", + "generics" ], "difficulty": 6 }, diff --git a/exercises/concept/bering-bearings/.docs/introduction.md b/exercises/concept/bering-bearings/.docs/introduction.md index dcd29c1..573dae6 100644 --- a/exercises/concept/bering-bearings/.docs/introduction.md +++ b/exercises/concept/bering-bearings/.docs/introduction.md @@ -91,6 +91,17 @@ against its class: a mismatch like `"x" triple` is rejected (*expected input value of type integer but got string*) rather than silently flowing through. +The output can be annotated the same way. `yes?` types both its +input and its result: + +```factor +TYPED:: yes? ( word: string -- answer: boolean ) + word "yes" = ; + +"yes" yes? . ! => t +"no" yes? . ! => f +``` + ## Math functions and constants This exercise also leans on a few words from `math.constants` diff --git a/exercises/concept/bering-bearings/.meta/exemplar.factor b/exercises/concept/bering-bearings/.meta/exemplar.factor index 377d388..1249edc 100644 --- a/exercises/concept/bering-bearings/.meta/exemplar.factor +++ b/exercises/concept/bering-bearings/.meta/exemplar.factor @@ -1,5 +1,5 @@ USING: accessors combinators kernel locals math math.constants - math.functions namespaces ; + math.functions namespaces typed ; IN: bering-bearings SYMBOLS: north east south west @@ -61,7 +61,7 @@ M: relative flip { port [ starboard ] } } case ] change-bearing ; -:: add-bearings ( a b -- x y ) +TYPED:: add-bearings ( a b -- x: number y: number ) a >cartesian :> ay :> ax b >cartesian :> by :> bx ax bx + diff --git a/exercises/concept/bering-bearings/bering-bearings/bering-bearings.factor b/exercises/concept/bering-bearings/bering-bearings/bering-bearings.factor index 79a5be0..fff141f 100644 --- a/exercises/concept/bering-bearings/bering-bearings/bering-bearings.factor +++ b/exercises/concept/bering-bearings/bering-bearings/bering-bearings.factor @@ -11,4 +11,5 @@ IN: bering-bearings ! Define `flip` as a generic word with one method per ! direction class. -! Define `add-bearings` here. +! Define `add-bearings` here, using TYPED:: to type its +! cartesian outputs: ( a b -- x: number y: number ). diff --git a/exercises/practice/bank-account/.meta/example.factor b/exercises/practice/bank-account/.meta/example.factor index 8383296..eab1f1f 100644 --- a/exercises/practice/bank-account/.meta/example.factor +++ b/exercises/practice/bank-account/.meta/example.factor @@ -3,7 +3,7 @@ IN: bank-account TUPLE: bank-account open? balance lock ; -: ( -- account ) +: ( -- account: bank-account ) bank-account new f >>open? 0 >>balance diff --git a/exercises/practice/bank-account/bank-account/bank-account.factor b/exercises/practice/bank-account/bank-account/bank-account.factor index 1caa4bd..1dd463e 100644 --- a/exercises/practice/bank-account/bank-account/bank-account.factor +++ b/exercises/practice/bank-account/bank-account/bank-account.factor @@ -1,9 +1,9 @@ -USING: kernel math typed ; +USING: kernel typed ; IN: bank-account TUPLE: bank-account ; -: ( -- account ) +: ( -- account: bank-account ) "unimplemented" throw ; TYPED:: open-account ( account: bank-account -- ) diff --git a/exercises/practice/nth-prime/nth-prime/nth-prime.factor b/exercises/practice/nth-prime/nth-prime/nth-prime.factor index 85315e1..99b55b6 100644 --- a/exercises/practice/nth-prime/nth-prime/nth-prime.factor +++ b/exercises/practice/nth-prime/nth-prime/nth-prime.factor @@ -1,4 +1,4 @@ -USING: kernel math typed ; +USING: kernel typed ; IN: nth-prime TYPED:: nth-prime ( n: integer -- prime: integer ) diff --git a/exercises/practice/say/.meta/example.factor b/exercises/practice/say/.meta/example.factor index c7bfc76..89f6517 100644 --- a/exercises/practice/say/.meta/example.factor +++ b/exercises/practice/say/.meta/example.factor @@ -1,4 +1,4 @@ -USING: kernel locals math math.order sequences strings ; +USING: kernel locals math math.order sequences strings typed ; IN: say CONSTANT: ones { "zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" @@ -31,6 +31,6 @@ CONSTANT: tens { f f "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty dup 1000 >= [ 1000 "thousand" say-chunk ] when dup 0 > [ say-below-1000 swap dup empty? [ drop ] [ swap " " glue ] if ] [ drop ] if ; -: say ( n -- str ) - dup 0 999999999999 between? [ "input out of range" throw ] unless - dup 0 = [ drop "zero" ] [ say-positive ] if ; +TYPED:: say ( n: integer -- str: string ) + n 0 999999999999 between? [ "input out of range" throw ] unless + n 0 = [ "zero" ] [ n say-positive ] if ; diff --git a/exercises/practice/say/say/say.factor b/exercises/practice/say/say/say.factor index 80ba3d2..ce3aec2 100644 --- a/exercises/practice/say/say/say.factor +++ b/exercises/practice/say/say/say.factor @@ -1,5 +1,5 @@ -USING: kernel ; +USING: kernel typed ; IN: say -: say ( n -- str ) +TYPED:: say ( n: integer -- str: string ) "unimplemented" throw ;