Skip to content

Instantly share code, notes, and snippets.

@ryotako
Last active January 22, 2017 08:35
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ryotako/48406902bf5653c622e842e7c83a9da6 to your computer and use it in GitHub Desktop.
fishのビルトインコマンドだけでegzactを書く練習
function addb
while read -l line
echo $line
end
if count $argv >/dev/null
echo $argv[1]
end
end
function addl
if count $argv >/dev/null
while read -l line
echo $argv[1]$line
end
end
end
function addr
if count $argv >/dev/null
while read -l line
echo $line$argv[1]
end
end
end
function addt
if count $argv >/dev/null
echo $argv[1]
end
while read -l line
echo $line
end
end
function comb
function __comb
set -l length $argv[1]
set -l i $argv[1]
read -al line
while test $i -le (count $line)
if test $length -le 1
echo $line[$i]
else
set -l i_ (math $i - 1)
for former in (echo $line[1..$i_] | __comb (math $argv[1] - 1))
echo $former $line[$i]
end
end
set -l i (math $i + 1)
end
end
while read -al line
if test "$argv[1]" -gt (count $line)
echo $line | __comb (count $line)
else if test "$argv[1]" -gt 0
echo $line | __comb $argv[1]
end
end
end
function conv
set -l buf
while read -al line
set buf $buf $line
end
# parse an argument
set -l step
if test "$argv[1]" -gt 1
set step $argv[1]
else
set step 1
end
set -l total (math (count $buf) - $step + 1)
set -l fst 1
while test "$fst" -le "$total"
set lst (math $fst + $step -1)
echo $buf[$fst..$lst]
set fst (math $fst + 1)
end
end
function crops
while read -l line
set -l length (string length $line)
set -l start 1
set -l buf
while test $start -le $length
set -l i 1
while test $i -le (math $length - $start + 1)
set -l sub (string sub -s $start -l $i $line)
if not contains $sub $buf; and string match -qr "^$argv[1]\$" "$sub"
set buf $buf $sub
end
set i (math $i + 1)
end
set start (math $start + 1)
end
for s in $buf
echo $s
end
end
end
function cycle
while read -al line
set -l i 1
while test $i -le (count $line)
if test $i -eq 1
echo $line
else
set -l i_ (math $i - 1)
echo $line[$i..-1] $line[1..$i_]
end
set i (math $i + 1)
end
end
end
function dropl
while read -al line
if test "$argv[1]" -gt 0
if test "$argv[1]" -lt (count $line)
set -l n (math $argv[1] + 1)
echo $line[$n..-1]
else
echo
end
end
end
end
function dropr
while read -al line
if test "$argv[1]" -gt 0
if test "$argv[1]" -lt (count $line)
set -l n (math (count $line) - $argv[1])
echo $line[1..$n]
else
echo
end
end
end
end
function dupl
while read -l line
if test "$argv[1]" -gt 0
echo $line
echo $line | dupl (math "$argv[1]" - 1)
end
end
end
function flat
set -l buf
set -l i 0
while read -al line
set buf $buf $line
set i (math $i+1)
if test "$i" -eq "$argv[1]"
echo $buf
set buf
set i 0
end
end
if test $i -ne 0
echo $buf
end
end
function mirror
while read -al line
echo $line[-1..1]
end
end
function nestl
function __nestl
read -al line
if test (count $line) -lt 2
string replace \* " $line[-1] " "$argv[1]"
else
set -l repl (echo $line[1..-2] | __nestl $argv)
string replace \* " $repl $line[-1] " "$argv[1]"
end
end
while read -l line
echo $line | __nestl $argv
end
end
function nestr
function __nestr
read -al line
if test (count $line) -lt 2
string replace \* " $line[1] " "$argv[1]"
else
set -l repl (echo $line[2..-1] | __nestr $argv)
string replace \* " $line[1] $repl " "$argv[1]"
end
end
while read -l line
echo $line | __nestr $argv
end
end
function obrev
while read -al line
echo "$line[1..-1]"
echo "$line[-1..1]"
end
end
function perm
function __perm
read -al line
if test "$argv[1]" -eq 1
for word in $line
echo $word
end
else if test "$argv[1]" -gt 1
set -l i 1
while test $i -le (count $line)
if test (count $line) -lt 2
echo $line[$i]
else
set -l rest
if test $i -eq 1
set rest $line[2..-1]
else if test $i -eq (count $line)
set rest $line[1..-2]
else
set -l i_ (math $i - 1)
set -l i__ (math $i + 1)
set rest $line[1..$i_] $line[$i__..-1]
end
for latter in (echo $rest | __perm (math "$argv[1]" - 1))
echo $line[$i] $latter
end
end
set i (math $i + 1)
end
end
end
while read -l line
echo $line | __perm "$argv[1]"
end
end
function slit
set -l buf
while read -al line
set buf $buf $line
end
# parse an argument
set -l rows
if test "$argv[1]" -gt 0
set rows $argv[1]
else
set rows (count $buf)
end
set -l step (math (count $buf) / $rows)
set -l mod (math (count $buf) \% $rows)
set -l fst 1
set -l loop 1
while true
set -l lst (math $fst + $step -1)
if test "$loop" -le "$mod"
set lst (math $lst + 1)
end
if test "$lst" -ge (count $buf)
echo $buf[$fst..-1]
break
end
echo $buf[$fst..$lst]
set fst (math $lst + 1)
set loop (math $loop + 1)
end
end
function stairl
while read -al line
set -l i 1
while test $i -le (count $line)
echo $line[1..$i]
set i (math $i + 1)
end
end
end
function stairr
while read -al line
set -l i -1
while test (math 0 - $i) -le (count $line)
echo $line[$i..-1]
set i (math $i - 1)
end
end
end
function sublist
while read -al line
set -l lst 1
while test $lst -le (count $line)
set -l fst 1
while test $fst -le $lst
echo $line[$fst..$lst]
set fst (math $fst + 1)
end
set lst (math $lst + 1)
end
end
end
function subset
function __subset
set -l length $argv[1]
set -l i $argv[1]
read -al line
while test $i -le (count $line)
if test $length -le 1
echo $line[$i]
else
set -l i_ (math $i - 1)
for former in (echo $line[1..$i_] | __subset (math $argv[1] - 1))
echo $former $line[$i]
end
end
set -l i (math $i + 1)
end
end
while read -al line
set -l i 1
while test $i -le (count $line)
echo $line | __subset $i
set i (math $i + 1)
end
end
end
function takel
while read -al line
if test "$argv[1]" -gt 0
if test "$argv[1]" -gt (count $line)
echo $line
else
echo $line[1..$argv[1]]
end
end
end
end
function takelx
while read -al line
set -l buf
set -l matched 0
if test -z "$argv[1]"
return 0
end
for word in $line
set buf $buf $word
if string match -qr "$argv[1]" $word
set matched 1
break
end
end
if test $matched -eq 1
echo $buf
else
echo
end
end
end
function taker
while read -al line
if test "$argv[1]" -gt 0
if test "$argv[1]" -gt (count $line)
echo $line
else
set -l n -$argv[1]
echo $line[$n..-1]
end
end
end
end
function takerx
while read -al line
set -l buf
set -l matched 0
if test -z "$argv[1]"
return 0
end
for word in $line[-1..1]
set buf $word $buf
if string match -qr "$argv[1]" $word
set matched 1
break
end
end
if test $matched -eq 1
echo $buf
else
echo
end
end
end
function wrap
while read -al line
set -l buf
for word in $line
set buf $buf (string replace \* $word "$argv[1]")
end
echo $buf
end
end
function zniq
while read -al line
set -l buf
for word in $line
if not contains $word $buf
set buf $buf $word
end
end
echo $buf
end
end
function zrep
if test -z "$argv[1]"
return 0
end
while read -al line
set -l buf
for word in $line
if string match -qr $argv[1] $word
set buf $buf $word
end
end
echo $buf
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment