kitplummer

kitplummer

I’ve got a bit of a real world problem that I’m trying to teach myself through and am straight stuck. I’ve tried a handful of things to get to where I want to be…but just don’t have enough base knowledge to work through it. Obviously I could put this away and come back to it, but figure asking for a little help/tutoring is better. I’ve started with enumerating over a ‘categories’ list, then piping it into a Map.update, accumulating things but bah, didn’t get close.

Here’s the module:

defmodule Categories do
  def categorize!(directory, categories) do
    result = %{}
    cat1 = find_types_for_cat(directory, categories[:cat1])

    result =
      if Kernel.length(cat1[:cat1]) > 0 do
        Map.merge(result, cat1)
      else
        result
      end

    cat2 = find_types_for_cat(directory, categories[:cat2])

    result =
      if Kernel.length(cat2[:cat2]) > 0 do
        Map.merge(result, cat2)
      else
        result
      end

    cat3 = find_types_for_cat(directory, categories[:cat3])

    result =
      if Kernel.length(cat3[:cat3]) > 0 do
        Map.merge(result, cat3)
      else
        result
      end

    result
  end

  def find_types_for_cat(directory, category) do
    %{category.name => Path.wildcard("#{directory}/**/#{Category.types_to_string(category)}")}
  end
end

The struct:

defmodule Category do
  alias __MODULE__

  defstruct name: nil, types: []

  def types_to_string(category) do
    types = Enum.join(category.types, ",")
    "{" <> types <> "}"
  end
end

And here’s the tests:

defmodule CategoriesTest do
  use ExUnit.Case, async: true
  doctest Categories

  setup_all do
    category_one = %Category{name: :cat1, types: ["type1", "type1.1"]}
    category_two = %Category{name: :cat2, types: ["type2", "type2.1"]}
    category_three = %Category{name: :cat3, types: ["type3"]}

    ## TODO: Make this an extensible list of categories, no keys
    cat_map = %{:cat1 => category_one, :cat2 => category_two, :cat3 => category_three}
    [categories: cat_map]
  end

  describe "greets the world with bad Elixir" do
    test "dir1", %{categories: categories} do
      expected = %{
        cat1: ["test/dirs/dir1/type1"]
      }

      assert Categories.categorize!("test/dirs/dir1", categories) == expected
    end

    test "dir2", %{categories: categories} do
      expected = %{
        cat1: ["test/dirs/dir2/subdir1/type1", "test/dirs/dir2/type1"],
        cat2: ["test/dirs/dir2/subdir2/type2", "test/dirs/dir2/type2.1"]
      }

      assert Categories.categorize!("test/dirs/dir2", categories) == expected
    end

    test "dir3", %{categories: categories} do
      expected = %{
        cat1: ["test/dirs/dir3/subdir2/type1", "test/dirs/dir3/type1", "test/dirs/dir3/type1.1"],
        cat2: ["test/dirs/dir3/subdir3/type2", "test/dirs/dir3/type2"],
        cat3: ["test/dirs/dir3/subdir1/type3", "test/dirs/dir3/type3"]
      }

      assert Categories.categorize!("test/dirs/dir3", categories) == expected
    end
  end
end

I’ve also created a simple project to work on the problem if that’s easier to look at than pasted code blocks here.

Appreciate any help/guidance I can get. TIA.

Kit

Showing Posts 1 to 3

kokolegorille

kokolegorille

When there is something repetitive like this, I tend to use Enum.reduce, or list comprehension…

This would look like this (pseudo code…and not tested)

Enum.reduce([:cat1, :cat2, :cat3], %{}, fn cat, acc -> 
  search = find_types_for_cat(directory, categories[cat])
  if Enum.empty?(search) do
    acc
  else
    Map.put(acc, cat, search.name)
  end
end)

BTW there is a missing iteration with categories because they can have multiple types, so You miss a piece here.

As You mention… it would be better to have a list of categories, and use Enum.filter on it instead of using a map. Something like…

list = [category_one, category_two, category_three]
Enum.filter(list, fn & &.name == :cat1)
kitplummer

kitplummer OP

This was step one. Have changed categories to a List. And am getting closer with Enum.reduce()

kitplummer

kitplummer OP

Got it working.

Categories module:

defmodule Categories do
  @spec categorize!(any, any) :: any
  def categorize!(directory, categories) do
    Enum.reduce(categories, %{}, fn category, acc ->
      search = find_types_for_cat(directory, category)

      if Enum.empty?(search) do
        acc
      else
        Map.put(acc, category.name, search)
      end
    end)
  end

  @spec find_types_for_cat(any, atom | %{types: any}) :: [binary]
  def find_types_for_cat(directory, category) do
    Path.wildcard("#{directory}/**/#{Category.types_to_string(category)}")
  end
end

And the updated test:

defmodule CategoriesTest do
  use ExUnit.Case, async: true
  doctest Categories

  setup_all do
    category_one = %Category{name: :cat1, types: ["type1", "type1.1"]}
    category_two = %Category{name: :cat2, types: ["type2", "type2.1"]}
    category_three = %Category{name: :cat3, types: ["type3"]}
    cat_list = [category_one, category_two, category_three]
    [categories: cat_list]
  end

  describe "greets the world with bad Elixir" do
    test "dir1", %{categories: categories} do
      expected = %{
        cat1: ["test/dirs/dir1/type1"]
      }

      assert Categories.categorize!("test/dirs/dir1", categories) == expected
    end

    # @tag :skip
    test "dir2", %{categories: categories} do
      expected = %{
        cat1: ["test/dirs/dir2/subdir1/type1", "test/dirs/dir2/type1"],
        cat2: ["test/dirs/dir2/subdir2/type2", "test/dirs/dir2/type2.1"]
      }

      assert Categories.categorize!("test/dirs/dir2", categories) == expected
    end

    # @tag :skip
    test "dir3", %{categories: categories} do
      expected = %{
        cat1: ["test/dirs/dir3/subdir2/type1", "test/dirs/dir3/type1", "test/dirs/dir3/type1.1"],
        cat2: ["test/dirs/dir3/subdir3/type2", "test/dirs/dir3/type2"],
        cat3: ["test/dirs/dir3/subdir1/type3", "test/dirs/dir3/type3"]
      }

      assert Categories.categorize!("test/dirs/dir3", categories) == expected
    end
  end
end

Thanks for the pointer @kokolegorille - greatly appreciated. Will dig into Enum.reduce a bit more now.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews