{"id":18645329,"url":"https://github.com/danigb/elm-twelve-tone","last_synced_at":"2025-11-05T03:30:31.382Z","repository":{"id":42220606,"uuid":"59641003","full_name":"danigb/elm-twelve-tone","owner":"danigb","description":"Simple tutorial to setup a test driven project in elm-lang","archived":false,"fork":false,"pushed_at":"2016-05-25T08:43:47.000Z","size":225,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-27T11:41:33.985Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Elm","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danigb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-25T07:32:10.000Z","updated_at":"2021-02-23T05:41:23.000Z","dependencies_parsed_at":"2022-08-27T12:31:54.190Z","dependency_job_id":null,"html_url":"https://github.com/danigb/elm-twelve-tone","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danigb%2Felm-twelve-tone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danigb%2Felm-twelve-tone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danigb%2Felm-twelve-tone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danigb%2Felm-twelve-tone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danigb","download_url":"https://codeload.github.com/danigb/elm-twelve-tone/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239449552,"owners_count":19640532,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T06:15:34.045Z","updated_at":"2025-02-18T09:46:02.766Z","avatar_url":"https://github.com/danigb.png","language":"Elm","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Test first elm\n\nRead this at medium: https://medium.com/@danigb/test-first-elm-5d5cceea0efc#.bpr6l1yxy\n\nI've started to learn elm-lang. It's seems to be a wondeful but strange language for people like me coming from an imperative (and mostly object oriented) languages background.\n\nWhile REPLs are great to do exploratory work and understand how things works in a programming language I'm more used to test driven approach that allows that exploratory approach with the adventage of persisted files (so I can come back to remember what I did).\n\nSurprisely I didn't found a quick guide to test driven elm programming (probably tests are not as fundamental in elm as in other languages due it's strong types nature). Anyway, this is my two cents.\n\n## Etudes Op. 1: Twelve tone\n\nI'm reading \"Notes from the Metalevel: An Introduction to Computer Composition\" so, for the test I'll steal some examples from Chapter 9 that implements some basic [twelve-tone](https://en.wikipedia.org/wiki/Twelve-tone_technique) and [set-theory] functions. If you don't know what is all about, don't worry, they are very simple functions.\n\nThe book it's used to be online, but I can't found it now. If you're interested, you can read an excerpt from the [Chapter 9 here](https://github.com/danigb/elm-twelve-tone/blob/master/ch9.pdf) but it's not necessary at all to follow this tutorial.\n\n## Project setup\n\nFirst thing to do is install elm 0.17. This is the latest version at the time of this writing and it brings big changes, starting with elm lang internals and covering elm package names, so it's important to ensure you have this version installed.\n\nThen, we will use `elm-package` cli to generate the `elm-package.json` automatically and download the dependencies (we'll use `elm-lang/core` and `elm-community/test` packages):\n\n```bash\nmkdir elm-twelve-tone\ncd elm-twelve-tone\nmkdir src # source files will be here\nmkdir test # test files will be here\nelm-package install -y elm-lang/core\nelm-package install -y elm-community/test\n```\n\nThe `-y` option allows `elm-package` to do it's work without asking questions, but it's completely optional.\n\n## Write the first test\n\nThe first function will convert from midi note numbers to pitch class numbers. In test driven programming, we start writing the test, so here is our first `test/TwelveToneTest.elm` attempt:\n\n```elm\nimport ElmTest exposing (..)\n\ntests : Test\ntests =\n  suite \"Chapter 9: Etudes, Op. 3: Iteration, Rows and Sets\"\n  [\n    test \"noteToPc\" (assertEqual 7 (noteToPc 55))\n  ]\n\nmain =\n    runSuite tests\n```\n\nNothing very fancy here: just importing ElmTest (from `elm-community/test`), create a function that returns a `suite` of tests and declaring a `main` function that uses `runSuite` to run the tests.\n\nNotice that, unlike Javascript, the expected value goes on the left. This is not important to pass the test, but matters when dealing with test assertion errors.\n\n## Run tests\n\nTo run the tests we need first to compile to javascript (using `elm-make`) and then run the result with `node`. Let's make a script:\n\n```sh\n#!/bin/bash\nmkdir -p tmp\nrm tmp/run.js\nelm-make test/TwelveToneTest.elm --output tmp/run.js\nnode tmp/run.js\n```\n\nElm is well known for it's detailed and helpful error messages. Here's our:\n\n```\n-- NAMING ERROR ---------------------------------------- test/TwelveToneTest.elm\n\nCannot find variable `noteToPc`\n\n7|           test \"noteToPc\" (assertEqual 7 (noteToPc 55))\n                                             ^^^^^^^^\n```\n\n## Write the actual code\n\nLet's write the actual code. The pitch class numbers are just the note numbers without the octave information. Since there are 12 notes per octave, and for each octave the first pitch class is 0 and the last is 11, we just only need to apply module operator:\n\n```elm\nmodule TwelveTone exposing (noteToPc)\n\ntype alias Note = Int\ntype alias Pc = Int\n\nnoteToPc : Note -\u003e Pc\nnoteToPc note =\n  note % 12\n```\n\nAs you can see, I defined a couple of type aliases, just to be clear about the purpose of the function.\n\n## Import module\n\nSince we are storing the `TwelveTone.elm` source code into the `src/` directory, we need first to change the the `elm-package.json` file so `source-directories` is `[\"src/\"]`.\n\nNow we can import the module into the tests:\n\n```elm\nimport ElmTest exposing (..)\nimport TwelveTone exposing (..)\n\ntests : Test\ntests =\n  suite \"Chapter 9: Etudes, Op. 3: Iteration, Rows and Sets\"\n  [\n    test \"noteToPc\" (assertEqual 7 (noteToPc 55))\n  ]\n\nmain =\n    runSuite tests\n```\n\nIf we run the tests now we'll see \"All tests passed\" message.\n\n## Coda: Learn some elm\n\nRemember the purpose of this setup is to do some exploratory programming and learn some test. Let's try it by implement a couple of functions from the book chapter. First a function to convert from a list of note numbers to a list of pitch class numbers. Test first:\n\n```elm\ntest \"listToPcs\" (assertEqual [7,10,2,6] (listToPcs [55,58,62,66]))\n```\n\nThen the code:\n\n```elm\nimport List exposing (map)\n\n...\nlistToPcs : List(Note) -\u003e List Pc\nlistToPcs notes =\n  map noteToPc notes\n```\n\nPretty straightforward. Just remember to import [map](http://package.elm-lang.org/packages/elm-lang/core/4.0.1/List#map) function from [List](http://package.elm-lang.org/packages/elm-lang/core/4.0.1/List) module.\n\nBut the next one is more tricky. We want to _normalize_ the pitch class notes, so always this first note of the list is 0 and the rest are relative to that one. Here's the test:\n\n```elm\ntest \"normalizedPcs\" (assertEqual [0,3,7,11] (normalizedPcs [55,58,62,66]))\n```\n\nBasically, we want to substract the first element of the list to the rest of the elements. In my first attempt I would write something like this:\n\n```elm\nimport List exposing (map, head)\n\n...\nnormalizedPcs : List(Note) -\u003e List Pc\nnormalizedPcs notes =\n  let\n    first = head notes\n  in\n    map (\\note -\u003e noteToPc (note - first)) notes\n```\n\nThe `(\\param -\u003e body)` construct is just an anonymouse function that receives a note and returns the pitch class number of the result of substract the first element of the list to the note. And then we map the notes with the function.\n\nLet's run our tests:\n\n```\n-- TYPE MISMATCH ------------------------------------------ ./src/TwelveTone.elm\n\nThe right argument of (-) is causing a type mismatch.\n\n22|                             note - first)\n                                       ^^^^^\n(-) is expecting the right argument to be a:\n\n    number\n\nBut the right argument is:\n\n    Maybe a\n\nHint: I always figure out the type of the left argument first and if it is\nacceptable on its own, I assume it is \"correct\" in subsequent checks. So the\nproblem may actually be in how the left and right arguments interact.\n```\n\nThe problem is that `head` doesn't return an integer as expected, but a `Maybe(Int)`. If we don't get an integer is because the list is empty, so we should return an empty list. `case` to the rescue:\n\n```elm\nnormalizedPcs : List(Note) -\u003e List Pc\nnormalizedPcs notes =\n  let\n    first = head notes\n  in\n    case first of\n      Just f -\u003e map (\\note -\u003e noteToPc (note - f)) notes\n      Nothing -\u003e []\n```\n\nThis is a very common pattern when dealing with [`Maybe`](http://package.elm-lang.org/packages/elm-lang/core/4.0.1/Maybe).\n\n## The full score\n\nYou can get the full source code at [elm-twelve-tone](https://github.com/danigb/elm-twelve-tone). I hope you find it useful.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanigb%2Felm-twelve-tone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanigb%2Felm-twelve-tone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanigb%2Felm-twelve-tone/lists"}