Get an Epic Experience with Premium

LibLinq-1.0

Libraries Login to Add Favorites
  • 0 Likes
  • World of Warcraft
  • 12 Monthly Downloads
  • Supports: 4.0.3a
  • 1,187 Total Downloads
  • Updated 01/14/2011
  • Created 08/30/2010
  • 0 Favorites
  • Project Site
  • Release Type: Inactive
  • License: MIT License
  • Newest File: v40000
or

About LibLinq-1.0

LibLinq-1.0 is a collection management library similar to .NET's System.Linq.

It provides a very functional approach to dealing with collections and sequences. Rather than handling collection data imperatively, it is handled declaratively.

It also is thoroughly unit-tested.

The library is built around the concept of an Enumerable, which nearly all collections could be theoretically classified as.

Many generators are provided, such as Enumerable.Range(0, 10), which generates an increasing sequence starting at 0 with a count of 10. See the API documentation for a list of more generators.

On any Enumerable, there are many methods that can be called to lazily gleam data or generate other Enumerables.

In this example:

tostring(Enumerable.From({ 1, 2, 3 }):Select(function(x) return x * x end)) == "[1, 4, 9]"
-- or
tostring(Enumerable.From({ 1, 2, 3 }):Select("x => x*x")) == "[1, 4, 9]"

Given an initial Enumerable containing the elements 1, 2, and 3, they are each multiplied by themselves to provide an enumerable containing 1, 4, and 9.

The other typical case is filtering down a larger sequence to a smaller one

tostring(Enumerable.From({ 1, 2, 3, 4 }):Where(function(x) return x % 2 == 0 end)) == "[2, 4]"
-- or
tostring(Enumerable.From({ 1, 2, 3, 4 }):Where("x => x%2 == 0")) == "[2, 4]"

As you can see, it got rid of 1 and 3 since they didn't match the where clause.

For efficiency's sake, and to allow for easy handling of infinite sequences, nearly all such methods are handled lazily and only performed on iteration. The exception to this is one that has to iterate over all items such as :Aggregate() or :Last().

See the API for the many more methods available.

Three types of standard collections provided, which all inherit the Enumerable methods, are:

  • List (similar to Lua's 1-indexed numerically-keyed tables)
  • Set (similar to a Lua table where all values are true)
  • Dictionary (A typically 1-to-1 mapping similar to a typical Lua table if it were used that way).

Each of these can transparently handle nil, unlike typical lua tables.

Each of these can be instantiated typically with a New function

  • local list = List.New() or List.New({ 1, 2, 3 }) or List.FromArguments(1, 2, 3)
  • local set = Set.New() or Set.New({ 1, 2, 3 }) or Set.FromArguments(1, 2, 3)
  • local dict = Dictionary.New() or Dictionary.New({ alpha = 1, bravo = 2 })

You can also wrap existing tables as such:

  • local list = List.WrapTable({ 1, 2, 3 })
  • local set = Dictionary.WrapTable({ [1] = true, [2] = true, [3] = true })
  • local dict = Set.WrapTable({ alpha = 1, bravo = 2 }) Any changes to these tables will affect the wrapped collection and vice-versa. These cannot gracefully handle nil, though, a deficiency of the Lua table system.

One nice thing about Set and Dictionary is that a comparison function can be supplied to make it so that the keys follow a different unique constraint rather than a simple equality check. For example, if you wanted a Set that was case-insensitive:

local set = Set.New(nil, string.upper)
set:Add("Hey")
set:Add("HEY")
set:Add("hey")
assert(set:Count() == 1)
assert(set:ToString() == 'Set["Hey"]')

tag v40000
a72548d35a8875ad27466815e14bb9f616fc8cbf
root <root@db06.curselive.local>
2011-01-14 16:57:37 +0000

Tagging as v40000

--------------------

unknown:
    - Updating TOC to 40000
ckknight:
    - Add BidirectionalDictionary, which acts like a regular dictionary only it represents a one-to-one relationshipp between keys and values rather than potentially a many-to-one
    - Forgot to add Queue.lua to the lib.xml
    - Add Queue as a Collection type. It has :Enqueue, :Dequeue, and :Peek. Dequeues are fast and nil-safe, and shrinking only happens when actually necessary, as the internal structure of the table isn't needed to be pristine.
    - Add Stack as another collection type. Has the standard :Push, :Pop, and :Peek methods we've all come to love
    - Make :ToTable() accept an argument for whether to convert to a "list"-like table or a "set"-like table.
    - Remove improper globals, making them upvalues instead
    - Add List.prototype:PickAndRemoveRandom() Set.prototype:PickAndRemoveRandom() and Dictionary.prototype:PickAndRemoveRandom()
    - Add Enumerable.prototype:PickRandom() and Enumerable.prototype:PickRandomOrDefault(default)
    - Make the :GroupBy methods' key_selector and the like accept the index of the element in the sequence
    - Add List.prototype:ShuffleInPlace()
    - Fix some docs and properly update the minor version
    - Add Set.FromSequenceOrItem(sequence_or_item)
    - Add Dictionary.prototype:ToKeyValuePairs() which returns an Enumerable of KeyValuePairs, which are lightweight proxy objects that store two items.
    - Make List.prototype:Sort() accept nil as a comparer
    - Add List.prototype:Sort(comparer) and OrderedEnumerable.prototype:GetComparer()
    - Add read-only capabilities to Set and Dictionary as well. Up the minor version to 2, forgot about that before.
    - Add List.prototype:ConvertToReadOnly() and List.prototype:IsReadOnly() make :GroupBy use it when generating the Groupings, just for safety's sake.
    - Add List.prototype:ReverseInPlace()
    - Make Grouping more efficient by routing all methods called on it directly onto List, which has more efficient methods than the standard Enumerable for most things.
    - Add :Iterate() to Enumerable, which creates a lua iterator that can be used in for loops. Can improperly leave garbage around if the for loop is not fully iterated through.
    - Add :Clone() methods to List, Set, and Dictionary
    - Make it so that if you wrap a table more than once, it'll use the same wrapper rather than making a new one.
    - Add Dictionary.prototype:Merge(other) which merges the other dict onto the self dict
    - Add Replace methods to List, so that items in the list can be replaced one or more at a time.
    - Add dict:Keys() and dict:Values()
    - Add contracts to List, Set, and Dictionary to assure what elements are going to be in those collections. e.g. If you want to force that a List has only positive integers, that's possible and will error if trying to put in a string or anything that doesn't follow the contract provided
    - Add support for passing C#-like lambdas instead of full lua functions, e.g. "x => x*x" will transparently convert to function(x) return x*x end
    - Remove old suo file
    - Do not include LibStub in lib.xml since it is included in the TOC
    - Fix .pkgmeta issue
    - Initial commit of LibLinq-1.0, a collection management library similar to .NET's System.Linq

Login to Curse

Don't have an account? Create One.

Get an epic experience with Curse Premium
  • Faster addon downloads
  • Premium-Only Beta Giveaways
  • Ad-Free Curse experience
  • Premium Curse Client
  • and many More Features
  • Learn More »

Leaguepedia PAX Skin Giveaway