GithubHelp home page GithubHelp logo

add support for structures about bosl2 HOT 1 CLOSED

belfryscad avatar belfryscad commented on August 24, 2024
add support for structures

from bosl2.

Comments (1)

adrianVmariano avatar adrianVmariano commented on August 24, 2024

I wrote merged parse_pair() into struct_set, and also added is_struct. I was wondering if is_struct should check that there are no duplicated keywords, which might make it slow.

// Function: struct_set()
// Usage:
//    struct_set(struct, keyword, value, [grow])
//    struct_set(struct, [keyword1, value1, keyword2, value2, ...], [grow])
// Description:
//    Sets the keyword(s) in the structure to the specified value(s), returning a new updated structure.  If a keyword
//    exists its value is changed, otherwise the keyword is added to the structure.  If grow is set to false then
//    it is an error to set a keyword not already defined in the structure.  If you specify the same keyword twice
//    that is also an error.  If speed matters, use the first form with scalars rather than the list form: this is
//    about thirty times faster. 
// Arguments:
//    struct = input structure
//    keyword = keyword to set
//    value = value to set the keyword to
//    grow = Set to true to allow structure to grow, or false for new keywords to generate an error.  Default: true
function struct_set(struct, keyword, value=undef, grow=true) =
    let(aa=echo(struct=struct, keyword=keyword, value=value, grow=grow))
     !is_list(keyword) ?
        let(ind=search([keyword],struct,1,0)[0])
        (ind==[] ? assert(grow,str("Unknown keyword \"",keyword))
                   concat(struct, [[keyword,value]]) :
               list_set([ind], [[keyword,value]],struct)) :
               _parse_pairs(struct,keyword,grow);

function _parse_pairs(spec, input, grow=true, index=0, result=undef) =
     assert(len(input)%2==0,"Odd number of entries in [keyword,value] pair list")
     let( result = result==undef ? spec : result)
     index == len(input) ? result :  let(dd=echo(dd=spec, input, grow,index, result))
   _parse_pairs(spec,input,grow,index+2,struct_set(result, input[index], input[index+1],grow));

// Function is_struct()
// Usage:
//    is_struct(struct)
// Description: Returns true if the input has the form of a structure, false otherwise.  
function is_struct(struct, ind=0) =
     struct == [] ||
       let(dim = array_dim(struct))
       len(dim)==2 && dim[1]==2;

from bosl2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.