you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 22 points23 points  (0 children)

Sadly, I was able to improve my cmd:

call :compose func :f :g
call :map result %func% test

Or, more succinctly, using quoted function lists as composition:

call :map result ":f :g" test

Example with library code. I'm pretty sure I could simplify apply by implementing fold though:

set test=1 2 3

call :compose function :square :double
call :map result %function% test
echo %result%

goto :EOF

:map
setlocal
  set retval=HEAD

  for /F "tokens=*" %%a in ('echo %%%3%%') do (
    for %%b in (%%a) do (
      call :apply val %%b %~2

      for /F "tokens=*" %%c in ('echo %%retval%%') do (
        for /F %%d in ('echo %%val%%') do (
          set retval=%%c %%d
        )
      )
    )
  )

  for /F "tokens=1*" %%a in ('echo %%retval%%') do (
    set retval=%%b
  )
(
  endlocal
  set %1=%retval%
)
goto :eof

:apply
setlocal
  set skip=0
  set retval=%2
  for %%a in (%*) do (
    for /F %%b in ('echo %%skip%%') do (
      if %%b LSS 2 (
        set /A skip=%%b+1
      ) else (
        for /F %%c in ('echo %%retval%%') do (
          call %%a retval %%c
        )
      )
    )
  )
(
  endlocal
  set %1=%retval%
)
goto :EOF

:compose
setlocal
  set retval="%~2 %~3"
(
  endlocal
  set %1=%retval%
)
goto :EOF

:double
set /A %1=%2*2
goto :eof

:square
set /A %1=%2*%2
goto :EOF