Fork me on GitHub
#off-topic
<
2019-12-02
>
jaide04:12:38

I could use a bit of help thinking through the approach. I'm in JS and I need to transform a string like "(((a,1),(b,2),(c,3)))` into an array. I was able to figure out an algorithm but I feel it would benefit from being more functional and separated into smaller, more meaningful pieces. However, I'm not sure how to start refactoring it. Where would you start?

const R = require("ramda");

function deserializeDFS (input) {
  let results = [];
  let seq = "";
  let rem = input;

  if (!input) {
    return {
      results,
      rem,
    };
  }

  for (let i = 0; i < input.length; i += 1) {
    let char = R.head(rem);

    rem = R.tail(rem);

    if (char === "(") {
      let res = deserializeDFS(rem);

      rem = res.rem;
      results.push(res.results);
    }
    else if (char === ")") {
      if (seq) results.push(seq);
      break;
    }
    else if (char === ",") {
      if (seq) results.push(seq);
      seq = "";
    }
    else {
      seq += char;
    }
  }

  return {
    results,
    rem,
  };
}

console.log(deserializeDFS("(((a,1),(b,2),(c,3)))"));

jaide22:12:41

Made some progress and arrived at https://codepen.io/jayzawrotny/pen/qBEBKJR?editors=0010. It's not perfect but it's good enough for now. I think the next step would be to implement some minimal FP parser combinators but I think that will take me about a week to learn and work my way through as per my experience last time I tried to write parser combinators.

jaide22:12:49

If anyone has a better approach, I'm all ears! This is far from my area of expertise.

eggsyntax14:12:10

@denik I really enjoyed your "Embodied Runtime" talk at Conj, thanks! It was especially cool to see the sped-up videos of the human implementations of pure functions. Just in case you're not already aware of it, I wanted to point out Bret Victor's amazing work on programming environments where you can directly, visually observe the dynamic runtime behavior of your program (posting this here rather than PM because I try to make sure everyone's aware of Victor's work): PS - for anyone who hasn't seen Dennis's talk, it's here: https://www.youtube.com/watch?v=0GAdjZQAp4E http://worrydream.com/#!/LearnableProgramming

denik03:12:58

Thanks! I’m aware of Brett Victors work. Thanks for sharing! And glad got something out of my talk!

👍 4