GithubHelp home page GithubHelp logo

RSV in Elixir about rsv-challenge HOT 3 OPEN

dcoai avatar dcoai commented on May 28, 2024
RSV in Elixir

from rsv-challenge.

Comments (3)

Stenway avatar Stenway commented on May 28, 2024

Awesome :-) My Elixir understanding currently only goes to the extend of Fireships "Elixir in 100 Seconds" video.

Could you also try to check the 79 valid and 29 invalid test files in this directory?
https://github.com/Stenway/RSV-Challenge/tree/main/TestFiles

I always like to check against these test files, to know if everything is working:
https://github.com/Stenway/RSV-Challenge/blob/main/Python/rsv.py#L153

If it would comply and you don't have a problem with the MIT-0 license, we could surely add it to repository.

Thanks for your valuable input.

from rsv-challenge.

gausby avatar gausby commented on May 28, 2024

Here is my solution in Elixir:

defmodule RSV do
  @moduledoc """
  Documentation for `RSV`.
  """

  @terminate_value 0xFF
  @terminate_row 0xFD
  @null 0xFE

  @doc """
  Encode data containing list of lists of Strings and Nil
  """
  def encode(data_rows) when is_list(data_rows) do
    for row <- data_rows, into: <<>> do
      <<for(value <- row, into: <<>>, do: encode_value(value))::binary, @terminate_row>>
    end
  end

  defp encode_value(nil), do: <<@null, @terminate_value>>
  defp encode_value(value), do: <<value::binary, @terminate_value>>

  @doc """
  Decode RSV encoding data
  """
  def decode(<<data::binary>>), do: do_decode(data, [], [], [])

  defp do_decode(<<>>, [], [], acc), do: Enum.reverse(acc)

  defp do_decode(<<@terminate_row, rest::binary>>, [], current_row, acc) do
    do_decode(rest, [], [], [Enum.reverse(current_row) | acc])
  end

  defp do_decode(<<@terminate_value, rest::binary>>, current_value, current_row, acc) do
    value = current_value |> Enum.reverse() |> IO.iodata_to_binary()
    do_decode(rest, [], [value | current_row], acc)
  end

  defp do_decode(<<@null, @terminate_value, rest::binary>>, [], current_row, acc) do
    do_decode(rest, [], [nil | current_row], acc)
  end

  defp do_decode(<<char, rest::binary>>, value_acc, row_acc, acc) do
    do_decode(rest, [char | value_acc], row_acc, acc)
  end
end

It will complain loudly if the input data is invalid—which is good. Perhaps it performs too many Enum.reverse/1 operations in the decoder, and it doesn't do streaming.

from rsv-challenge.

gausby avatar gausby commented on May 28, 2024

I tried the implementation I mentioned yesterday (#3 (comment)) against the test fixtures, and it didn't pass the invalid cases—so I added a test setup that load the test fixtures from the /TestFiles/ directory (may it never move), and fixed the errors. I turned it into a PR.

@Stenway You are welcome to the code, and the MIT license is fine with me.

from rsv-challenge.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.