RangeSet

Hello guys ,

I would like to know if there is a way to make RangeSet works with ClosedRange cause the 7..<7 is considered as empty , I tried a stupid way to copy all RangeSet and modify all the Range to ClosedRange .... seems okay until I found RangeReplaceableCollection and I think this is the source of trouble it raises an exception ( I made some changes and finally it works but the insertion replace the range .... )

the example tested :

    var closedRange = ClosedRangeSet([1...10 ,11...11 ,  15...20])
    print(closedRange)
    closedRange.insert(contentsOf: 12...14)
    print(closedRange)

result:

ClosedRangeSet(1..<10, 11..<11, 15..<20)
ClosedRangeSet(1..<10, 11..<11, 12..<14)

At least a tried a shortcut but not any shortcut works.
anyway maybe I'm still struggling with the Swift fundamentals I have started swift since 2 weeks :/ and I really need guidance.

Re-creating a standard library type is pretty much never the solution. Instead of troubleshooting how to create your own standard library, let’s explore the following: what are you trying to do with range sets, and what troubles are you having with it?

Half-open ranges are a currency type in Swift, meaning: whenever possible, APIs will use them in preference to other range types. Therefore, it’s important that you become comfortable using them.

1 Like

I concur with @xwu. Standard library can show you a lot of techniques, but is by no means easy for a starter work to get familiarised with the language. More so if it's to fix something you think is frustrating about the language.

Maybe we can instead see why you're not pleased with Range?

Note that as a range of integer, 7...7 is much closer to 7..<8 rather than 7..<7 since the Range excludes the end while ClosedRange does not.

1 Like

thank you for the quick answer , Well I managed to find another solution for my problem with the RangeSet and everything is fine, however I will explain my usecase ,

I'm trying to create a Merkle Tree who will represent a sequence of wallet commitments (the Merkle Tree will contain only the valid transfers sequences) for example a user fails 2 operations with ID : 5 & 7 , so 5 should not be included in the tree and never use it again , and user can retrieve his MerkleTree from our blockchain and here I got the problem of serialization and deserialization :stuck_out_tongue: so the tree should be like this [[1,4],[6,6] [8,10] that's why my first thought was to work with closed ranges.

FWIW, you can use third-party modules like SwiftRanges. :nerd_face:

Terms of Service

Privacy Policy

Cookie Policy