def fetch(html) when is_binary(html), do: parse_html
iex(1)> html = HTTPoison.get!("http://portal.nifty.com/").body
iex(2)> page = MetaInvestigator.fetch(html)def is_string(<<_ :: utf8, t :: binary>>), do: is_string(t)
def is_string(<<>>), do: true
def is_string(_), do: false--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/d545e2a0-f90a-48c6-bd15-5fd33cab5be5%40googlegroups.com.
iex(1)> html = HTTPoison.get!("http://elixir-lang.org/").body
"<!DOCTYPE html>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n......"
iex(2)> is_binary(html)
true
iex(3)>String.valid?(html)
true<<60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 72, 84, 77, 76, 32, 80, 85, 66, 76, 73, 67, 32, 34, 45, 47, 47, 87, 51, 67, 47, 47, 68, 84, 68, 32, 72, 84, 77, 76, 32, 52, 46, 48, 49, 32, 84, 114, 97, 110, 115, ...>>
iex(2)> is_binary(html)
true
iex(3)> String.valid?(html)
false
So unfortunately it is impossible to create a new function that can be called from a guard.
The VM puts restrictions on what kind of expressions guards are allowed in guards, only a predefined set of functions can be called. So unfortunately it is impossible to create a new function that can be called from a guard.
On Sunday, November 15, 2015 at 11:11:14 PM UTC-5, Kobayakawa Ken wrote:I faced a problem when I created a library for web scraping. ( https://github.com/nekova/metainvestigator )in metainvestigator.exdef fetch(html) when is_binary(html), do: parse_htmliex(1)> html = HTTPoison.get!("http://portal.nifty.com/").body
iex(2)> page = MetaInvestigator.fetch(html)It fails.fetch/1 expected String, but it got Binary.Because the page's charset is not UTF-8 but Shift_JIS.is_binary/1 is not enough to use as is_string/1, so I want to use is_string/1 or is_utf8/1 inside guards.def is_string(<<_ :: utf8, t :: binary>>), do: is_string(t)
def is_string(<<>>), do: true
def is_string(_), do: falseThis implementation brought from String.valid?/1
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/d545e2a0-f90a-48c6-bd15-5fd33cab5be5%40googlegroups.com.
--Eric Meadows-Jönsson