J-63字符
该函数采用旧路径在左侧,新路径在右侧。
}.@;@(c=.c&}.`(,~(<'/..')"0)@.(~:&{.))&('/'<;.1@,'\/'&charsub)~
该解决方案分为三个部分,看起来像post@loop&pre~。爆炸解释:
post @ loop & pre ~ NB. the full golf
~ NB. swap the arguments: new on left, old on right
& pre NB. apply pre to each argument
loop NB. run the recursive loop on both
post @ NB. apply post to the final result
'/'<;.1@,'\/'&charsub NB. pre
'\/'&charsub NB. replace every \ char with /
'/' , NB. prepend a / char
<;.1@ NB. split string on the first char (/)
c=.c&}.`(,~(<'/..')"0)@.(~:&{.) NB. loop
@.(~:&{.) NB. if the top folders match:
&}. NB. chop off the top folders
c NB. recurse
` NB. else:
(<'/..')"0 NB. change remaining old folders to /..
,~ NB. append to front of remaining new folders
c=. NB. call this loop c to recurse later
}.@; NB. post
; NB. turn the list of folders into a string
}.@ NB. chop off the / in the front
请注意,/在拆分之前,我们在每个路径上都添加了一个前导,以便我们通过制作C:一个“文件夹”来处理Windows样式的路径。这将导致在Unix样式路径的开始处出现一个空文件夹,但始终会被循环删除。
实际观看:
NB. you can use it without a name if you want, we will for brevity
relpath =. }.@;@(c=.c&}.`(,~(<'/..')"0)@.(~:&{.))&('/'<;.1@,'\/'&charsub)~
'/usr/share/geany/colorschemes' relpath '/usr/share/vim/vim73/ftplugin'
../../vim/vim73/ftplugin
'C:\Windows\System32\drivers' relpath 'C:\Windows\System32\WindowsPowerShell\v1.0'
../WindowsPowerShell/v1.0
您也可以在tryj.tk上尝试一下。
../../vim\vim73\ftplugin。