std库——构建类似python的自带函数

; 引用头参考
#Include <std\init> ; 全部引用使用

#Include <std\metafunc\_class>
#Include <std\metafunc\assert>
#Include <std\metafunc\cmp>
#Include <std\metafunc\enum>
#Include <std\metafunc\hash>
#Include <std\metafunc\help>
#Include <std\metafunc\iter>
#Include <std\metafunc\len>
#Include <std\metafunc\oneloop>
#Include <std\metafunc\rely>
#Include <std\metafunc\sum>
#Include <std\metafunc\with>
#Include <std\metafunc\zip>

功能介绍:

_class

包含analy_class、classname、parent_class三个函数

analy_class输入参数可为类,函数,类名,函数名;返回值为类或函数

classname输入参数可为类、函数;返回值为类或函数所在类

parent_class输入参数可为类、函数;返回值为类的父类或函数所在类

#Include <std\metafunc\_class>

class a
{
    class b
    {
        static c()
        {
            
        }
    }
}

; analy_class
tmp := analy_class("a.b") ; (equal to) tmp := a.b
tmp := analy_class("a.b.c") ; (equal to) tmp := a.b.c
tmp := analy_class(a.b) ; (equal to) tmp := a.b
tmp := analy_class(a.b()) ; (equal to) tmp := a.b
tmp := analy_class(a.b.c) ; (equal to) tmp := a.b.c
; cannot use as tmp := analy_class(a.b.c())

; classname
tmp := classname(a.b) ; (equal to) tmp := "a.b"
tmp := classname(a.b()) ; (equal to) tmp := "a.b"
tmp := classname(a.b.c) ; (equal to) tmp := "a.b"
; cannot use as tmp := classname(a.b.c())

; parent_class
tmp := parent_class("a") ; (equal to) tmp := class
tmp := parent_class("a.b") ; (equal to) tmp := a
tmp := parent_class("a.b.c") ; (equal to) tmp := a.b
tmp := parent_class(a) ; (equal to) tmp := class
tmp := parent_class(a.b) ; (equal to) tmp := a
tmp := parent_class(a.b()) ; (equal to) tmp := a
tmp := parent_class(a.b.c) ; (equal to) tmp := a.b
; cannot use as tmp := parent_class(a.b.c())

assert

包含assert函数

输入参数为表达式;返回值为若表达式为假,则报错,否则无返回

#Include <std\metafunc\assert>

; assert
assert(1 = 2) ; throw error
assert(1 = 1) ; pass

cmp

包含cmp函数

输入参数为两个可比较对象,例如具有__cmp函数的相同的类或字符串数组等;返回值根据你自己的设定或bi’jiaobijiao类型不同而有所不同,一般为-1、0或1

#Include <std\metafunc\cmp>

class a
{
    __new(value)
    {
        this.data := value
    }
    
    __cmp(another_class)
    {
        return this.data > another_class.data ? 1 : this.data == another_class.data ? 0 : -1
    }
}

class b
{
    
}

; cmp
obj1 := a(2)
obj2 := a(3)
obj3 := b()
ret := cmp(obj1, obj2) ; (equal to) ret := -1
ret := cmp(obj1, obj3) ; (equal to) ret := "uncomparable"

enum

包含enum函数

输入参数为多个参数的引用;无返回值,默认为第一个参数赋值为1,后序依次+1,若当前参数有值,则跳过并且下一个参数继续+1

#Include <std\metafunc\enum>

; enum
enum(&a := 7, &b, &c := 9.1, &d, &e := 20)
; (equal to) a := 7, b := 8, c := 9.1, d := 10.1, e := 20

hash

包含hash函数

输入参数为可哈希化的对象;返回值为类的__hash函数或输入对象的md5值,若不可哈希化则返回”unhashable”

#Include <std\metafunc\hash>

class a
{
    __new(value)
    {
        this.data := value
    }
    
    __hash()
    {
        return hash(this.data)
    }
}

; hash
ret := hash(a(5)) ; (equal to) ret := "e4da3b7fbbce234577db76274a318d5" => md5(5)

help

包含help函数

输入参数为任意对象;返回值为当前对象的__help函数或”no help message”

#Include <std\metafunc\help>

class a
{
    __new(value)
    {
        this.data := value
    }
    
    static __help()
    {
        return "a(value) => a.data := value"
    }
}

class b
{
    
}

; help
ret := help(a) ; (equal to) ret := "a(value) => a.data := value"
ret := help(b) ; (equal to) ret := "no help message"

iter

包含iter、next函数

iter输入参数为多个可迭代对象;返回值为迭代器或闭包,若不可迭代则返回”uniterable”

next输入参数为迭代器或闭包,第二个可选参数为iternum,即需要从迭代器里下一个值集合中取出的值数量, 默认为1。注意,一般来说输入参数需要先进行iter函数处理;返回值为当前迭代器的下一个值

#Include <std\metafunc\iter>

class a
{
    __new(value*)
    {
        this.array := value
    }
}

class b
{
    __new(value*)
    {
        this.data := value
    }
    
    __enum(_ := 1)
    {
        this.looptimes := this.data.length
        return fn
        
        fn(output*)
        {
            if !this.looptimes
                return false
            %output[1]% := this.data[this.looptimes--]
            return true
        }
    }
}

c := {m: 5}
d := 1

; iter
ret := iter(a(5, 100))
ret := iter(b(5, 100))
ret := iter(c)
ret := iter(d) ; (equal to) ret := "uniterable"
; usage
for i, j, ... in ret
; ...

; next
ret := next(iter(c)) ; (equal to) ret := "m"
; before use next, please use iter() to make param iterable

len

包含len函数

输入参数为可测量对象;返回值为类的__len函数或数组的长度、字典的容量等等,若为不可测量对象则返回,”unlenable”

#Include <std\metafunc\len>

class a
{
    __new(value*)
    {
        this.data := value
    }
    
    __len()
    {
        return this.data.length
    }
}

; len
ret := len(a(100, 5)) ; (equal to) ret := a(100, 5).__len()
ret := len([1, 2]) ; (equal to) ret := [1, 2].length
ret := len(map(1, 2)) ; (equal to) ret := map(1, 2).count

oneloop

包含oneloop函数,依赖iter

输入参数依次为函数、可迭代对象,还有一个可选参数为函数,默认为空;返回值为一个数组:从迭代器中取出值,先用第二个函数判断是否满足条件,若满足条件再交由第一个函数处理后放入数组中。注意第一个函数和第二个函数所需输入的参数数量一定要相等或者第二个函数包含动态参数,且第一个函数不得使用动态参数

#Include <std\metafunc\oneloop>

a := [1, 2, 3, 4]

; oneloop
ret := oneloop(func1(var) => var + 1, a) ; (equal to) ret := [2, 3, 4, 5]
ret := oneloop(func2(var, var2) => var + var2, a) ; (equal to) ret := [2, 4, 6, 8]
ret := oneloop(func3(var) => var + 1, a, cmp_func(var) => var > 3) ; (equal to) ret := [5]

rely

包含rely、version函数

rely输入参数为类;返回值为数组或true

version输入参数为类;返回值为版本号

#Include <std\metafunc\rely>

class a
{
    static __version()
    {
        return "1.0.0"
    }
}

class b
{
    static __rely()
    {
        return ["a:1.0.1"]
    }
}

class c
{
    static __rely()
    {
        return ["a:>=1.0"]
    }
}

; version
ret := version(a) ; (equal to) ret := "1.0.0"
ret := version(b) ; (equal to) ret := "0.0.0"

; rely
ret := rely(b) ; (equal to) ret := ["a:1.0.1"]
ret := rely(c) ; (equal to) ret := true

sum

包含sum函数,依赖iter

输入参数依次为可迭代对象、可运算对象;返回值为可运算对象运算后结果

#Include <std\metafunc\sum>

class a
{
    data := 0
    
    add(value)
    {
        this.data += value
    }
}

; sum
arr := [1, 2, 3, 4]
b := a() ; type can be array, map, object...
sum(arr, b) ; (equal to) b.data += 1 + 2 + 3 + 4

with

包含with、_with函数

with输入参数为具有__enter函数和__exit函数的类;无返回值

_with无输入函数;无返回值,用于结束先前运行的with

#Include <std\metafunc\with>

class open
{
    __new(params*)
    {
        this.fileobj := fileopen(params*)
    }
    
    __enter()
    {
        return this.fileobj
    }
    
    __exit()
    {
        this.fileobj.close()
    }
}

; with
with open("test.txt", "rw"), &f ; (equal to) f := fileopen("test.txt", "rw")
_with() ; if no _with and use with again, will throw error => "last with func doesn't exit"

zip

包含zip函数,依赖len、iter

输入参数为多个可迭代对象;返回值为根据最小长度可迭代对象生成的迭代器,生成的迭代器每个值依次是从每个可迭代对象中迭代一次的结果集合

#Include <std\metafunc\zip>

; zip
my_list1 := [11,12,13]
my_list2 := [21,22,23]
for i in zip(my_list1, my_list2)
    for j in i
    ; ...
; (equal to) for i in [[11,21],[12,22],[13,23]]

下载地址

std库

提取码:mono复制
解压码:无

给TA捐赠
共{{data.count}}人
人已捐赠
其他

[AHK]面向对象的实例--point和line

2023-1-6 10:08:56

其他应用案例

[股票]定时监控监解析通达信股票预警信息

2023-1-6 15:58:24

7 条回复 A文章作者 M管理员
  1. AHK中文社区

    我觉得可以把函数的参数及返回值明确一下

  2. sunwind

    把python的range函数也复刻一个吧,这样就可以弥补ahk没有for循环的缺点了

    • 陌诺Mono

      全新numahk那篇文章里源码里就有,一个class range

    • cnkiller

      求大佬打包啊。 那个包管理的用了,目前还是有问题,就没用了

    • 陌诺Mono

      包管理那个我挂载的服务器暂时停了,你其实就把zip下载下来,在ahk安装目录,新建一个lib文件夹,把zip解压到lib内就行

个人中心
购物车
优惠劵
有新私信 私信列表
搜索