Avatar of aimorris

aimorris's solution

to Acronym in the PureScript Track

Published at Feb 02 2020 · 0 comments
Test suite

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 Acronym (abbreviate)
import Effect (Effect)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
import Test.Unit.Main (runTest)

main :: Effect Unit
main = runTest suites

suites :: TestSuite
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 Data.Array (elemIndex, filter)
import Data.Maybe (fromMaybe)
import Data.String.Common (joinWith, toUpper)
import Data.String.CodeUnits (fromCharArray, toCharArray)
import Data.Char.Unicode (isUpper)
import Data.String.Regex (Regex, split)
import Data.String.Regex.Unsafe (unsafeRegex)
import Data.String.Regex.Flags (global)
import Prelude (map, (==), (||), (<<<), ($))

whitespaceRegex :: Regex
whitespaceRegex = unsafeRegex "\\s" global

uppercaseOrHead :: Array Char -> Char -> Boolean
uppercaseOrHead y x = isUpper x || (fromMaybe 0 $ elemIndex x y) == 0

toLetters :: String -> Array String
toLetters = map fromCharArray <<< map filterUOH <<< map toCharArray <<< split whitespaceRegex
  where filterUOH y = filter (uppercaseOrHead y) y

abbreviate :: String -> String
abbreviate = toUpper <<< joinWith "" <<< toLetters

Community comments

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

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?