• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Nested foreach loop

Stats

  • Locked Locked
  • Replies 7
  • Subscribers 143
  • Views 21003
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Nested foreach loop

T Opperman
T Opperman over 12 years ago

In an attemt to make parametric simulation scripts a bit more readable, I wrote a macro that allows foreach loops to be nested. I have not tested it fully, but I would like the readers' input on my code. The code generates a string containing the foreach loop, before evaluating it. It feels a bit clumsy, but I was unable to think of an simpler way to do this. Any suggestions?

TOForeachNestFunc=lambda( (Symbols Lists Commands)
let(((DeclareLoopVars nil) (DeclareLoopHead "") (DeclareLoopBody "") (DeclareLoopTail "")
	(MainLoop "foreach( (") (LetStringHead "let((") Result )
;We'll need to create a loop that declares the expanded sweep lists,
;before creating a main loop that runs through all the expanded sweep lists and
;executes the commands.

	foreach((List Symbol) Lists Symbols
	 DeclareLoopVars=cons(sprintf(nil "%sS" Symbol) DeclareLoopVars)
	 DeclareLoopBody=strcat(DeclareLoopBody sprintf(nil "%sS=cons(%s %sS) " Symbol Symbol Symbol))
	 DeclareLoopHead=strcat(DeclareLoopHead sprintf(nil "foreach(%s '%L " Symbol List ))
	 DeclareLoopTail=strcat(DeclareLoopTail ")")
	 MainLoop = strcat(MainLoop Symbol " ")
	)
	MainLoop = strcat(MainLoop ") ")

	foreach(Var reverse(DeclareLoopVars)
		LetStringHead=strcat(LetStringHead "(" Var " nil) ")
		MainLoop = strcat(MainLoop Var " ")
		)
	LetStringHead=strcat( LetStringHead ")")
	MainLoop = strcat(MainLoop "\n" Commands "\n) ")
	Result=strcat(LetStringHead "\n" DeclareLoopHead "\n" DeclareLoopBody "\n" DeclareLoopTail "\n" MainLoop "\n" ")")
	)
)


defmacro(TOForeachNest (Symbols Lists @rest Commands) ;Executes a nested foreach loop ;Symbols: A list of variables that are used in the commands. ;List:A list of lists that contain the values to be swept ;Example: ; TempL= '(-40.0 27.0 85.0) ; VSupplyL= '(3.2 3.3 3.4) ; ; TOForeachNest('(Temp VSupply) list(VSupplyL TempL) ; printf("Temperature: %e, Supply Voltage: %e", Temp VSupply) ; ) ;expands to: ; foreach(Temp TempL ; foreach(VSupply VSupplyL ; printf("Temperature: %e, Supply Voltage: %e\n", Temp VSupply) ;) ;) let((CommandString) CommandString=sprintf(nil "%L" ,Commands) CommandString=substring(CommandString 2 strlen(CommandString)-2) `let((Result) Result=funcall(,TOForeachNestFunc ,Symbols ,Lists ,CommandString) evalstring(Result) ) ) )
  • Cancel
Parents
  • ztzg
    ztzg over 12 years ago

    Tjaart, Andrew,

    I couldn't resist coming up with a non-macro version for completeness (also at http://s.crosstwine.com/spb/8cda9957).

    I usually implement iteration constructs via higher-order functions, and sugar-coat them in a thin layer of macrology where/when it really improves readability.

    This one is a bit nasty because a function cannot know the number of nested loops at compile time; moreover, it reuses/mutates a single list of arguments across the whole iteration to mimimize consing.

    A "pure macro" implementation is arguably nicer in this case, even if less generic. Anyway, here you are:

    defun(MyMapnested1 (fn args argsTail lists)
      cond(
        (lists
         let( ((moreLists cdr(lists))
               (nextArgsTail cdr(argsTail)))
           foreach(item car(lists)
             rplaca(argsTail item)
             MyMapnested1(fn args nextArgsTail moreLists)
           )
         )
        )
        (t
         apply(fn args)
        )
      )
    )
    
    defun(MyMapnested (fn @rest lists)
      let( ((args mapcar(lambda( (_ignore) nil) lists)))
        MyMapnested1(fn args args lists)
      )
    )
    

    Usage via higher-order functions:

    let( ((TempL '(-40 27 85))
          (VSupplyL '(3.2 3.3 3.4)))
      MyMapnested(
        lambda( (Temp VSupply)
          printf("Temperature: %L, Supply Voltage: %L\n" Temp VSupply)
        )
        TempL
        VSupplyL
      )
    )
    

    And here is TOForeachNest implemented in terms of MyMapnested:

    defmacro(TOForeachNest (Symbols Lists @rest Commands)
      `MyMapnested(
         lambda( ,Symbols
           ,@Commands
         )
         ,@Lists
       )
    )
    

    Cheers, -D

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • ztzg
    ztzg over 12 years ago

    Tjaart, Andrew,

    I couldn't resist coming up with a non-macro version for completeness (also at http://s.crosstwine.com/spb/8cda9957).

    I usually implement iteration constructs via higher-order functions, and sugar-coat them in a thin layer of macrology where/when it really improves readability.

    This one is a bit nasty because a function cannot know the number of nested loops at compile time; moreover, it reuses/mutates a single list of arguments across the whole iteration to mimimize consing.

    A "pure macro" implementation is arguably nicer in this case, even if less generic. Anyway, here you are:

    defun(MyMapnested1 (fn args argsTail lists)
      cond(
        (lists
         let( ((moreLists cdr(lists))
               (nextArgsTail cdr(argsTail)))
           foreach(item car(lists)
             rplaca(argsTail item)
             MyMapnested1(fn args nextArgsTail moreLists)
           )
         )
        )
        (t
         apply(fn args)
        )
      )
    )
    
    defun(MyMapnested (fn @rest lists)
      let( ((args mapcar(lambda( (_ignore) nil) lists)))
        MyMapnested1(fn args args lists)
      )
    )
    

    Usage via higher-order functions:

    let( ((TempL '(-40 27 85))
          (VSupplyL '(3.2 3.3 3.4)))
      MyMapnested(
        lambda( (Temp VSupply)
          printf("Temperature: %L, Supply Voltage: %L\n" Temp VSupply)
        )
        TempL
        VSupplyL
      )
    )
    

    And here is TOForeachNest implemented in terms of MyMapnested:

    defmacro(TOForeachNest (Symbols Lists @rest Commands)
      `MyMapnested(
         lambda( ,Symbols
           ,@Commands
         )
         ,@Lists
       )
    )
    

    Cheers, -D

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information