Avatar of erszcz

erszcz's solution

to Acronym in the PureScript Track

Published at Jul 13 2018 · 1 comment
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).


Julien Vanier https://github.com/monkbroc

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


module Test.Main where

import Prelude
import Test.Unit.Assert as Assert
import Acronym (abbreviate)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.AVar (AVAR)
import Control.Monad.Eff.Console (CONSOLE)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Console (TESTOUTPUT)
import Test.Unit.Main (runTest)

main :: forall eff
  . Eff ( avar :: AVAR
        , console :: CONSOLE
        , testOutput :: TESTOUTPUT
        | eff                     
main = runTest suites

suites :: forall e. TestSuite e
suites = do
  suite "Acronym.abbreviate" do
    test "acronyms from title case" $
        "PNG" $
        abbreviate "Portable Networks Graphic"

    test "acronyms from lower case" $
        "ROR" $
        abbreviate "Ruby on Rails"

    test "acronyms from inconsistent case" $
        "HTML" $
        abbreviate "HyperText Markup Language"

    test "punctuation is ignored" $
        "FIFO" $
        abbreviate "First in, First out"

    test "acronyms ignoring punctuation and casing" $
        "CMOS" $
        abbreviate "Complementary Metal-Oxide semiconductor"
module Acronym
  ( abbreviate
  ) where

import Prelude
import Data.Array as A
import Data.Char as C
import Data.Char.Unicode as U
import Data.Maybe ( Maybe(..) )
import Data.String as String
import Data.Tuple ( Tuple(..) )

abbreviate string =
  let (Tuple abbr _) = A.foldl step (Tuple [] []) $ String.toCharArray string
   in String.toUpper $ String.fromCharArray abbr

step (Tuple abbr last) c
  | U.isLetter c && last == [] =
	  Tuple (A.snoc abbr c) [c]
  | U.isLower c =
	  Tuple abbr [c]
  | U.isSpace c =
	  Tuple abbr []
  | U.isUpper c && last /= [] =
	  Tuple (A.snoc abbr c) [c]
  | otherwise =
	  Tuple abbr []

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of icyrockcom

This one is covered by otherwise: | U.isSpace c = Tuple abbr []

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?