Paste #8885: A little example of definitions and list manipulation.

Date: 2014/08/26 20:43:06 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


Definition_Example:
  type: world
  debug: false
  events:
    on defs command:
    - determine passively fulfilled
    - define args '<tern[<c.args.size.is[MORE].than[0]>]:<c.args> || li@This|is|a|default|sentence|you|silly!>'
    - run locally defs instantly def:%args%

  defs:
    - define defs <queue.definitions>
    # We can use <queue.definitions> at the begining of a new queue to show all
    # the definitions that were passed to it.
    - narrate "<&sp><&3><&n>Queue Definitions"
    - narrate ""
    # To illustrate, the output here is the definition name %def% followed by
    # the definition value <def[%def]>
    - foreach %defs% {
      - define def %value%
      - narrate "<&sp><&sp><&sp><&6>%def%<&co> <&f><def[%def%]>"
      }
    # Not quite the output you would expect. That's because the
    # <queue.definitions> list is built from an internal (to denizen) hashmap
    - narrate ""
    - narrate "*--------"
    - narrate ""

    # Since the list %defs% we are working with was passed from a run command,
    # the definition names are the order index li@1|2|3... as seen above. This
    # is helpfull since the order of the list %defs% was disturbed by using the
    # <queue.definitions> tag. We can sort this list by number order using the
    # <list.numerical> tag.
    - narrate "<&sp><&3><&n>Sorted List - Numeric"
    - narrate ""
    - define sortedDefs <def[defs].numerical>
    # Now that we have out list of definitions back in the correct order, let's
    # reconstruct the sentence you entered.
    # On the first iteration %sentence% will not exist so we have to use an
    # OR || statement, also called a fallback. By setting the fallback to
    # <def[%def%]> we are creating a list whose first entry is the first value
    # from our sorted list. Every itteration after that will add the next value
    # to the list. The value being added to the list is the word in your
    # sentence, not the definition name!
    - foreach %sortedDefs% {
      - define def %value%
      - narrate "<&sp><&sp><&sp><&6>%def%<&co> <&f><def[%def%]>"
      - define sentence '<def[sentence].as_list.include[<def[%def%]>]||<def[%def%]>>'
      }
    - narrate ""
    # Let's display the list of definition names in sorted order. We do not need
    # a fallback value here because sortedDefs will is a list object, even if it
    # has only one value. This is due to the .numerical tag.
    - narrate "<&sp><&3>Definition name order reconstructed<&co> <&f><def[sortedDefs].space_separated>"
    - narrate ""
    # We add another fallback when we display the reconstructed sentence in case
    # you only enter one word in your string!
    - narrate "<&sp><&3>Your string reconstructed<&co> <&f><def[sentence].space_separated||%sentence%>"
    - narrate ""
    - narrate "*--------"