Ruby hash with lazy keys












0















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 '18 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 '18 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 '18 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 '18 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 '18 at 15:02
















0















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 '18 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 '18 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 '18 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 '18 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 '18 at 15:02














0












0








0








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 '18 at 22:29







user183966

















asked Nov 21 '18 at 14:10









user183966user183966

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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 15:02












1 Answer
1






active

oldest

votes


















0














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',
    autoActivateHeartbeat: false,
    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









    0














    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




























      0














      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


























        0












        0








        0







        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 '18 at 15:11









        VolteVolte

        1,5771322




        1,5771322
































            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.




            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

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?