J-63文字
左側の古いパスと右側の新しいパスを使用する関数。
}.@;@(c=.c&}.`(,~(<'/..')"0)@.(~:&{.))&('/'<;.1@,'\/'&charsub)~
このソリューションは、次のような3つの部分で構成されてい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。