Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Haskell idioms can I use to implement realtime find-as-you-type autocomplete at the command line?

I'm trying to get a handle on the basics of Haskell by creating a Notational Velocity style find-as-you-type file search on the command line. Instead of tackling the whole problem, I'm trying a very basic version of it: There exists a file with 10 lines, and a 3 letter word on each line. After each letter I type, I want to update the display of a list of line numbers that might contain the word I'm typing, based on my input so far.

Can someone demonstrate a Haskell program that does this? I think my problem lies in forcing reevaluation on every character input. Thanks a million in advance.

like image 823
user2151143 Avatar asked Mar 09 '13 09:03

user2151143


2 Answers

I won't try to write the whole Haskell program you're asking for, but here is a very short example showing just the bit you claim to be stuck on at the moment: doing something after every key press. We won't do anything exciting (just bump a number and print it out), but it will show how to do that one small task, and you perhaps can start hacking from there.

The only thing you really need to know that you seem not to is that you can turn off line-buffering on the input.

import System.IO

loop n = do
    c <- getChar
    print n
    -- do whatever recalculation you need to do here, using
    -- n (which can be more complicated than an Integer, as
    -- it is here, of course) and c (which is a Char
    -- representing the key the user whacked)
    -- our recalculation is just to increase n by one
    loop (n+1)

main = do
    hSetBuffering stdin NoBuffering -- do this once before anything else
    loop 0
like image 80
Daniel Wagner Avatar answered Nov 15 '22 06:11

Daniel Wagner


Studing Reactive programming could be a good starting.
For this a good library seem to be reactive banana
There some base example.

If you want to know more, on FRP, an excellent topic on stack will give you a clear overview.

like image 27
zurgl Avatar answered Nov 15 '22 06:11

zurgl