Idiom for “repeat n times”?

后端 未结 6 2197
忘掉有多难
忘掉有多难 2020-12-15 02:24

EDIT: The votes to close are wrong. The accepted answer in Repeat Character N Times is not applicable in general. E.g.:

>>>         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 03:06

    shortmost elegant ES6:

    let times=(n,f)=>{while(n-->0)f();}
    

    oh, That's not for creating an array, but it's still neat!

    times(3,()=>print('wow'))
    

    or Ruby style:

    Object.assign(Number.prototype,{times(f){x=this;while(x-->0)f();}})
    3..times(()=>print('wow'))
    

提交回复
热议问题