I found a function to turn maps into keyword lists on the internet, but it is not recursive:
def to_keyword_list(dict) do
Enum.map(dict, fn({key, value}) -> {String.to_atom(key), value} end)
end
I then made this one, but it gives me an error.
def tokey(dict) do
Enum.map(dict, fn({key, value}) ->
if is_map value do
{String.to_atom(key), tokey(value)}
else
{String.to_atom(key), value}
end
end)
end
Result of the first one:
["calig├╝eva": %{speeking: "speeeeeeee"}, test: "teet", tututu: "tururuuu"]
Result of the second one:
** (ArgumentError) argument error
:erlang.binary_to_atom(:speeking, :utf8)
code.exs:10: anonymous fn/1 in Util.tokey/1
(elixir) lib/enum.ex:1233: anonymous fn/3 in Enum.map/2
(stdlib) lists.erl:1263: :lists.foldl/3
(elixir) lib/enum.ex:1772: Enum.map/2
code.exs:8: anonymous fn/1 in Util.tokey/1
(elixir) lib/enum.ex:1233: anonymous fn/3 in Enum.map/2
(stdlib) lists.erl:1263: :lists.foldl/3
Is there an easier or more effective way of doing this? And why does it show that error? Can I not call a function from within itself?
Result of first one changing ü for u:
[caligueva: %{speeking: "speeeeeeee"}, test: "teet", tututu: "tururuuu"]
Second one outputs the same error. This is the map I'm using:
map = %{
"test" => "teet",
"tututu" => "tururuuu",
"caligueva" => %{"speeking": "speeeeeeee"}
}
For recursively converting Maps to Keyword Lists in Elixir:
defmodule MyMap do
def to_keyword_list(map) do
Enum.map(map, fn {k,v} ->
v = cond do
is_map(v) -> to_keyword_list(v)
true -> v
end
{String.to_atom("#{k}"), v}
end)
end
end
But as #Dogbert already mentioned, "Pure" Atoms cannot contain codepoints above 255, so your map keys should be simple Strings / Atoms:
iex(1)> MyMap.to_keyword_list(%{"caligueva" => %{speeking: "speeeeeeee"}, "test" => "teet", "tututu" => "tururuuu"})
[caligueva: [speeking: "speeeeeeee"], test: "teet", tututu: "tururuuu"]
Here is the refactored code for conversion:
def map_to_keyword_list(map), do: convert(map)
defp convert(map) when is_map(map), do: Enum.map(map, fn {k,v} ->{String.to_atom(k),convert(v)} end)
defp convert(v), do: v
Related
When I run an function with the code below:
elements = ["A", "B"]
for element <- elements, into: [] do
struct(element, [])
end
Elixir raise this exception:
** (FunctionClauseError) no function clause matching in Kernel.struct/3
The following arguments were given to Kernel.struct/3:
# 1
"A"
# 2
[]
# 3
#Function<18.114860832/2 in Kernel.struct/2>
Attempted function clauses (showing 4 out of 4):
defp struct(struct, [], _fun) when is_atom(struct)
defp struct(struct, fields, fun) when is_atom(struct)
defp struct(%_{} = struct, [], _fun)
defp struct(%_{} = struct, fields, fun)
Why Elixir pass an function as third parameter to Kernel.struct function?
The reason you are seeing Kernel.struct/3 is that Kernel.struct/2 calls a private Kernel.struct/3 clause internally.
The reason for the error, that there is no matching function clause, is because the first argument "A" is not a struct or an atom. Please read the documentation for struct/2:
The struct argument may be an atom (which defines defstruct) or a struct itself.
You are passing a string, which is not a valid argument.
I'm coming to Elixir from primarily a Javascript background. in JS, it's possible to write a higher order function "once" which returns a function that will invoke the passed in function only once, and returns the previous result on subsequent calls- the trick is manipulating variables that were captured via closure:
var once = (func) => {
var wasCalled = false, prevResult;
return (...args) => {
if (wasCalled) return prevResult;
wasCalled = true;
return prevResult = func(...args);
}
}
It seems to me that it's not possible to create this function in Elixir, due to its different variable rebinding behavior. Is there some other clever way to do it via pattern matching or recursion, or is it just not possible? Without macros that is, I'd imagine those might enable it. Thanks
Using the current process dictionary:
defmodule A do
def once(f) do
key = make_ref()
fn ->
case Process.get(key) do
{^key, val} -> val
nil ->
val = f.()
Process.put(key, {key, val})
val
end
end
end
end
Or if the function will be passed across processes, an ets table can be used:
# ... during application initialization
:ets.new(:cache, [:set, :public, :named_table])
defmodule A do
def once(f) do
key = make_ref()
fn ->
case :ets.lookup(:cache, key) do
[{^key, val}] -> val
[] ->
val = f.()
:ets.insert(:cache, {key, val})
val
end
end
end
end
Application.put_env / Application.get_env can also be used to hold global state, though usually is used for configuration settings.
It's not considered idiomatic in most cases, but you can do this with Agent:
defmodule A do
def once(fun) do
{:ok, agent} = Agent.start_link(fn -> nil end)
fn args ->
case Agent.get(agent, & &1) do
nil ->
result = apply(fun, args)
:ok = Agent.update(agent, fn _ -> {:ok, result} end)
result
{:ok, result} ->
result
end
end
end
end
Now if you run this:
once = A.once(fn sleep ->
:timer.sleep(sleep)
1 + 1
end)
IO.inspect once.([1000])
IO.inspect once.([1000])
IO.inspect once.([1000])
IO.inspect once.([1000])
You'll see that the first line is printed after 1 second, but the next 3 are printed instantly, because the result is fetched from the agent.
While both already given answers are perfectly valid, the most precise translation from your javascript is shown below:
defmodule M do
use GenServer
def start_link(_opts \\ []) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
def init(_args) do
Process.sleep(1_000)
{:ok, 42}
end
def value() do
start_link()
GenServer.call(__MODULE__, :value)
end
def handle_call(:value, _from, state) do
{:reply, state, state}
end
end
(1..5) |> Enum.each(&IO.inspect(M.value(), label: to_string(&1)))
Use the same metric as in #Dogbert’s answer: the first value is printed with a delay, all subsequent are printed immediately.
This is an exact analog of your memoized function using GenServer stage. GenServer.start_link/3 returns one of the following:
{:ok, #PID<0.80.0>}
{:error, {:already_started, #PID<0.80.0>}}
That said, it is not restarted if it’s already started. I do not bother to check the returned value since we are all set in any case: if it’s the initial start, we call the heavy function, if we were already started, the vaklue is already at fingers in the state.
I'm converting a Ruby project to Elixir. How does Ruby's until loop translate to Elixir?
until scanner.eos? do
tokens << scan(line + 1)
end
Here's the full Ruby method:
def tokenize
#tokens = []
#lines.each_with_index do |text, line|
#scanner = StringScanner.new(text)
until #scanner.eos? do
#tokens << scan(line + 1)
end
end
#tokens
end
#lines is just a text file split by new lines. #lines = text.split("\n")
In Elixir, I've already converted the string scanner which looks like this: StringScanner.eos?(scanner):
#spec eos?(pid) :: boolean
def eos?(pid) when is_pid(pid) do
Also, in Elixir, tokens are tuples: #type token :: {:atom, any, {integer, integer}}. Where the {integer, integer} tuple is the line and position of the token.
This is the Elixir psuedo-code which doesn't quite work.
#spec scan(String.t, integer) :: token
def scan(text, line) when is_binary(text) and is_integer(line) do
string_scanner = StringScanner.new(text)
until StringScanner.eos?(string_scanner) do
result = Enum.find_value(#scanner_tokenizers, fn {scanner, tokenizer} ->
match = scanner.(string_scanner)
if match do
tokenizer.(string_scanner, match, line)
end
end)
IO.inspect result
end
StringScanner.stop(string_scanner)
result
end
Someone on the slack channel suggested using recursion, however they didn't elaborate with an example. I've seen recursion examples for summing / reducing which use accumulators etc. However, I don't see how that applies when evaluating a boolean.
Can anyone provide a working example which uses StringScanner.eos?(scanner)? Thanks.
It may be something like
def tokens(scanner) do
tokens(scanner, [])
end
defp tokens(scanner, acc) do
if StringScanner.eos?(scanner) do
acc
else
tokens(scanner, add_to_acc(scan_stuff(), acc))
end
end
At least this can be the general idea. As you'll see I kept a couple of functions very generic (scan_stuff/0 and add_to_acc/2) as I don't know how you mean to implement those; the first one is meant to do what scan(line + 1) does in the Ruby code, while the second one is meant to do what << does in the Ruby code (e.g., it could add the scanned stuff to the list of tokens or something similar).
I need a main function to run the others functions.
I tried this:
let main () =
let deck = make_mazo in
let jugadores = players [] 0 in
dothemagic deck jugadores 0 [] [] [];;
But I got this error:
File "game.ml", line 329, characters 37-39:
Error: Syntax error
I think ;; is the problem and I need a different way to end the code. Also try with only ; and the problem is the same.
[EDIT]
An update here
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] [];
let () = main;;
Error persist:
File "game.ml", line 253, characters 13-15:
Error: Syntax error
The other functions are working perfectly fine, but i need a main function because I want to run the program with ocaml game.ml or ocamlbuild game.native
[SECOND EDIT]
After #camlspotter response: The use of ; of your code is wrong. Remove it.
Update 2.0
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] []
let () = main;;
New Error:
File "game.ml", line 253, characters 0-3: Error: Syntax error
Think let is the problem now, so i try with this
let main =
let deck = make_mazo [] in
let game = players deck [] 0 in
let dd = fst game in
let jugadores = snd game in
dothemagic dd jugadores 0 [] [] [] []
main;;
But Error is:
File "game.ml", line 253, characters 4-6:
Error: Syntax error
There's nothing syntactically wrong with the code you show here.
Most likely the problem is near the end of the part you don't show, like around line 324 of the file.
If I had to guess, I'd say that line 324 ends with in :-)
As a side comment, you'll also need to call this main function. You might want the last line of the file to be something like this:
let () = main ()
(This line appears in many of my OCaml projects.)
In ocaml, there is no main function unlike other languages, see the code below :
let () = print_string "hello\n";;
let f = print_string "hello, this is f\n";;
let () = f;;
OCaml programs, unlike programs in many other languages, do not have a specific entry-point: all the code in a module (file) is evaluated in order from top to bottom, sort of like in a scripting language. A common idiom you'll see is:
let name = "World" (* I could have added a ;; at the
* end of this line, but that would
* have been unnecessary *)
let () =
Printf.printf "Hello, %s!\n" name
which will output
Hello, World!
The let () = ... may seem a bit wonky, but it's really just pattern-matching: the return type of Printf.printf is unit, () is also of type unit, the you're really just saying "match this unit value with the result of evaluating this expression". Basically, this idiom means "run this unit-type expression in a safe way".
A similar, although highly discouraged, idiom, uses the catch-all pattern:
let greet s =
match s with
| "" -> false
| _ ->
Printf.printf "Hello, %s!\n" s;
true
let name = "World"
let _ =
greet world
The catch-all pattern doesn't care about the type (or value) of the expression it's being matched against, and this idiom means "run this expression and discard whatever it returned".
To solve my problem, I had to write the functions in the following way,
let fun x =
let y = blabla in
code_returning_unit; (* Use ; for unit returns *)
return_value;; (* Use ;; for end of fun *)
Thanks all for the help.
I need to pass two list as command line arguments in ocaml.
I used the following code to access it in the program.
let list1=Sys.argv.(1);;
let list2=Sys.argv.(2);;
I need to have the list1 and list2 as list of integers.
I am getting the error
This expression has type string but an expression was expected of type
int list
while processing.
How can I convert that arguments to a list of integers.
The arguments are passed in this format [1;2;3;4] [1;5;6;7]
Sys.argv.(n) will always be a string. You need to parse the string into a list of integers. You could try something like this:
$ ocaml
OCaml version 4.01.0
# #load "str.cma";;
# List.map int_of_string (Str.split (Str.regexp "[^0-9]+") "[1;5;6;7]");;
- : int list = [1; 5; 6; 7]
Of course this doesn't check the input for correct form. It just pulls out sequences of digits by brute force. To do better you need to do some real lexical analysis and simple parsing.
(Maybe this is obvious, but you could also test your function in the toplevel (the OCaml read-eval-print loop). The toplevel will handle the work of making a list from what you type in.)
As Sys.argv is a string array, you need to write your own transcription function.
I guess the simplest way to do this is to use the Genlex module provided by the standard library.
let lexer = Genlex.make_lexer ["["; ";"; "]"; ]
let list_of_string s =
let open Genlex in
let open Stream in
let stream = lexer (of_string s) in
let fail () = failwith "Malformed string" in
let rec aux acc =
match next stream with
| Int i ->
( match next stream with
| Kwd ";" -> aux (i::acc)
| Kwd "]" -> i::acc
| _ -> fail () )
| Kwd "]" -> acc
| _ -> fail ()
in
try
match next stream with
| Kwd "[" -> List.rev (aux [])
| _ -> fail ()
with Stream.Failure -> fail ()
let list1 = list_of_string Sys.argv.(1)
let list2 = list_of_string Sys.argv.(2)
Depending on the OCaml flavor you want to use, some other library may look more interesting. If you like yacc, Menhir may solve your problem in a few lines of code.