Upcoming SlideShare
×

# Linear structures lightning talk

731 views
618 views

Published on

very condensed version of my Lambda Jam presentation

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
731
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide
• It&apos;s fair to say the singly-linked list is the foundational linear data structure of functional programming. With it you can do most anything you can ask of a linear data structure or a linked data structure, but not always efficiently; and not just efficiency in terms of computational resources, but also expressively efficient.Disregarding for the moment range operations, let&apos;s look at the characteristics of and actions on linear data structures.Order is usually by construction, representing the order of the sequence consumed to create the structure, and/or the order individual elements were added. Sorted or even a random order is however possible.Evaluation in F# is eager by default, so you have to go out of your way to lazily evaluate anything (except sequences).Construction, peeking, and removing (or de-construction) are all the same for the singly-linked list (i.e. cons/tail), it always takes place at the beginning, but we might want a structure that mixes this up in any conceivable combination.If you work out the combinatorics, you would have a ridiculous number of potential signatures, but lazy evaluation for instance is not practical or desirable in most signatures, and there are only about a half dozen or so configurations that are uniquely useful as purely functional data structures.But the unifying theme here, indeed the concept that orients us across linear structures is sequence.
• Let&apos;s look at 3 structures available in FSharpx.Collections. If you don&apos;t know about FSharpx, it&apos;s an open source foundational library for F# maintained in Github and available through NuGet.1) Steffen Forkmann’s implementation of Vector from Clojure. It implements a hash array mapped trie internally. It’s very fast, gives you essentially all the functionality of F# Array, with the added qualities of persistence and the ability to grow and shrink efficiently at the end of the sequence. Vector is probably the most versatile and performant purely functional structure for covering most of your needs involving eager evaluation since it includes indexed lookup and update.2) Queue, a persistent implementation of the well-known FIFO structure.3) DList (append list), this structure&apos;s signature in some ways marries the Queue and the singly linked list, with the added benefit we can not only enqueue single elements, but actually append an additional DList.So let&apos;s look at an example of how we can use purely functional linear structures to efficiently express program intent.
• Let&apos;s take the example of a Multiway Forest. Many kinds of documents can assume this form, HTML, XML, JSON docs, for example. We could model the sequences represented here with any linear data structure. Let&apos;s say it&apos;s Vector for arguments sake, and let&apos;s say the task at hand is to do a breadth first traversal.
• The ease of composing linear structures lets us transform the forest segments across a range of structures. We use the canonical recursive loop over match.Our end product is a Queue so we seed it with an empty Queue, and start by transforming the top level forest segment into a DList. (Remember how sequence orients us across all linear structures? It also provides us with graceful transforms from any linear structure to any other.)Looping through the match, we deconstruct the DList into head and tail. Enqueue the data in the head element (note &quot;conj&quot; is the common function name across the FSharpx.Collections linear structures for adding an element on the right), and transform the head&apos;s children segment into a DList. This gets appended to the tail from the deconstruction and cycled through the loop. (Conveniently, &quot;append&quot; is an O(1) operation.)When the DList is empty output the accumulated Queue.
• ### Linear structures lightning talk

1. 1. • Order by construction / sorted / random • Evaluation eager / lazy • Peek first / last / indexed • Construction first / last / insert • Remove (Deconstruct) first / last / indexed  choose 1  choose 1  choose 1 – 2, or #3  choose 0 – 2, or #3  choose 0 – 2, or #3 (insert only for sorted & random)
2. 2. Vector 54321 Construct Deconstruct [ ] ; ; Queue ::1 ;; Deconstruct Construct DList ::1 ;; Construct Deconstruct Construct …
3. 3. Multiway Forest
4. 4. Breadth 1st Traversal let inline breadth1stForest forest = let rec loop acc dl = match dl with | DList.Nil -> acc | DList.Cons(head, tail) -> loop (Queue.conj head.Root acc) (DList.append tail (DList.ofSeq head.Children)) loop Queue.empty (DList.ofSeq forest)