Sha256: 1a1a9cfb3a7e7199fbafca4d87ab435c59758972d4b358a268f5cc712df9eb2e

Contents?: true

Size: 891 Bytes

Versions: 84

Compression:

Stored size: 891 Bytes

Contents

-module(phone).

-export([number/1, areacode/1, pretty_print/1]).

-define(ZEROS, "0000000000").

number(String) ->
  Digits = digits(String),
  case length(Digits) of
    10 -> Digits;
    11 -> strip_one(Digits);
    _ -> ?ZEROS
  end.

% Strips a leading '1' off the front of a string.
strip_one([49|T]) -> T;
strip_one([_P|_T]) -> ?ZEROS.

parts(Number) ->
  PhoneNumber = number(Number),
  AreaCode = string:sub_string(PhoneNumber, 1, 3),
  Exchange = string:sub_string(PhoneNumber, 4, 6),
  Subscriber = string:sub_string(PhoneNumber, 7, 10),
  {AreaCode, Exchange, Subscriber}.

digits(String) ->
  re:replace(String, "\\D", "", [global, {return, list}]).

areacode(String) ->
  {AreaCode, _, _} = parts(String),
  AreaCode.

pretty_print(String) ->
  {AreaCode, Exchange, Subscriber} = parts(String),
  lists:flatten(io_lib:format("\(~s\) ~s-~s", [AreaCode, Exchange, Subscriber])).

Version data entries

84 entries across 84 versions & 1 rubygems

Version Path
trackler-2.0.6.28 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.27 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.26 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.25 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.24 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.23 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.22 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.21 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.20 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.19 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.18 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.17 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.16 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.15 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.14 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.13 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.12 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.11 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.10 tracks/erlang/exercises/phone-number/example.erl
trackler-2.0.6.9 tracks/erlang/exercises/phone-number/example.erl