Created
August 19, 2016 11:34
-
-
Save MikeMKH/be82f72c1037aba81a46a9aeefdd0698 to your computer and use it in GitHub Desktop.
Coin Changer kata in F# using List.mapFold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8" ?> | |
| <configuration> | |
| <runtime> | |
| <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | |
| <dependentAssembly> | |
| <assemblyIdentity name="FSharp.Core" | |
| publicKeyToken="b03f5f7f11d50a3a" | |
| culture="neutral"/> | |
| <bindingRedirect oldVersion="4.3.1.0" | |
| newVersion="4.4.0.0"/> | |
| </dependentAssembly> | |
| </assemblyBinding> | |
| </runtime> | |
| </configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace CoinChanger | |
| module Changer = | |
| let map coins amount = | |
| List.mapFold | |
| (fun amount coin -> (amount / coin, amount % coin)) | |
| amount | |
| coins | |
| |> fst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace CoinChanger.Tests | |
| module ChangerTests = | |
| open Xunit | |
| open Swensen.Unquote | |
| open CoinChanger.Changer | |
| [<Fact>] | |
| let ``given a full register and 0 cents it must return 0 coins`` () = | |
| test <@ map [25; 10; 5; 1] 0 = [0; 0; 0; 0] @> | |
| [<Fact>] | |
| let ``given a full register and 99 cents it must return 3 quarters, 2 dimes, 0 nickels, and 4 pennies`` () = | |
| test <@ map [25; 10; 5; 1] 99 = [3; 2; 0; 4] @> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did not really TDD this out, I just want to revisit the List.mapFold function.