Making a "Linked List" in Julia

1,055 views
Skip to first unread message

Emily M

unread,
Aug 22, 2012, 7:08:58 PM8/22/12
to juli...@googlegroups.com
How would I go about making something like a linked list in Julia? I saw bits and pieces of information in the docs but nothing that I could understand that well. Any help is wanted!

Stefan Karpinski

unread,
Aug 22, 2012, 7:14:30 PM8/22/12
to juli...@googlegroups.com
There's a fairly complete example List implementation here:

On Wed, Aug 22, 2012 at 7:08 PM, Emily M <esm...@gmail.com> wrote:
How would I go about making something like a linked list in Julia? I saw bits and pieces of information in the docs but nothing that I could understand that well. Any help is wanted!

--
 
 
 

Patrick O'Leary

unread,
Aug 22, 2012, 9:50:17 PM8/22/12
to juli...@googlegroups.com
I've also written a doubly-linked list, which you can grab here:

https://github.com/pao/julia/blob/storage/llru/extras/dllist.jl

Depending on your application, you might still be better served by an Array. I wrote this implementation to use as a backing store for the LRU cache now found in extras/, but it wasn't a clear win, and fell down as the structure got into the hundreds of thousands of elements.

Kevin Squire

unread,
Aug 22, 2012, 10:21:10 PM8/22/12
to juli...@googlegroups.com
The show() for list.jl needs a small patch (adding "io," before "l::List{T}") that I haven't gotten around to committing.  (Do the print and show calls in this method also need "io"?)

Kevin

Patrick O'Leary

unread,
Aug 22, 2012, 10:55:43 PM8/22/12
to juli...@googlegroups.com
dllist.jl probably needs the same thing. I consider it archived so I don't plan to fix it, but it would pretty much work the same way.

Stefan Karpinski

unread,
Aug 22, 2012, 11:16:09 PM8/22/12
to juli...@googlegroups.com
Yes, all user-defined show methods need an io object as their first argument. This is kind of an easy trap that I've fallen into and then wondered why my show method doesn't work. The old business of printing to the current output stream was convenient, but it was terrible for performance.

--
 
 
 

Kevin Squire

unread,
Aug 22, 2012, 11:47:16 PM8/22/12
to juli...@googlegroups.com
Just to clarify, in addition to the method definition, the any calls to show/print also should have an io object as their first argument, then?

Kevin

Stefan Karpinski

unread,
Aug 22, 2012, 11:53:27 PM8/22/12
to juli...@googlegroups.com
On Wed, Aug 22, 2012 at 11:47 PM, Kevin Squire <kevin....@gmail.com> wrote:
Just to clarify, in addition to the method definition, the any calls to show/print also should have an io object as their first argument, then?

Calls don't have to because there's a default method that uses the current output stream, but you shouldn't jam method into that single-argument version of the function, but rather add "low-level" two-argument methods that take an io argument. The hard part is enforcing not adding single-argument methods. Currently you can do that and it just has not much effect. 

Jeffrey Sarnoff

unread,
Aug 23, 2012, 2:29:03 AM8/23/12
to juli...@googlegroups.com
all of the examples I have noticed of two-argument methods that take an io argument have this partial template
   functionName(io::IO, secondArg::secondArgType)

in what circumstances would the IO in "io::IO" be something other than IO?

Toivo Henningsson

unread,
Aug 23, 2012, 3:28:23 AM8/23/12
to juli...@googlegroups.com


On Thursday, August 23, 2012 8:29:03 AM UTC+2, Jeffrey Sarnoff wrote:
all of the examples I have noticed of two-argument methods that take an io argument have this partial template
   functionName(io::IO, secondArg::secondArgType)

in what circumstances would the IO in "io::IO" be something other than IO?

For show()  show(io::IO, x::SomeType) is the template.
For print(), print(io::IO, x::SomeType) is the template used by those few types that have a natural string representation.
For those, if it just says io instead of io::IO, it's generally an oversight. (There's also a few show() methods defined before the IO type)
 
If you create your own function, you're of course free to use whatever conventions you like.
But the IO type should guarantee the existence of some basic IO operations. Which ones, I would like to know. (since I've been writing my IO types)
Reply all
Reply to author
Forward
0 new messages