Pure functions as a replacement of constant mappings?












2














I need to create a lookup table [uint -> uint] that is constant.



Constant mappings or switch-case are not available in solidity. Are pure functions full of if-else the only option?



Any better suggestion?



Update Ideally, I would like an option that minimizes gas utilization










share|improve this question
























  • It seems from discussion below that this question is about gas optimization.
    – smarx
    Jan 2 at 23:48
















2














I need to create a lookup table [uint -> uint] that is constant.



Constant mappings or switch-case are not available in solidity. Are pure functions full of if-else the only option?



Any better suggestion?



Update Ideally, I would like an option that minimizes gas utilization










share|improve this question
























  • It seems from discussion below that this question is about gas optimization.
    – smarx
    Jan 2 at 23:48














2












2








2


1





I need to create a lookup table [uint -> uint] that is constant.



Constant mappings or switch-case are not available in solidity. Are pure functions full of if-else the only option?



Any better suggestion?



Update Ideally, I would like an option that minimizes gas utilization










share|improve this question















I need to create a lookup table [uint -> uint] that is constant.



Constant mappings or switch-case are not available in solidity. Are pure functions full of if-else the only option?



Any better suggestion?



Update Ideally, I would like an option that minimizes gas utilization







solidity mapping pure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked Jan 2 at 21:03









purpletentacle

1164




1164












  • It seems from discussion below that this question is about gas optimization.
    – smarx
    Jan 2 at 23:48


















  • It seems from discussion below that this question is about gas optimization.
    – smarx
    Jan 2 at 23:48
















It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48




It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48










2 Answers
2






active

oldest

votes


















5














I don't see a dilemma.



You can have a mapping:



mapping(uint => uint) public myMap;


You can populate some locations in the constructor or elsewhere:



constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}


That gives you a simple view function (myMap(uint) public view returns(uint)) that returns the number stored at an index.



You can also make view functions full of if/else if that's the best approach for your use-case.



Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.



Hope it helps.






share|improve this answer





















  • Well, clearly I want to minimize gas. What do you think would be more efficient?
    – purpletentacle
    Jan 2 at 23:14



















0














The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.



If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "642"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fethereum.stackexchange.com%2fquestions%2f64925%2fpure-functions-as-a-replacement-of-constant-mappings%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    I don't see a dilemma.



    You can have a mapping:



    mapping(uint => uint) public myMap;


    You can populate some locations in the constructor or elsewhere:



    constructor() public {
    myMap[1] = 101;
    myMap[2] = 201;
    }


    That gives you a simple view function (myMap(uint) public view returns(uint)) that returns the number stored at an index.



    You can also make view functions full of if/else if that's the best approach for your use-case.



    Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.



    Hope it helps.






    share|improve this answer





















    • Well, clearly I want to minimize gas. What do you think would be more efficient?
      – purpletentacle
      Jan 2 at 23:14
















    5














    I don't see a dilemma.



    You can have a mapping:



    mapping(uint => uint) public myMap;


    You can populate some locations in the constructor or elsewhere:



    constructor() public {
    myMap[1] = 101;
    myMap[2] = 201;
    }


    That gives you a simple view function (myMap(uint) public view returns(uint)) that returns the number stored at an index.



    You can also make view functions full of if/else if that's the best approach for your use-case.



    Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.



    Hope it helps.






    share|improve this answer





















    • Well, clearly I want to minimize gas. What do you think would be more efficient?
      – purpletentacle
      Jan 2 at 23:14














    5












    5








    5






    I don't see a dilemma.



    You can have a mapping:



    mapping(uint => uint) public myMap;


    You can populate some locations in the constructor or elsewhere:



    constructor() public {
    myMap[1] = 101;
    myMap[2] = 201;
    }


    That gives you a simple view function (myMap(uint) public view returns(uint)) that returns the number stored at an index.



    You can also make view functions full of if/else if that's the best approach for your use-case.



    Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.



    Hope it helps.






    share|improve this answer












    I don't see a dilemma.



    You can have a mapping:



    mapping(uint => uint) public myMap;


    You can populate some locations in the constructor or elsewhere:



    constructor() public {
    myMap[1] = 101;
    myMap[2] = 201;
    }


    That gives you a simple view function (myMap(uint) public view returns(uint)) that returns the number stored at an index.



    You can also make view functions full of if/else if that's the best approach for your use-case.



    Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.



    Hope it helps.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 21:12









    Rob Hitchens B9lab

    26k54379




    26k54379












    • Well, clearly I want to minimize gas. What do you think would be more efficient?
      – purpletentacle
      Jan 2 at 23:14


















    • Well, clearly I want to minimize gas. What do you think would be more efficient?
      – purpletentacle
      Jan 2 at 23:14
















    Well, clearly I want to minimize gas. What do you think would be more efficient?
    – purpletentacle
    Jan 2 at 23:14




    Well, clearly I want to minimize gas. What do you think would be more efficient?
    – purpletentacle
    Jan 2 at 23:14











    0














    The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.



    If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.






    share|improve this answer


























      0














      The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.



      If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.






      share|improve this answer
























        0












        0








        0






        The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.



        If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.






        share|improve this answer












        The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.



        If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 0:29









        Rick Park

        825111




        825111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ethereum Stack Exchange!


            • 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%2fethereum.stackexchange.com%2fquestions%2f64925%2fpure-functions-as-a-replacement-of-constant-mappings%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?