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?










share|improve this question






















  • It seems from discussion below that this question is about gas optimization.
    – smarx
    3 hours ago
















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?










share|improve this question






















  • It seems from discussion below that this question is about gas optimization.
    – smarx
    3 hours ago














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?










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?







solidity mapping pure






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 6 hours ago









purpletentacle

1163




1163












  • It seems from discussion below that this question is about gas optimization.
    – smarx
    3 hours ago


















  • It seems from discussion below that this question is about gas optimization.
    – smarx
    3 hours ago
















It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago




It seems from discussion below that this question is about gas optimization.
– smarx
3 hours ago










2 Answers
2






active

oldest

votes


















2














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
    3 hours ago



















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









    2














    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
      3 hours ago
















    2














    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
      3 hours ago














    2












    2








    2






    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 5 hours ago









    Rob Hitchens B9lab

    25.9k54278




    25.9k54278












    • Well, clearly I want to minimize gas. What do you think would be more efficient?
      – purpletentacle
      3 hours ago


















    • Well, clearly I want to minimize gas. What do you think would be more efficient?
      – purpletentacle
      3 hours ago
















    Well, clearly I want to minimize gas. What do you think would be more efficient?
    – purpletentacle
    3 hours ago




    Well, clearly I want to minimize gas. What do you think would be more efficient?
    – purpletentacle
    3 hours ago











    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 2 hours ago









        Rick Park

        778111




        778111






























            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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)