Avatar of tex

tex's solution

to Acronym in the PureScript Track

Published at Mar 29 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 Prelude

import Control.Alt ((<|>))
import Data.Array ((:))
import Data.Char.Unicode (isLetter, isLower)
import Data.Either (Either, either)
import Data.String.CodeUnits (fromCharArray)
import Data.String.Common (toUpper)
import Text.Parsing.Parser (ParseError, ParserT, runParser)
import Text.Parsing.Parser.Combinators (skipMany, try)
import Text.Parsing.Parser.String (satisfy)
import Text.Parsing.Parser.Token (letter, upper)

skipManyNotLetters :: ∀ m. Monad m ⇒ ParserT String m Unit
skipManyNotLetters = skipMany $ satisfy $ not <<< isLetter

remainingLetters :: ∀ m. Monad m ⇒ ParserT String m (Array Char)
remainingLetters = do
  l ← try ((skipMany $ satisfy isLower) *> upper)
    <|> try (skipMany letter *> skipManyNotLetters *> letter)
  ls ← remainingLetters <|> pure []
  pure (l : ls)

parser :: String → Either ParseError (Array Char)
parser x = runParser x do
  l ← skipManyNotLetters *> letter
  ls ← remainingLetters <|> pure []
  pure (l : ls)

abbreviate :: String → String
abbreviate = (either show $ toUpper <<< fromCharArray) <<< parser

Community comments

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

tex's Reflection

Parser noob here. This seemed like a good opportunity to get some practice.