Here is my solution:

https://github.com/bjourne/playground-factor/blob/master/examples/golf/challenge-252/challenge-252.factor

It's a straight transliteration of the Python solution that was posted
in the reddit thread. The hard part was getting the loops right.
Factor doesn't have any iteration syntax so you have to use
combinators and recursion instead. The trickiest word to translate was
widest_leftmost_pair because it uses many variables and mutable state.
The body of the function is the loop in "for i, ch in enumerate(s)"
and it corresponds to Factors each-index combinator.

When I wrote the code, I used the locals vocab and lexical variables
almost everywhere. Because if you are not sure how the data should
flow through your words it is easier to first get something ugly
working with locals, then when you have it working you can easily
rewrite it in the stack-oriented style. It helps to write tests so
that you don't break your code while refactoring. Some words, like the
update-pair word, can't or shouldn't be translated to stack-oriented
style because juggling five data items on the stack gets confusing.

2016-02-24 5:50 GMT+01:00 Sankaranarayanan Viswanathan
<rationalrev...@gmail.com>:
> Hi,
>
> It had been awhile since I wrote any Factor code, so I was trying to
> solve the following problem from reddit's dailyprogrammer subreddit:
> http://bit.ly/1OtP8Qj
>
> The solution I came up with in Factor looks like this:
> http://bit.ly/1PY8j98
>
> I struggled quite a lot coming up with this. Mainly in keeping things in
> my head and figuring out what I needed to do to bring the stack in order
> for the operations I was attempting..
>
> Coming from an iterative programming background (with a little bit of
> exposure to functional programming), I find it quite hard to formulate
> solutions in Factor. Can someone help me with tips on how they approach
> writing code in Factor? What is your though process to turn a given
> pseudo code into a factor program?
>
> For example, my pseudo code for the main portion of this problem (the
> find-match function) was as follows:
>
> let buff = ""
> let match = (indx=0, length=-1)
> for each char c in input:
>         find count(c) in buff
>         if count == 2
>                 append c to buff
>                 check if str(first c to end) is longest and update match
>                 remove chars in buff before second c occurrence
>         else if count == 1
>                 append c to buff
>                 check if str(first c to end) is longest and update match
>                 remove chars in buff before first c occurrence
>         else
>                 append c to buff
> discard buff and return match
>
> Any pointers is greatly appreciated.
>
> Thanks,
> Sankar
>
>
> ------------------------------------------------------------------------------
> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> Monitor end-to-end web transactions and take corrective actions now
> Troubleshoot faster and improve end-user experience. Signup Now!
> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk



-- 
mvh/best regards Björn Lindqvist

------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785111&iu=/4140
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to