Libraries++, the sequel

Intro

Over a year ago I made a library extension module. It kind of sucks though, now that I looked back on it and it was just me being silly and not taking it seriously at all. Please don’t use that one.

It’s been over a year, and why not make a sequel to this thing. This one is much much much much better, objectively.

Changes

So a few changes have been made:

  • Structural improvements (libraries are their own module, its members are also in their own modules)
  • Rewritten completely to use Luau type annotations as well as improvements to code itself
  • Clutter removed
  • It is now on GitHub, with its own page for documentation
  • Niche functions moved to custom libraries (e.g string.digitalformatformat.digital_format)
  • Removed any functions Roblox has added, like CFrame.lookAt and table.clear.

While a few things have been removed I believe all the benefits of the changes outweigh the removal of those functions that were clutter/useless anyway. There are some new additions though, just as not as many as removals.

Repository / Documentation

You can find the source and documentation here:

Roblox Model

https://www.roblox.com/library/6445683851/Libraries

Licensing

This module is licensed under the MIT License. So this means that you can fork the module for public/private use and use it commercially, however you must preserve the copyright and license notice when doing this.

Contributions

Pull requests are welcome, but when contributing please respect the style guide found in CONTRIBUTING.md or reply below with your contribution, still respecting the style guide.

38 Likes

oh yes this is great… and the documentation… yes… a lot better than the previous one

Since you added the e constant perhaps you would want to add other mathematical constants! (math.tau, math.phi)

Also maybe an nroot function? A simple definition would be very similar to math.cbrt except you would need to check if the root is even.

local function nroot(x, root)
    local value = math.abs(x) ^ (1 / root)

    if ((x < 0) and (root % 2 == 0)) then
        return nil
    end

    return math.sign(x) * value
end

P.S. I made pull requests

Sometimes you just wish things like this are already in roblox, such as table.shuffle and table.reverse. Amazing library though! I wonder if roblox will ever add some of these.

a table.map would be nice too. I don’t think there is one in Libraries++ or Roblox though.
There is a table.foreach but if I remember it is deprecated for some reason?

I made a pull request anyways

While we’re at it, table.filter and table.reduce would also be nice.

table.foreach and table.foreachi is depcreated in Lua 5.1 (it should’ve been deprecated in Lua 4.0), because it seems like it’s been replaced by the for loop in Lua 4.0

I don’t think Roblox will likely to add table.map, table.filter, etc. IIRC someone said that it’ll be slower as every function call will go through the C → Lua(u) boundary.

1 Like

Update

Just wanted to announce a few updates on this module:

Additions

  • Added function table.map
  • Added function table.filter
  • Added function table.reduce
  • Added constant math.tau
  • Added constant math.phi
  • Added constant math.nhuge (negative infinity)
  • Added function string.isupper
  • Added function string.islower
  • Added function string.swapcase

Changes

  • Renamed table.getoccurrencesoftable.count (less verbose and more consistent with Python’s list.clear())
1 Like

https://github.com/zapacni/libraries-plus-plus/blob/main/libraries_plus_plus/table/zip.lua#1
This line, you ask about variable returns, by variable returns, do you mean variable types, or variable return counts (tuples)
For variable types:

-- foo returns a string or number
function foo(): string | number end
-- bar returns anything
function bar(): any end

For tuples:

-- foo returns a string and number
function foo(): (string, number) end

Variable returns, yes, however zip wouldn’t return the same exact values each time – it depends on how long the arrays given to it are.

Try this then:

-- foo returns an any[]
function foo(): {any} end

It will be better when generics or templates are added to luau

1 Like

But zip should return all the values individually, not packed into an array. Maybe I’m overthinking it or Luau just doesn’t support this kind of thing

Update

Just wanted to announce some more updates on this module:

