From 934118e5c73a7cbd16fb3d5461f87c765bab4551 Mon Sep 17 00:00:00 2001 From: Joshua Stoutenburg Date: Thu, 25 Aug 2016 21:31:54 -0600 Subject: [PATCH 1/3] Adds Gigasecond Exercise --- config.json | 3 +- elm-package.json | 3 +- exercises/gigasecond/Gigasecond.elm | 1 + exercises/gigasecond/Gigasecond.example | 121 +++++++++++++++++++++++ exercises/gigasecond/GigasecondTests.elm | 38 +++++++ exercises/gigasecond/elm-package.json | 16 +++ exercises/gigasecond/runtests.bat | 1 + exercises/gigasecond/runtests.sh | 2 + 8 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 exercises/gigasecond/Gigasecond.elm create mode 100644 exercises/gigasecond/Gigasecond.example create mode 100644 exercises/gigasecond/GigasecondTests.elm create mode 100644 exercises/gigasecond/elm-package.json create mode 100644 exercises/gigasecond/runtests.bat create mode 100644 exercises/gigasecond/runtests.sh diff --git a/config.json b/config.json index 3e020c7..18af404 100644 --- a/config.json +++ b/config.json @@ -31,7 +31,8 @@ "atbash-cipher", "say", "largest-series-product", - "roman-numerals" + "roman-numerals", + "gigasecond" ], "exercises": [ { diff --git a/elm-package.json b/elm-package.json index 1d6b953..2304e34 100644 --- a/elm-package.json +++ b/elm-package.json @@ -32,7 +32,8 @@ "./exercises/atbash-cipher", "./exercises/say", "./exercises/largest-series-product", - "./exercises/roman-numerals" + "./exercises/roman-numerals", + "./exercises/gigasecond" ], "exposed-modules": [], "dependencies": { diff --git a/exercises/gigasecond/Gigasecond.elm b/exercises/gigasecond/Gigasecond.elm new file mode 100644 index 0000000..399c768 --- /dev/null +++ b/exercises/gigasecond/Gigasecond.elm @@ -0,0 +1 @@ +module Gigasecond exposing (add) diff --git a/exercises/gigasecond/Gigasecond.example b/exercises/gigasecond/Gigasecond.example new file mode 100644 index 0000000..8ee223c --- /dev/null +++ b/exercises/gigasecond/Gigasecond.example @@ -0,0 +1,121 @@ +module Gigasecond exposing (add) + +import Date +import Time +import String + + +add : String -> Result String String +add x = + x + |> Date.fromString + |> Result.map (add' gigasecond) + |> Result.map format + + +add' : Time.Time -> Date.Date -> Date.Date +add' t d = + d + |> Date.toTime + |> flip (+) t + |> Date.fromTime + + +gigasecond : Time.Time +gigasecond = + 10 ^ 12 + + +format : Date.Date -> String +format date = + let + date = + toGMT date + in + --toString date + [ Date.year date + |> toString + , "-" + , Date.month date + |> monthToInt + |> Maybe.withDefault 1 + |> toString + |> String.pad 2 '0' + , "-" + , Date.day date + |> toString + |> String.pad 2 '0' + , "T" + , Date.hour date + |> toString + |> String.pad 2 '0' + , ":" + , Date.minute date + |> toString + |> String.pad 2 '0' + , ":" + , Date.second date + |> toString + |> String.pad 2 '0' + ] + |> String.concat + + +monthToInt : Date.Month -> Maybe Int +monthToInt n = + case n of + Date.Jan -> + Just 1 + + Date.Feb -> + Just 2 + + Date.Mar -> + Just 3 + + Date.Apr -> + Just 4 + + Date.May -> + Just 5 + + Date.Jun -> + Just 6 + + Date.Jul -> + Just 7 + + Date.Aug -> + Just 8 + + Date.Sep -> + Just 9 + + Date.Oct -> + Just 10 + + Date.Nov -> + Just 11 + + Date.Dec -> + Just 12 + + +toGMT : Date.Date -> Date.Date +toGMT date = + let + tzOffset = + date + |> toString + |> String.split " " + |> List.drop 5 + |> List.head + |> Maybe.withDefault "GMT-0000" + |> String.dropLeft 3 + |> String.toInt + |> Result.withDefault 0 + |> flip (//) 100 + |> toFloat + |> (*) -Time.hour + in + add' tzOffset date diff --git a/exercises/gigasecond/GigasecondTests.elm b/exercises/gigasecond/GigasecondTests.elm new file mode 100644 index 0000000..6880402 --- /dev/null +++ b/exercises/gigasecond/GigasecondTests.elm @@ -0,0 +1,38 @@ +port module Main exposing (..) + +import Test.Runner.Node exposing (run) +import Json.Encode exposing (Value) +import Test exposing (..) +import Expect +import Gigasecond exposing (add) + + +tests : Test +tests = + describe "Gigasecond" + [ describe "add" + [ test "2011-04-25" <| + \() -> + Expect.equal (Ok "2043-01-01T01:46:40") (Gigasecond.add "2011-04-25") + , test "1977-06-13" <| + \() -> + Expect.equal (Ok "2009-02-19T01:46:40") (Gigasecond.add "1977-06-13") + , test "1959-07-19" <| + \() -> + Expect.equal (Ok "1991-03-27T01:46:40") (Gigasecond.add "1959-07-19") + , test "full time specified" <| + \() -> + Expect.equal (Ok "2046-10-02T23:46:40") (Gigasecond.add "2015-01-24T22:00:00") + , test "full time with day roll-over" <| + \() -> + Expect.equal (Ok "2046-10-03T01:46:39") (Gigasecond.add "2015-01-24T23:59:59") + ] + ] + + +main : Program Value +main = + run emit tests + + +port emit : ( String, Value ) -> Cmd msg diff --git a/exercises/gigasecond/elm-package.json b/exercises/gigasecond/elm-package.json new file mode 100644 index 0000000..82d2ab6 --- /dev/null +++ b/exercises/gigasecond/elm-package.json @@ -0,0 +1,16 @@ +{ + "version": "3.0.0", + "summary": "Exercism problems in Elm.", + "repository": "https://github.com/exercism/xelm.git", + "license": "BSD3", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "elm-lang/core": "4.0.0 <= v < 5.0.0", + "elm-community/elm-test": "2.0.0 <= v < 3.0.0", + "rtfeldman/node-test-runner": "2.0.0 <= v < 3.0.0" + }, + "elm-version": "0.17.0 <= v < 0.18.0" +} diff --git a/exercises/gigasecond/runtests.bat b/exercises/gigasecond/runtests.bat new file mode 100644 index 0000000..9ce824b --- /dev/null +++ b/exercises/gigasecond/runtests.bat @@ -0,0 +1 @@ +ECHO We've changed how tests are run! Please review the latest install/running docs at http://exercism.io/languages/elm and report any issues at https://github.com/exercism/xelm diff --git a/exercises/gigasecond/runtests.sh b/exercises/gigasecond/runtests.sh new file mode 100644 index 0000000..6885122 --- /dev/null +++ b/exercises/gigasecond/runtests.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +echo "We've changed how tests are run! Please review the latest install/running docs at http://exercism.io/languages/elm and report any issues at https://github.com/exercism/xelm" From bb4b15ce0ca82dc0206cb3788c4243263e7cbc6a Mon Sep 17 00:00:00 2001 From: Joshua Stoutenburg Date: Sun, 28 Aug 2016 22:01:33 -0600 Subject: [PATCH 2/3] Refactor Gigasecond.add to Speak Date Instead of String --- exercises/gigasecond/Gigasecond.example | 113 +---------------------- exercises/gigasecond/GigasecondTests.elm | 21 ++++- 2 files changed, 19 insertions(+), 115 deletions(-) diff --git a/exercises/gigasecond/Gigasecond.example b/exercises/gigasecond/Gigasecond.example index 8ee223c..c6526b8 100644 --- a/exercises/gigasecond/Gigasecond.example +++ b/exercises/gigasecond/Gigasecond.example @@ -2,120 +2,13 @@ module Gigasecond exposing (add) import Date import Time -import String -add : String -> Result String String -add x = - x - |> Date.fromString - |> Result.map (add' gigasecond) - |> Result.map format - - -add' : Time.Time -> Date.Date -> Date.Date -add' t d = - d - |> Date.toTime - |> flip (+) t - |> Date.fromTime +add : Date.Date -> Date.Date +add = + Date.toTime >> (+) gigasecond >> Date.fromTime gigasecond : Time.Time gigasecond = 10 ^ 12 - - -format : Date.Date -> String -format date = - let - date = - toGMT date - in - --toString date - [ Date.year date - |> toString - , "-" - , Date.month date - |> monthToInt - |> Maybe.withDefault 1 - |> toString - |> String.pad 2 '0' - , "-" - , Date.day date - |> toString - |> String.pad 2 '0' - , "T" - , Date.hour date - |> toString - |> String.pad 2 '0' - , ":" - , Date.minute date - |> toString - |> String.pad 2 '0' - , ":" - , Date.second date - |> toString - |> String.pad 2 '0' - ] - |> String.concat - - -monthToInt : Date.Month -> Maybe Int -monthToInt n = - case n of - Date.Jan -> - Just 1 - - Date.Feb -> - Just 2 - - Date.Mar -> - Just 3 - - Date.Apr -> - Just 4 - - Date.May -> - Just 5 - - Date.Jun -> - Just 6 - - Date.Jul -> - Just 7 - - Date.Aug -> - Just 8 - - Date.Sep -> - Just 9 - - Date.Oct -> - Just 10 - - Date.Nov -> - Just 11 - - Date.Dec -> - Just 12 - - -toGMT : Date.Date -> Date.Date -toGMT date = - let - tzOffset = - date - |> toString - |> String.split " " - |> List.drop 5 - |> List.head - |> Maybe.withDefault "GMT-0000" - |> String.dropLeft 3 - |> String.toInt - |> Result.withDefault 0 - |> flip (//) 100 - |> toFloat - |> (*) -Time.hour - in - add' tzOffset date diff --git a/exercises/gigasecond/GigasecondTests.elm b/exercises/gigasecond/GigasecondTests.elm index 6880402..6bcf89c 100644 --- a/exercises/gigasecond/GigasecondTests.elm +++ b/exercises/gigasecond/GigasecondTests.elm @@ -4,6 +4,7 @@ import Test.Runner.Node exposing (run) import Json.Encode exposing (Value) import Test exposing (..) import Expect +import Date import Gigasecond exposing (add) @@ -13,23 +14,33 @@ tests = [ describe "add" [ test "2011-04-25" <| \() -> - Expect.equal (Ok "2043-01-01T01:46:40") (Gigasecond.add "2011-04-25") + Expect.equal (actual "2011-04-25") (expected "2043-01-01T01:46:40") , test "1977-06-13" <| \() -> - Expect.equal (Ok "2009-02-19T01:46:40") (Gigasecond.add "1977-06-13") + Expect.equal (actual "1977-06-13") (expected "2009-02-19T01:46:40") , test "1959-07-19" <| \() -> - Expect.equal (Ok "1991-03-27T01:46:40") (Gigasecond.add "1959-07-19") + Expect.equal (actual "1959-07-19") (expected "1991-03-27T01:46:40") , test "full time specified" <| \() -> - Expect.equal (Ok "2046-10-02T23:46:40") (Gigasecond.add "2015-01-24T22:00:00") + Expect.equal (actual "2015-01-24T22:00:00") (expected "2046-10-02T23:46:40") , test "full time with day roll-over" <| \() -> - Expect.equal (Ok "2046-10-03T01:46:39") (Gigasecond.add "2015-01-24T23:59:59") + Expect.equal (actual "2015-01-24T23:59:59") (expected "2046-10-03T01:46:39") ] ] +actual : String -> Result String Date.Date +actual = + Date.fromString >> Result.map Gigasecond.add + + +expected : String -> Result String Date.Date +expected = + Date.fromString + + main : Program Value main = run emit tests From 071fed782d83511556a5683d603b3c04bb6c0773 Mon Sep 17 00:00:00 2001 From: Joshua Stoutenburg Date: Tue, 30 Aug 2016 08:31:37 -0600 Subject: [PATCH 3/3] Gigasecond: Refactor Indirection out of Test --- exercises/gigasecond/GigasecondTests.elm | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/exercises/gigasecond/GigasecondTests.elm b/exercises/gigasecond/GigasecondTests.elm index 6bcf89c..ccc291f 100644 --- a/exercises/gigasecond/GigasecondTests.elm +++ b/exercises/gigasecond/GigasecondTests.elm @@ -14,31 +14,31 @@ tests = [ describe "add" [ test "2011-04-25" <| \() -> - Expect.equal (actual "2011-04-25") (expected "2043-01-01T01:46:40") + Expect.equal (date "2043-01-01T01:46:40") (Gigasecond.add (date "2011-04-25")) , test "1977-06-13" <| \() -> - Expect.equal (actual "1977-06-13") (expected "2009-02-19T01:46:40") + Expect.equal (date "2009-02-19T01:46:40") (Gigasecond.add (date "1977-06-13")) , test "1959-07-19" <| \() -> - Expect.equal (actual "1959-07-19") (expected "1991-03-27T01:46:40") + Expect.equal (date "1991-03-27T01:46:40") (Gigasecond.add (date "1959-07-19")) , test "full time specified" <| \() -> - Expect.equal (actual "2015-01-24T22:00:00") (expected "2046-10-02T23:46:40") + Expect.equal (date "2046-10-02T23:46:40") (Gigasecond.add (date "2015-01-24T22:00:00")) , test "full time with day roll-over" <| \() -> - Expect.equal (actual "2015-01-24T23:59:59") (expected "2046-10-03T01:46:39") + Expect.equal (date "2046-10-03T01:46:39") (Gigasecond.add (date "2015-01-24T23:59:59")) ] ] -actual : String -> Result String Date.Date -actual = - Date.fromString >> Result.map Gigasecond.add +date : String -> Date.Date +date input = + case Date.fromString input of + Ok date -> + date - -expected : String -> Result String Date.Date -expected = - Date.fromString + Err reason -> + Debug.crash reason main : Program Value