[爆卦]Iterable是什麼?優點缺點精華區懶人包

雖然這篇Iterable鄉民發文沒有被收入到精華區:在Iterable這個話題中,我們另外找到其它相關的精選爆讚文章

在 iterable產品中有7篇Facebook貼文,粉絲數超過3,460的網紅Taipei Ethereum Meetup,也在其Facebook貼文中提到, 📜 [專欄新文章] Gas Efficient Card Drawing in Solidity ✍️ Ping Chen 📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium Assign r...

 同時也有1部Youtube影片,追蹤數超過17萬的網紅魏巍,也在其Youtube影片中提到,記錄學習 python 語言裡面 Iterator 跟Generator 的概念。在介紹 Iterator 之前,先介紹什麼叫做 Iterable。 Iterable 是可以迭代對象,如字串或是List,經過 iter 方法處理之後,會變成 Iterator。這樣講實在很模糊,請看我的短片,裡面有詳...

  • iterable 在 Taipei Ethereum Meetup Facebook 的精選貼文

    2021-09-23 13:53:53
    有 6 人按讚

    📜 [專欄新文章] Gas Efficient Card Drawing in Solidity

    ✍️ Ping Chen

    📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium

    Assign random numbers as the index of newly minted NFTs

    Scenario

    The fun of generative art NFT projects depends on randomness. The industry standard is “blind box”, where both the images’ serial number and the NFTs’ index are predetermined but will be shifted randomly when the selling period ends. (They call it “reveal”) This approach effectively solves the randomness issue. However, it also requires buyers to wait until the campaign terminates. What if buyers want to know the exact card right away? We’ll need a reliable onchain card drawing solution.

    The creator of Astrogator🐊 isn’t a fan of blind boxes; instead, it thinks unpacking cards right after purchase is more interesting.

    Spec

    When initializing this NFT contract, the creator will determine the total supply of it. And there will be an iterable function that is randomly picking a number from the remaining pool. The number must be in range and must not collide with any existing ones.

    Our top priority is accessibility/gas efficiency. Given that gas cost on Ethereum is damn high nowadays, we need an elegant algorithm to control gas expanse at an acceptable range.

    Achieving robust randomness isn’t the primary goal here. We assume there’s no strong financial incentive to cheat, so the RNG isn’t specified. Implementers can bring their own source of randomness that they think is good enough.

    Implementation

    Overview

    The implementation is pretty short and straightforward. Imagine there’s an array that contains all remaining(unsold) cards. When drawIndex() is called, it generates a (uniform) random seed to draw a card from the array, shortens the array, and returns the selected card.

    Algorithm

    Drawing X cards from a deck with the same X amount of cards is equal to shuffling the deck and dealing them sequentially. It’s not a surprise that our algorithm is similar to random shuffling, and the only difference is turning that classic algo into an interactive version.

    A typical random shuffle looks like this: for an array with N elements, you randomly pick a number i in (0,N), swap array[0] and array[i], then choose another number i in (1,N), swap array[1] and array[i], and so on. Eventually, you’ll get a mathematically random array in O(N) time.

    So, the concept of our random card dealing is the same. When a user mints a new card, the smart contract picks a number in the array as NFT index, then grabs a number from the tail to fill the vacancy, in order to keep the array continuous.

    Tweak

    Furthermore, as long as the space of the NFT index is known, we don’t need to declare/initialize an array(which is super gas-intensive). Instead, assume there’s such an array that the n-th element is n, we don’t actually initialize it (so it is an array only contains “0”) until the rule is broken.

    For the convenience of explanation, let’s call that mapping cache. If cache[i] is empty, it should be interpreted as i instead of 0. On the other hand, when a number is chosen and used, we’ll need to fill it up with another unused number. An intuitive method is to pick a number from the end of the array, since the length of the array is going to decrease by 1.

    By doing so, the gas cost in the worst-case scenario is bound to be constant.

    Performance and limitation

    Comparing with the normal ascending index NFT minting, our random NFT implementation requires two extra SSTORE and one extra SLOAD, which cost 12600 ~ 27600 (5000+20000+2600) excess gas per token minted.

    Theoretically, any instantly generated onchain random number is vulnerable. We can restrict contract interaction to mitigate risk. The mitigation is far from perfect, but it is the tradeoff that we have to accept.

    ping.eth

    Gas Efficient Card Drawing in Solidity was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.

    👏 歡迎轉載分享鼓掌

  • iterable 在 BorntoDev Facebook 的最佳貼文

    2021-07-07 05:49:08
    有 110 人按讚

    💡 หลาย ๆ คนที่เขียน Python มาสักพักแล้ว หรือบางคนเพิ่งเริ่มเขียน อาจจะยังไม่รู้จักการใช้งาน Iterator และ Iterable กันสักเท่าไหร่ หรือบางคนอาจจะใช้งานอยู่แล้ว แต่ยังไม่รู้ความหมายของมัน…
    .
    🔥 วันนี้แอดมาสรุปการใช้งาน Iterator และ Iterable มาให้เพื่อน ๆ อ่าน จะได้กระจ่างกันเลยว่าทั้งสองเนี่ยมันคืออะไร ใช้งานยังไง ถ้าพร้อมแล้วไปอ่านกันเลยยยยย!!
    .
    Iterable - Object ที่สามารถวนซ้ำได้ (List, Tuple, และ Strings ก็เป็น Iterable)
    Iterator - ตัวที่ใช้วนซ้ำ
    .
    ประกอบด้วย Methods ดังนี้
    🔹 __iter __ () - ใช้สร้าง Iterator เพื่อวนซ้ำใน Iterable
    🔹 __next __ () - ใช้ดึงข้อมูลออกจาก Iterable
    .
    ⚙️ การใช้งาน
    iter() จะทำการสร้าง Iterator เพื่อกำหนดการวนซ้ำให้กับ Iterable จากนั้น และ next() จะดึงข้อมูลใน Iterable ออกมาตามลำดับการวนซ้ำนั่นเอง
    .
    👨‍💻 ตัวอย่าง1 : ดึงค่าใน Iterable ออกมาตามลำดับ Index
    fruit = ["Apple", "papaya", "banana", "orange"]

    iterator = iter(fruit)

    print(next(iterator))
    print(next(iterator))
    print(next(iterator))
    print(next(iterator))
    .
    หากเรียกใช้ 'next(iterator_obj)' อีกครั้ง มันจะ Return 'StopIteration' ออกมา เพราะค่าถูกดึงออกมาครบแล้วนั่นเอง
    .
    📑 ผลลัพธ์
    Apple
    papaya
    banana
    orange
    .

    👨‍💻 ตัวอย่าง2 : ตรวจสอบค่าใน Object ที่กำหนดว่าเป็น Iterable หรือไม่
    def iterable(y):
    try:
    iter(y)
    return True

    except TypeError:
    return False

    arr = [34, [24, 35], (44, 112), {"Prayut":250}]

    for i in arr:

    print(i, " is iterable : ", iterable(i))
    .
    📑 ผลลัพธ์
    34 is iterable : False
    [24, 35] is iterable : True
    (44, 112) is iterable : True
    {'Prayut': 250} is iterable : True
    จะเห็นว่า 34 ไม่ได้เป็น Iterable นั่นเอง
    .
    💥 Source : https://www.geeksforgeeks.org/python-difference-iterable-iterator/
    .
    borntoDev - 🦖 สร้างการเรียนรู้ที่ดีสำหรับสายไอทีในทุกวัน

  • iterable 在 張渝江 Facebook 的精選貼文

    2020-10-20 00:01:45
    有 20 人按讚

    2017年前100台特斯拉車主有30多位同我們一起對特斯拉提起告訴,這些「心碎」大概是他們四位所不理解的,這可能是文化(台灣/香港, 古代/現代)的差異,但車子還是很好的,只是他們總不給舊車主一定的尊重,我們可是花了錢(溢價)donate了台灣最早北中南東的超充站的。

    https://www.tesla.com/zh_TW/videos/she-says?redirect=no&utm_source=Iterable&utm_medium=email&utm_campaign=newsletter&utm_locale=zh_TW&utm_content=sales&utm_term=october_owners

  • iterable 在 魏巍 Youtube 的最讚貼文

    2019-05-10 09:00:03

    記錄學習 python 語言裡面 Iterator 跟Generator 的概念。在介紹 Iterator 之前,先介紹什麼叫做 Iterable。 Iterable 是可以迭代對象,如字串或是List,經過 iter 方法處理之後,會變成 Iterator。這樣講實在很模糊,請看我的短片,裡面有詳細的解釋。

    $$ 訂閱《魏巍》頻道吧~ https://bit.ly/2EIy6Rz
    ----------------------------------------­­---------------------------------------­-­---------------------------
    ***** 訂閱魏巍頻道 ***** @@ ***** 跟我一起玩遊戲 *****
    ***** 還會分享各種有趣的短片** @@ ** 趕快去訂閱吧! *****,
    ----------------------------------------­­------------­­---------------------------------------­-­---------------------------

    #python #programming #魏巍

你可能也想看看

搜尋相關網站