Additions

  • Added color3 library

    • Added color3.add(lhs: Color3, rhs: Color3): Color3, should be self-explanatory. Adds 2 Color3s and returns the sum of that, since Roblox doesn’t implement __add for Color3s.
    • Added color3.subtract(lhs: Color3, rhs: Color3): Color3, should be self-explanatory. Subtracts 2 Color3s and returns the difference of that, since Roblox doesn’t implement __sub for Color3s.
    • Added color3.multiply(lhs: Color3, rhs: Color3 | number): Color3, should be self-explanatory. Multiplies 2 Color3s (or Color3 by number) and returns the product of that, since Roblox doesn’t implement __mul for Color3s.
    • Added color3.divide(lhs: Color3, rhs: Color3 | number): Color3, should be self-explanatory. Divides 2 Color3s (or Color3 by number) and returns the quotient of that, since Roblox doesn’t implement __div for Color3s.
  • Added instance library

    • Added get_ancestors(instance: Instance): { Instance? }, which gets all the ancestors of a given instance.
    • Added get_ancestors_of_class(instance: Instance, class_name: string): { Instance? }, which functions like instance.get_ancestors but only gets ancestors of a given class.
    • Added get_ancestors_which_are_a(instance: Instance, class_name: string): { Instance? }, which functions like instance.get_ancestors_of_class except it takes into account inheritance.
    • Added get_children_of_class(instance: Instance, class_name: string): { Instance? }, which functions like Instance:GetChildren() but only gets children of a certain class.
    • Added get_children_which_are_a(instance: Instance, class_name: string): { Instance? }, which functions like instance.get_children_of_class except it takes into account inheritance.
    • Added get_descendants_of_class(instance: Instance, class_name: string): { Instance? }, which functions like Instance:GetDescendants() but only gets descendants of a certain class.
    • Added get_children_which_are_a(instance: Instance, class_name: string): { Instance? }, which functions like instance.get_descendants_of_class except it takes into account inheritance.
    • Added get_siblings(instance: Instance): { Instance? }, which gets the siblings of an instance.
    • Added get_siblings_of_class(instance: Instance, class_name: string): { Instance? }, which functions like instance.get_siblings but only gets siblings of a certain class.
    • Added get_siblings_which_are_a(instance: Instance, class_name: string): { Instance? }, which functions like instance.get_siblings_of_class except it takes into account inheritance.
    • Added wait_for_child_of_class(instance: Instance, class_name: string): Instance?, which waits for a child of the given class. Does not have a timeout parameter, though feel free to make a PR for this.
    • Added wait_for_child_which_is_a(instance: Instance, class_name: string): Instance?, which functions like instance.wait_for_child_of_class except it takes into account inheritance. Does not have a timeout parameter, though feel free to make a PR for this.

Changes

  • Changed array type syntax { [number]: T } to { T } throughout the source since the parser of the linter I use didn’t support the latter until recently.

Documentation will be updated soon documentation has been updated

2 Likes

Update

Just wanted to announce some more updates on this module:

Additions:

  • Documented instance
  • Documented color3
  • Added misc
    • Added misc.spawn(f, ...), a “fast spawn” implementation that uses bindable events to mitigate the ~1/30th of a second you yield with the stock spawn function. Passes arguments ... to f.
    • Added misc.wait(num: number): number, an alternative to Roblox’s stock wait function that uses RunService.Heartbeat.
    • Added misc.delay(num: number, f: (number?) -> ()), an alternative to Roblox’s stock delay that uses the above functions.

misc will be documented soon

1 Like

Update

Just wanted to announce some more updates on this module:

Additions:

  • Added math.comb
  • Added string.startswith
  • Documented scheduler
  • Documented math.comb
  • Documented string.startswith

Changes:

  • Renamed miscscheduler (I only used the former since I didn’t have any ideas, went with something generic that encompasses the functions misc had)
  • zfill now does sign checks!
  • Updated zfill documentation to address this
3 Likes

I have now published Libraries++ to the library (heh) on Roblox:
https://www.roblox.com/library/6445683851/Libraries

Should hopefully make it easier for some, since not everyone might (want to) use a file synching tool like rojo to sync the code over into their game from their file system.

Also uploading to roblox does mean that it is easier to update (via roblox)

Wait you could only use Rojo to use this before?

The source was originally just on github, so you’d have to import them by hand or use a plugin like RepoToRoblox (which you should totally buy if you haven’t already), or the preferred method, which is using rojo to sync them into your game. Which is kind of how you were meant to do it in the first place. The goal of uploading this to the library is to make it a bit more accessible, since the majority of developers have everything in Roblox studio

1 Like

Sorry for necro, I have no honest clue what I am doing wrong, but for some reason, I can’t get this to work. First, comb has an infinite wait for script.Parent.math, and when I move the module out, the code stops yielding. Second, when I call a function listed in the documentation page, it says attempt to call nil value.

Bug? Or am I missing something?

4 Likes