Hello. I’m using something like this [“{red, blue, green, orange, lime}”.selectMany(3).joinItems(", ")]. It works well enough, but when I try to implement uniqueSelect or consumableList it fails. These function seem to only work for lists but is there any way to select items from a string like that without repetition. Thanks!

  • VioneT@lemmy.worldM
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 days ago

    Do you mean {red|blue|green|orange|lime} with ‘shorthand lists’?

    Well, it is because "{red|blue|green|orange|lime}" is only one and is a string. You can use .selectMany() because, it would essentially select the same string multiple times, and when it is evaluated by the engine it would then select from those items in the string.

    You can’t use .selectUnique() since it is a single item, a single string. Same with .consumableList since it isn’t a list, it is a string.

    Best way is to have those into a list themselves:

    // Regular
    colors
      red
      blue
      green
      orange
      lime
    
    colorsV2 = {red|blue|green|orange|lime} // Shorthand list
    

    And then, you can use those .selectUnique and .consumableList methods like so [colors.selectUnique(3).joinItems(", ")] or [x = colorsV2.consumableList, ''] [x.selectOne], [x.selectOne], [x.selectOne].

    • Koto@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      11 hours ago

      Thanks, VioneT, it’s not the first time you help me. I understand the mistake now. I need a list, not a choice of one. But is there a way to create the following example: “one,two,three,four”.consumableList.select(3)? It should have many items even though it’s a string? This example example doesn’t work, of course. I just don’t wanna create infinite lists for one time use.

      • VioneT@lemmy.worldM
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        8 hours ago

        Sadly, lists created from a string doesn’t work like that. If you want to create a list inline, you can do this instead.

        [createPerchanceTree("list = {one|two|three|four}").list.selectUnique(3).joinItems(", ")]
        // Create a list with `createPerchanceTree` with the name 'list' and the syntax `list = {one|two|three|four}`
        // Access the new list with `.list` where `list` is the name of the list that is created.
        // Select unique items from that list with `.selectUnique`
        // join them with `.joinItems`
        
        // Here's with consumableList
        [x = createPerchanceTree("list = {one|two|three|four}").list.consumableList, x.selectOne] [x.selectOne]
        

        I would still recommend it to be on the list forms that I’ve previously shown for readability and refactorability since if you have many of those one lines, it makes it hard to debug/see problems.