Can I concatenate strings in Elixir and use the pipe operator?
Yes, you can, by passing the full path to the function, which in this case is Kernel.<>: iex(1)> “foo” |> Kernel.<>(“bar”) “foobar”
Yes, you can, by passing the full path to the function, which in this case is Kernel.<>: iex(1)> “foo” |> Kernel.<>(“bar”) “foobar”
This is an old question and the previously accepted answer was no longer the de facto way. Ecto now supports HABTM or many to many associations. https://hexdocs.pm/ecto/Ecto.Schema.html#many_to_many/3 many_to_many :users, MyApp.User, join_through: “chat_group_users”
Yes it is. In your mix.exs, you can list the dependency with the :path keyword (very similarly to what you do with gems). def dependencies do [{:testing_dep, path: “/Users/me/testing_dep”}] end You can read a list of all the supported options (e.g., pulling the dependency from GitHub or from a Git repository) in the documentation for … Read more
Turns out you iterate over a Map exactly like you do over a Keyword List (i.e. you use a tuple): Enum.each(%{a: 1, b: 2, c: 3}, fn {k, v} -> IO.puts(“#{k} –> #{v}”) end) Comprehensions also work: for {k, v} <- %{a: 1, b: 2, c: 3} do IO.puts(“#{k} –> #{v}”) end Note: If you … Read more
The options that Dogbert has provided are both correct and should be used for Ecto 1.x. In Ecto 2.0 you can use Repo.aggregate/4 Repo.aggregate(Post, :count, :id)
If you are looking for something similar to npm install –save, then this does not exist in Elixir. You install things by adding them to deps: in the mix.exs file in your project then running mix deps.get. The other way you may wish to install certain applications is via a mix archive allowing this mix … Read more
Enum.uniq does what you want, for example: iex(6)> Enum.uniq([1,26,3,40,5,6,6,7]) [1, 26, 3, 40, 5, 6, 7] In terms of how you’d implement it you could write a recursive function like so: defmodule Test do def uniq(list) do uniq(list, MapSet.new) end defp uniq([x | rest], found) do if MapSet.member?(found, x) do uniq(rest, found) else [x | … Read more
defmodule MatchStick do def doMatch(“a” <> rest) do 1 end def doMatch(_) do 0 end end You need to use the string concatenation operator seen here Example: iex> “he” <> rest = “hello” “hello” iex> rest “llo”
This is the pipe operator. From the linked docs: This operator introduces the expression on the left-hand side as the first argument to the function call on the right-hand side. Examples iex> [1, [2], 3] |> List.flatten() [1, 2, 3] The example above is the same as calling List.flatten([1, [2], 3]).
String.trim/1 seems to do the trick as of Elixir 1.3.0. strip still works, but it was soft deprecated in the 1.3.0 release and it isn’t listed in the String docs.