[Class] biga.ahk (166 utility methods)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 04 May 2020, 13:04

I have been thinking about how best to remove or replace the A.caseSensitive property which was not necessary when ahk should probably just concern itself with that matter through https://www.autohotkey.com/docs/commands/StringCaseSense.htm


edit: this may take a bit more experimenting because StringCaseSense isn't as well designed as I imagined.

Code: Select all

if ($1=$2)     ; case insensitive no matter StringCaseSense
if ($1 == $2)  ; case sensitive no matter StringCaseSense
if $1=BLAH     ; depending on StringCaseSense
if $1=%$2%     ; depending on StringCaseSense

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 12 May 2020, 19:30

0.26.0 is now on npm and github (comit) A.caseSensitive is no more. biga.ahk will now follow StringCaseSense

Code: Select all

A.indexOf(["fred", "barney"], "Fred")
; => 1

StringCaseSense, On
A.indexOf(["fred", "barney"], "Fred")
; => -1


.endsWith was also updated to fix bugs and missing documentation

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 13 May 2020, 07:45

0.28.2 is now on npm and github


.flattenDepth was added (docs)
Recursively flatten array up to depth times.

Code: Select all

A.flattenDepth([1, [2, [3, [4]], 5]], 1)
; => [1, 2, [3, [4]], 5]

A.flattenDepth([1, [2, [3, [4]], 5]], 2)
; => [1, 2, 3, [4], 5]

.flattenDeep was added (docs)
Recursively flattens array.

Code: Select all

A.flattenDeep([1, [2, [3, [4]], 5]])
; => [1, 2, 3, 4, 5]

.reject was added (docs)
The opposite of .filter; this method returns the elements of collection that predicate does not return truthy for.

Code: Select all

users := [{"user":"barney", "age":36, "active":false}, {"user":"fred", "age":40, "active":true}]
A.reject(users, Func("fn_reject1"))
; => [{"user":"fred", "age":40, "active":true}]

fn_reject1(o) {
    return !o.active
}
; The A.matches shorthand
A.reject(users, {"age":40, "active":true})
; => [{"user":"barney", "age":36, "active":false}]

; The A.matchesProperty shorthand
A.reject(users, ["active", false])
; => [{"user":"fred", "age":40, "active":true}]

;the A.property shorthand 
A.reject(users, "active")
; => [{"user":"barney", "age":36, "active":false}]

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by hasantr » 13 May 2020, 07:59

These are great things. Although it may be difficult to use at first, it will save a lot of time. I will start using it in my study, taking into account the small learning curve. Thanks for bringing them to Ahk.

iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by iseahound » 13 May 2020, 19:10

Can you rebrand this as bigA? I don't know what a bee-ga is. Also a short description without a reference to Lodash would be helpful!
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.
bigA is an AutoHotkey library that provides a whole mess of useful functional programming helpers without extending any built-in objects.

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 13 May 2020, 20:39

Could be cool. I think that was an original idea but to keep things consistent and predictable between downloading, including, and assigning; lowercase was an easier choice at the time.


I fixed a missing feature of .meanBy
The A.property shorthand is possible in v0.28.3

Code: Select all

objects := [{"n": 4}, {"n": 2}, {"n": 8}, {"n": 6}]
A.meanBy(objects, "n")
; => 5

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by BoBo » 14 May 2020, 00:23

A-M-A-Z-I-N-G ! :clap: Thx for sharing this tool. :thumbup:


1+ for a rebrand to bigA ... and btw, I can't see a reason why removing the reference to Lodash would make sense? :think:

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Helgef » 14 May 2020, 01:38

0.26.0 is now on npm and github (comit) A.caseSensitive is no more. biga.ahk will now follow StringCaseSense
Why? I think this change is inconvenient.

Cheers, and keep up the good work :thumbup: .

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 18 May 2020, 19:11

0.29.0 is now on npm and github

.count was added (docs)
Gets the number of occurrences of value if found in collection, else 0

Code: Select all

A.count([1, 2, 3], 2)
; => 1

A.count("pebbles", "b")
; => 2

A.count(["fred", "barney", "pebbles"], "barney")
; => 1

users := [ {"user": "fred", "age": 40, "active": true}
        , {"user": "barney", "age": 36, "active": false}
        , {"user": "pebbles", "age": 1, "active": false} ]
