Created
July 27, 2016 01:40
-
-
Save anonymous/84341761fc0e6e783889bdbb6658ef67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Elindex.Searcher do | |
def search(word) do | |
File.ls!("sample") | |
|> Stream.map(fn(filename) -> | |
fn -> | |
Path.join("sample", filename) | |
|> File.read! | |
|> hit_info(word) | |
end | |
end) | |
|> Stream.map(&Task.async/1) | |
|> Stream.map(&Task.await/1) | |
|> Stream.filter(fn({count, _}) -> count > 0 end) | |
|> Enum.sort(&>=/2) | |
|> Enum.take(10) | |
end | |
def hit_info(blob, word) do | |
count = Enum.count(Regex.scan(~r(\b#{word}\b), blob)) | |
title = String.split(blob, "\n") |> List.first | |
{count, title} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment