Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fuzzy finder as a Go library

ktr
May 18, 2019

Fuzzy finder as a Go library

Go Conference 2019 Spring

ktr

May 18, 2019
Tweet

More Decks by ktr

Other Decks in Programming

Transcript

  1. $ git branch | egrep -v '^\*' | fzf |

    
 xargs git checkout ΠϯλϥΫςΟϒʹνΣοΫΞ΢τઌ
 ϒϥϯνΛબ୒
  2. func main() { var slice []string s := bufio.NewScanner(os.Stdin) for

    s.Scan() { slice = append(slice, s.Text()) } idx, err := fuzzyfinder.Find(slice, func(i int) string { return slice[i] }) if err != nil { fmt.Fprintf(os.Stderr, "failed to find: %s\n", err) os.Exit(1) } fmt.Println(slice[idx]) }
  3. func main() { idx, err := fuzzyfinder.FindMulti(songs, func(i int) string

    { return songs[i].Title }, fuzzyfinder.WithPreviewWindow(func(i, w, h int) string { if i == -1 { return "" } return fmt.Sprintf( "Title: %s\nArtist: %s\nAlbum: %s\n", songs[i].Title, songs[i].ArtistName, songs[i].AlbumName) })) if err != nil { fmt.Fprintf(os.Stderr, "failed to find: %s\n", err) os.Exit(1) } for _, i := range idx { fmt.Println(fmt.Sprintf( "%s / %s / %s", songs[i].Title, songs[i].ArtistName, songs[i].AlbumName)) } }
  4. /FFEMFNBO8VOTDIΞϧΰϦζϜ e.g.
 
 “ATAC” ʹରͯ͠ “” ΛΞϥ Πϯϝϯτ͢Δʹ͸ 4 ͭ

    ΪϟοϓΛૠೖ͢Ε͹ྑ͍ | A T A C ------------------------- | 0 -2 -4 -6 -8 A| -2 C| -4
  5. /FFEMFNBO8VOTDIΞϧΰϦζϜ “A” ͱ “” ͕͋Γɺ
 ޙऀʹΪϟοϓΛૠೖ
 ͢ΔͷͰ -2 (-2 -

    2 = -4) | A T A C ------------------------- | 0 -2 -4 -6 -8 A| -2 C| -4
  6. /FFEMFNBO8VOTDIΞϧΰϦζϜ “” ͱ “A” ͕͋Γɺ
 લऀʹΪϟοϓΛૠೖ
 ͢ΔͷͰ -2
 (-2 -

    2 = -4) | A T A C ------------------------- | 0 -2 -4 -6 -8 A| -2 C| -4
  7. /FFEMFNBO8VOTDIΞϧΰϦζϜ match = 2 left gap = -4 top gap

    = -4
 
 Ͳ͔͜ΒٻΊ͔ͨΛه࿥ | A T A C ------------------------- | 0 -2 -4 -6 -8 A| -2 2 C| -4
  8. /FFEMFNBO8VOTDIΞϧΰϦζϜ mismatch = -3 left gap = 0 top gap

    = -6 | A T A C ------------------------- | 0 -2 -4 -6 -8 A| -2 2 0 C| -4
  9. /FFEMFNBO8VOTDIΞϧΰϦζϜ match = -2 left gap = -2 top gap

    = -8 | A T A C ------------------------- | 0 -2 -4 -6 -8 A| -2 2 0 -2 C| -4
  10. /FFEMFNBO8VOTDIΞϧΰϦζϜ mismatch = -7 left gap = -4 top gap

    = -10 | A T A C ------------------------- | 0 -2 -4 -6 -8 A| -2 2 0 -2 -4 C| -4
  11. | A T A C ------------------------- | 0 -2 -4

    -6 -8 A| -2 2 0 -2 -4 C| -4 0 1 -1 0 /FFEMFNBO8VOTDIΞϧΰϦζϜ ࠷ऴతͳঢ়ଶ
  12. | A T A C ------------------------- | 0 -2 -4

    -6 -8 A| -2 2 0 -2 -4 C| -4 0 1 -1 0 /FFEMFNBO8VOTDIΞϧΰϦζϜ ຤ඌͷείΞ͕
 ͜ͷจࣈྻؒͷείΞ 
 είΞ = 0
  13. | A T A C ------------------------- | 0 -2 -4

    -6 -8 A| -2 2 0 -2 -4 C| -4 0 1 -1 0 /FFEMFNBO8VOTDIΞϧΰϦζϜ A T A C | | A - - C ຤ඌ͔Β໼ҹͷॱʹḷΔ
  14. | A T A C ------------------------- | 0 0 0

    0 0 A| 0 2 0 2 0 C| 0 0 1 0 4 4NJUI8BUFSNBOΞϧΰϦζϜ ຤ඌ͔Βઌ಄Ͱ͸ͳ͘ɺ ෦෼తͳΞϥΠϯϝϯτ (຤ඌ͔Β 0 ·Ͱ)
  15. | A T A C ------------------------- | 0 0 0

    0 0 A| 0 2 0 2 0 C| 0 0 1 0 4 4NJUI8BUFSNBOΞϧΰϦζϜ είΞ = 4
  16. | A T A C ------------------------- | 0 0 0

    0 0 A| 0 2 0 2 0 C| 0 0 1 0 4 4NJUI8BUFSNBOΞϧΰϦζϜ A T A C | | - - A C