The enumerate method in Magik Programming

  • 时间:2020-09-16 12:48:17
  • 分类:网络文摘
  • 阅读:75 次

The Magik programming does not have an inherent enumerate method like Python, however, it would be trivial to make one:

_global enumerate << _iter [email protected](list) 
    _local i << 0
    _for x _over list.fast_elements()
    _loop
        _loopbody({i, x}) # use simple-vector as a tuple
        i +<< 1
    _endloop
_endproc

Please note that the Magik does support the tuple, however, the tuple cannot be assigned to a tuple variable directly.

# tuple assignment works, x is 1 and y is 2
(x, y) << (1, 2) 

However, this does not:

a_tuple << (1, 2)
# Can only have a tuple on the right hand side if there is one on the left hand side

We can use the simple vector or rope to act as a tuple anyway (Tuples are just immutable array/vectors)

Example usages:

_for a _over enumerate({'a', 'b', 'c'})
_loop
    print(a)
_endloop

_global data << rope.new_with('a', 'b', 'c', 'd')
_for a _over enumerate(data)
_loop
    print(a)
_endloop

And you should expect the following in the Magik console.

simple_vector(1,2):
1       0
2       "a"
simple_vector(1,2):
1       1
2       "b"
simple_vector(1,2):
1       2
2       "c"
simple_vector(1,2):
1       0
2       "a"
simple_vector(1,2):
1       1
2       "b"
simple_vector(1,2):
1       2
2       "c"
simple_vector(1,2):
1       3
2       "d"
True 0

For more details on Magik programming, visit Magik Wiki

Similarly, we can implement the enumerate() in Javascript: The enumerate function in Javascript

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
炫动卡通直播-上海炫动卡通卫视直播「高清」  BTV卡酷少儿直播-北京卡酷动画卫视直播「高清」  嘉佳卡通卫视直播-嘉佳卡通直播在线观看「高清」  浙江少儿频道直播-浙江少儿在线直播观看「高清」  广州少儿频道直播-广州少儿在线直播观看「高清」  福建少儿频道直播-福建少儿在线直播观看「高清」  内蒙古少儿频道直播-内蒙古少儿在线直播观看「高清」  劲爆体育直播-劲爆体育在线直播观看「高清」  辽宁体育直播-辽宁体育在线直播观看「高清」  北京体育频道直播-北京体育在线直播观看「高清」 
评论列表
添加评论