Étude 5-2: Using the re Module

Write a module named dates that contains a function date_parts/1, which takes a string in ISO date format ("yyyy-mm-dd") and returns a list of integers in the form [yyyy, mm, dd]. This function does not need to do any error checking.

You’ll use the re:split/3 function from Erlang’s regular expression (re) module to accomplish the task. How, you may ask, does that function work? Ask Erlang! The command erl -man re will give you the online documentation for the re module.

Scroll down the resulting page until you find split(Subject, RE, Options) → SplitList and read the examples.

When you write the -spec for this function (you have been writing documentation for your functions, haven’t you?), the type you will use for the parameter is string().

Note

You can see a complete list of the built-in types at http://www.erlang.org/doc/reference_manual/typespec.html

Yes, I know this étude seems pointless, but trust me: I’m going somewhere with this. Stay tuned.

See a suggested solution in Appendix A.

Get Études for Erlang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.