Ruby hash with lazy keys











up vote
0
down vote

favorite












I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.



Some examples of how I want my object to behave:



endpoints = get_endpoints.call        # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints


The comments describe how the object internally makes requests to the 'data endpoints'.



It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.



Thank you for taking a look!










share|improve this question
























  • Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
    – MrAleister
    Nov 21 at 14:18










  • Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
    – user183966
    Nov 21 at 14:22










  • What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
    – Tom Lord
    Nov 21 at 14:42








  • 1




    I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use my_hash.keys.lazy, but... why? What would be the purpose?
    – Tom Lord
    Nov 21 at 14:45












  • Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
    – user183966
    Nov 21 at 15:02















up vote
0
down vote

favorite












I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.



Some examples of how I want my object to behave:



endpoints = get_endpoints.call        # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints


The comments describe how the object internally makes requests to the 'data endpoints'.



It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.



Thank you for taking a look!










share|improve this question
























  • Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
    – MrAleister
    Nov 21 at 14:18










  • Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
    – user183966
    Nov 21 at 14:22










  • What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
    – Tom Lord
    Nov 21 at 14:42








  • 1




    I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use my_hash.keys.lazy, but... why? What would be the purpose?
    – Tom Lord
    Nov 21 at 14:45












  • Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
    – user183966
    Nov 21 at 15:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.



Some examples of how I want my object to behave:



endpoints = get_endpoints.call        # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints


The comments describe how the object internally makes requests to the 'data endpoints'.



It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.



Thank you for taking a look!










share|improve this question















I have a collection of 'data endpoints'. Each endpoint has a name and can be available or unavailable. In Ruby I want to present the available endpoints as a Hash to make it easy to work with them. The difficulty is that getting information about the endpoints is costly and should be done lazily.



Some examples of how I want my object to behave:



endpoints = get_endpoints.call        # No endpoint information is accessed yet
result = endpoints['name1'] # This should only query endpoint "name1"
is_available = endpoints.key? 'name2' # This should only query endpoint "name2"
all_available = endpoints.keys # This has to query all endpoints


The comments describe how the object internally makes requests to the 'data endpoints'.



It is straightforward to make a Hash that can do the first 2 lines. However I don't know how to support the last 2 lines. To do this I need a way to make the keys lazy, not just the values.



Thank you for taking a look!







ruby hashmap key lazy-evaluation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 22:29

























asked Nov 21 at 14:10









user183966

11




11












  • Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
    – MrAleister
    Nov 21 at 14:18










  • Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
    – user183966
    Nov 21 at 14:22










  • What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
    – Tom Lord
    Nov 21 at 14:42








  • 1




    I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use my_hash.keys.lazy, but... why? What would be the purpose?
    – Tom Lord
    Nov 21 at 14:45












  • Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
    – user183966
    Nov 21 at 15:02


















  • Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
    – MrAleister
    Nov 21 at 14:18










  • Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
    – user183966
    Nov 21 at 14:22










  • What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
    – Tom Lord
    Nov 21 at 14:42








  • 1




    I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use my_hash.keys.lazy, but... why? What would be the purpose?
    – Tom Lord
    Nov 21 at 14:45












  • Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
    – user183966
    Nov 21 at 15:02
















Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18




Not sure what aspect of Hash you would like to be lazy-implemented ? Keys evaluation ? How would it be addressed in memory ?
– MrAleister
Nov 21 at 14:18












Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22




Hi @MrAleister Yes, keys evaluation (or rather, all aspects of the map). I have no idea how it would be addressed in memory. I thought that didn't really matter since it's object oriented, the same way I can just provide a lazy evaluation block for the values I would hope to provide a lazy evaluation block for the keys.
– user183966
Nov 21 at 14:22












What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42






What do you mean by "I have made the value lookup lazy in the straightforward way"? Why is that "no advantage in [your] scenario if [you] have to look up the keys eagerly"?
– Tom Lord
Nov 21 at 14:42






1




1




I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use my_hash.keys.lazy, but... why? What would be the purpose?
– Tom Lord
Nov 21 at 14:45






I don't understand what you're trying to achieve. (Why do you want to do this?) Can you show what code you've written so far? You could just use my_hash.keys.lazy, but... why? What would be the purpose?
– Tom Lord
Nov 21 at 14:45














Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02




Hi @TomLord . It is not laziness on the output side of the hash that I want, but laziness on the input side of the hash. I hope that makes sense.
– user183966
Nov 21 at 15:02












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You'd have to override the key? method, and do your own checking in there.



class LazyHash < Hash
def key?(key)
# Do your checking here. However that looks for your application
end
end


In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.



Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.



I recommend starting fresh with a new class and building out your desired interface and functionality.



class LazyEndpoints
def on?(name)

end

def set(name, value)

end
end


(Or something like that, the world is yours for the taking!)






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413940%2fruby-hash-with-lazy-keys%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You'd have to override the key? method, and do your own checking in there.



    class LazyHash < Hash
    def key?(key)
    # Do your checking here. However that looks for your application
    end
    end


    In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.



    Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.



    I recommend starting fresh with a new class and building out your desired interface and functionality.



    class LazyEndpoints
    def on?(name)

    end

    def set(name, value)

    end
    end


    (Or something like that, the world is yours for the taking!)






    share|improve this answer

























      up vote
      0
      down vote













      You'd have to override the key? method, and do your own checking in there.



      class LazyHash < Hash
      def key?(key)
      # Do your checking here. However that looks for your application
      end
      end


      In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.



      Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.



      I recommend starting fresh with a new class and building out your desired interface and functionality.



      class LazyEndpoints
      def on?(name)

      end

      def set(name, value)

      end
      end


      (Or something like that, the world is yours for the taking!)






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You'd have to override the key? method, and do your own checking in there.



        class LazyHash < Hash
        def key?(key)
        # Do your checking here. However that looks for your application
        end
        end


        In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.



        Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.



        I recommend starting fresh with a new class and building out your desired interface and functionality.



        class LazyEndpoints
        def on?(name)

        end

        def set(name, value)

        end
        end


        (Or something like that, the world is yours for the taking!)






        share|improve this answer












        You'd have to override the key? method, and do your own checking in there.



        class LazyHash < Hash
        def key?(key)
        # Do your checking here. However that looks for your application
        end
        end


        In my opinion, you're asking for trouble though. One of the most powerful virtues in computer science is expectability. If you're changing the behavior of something, modifying it far beyond it's intent, it doesn't serve you to continue calling it by the original name. You don't need to shoe-horn your solution into existing classes/interfaces.



        Programming offers you plenty of flexibility, so you can do stuff like this (dependent on the language of course), but in that same argument, you have no reason not to simply build a new object/service with it's own API.



        I recommend starting fresh with a new class and building out your desired interface and functionality.



        class LazyEndpoints
        def on?(name)

        end

        def set(name, value)

        end
        end


        (Or something like that, the world is yours for the taking!)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 15:11









        Volte

        1,5731322




        1,5731322






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413940%2fruby-hash-with-lazy-keys%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

            Sphinx de Gizeh