; The `A.matches` iteratee shorthand.
A.count(users, {"age": 1, "active": false})
; => 1

; The `A.matchesProperty` iteratee shorthand.
A.count(users, ["active", false])
; => 2

; The `A.property` iteratee shorthand.
A.count(users, "active")
; => 1

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 19 May 2020, 16:19

0.29.1 is now on npm and github

fixes a small issue with .count

When used with a string, it was previously not possible to search for more than a single character. Now any length is acceptable.

Code: Select all

A.count("pebbles", "bb")
; => 1

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 01 Jul 2020, 22:38

Used this today for something I thought was cool. My app has a large array of objects and each object has 8 or so pieces of data stored as key/value pairs.
I need to output all of these objects in two ways. One acts like a database and holds all the data. the other needs all the objects, but not all the fields of data; about 5 of them. So as not to confuse other people in the department, I need to not include that extra data in this mini-output. Both of these are written to a files if it matters.

In regular plain ahk I could see accomplishing this in two ways.
1) Looping the entire array and manually picking out the data to be saved to the new object, easily a page of code to manually copy the data. Many lines of newObject.key := oldObject.key or as many is needed.
2) Making a clone of the array and using newObject.Delete(Key) for each item to be removed. If the objects ever get another field, I'll need to remember to delete it here or it'll endup in the mini-output.

With biga.ahk; .pick can be used to pick out only the key/values we want, then .map performs that action over the entire array of objects.

Since ahk does not have anonymous functions, a helper function fn_declutterUsers is used. One could specify global A here since it needs access to biga.ahk at the global scope, but in this example I've decided to access the superglobal object biga in an attempt to keep the helper function brief.

Code: Select all

A := new biga() ;requires https://www.npmjs.com/package/biga.ahk
users := [{name: "Ronald", passwordhash: "kuhfiuahsdoijasd", note: "cool"}
        , {name: "Geralt", passwordhash: "jksafd8398usadaj", note: "chill"}]

declutteredData := A.map(users, Func("fn_declutterUsers"))
; => [{name: "Ronald", note: "cool"}, {name: "Geralt", note: "chill"}]

fn_declutterUsers(inputObj)
{
    return biga.pick(inputObj, ["name", "note"])
}
for example simplicity, only the "passwordhash" has been left out of the output; but this scales if you had a really cluttered object like in my app.

I will be looking at adding .omit; which works in the opposite fashion of .pick. Instead of listing the keys to be copied to the new object; the keys to be omitted from the object are specified. For example if you wanted to output everything except sensitive data like a password or internal note.

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 15 Jul 2020, 18:44

0.30.0 is now on npm and github

added .omit

The opposite of .pick; this method creates an object composed of the own property paths of object that are not omitted.

Code: Select all

user := {name: "Jon", age: 55, password: "securepassword123"}
A.omit(user , "password")
; => {name: "Jon", age: 55}

A.omit(user , ["age", "password"])
; => {name: "Jon"}

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (utilities mirroring Lodash)

Post by Chunjee » 22 Jul 2020, 08:06

0.31.1 is now on npm and github

added .dropwhile https://biga-ahk.github.io/biga.ahk/#/?id=dropwhile

Creates a slice of array excluding elements dropped from the beginning. Elements are dropped until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).

Code: Select all

users := [ {"user": "barney", "active": false }
        , { "user": "fred", "active": false }
        , { "user": "pebbles", "active": true } ]
A.dropWhile(users, Func("fn_dropWhile"))
; => [{ "user": "pebbles", "active": true }]
fn_dropWhile(o)
{
    return !o.active
}

; The `A.matches` iteratee shorthand.
A.dropWhile(users, {"user": "barney", "active": false})
; => [ { "user": "fred", "active": false }, { "user": "pebbles", "active": true }  ]

; The `A.matchesProperty` iteratee shorthand.
A.dropWhile(users, ["active", false])
; => [ {"user": "pebbles", "active": true } ]

; The `A.property` iteratee shorthand.
A.dropWhile(users, "active")
; => [ {"user": "barney", "active": false }, { "user": "fred", "active": false }, { "user": "pebbles", "active": true } ]

also a bug was fixed in .join :salute:

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (immutable utility library for arrays, strings, etc)

