]> gitweb.factorcode.org Git - factor.git/commitdiff
gilded-rose: new vocab 2956/head
authorAlexander Ilin <alex.ilin@protonmail.com>
Sun, 3 Mar 2024 20:34:34 +0000 (21:34 +0100)
committerAlexander Ilin <alex.ilin@protonmail.com>
Sun, 3 Mar 2024 20:35:24 +0000 (21:35 +0100)
Translated from:
https://github.com/emilybache/GildedRose-Refactoring-Kata/blob/main/Delphi/GildedRose.pas
https://github.com/emilybache/GildedRose-Refactoring-Kata/blob/main/Delphi/TextTestFixture.dpr

extra/gilded-rose/authors.txt [new file with mode: 0644]
extra/gilded-rose/gilded-rose-docs.factor [new file with mode: 0644]
extra/gilded-rose/gilded-rose.factor [new file with mode: 0644]

diff --git a/extra/gilded-rose/authors.txt b/extra/gilded-rose/authors.txt
new file mode 100644 (file)
index 0000000..8e1955f
--- /dev/null
@@ -0,0 +1 @@
+Alexander Ilin
diff --git a/extra/gilded-rose/gilded-rose-docs.factor b/extra/gilded-rose/gilded-rose-docs.factor
new file mode 100644 (file)
index 0000000..6436290
--- /dev/null
@@ -0,0 +1,15 @@
+! Copyright (C) 2024 Alexander Ilin.
+! See https://factorcode.org/license.txt for BSD license.
+USING: help.markup help.syntax kernel strings ;
+IN: gilded-rose
+
+ARTICLE: "gilded-rose" "gilded-rose"
+"The " { $vocab-link "gilded-rose" } " vocab contains the Gilded Rose kata, "
+"which is a bit of source code for you to practice refactoring. See the history "
+"behind the original code, as well as the task description in the READMEs "
+"here: " { $url "https://github.com/emilybache/GildedRose-Refactoring-Kata" }
+"."
+$nl
+"The Factor source is based on the Delphi translation of the C# original." ;
+
+ABOUT: "gilded-rose"
diff --git a/extra/gilded-rose/gilded-rose.factor b/extra/gilded-rose/gilded-rose.factor
new file mode 100644 (file)
index 0000000..034a019
--- /dev/null
@@ -0,0 +1,98 @@
+! Copyright (C) 2024 Alexander Ilin.
+! See https://factorcode.org/license.txt for BSD license.
+USING: accessors command-line formatting io kernel make math
+math.parser namespaces sequences ;
+IN: gilded-rose
+
+TUPLE: GildedRose
+    Items ;
+
+TUPLE: Item
+    Name SellIn Quality ;
+
+: Item.ToString ( item -- str )
+    [ Name>> ] [ SellIn>> ] [ Quality>> ] tri "%s %d %d" sprintf ;
+
+:: GildedRose.UpdateQuality ( this -- )
+    this Items>> length [| i |
+        i this Items>> nth Name>> "Aged Brie" = not
+        i this Items>> nth Name>> "Backstage passes to a TAFKAL80ETC concert" = not
+        and [
+            i this Items>> nth Quality>> 0 > [
+                i this Items>> nth Name>> "Sulfuras, Hand of Ragnaros" = not [
+                    i this Items>> nth Quality>> 1 -
+                    i this Items>> nth Quality<<
+                ] when
+            ] when
+        ] [
+            i this Items>> nth Quality>> 50 < [
+                i this Items>> nth Quality>> 1 +
+                i this Items>> nth Quality<<
+                i this Items>> nth Name>> "Backstage passes to a TAFKAL80ETC concert" = [
+                    i this Items>> nth SellIn>> 11 < [
+                        i this Items>> nth Quality>> 50 < [
+                            i this Items>> nth Quality>> 1 +
+                            i this Items>> nth Quality<<
+                        ] when
+                    ] when
+                    i this Items>> nth SellIn>> 6 < [
+                        i this Items>> nth Quality>> 50 < [
+                            i this Items>> nth Quality>> 1 +
+                            i this Items>> nth Quality<<
+                        ] when
+                    ] when
+                ] when
+            ] when
+        ] if
+
+        i this Items>> nth Name>> "Sulfuras, Hand of Ragnaros" = not [
+            i this Items>> nth SellIn>> 1 -
+            i this Items>> nth SellIn<<
+        ] when
+
+        i this Items>> nth SellIn>> 0 < [
+            i this Items>> nth Name>> "Aged Brie" = not [
+                i this Items>> nth Name>> "Backstage passes to a TAFKAL80ETC concert" = [
+                    i this Items>> nth Quality>> 0 > [
+                        i this Items>> nth Name>> "Sulfuras, Hand of Ragnaros" = not [
+                            i this Items>> nth Quality>> 1 -
+                            i this Items>> nth Quality<<
+                        ] when
+                    ] when
+                ] [
+                    i this Items>> nth Quality>> i this Items>> nth Quality>> -
+                    i this Items>> nth Quality<<
+                ] if
+            ] [
+                i this Items>> nth Quality>> 50 < [
+                    i this Items>> nth Quality>> 1 +
+                    i this Items>> nth Quality<<
+                ] when
+            ] if
+        ] when
+    ] each-integer ;
+
+: main ( -- )
+    "OMGHAI!" print
+    [
+        "+5 Dexterity Vest" 10 20 Item boa ,
+        "Aged Brie" 2 0 Item boa ,
+        "Elixir of the Mongoose" 5 7 Item boa ,
+        "Sulfuras, Hand of Ragnaros" 0 80 Item boa ,
+        "Sulfuras, Hand of Ragnaros" -1 80 Item boa ,
+        "Backstage passes to a TAFKAL80ETC concert" 15 20 Item boa ,
+        "Backstage passes to a TAFKAL80ETC concert" 10 49 Item boa ,
+        "Backstage passes to a TAFKAL80ETC concert" 5 49 Item boa ,
+        ! This conjured item does not work properly yet.
+        "Conjured Mana Cake" 3 6 Item boa ,
+    ] { } make GildedRose boa
+
+    command-line get [ 2 ] [ first dec> ] if-empty [
+        "-------- day %d --------\n" printf
+        "name, sellIn, quality" print
+        dup Items>> [ Item.ToString print ] each
+        nl
+        GildedRose.UpdateQuality
+    ] with each-integer ;
+
+MAIN: main