A Ramble Through Erlang IO Lists
Programming in the 21st Century - James Hague - June 13, 2010The IO List is a handy data type in Erlang, but not one that’s not often discussed in tutorials. It’s any binary. Or any list containing integers between 0 and 255. Or any arbitrarily nested list containing either of those two things. Like this:
[10, 20, "hello", <<"hello",65>>, [<<1,2,3>>, 0, 255]]
The key to IO lists is that you never flatten them. They get passed directly into low-level runtime functions (such as file:write_file), and the flattening happens without eating up any space in your Erlang process. Take advantage of that! Instead of appending values to lists, use nesting instead. For example, here’s a function to put a string in quotes:
quote(String) -> $" ++ String ++ $".
If you’re working with IO lists, you can avoid the append operations completely (and the second “++” above results in an entirely new version of String being created). This version uses nesting instead:
quote(String) -> [$", String, $"].
This creates three list elements no matter how long the initial string is. The first version creates length(String) + 2 elements. It’s also easy to go backward and un-quote the string: just take the second list element. Once you get used to nesting you can avoid most append operations completely.
One thing that that nested list trick is handy for is manipulating filenames. Want to add a directory name and “.png” extension to a filename? Just do this:
[Directory, $/, Filename, ".png"]
Unfortunately, filenames in the file module are not true IO lists. You can pass in deep lists, but they get flattened by an Erlang function (file:file_name/1), not the runtime system. That means you can still dodge appending lists in your own code, but things aren’t as efficient behind the scenes as they could be. And “deep lists” in this case means only lists, not binaries. Strangely, these deep lists can also contain atoms, which get expanded via atom_to_list.
Ideally filenames would be IO lists, but for compatibility reasons there’s still the need to support atoms in filenames. That brings up an interesting idea: why not allow atoms as part of the general IO list specification? It makes sense, as the runtime system has access to the atom table, and there’s a simple correspondence between an atom and how it gets encoded in a binary; ‘atom’ is treated the same as “atom”. I find I’m often calling atom_to_list before sending data to external ports, and that would no longer be necessary.
Categories: Blogs Programming in the 21st Century
Comments
Thank you admın good post.
Posted by chat on 23 Jun 2010 at 12:27
Add comment
Erlang on Twitter
» dlsspy (Dustin Sallings): @IbnFirnas heh. The erlang parts are still solid. The currently active alerting box is arm5, failed over from a pc that died one day.
» quercialwji2 (Quercia Quinn): @MikeSmooth_ABCs http://t.co/pPiIpTCx
» levicole (Levi Kennedy): @pharkmillups the homebrew version of erlang is the most recent version, and riak requires R14B I think.
» Mgnadya (Mega Nadya Rustanti): WooooRT @erlangtriaji: Maap bude salah liat ._. RT @Mgnadya: Elaaaang bukan erlang -_- RT @erlangtriaji: sama” RT… http://t.co/O0DxJgAr
» levicole (Levi Kennedy): @pharkmillups Still seeing it. I might just have to manually install it. The version of Erlang required by Riak is not current version in HB
» quercialwji2 (Quercia Quinn): @Maro7861 http://t.co/pPiIpTCx
» quercialwji2 (Quercia Quinn): @RolArens http://t.co/pPiIpTCx
» quercialwji2 (Quercia Quinn): @kreese555 http://t.co/pPiIpTCx
» erlang (Andreas Åkre Solberg): RT @SaraJChipps: Node.js is like taking a bubble bath in JavaScript.
» trabajosit (Empleos en IT): argentina Desarrolladores - iOS, Ruby, Erlang: Estamos buscando un líder de desarrollo que quiera hacer una gran… http://t.co/zhTLpGI1
Statistics
Number of aggregated posts: 10456
Number of comments: 1445
Most recent article: February 06, 2012
Latest comments
» simple smile on Scale means Skills: Very informative article. Pretty sure people would love to go to that place for shopping. Specially to those who are…
» simplesmile on 27 January 2012: Erlang Solutions embarks on an Erlang Embedded KTP: Your article will make the world better. Thanks again and good luck to you in your life. See you next time.simplesmile
» tandblekning easewhite on 08 February 2012: Erlang Express 3-day Course in San Francisco on 8 February: ncomprehensible to me now, but in general, the usefulness and significance is overwhelmingtandblekning easewhite