Post by Chunjee » 23 Jul 2020, 23:24

v0.31.2 is now on npm and github

There were some issues with .intersection
- type checking was missing
- when used on more than two arrays, only the first and last were being truly intersected
- was probably possible to have the same value in the resulting array more than once

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (immutable utility library for arrays, strings, etc)

Post by Chunjee » 26 Jul 2020, 12:56

v0.32.0 has been published

There was a bug with .reverse; it was mutating the input array. If someone wants to be a total bro they could write tests to ensure no mutations for every single method. I've been thinking about it for a while and I think I'm going to demand it be in place before v0.33.0


.dropRightWhile was added. https://biga-ahk.github.io/biga.ahk/#/?id=droprightwhile
Creates a slice of array excluding elements dropped from the end. Elements are dropped until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).

Code: Select all

users := [ {"user": "barney",     "active": true }
        , { "user": "fred",     "active": false }
        , { "user": "pebbles",     "active": false } ]
A.dropRightWhile(users, Func("fn_dropRightWhile"))
; => [{"user": "barney", "active": true }]

fn_dropRightWhile(o)
{
    return !o.active
}

Code: Select all

; The `A.matches` iteratee shorthand.
A.dropRightWhile(users, {"user": "pebbles", "active": false})
; => [ {"user": "barney", "active": true }, { "user": "fred", "active": false } ]

; The `A.matchesProperty` iteratee shorthand.
A.dropRightWhile(users, ["active", false])
; => [  {"user": "barney", "active": true } ]

; The `A.property` iteratee shorthand.
A.dropRightWhile(users, "active")
; => [ {"user": "barney", "active": true }, { "user": "fred", "active": false }, { "user": "pebbles", "active": false } ]

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (immutable utility library for arrays, strings, etc)

Post by Chunjee » 30 Jul 2020, 12:18

v0.33.0 has been published
Two methods have been added.

.constant https://biga-ahk.github.io/biga.ahk/#/?id=constant
Creates a function that returns value.

Code: Select all

object := A.times(2, A.constant({"a": 1}))
; => [{"a": 1}, {"a": 1}]

.times https://biga-ahk.github.io/biga.ahk/#/?id=times
Invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with one argument; (index).

Code: Select all

A.times(4, A.constant(0))
; => [0, 0, 0, 0]

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (immutable utility library for arrays, strings, etc)

Post by Chunjee » 27 Aug 2020, 10:34

v0.33.2 has been published

.chunk should perform slightly faster
There was some imperfect type checking on .repeat and .times

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (immutable utility library for arrays, strings, etc)

Post by Chunjee » 07 Oct 2020, 12:58

v0.34.0 has been published

.shuffle will perform much faster; 220% faster I think. .sample will perform slightly faster.

.keys was added and facilitates this change in performance. https://biga-ahk.github.io/biga.ahk/#/?id=keys


The old shuffle called .sample over and over but the overhead on that is somewhat high because it handles for both linear and sparse arrays and re-doing that for every single element was not best.

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (immutable utility library for arrays, strings, etc)

Post by Chunjee » 08 Oct 2020, 23:13

v0.34.1 has been published.

Performance of .shuffle is further enhanced. If you read wikipedia's entry on the Fisher-Yates shuffle you may notice a section on The modern algorithm wherein a modernized version is described that performs at O(n) time complexity. The package has been updated to use this approach, which is a further 3 times faster in my tests.

I'm getting new inspiration to work on a performance measurement suite since it is fun to see how the latest version compares.

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] biga.ahk (immutable utility library for arrays, strings, etc)

Post by Chunjee » 28 Oct 2020, 14:40

v0.34.3 has been published.

Documentation for .filter reads "The predicate is invoked with three arguments: (value, index|key, collection)." but this was not true, it was only called with value. That additional feature has been added. Is is now possible to perform logic on the index|key, and collection; if desired.

For example filtering only even keys:

Code: Select all

A.filter([1,2,3,-10,1.9,"even"], Func("fn_filterEven"))
; => [2,-10,"even"]

fn_filterEven(param_iteratee, param_key) {
	if (mod(param_key, 2) = 0) {
		return true
	}
}

Post Reply

Return to “Scripts and Functions (v1)”