Re: [Factor-talk] How would I solve this better in Factor?

2016-03-19 Thread John Benediktsson
Björn's can be sped up by not using assoc-map! and replacing
``refine-index`` with this:

:: refine-index ( max-i index prev-index -- )
index keys [ [ prev-index at ] [ index set-at ] bi ] each
index [ nip max-i >= ] assoc-filter! drop ;

That takes it from 2.0 seconds to 1.3 seconds on my laptop.


On Thu, Mar 17, 2016 at 11:11 AM, Alexander Ilin  wrote:

> Hello!
>
>   I've finally finished my solution:
>   http://paste.factorcode.org/paste?id=3852
>
>   I took a different approach. Instead of manipulating the source input
> string I create a hash table of all character positions, then remove and
> add items to it as I go until it's empty. Output is collected as a
> side-effect of that process in a global variable. This approach combines
> sub-par performance with difficult-to-read code.
>
>   On my machine the solve2 word runs in 28.5 seconds, compared to
> Sankaranarayan's 2.6 seconds, and Bjoern's 3.28 seconds. (Jon's solution
> never completes: Factor terminates without an error message after about 20
> minutes of work.)
>
>   I think the approach is interesting, but the performance seems to suffer
> due to the many searches, sorts and hash table manipulations. Apparently,
> working directly over the one input string is faster.
>
>   After initially implementing a solution with 6 global variables, I
> managed to reduce the number to 1 by moving stuff around and using locals.
> This was fun, and quite easy, although this one time I got the order of
> arguments wrong and had a hard time finding the cause. Apparently, it's not
> so easy to simulate code runs when locals are used: you can't just populate
> stack in the Listener and paste pieces of code into it.
>
>   Overall, this was a useful excersise in learning Factor. Thank you,
> Sankaranarayan!
>
> ---=---
>  Александр
>
>
> --
> 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=278785231=/4140
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
--
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=278785231=/4140___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How would I solve this better in Factor?

2016-03-12 Thread Björn Lindqvist
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
:
> 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=/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=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How would I solve this better in Factor?

2016-02-29 Thread Jon Harper
Hi,
when I'm writing a unoptimized solution to a problem, I usually go top
down, thinking about what l lists I have to build and transform. I try
to split code into words with names instead of adding comments. I try
to use the conventions (
http://docs.factorcode.org/content/article-conventions.html ). Also, I
try to follow this quote from
http://docs.factorcode.org/content/article-cookbook-philosophy.html:
"If you find yourself writing a heavily nested loop which performs
several steps on each iteration, there is almost always a better way.
Break the problem down into a series of passes over the data instead,
gradually transforming it into the desired result with a series of
simple loops. Factor the loops out and reuse them."

cheers,

Jon


On Sat, Feb 27, 2016 at 6:43 AM, Sankaranarayanan Viswanathan
 wrote:
> Wow, your words are simple and fit in single lines. Do you write pseudo
> code that is more expressible in factor or do you write traditional
> imperative style pseudo code and then translate them to a factor
> representation?
>
> Thanks,
> Sankar
>
> On 2/25/16 12:01 PM, Jon Harper wrote:
>> "Hi,
>> I wrote a basic o(n^4) solution to see how simple it could get. The
>> code looks nice to me, but it's sllw :) like 1 hour on the 3200
>> chars string.. http://paste.factorcode.org/paste?id=3843
>>
>> I noticed we have 1 function in common : remove-after-underscore. You
>> can can see how head and when* make my implementation shorter (and
>> more readable?)
>>
>> Cheers,
>> Jon
>>
>>
>> On Wed, Feb 24, 2016 at 5:50 AM, Sankaranarayanan Viswanathan
>>  wrote:
>>> 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=/4140
>>> ___
>>> Factor-talk mailing list
>>> Factor-talk@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>> --
>> 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=/4140
>>
>
>
>
> --
> 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=/4140
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How would I solve this better in Factor?

2016-02-26 Thread Sankaranarayanan Viswanathan
Wow, your words are simple and fit in single lines. Do you write pseudo 
code that is more expressible in factor or do you write traditional 
imperative style pseudo code and then translate them to a factor 
representation?

Thanks,
Sankar

On 2/25/16 12:01 PM, Jon Harper wrote:
> "Hi,
> I wrote a basic o(n^4) solution to see how simple it could get. The
> code looks nice to me, but it's sllw :) like 1 hour on the 3200
> chars string.. http://paste.factorcode.org/paste?id=3843
>
> I noticed we have 1 function in common : remove-after-underscore. You
> can can see how head and when* make my implementation shorter (and
> more readable?)
>
> Cheers,
> Jon
>
>
> On Wed, Feb 24, 2016 at 5:50 AM, Sankaranarayanan Viswanathan
>  wrote:
>> 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=/4140
>> ___
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
> --
> 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=/4140
>



--
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=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How would I solve this better in Factor?

2016-02-25 Thread Alexander Ilin
PPS: even shorter:

: remove-after-underscore ( seq -- seq' )
"_" split1 drop ;

---=---
 Александр

--
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=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How would I solve this better in Factor?

2016-02-25 Thread Alexander Ilin
Hello, Jon!

25.02.2016, 20:02, "Jon Harper" :
> I wrote a basic o(n^4) solution to see how simple it could get. The
> code looks nice to me, but it's sllw :) like 1 hour on the 3200
> chars string.. http://paste.factorcode.org/paste?id=3843

  Wow, your code looks very nice!
  I didn't know about collect-index-by, so I had to invent it yesterday.
  Today I was also about to invent all-combinations : ))

> I noticed we have 1 function in common : remove-after-underscore. You
> can can see how head and when* make my implementation shorter (and
> more readable?)

  Here's another version of that same function, I hope you like it:

USE: splitting

: remove-after-underscore ( seq -- seq' )
{ CHAR: _ } split1 drop ;

  : )

PS: Can someone approve two of my messages that are stuck in the mailing list's 
moderation queue?

---=---
 Александр

--
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=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How would I solve this better in Factor?

2016-02-25 Thread Jon Harper
"Hi,
I wrote a basic o(n^4) solution to see how simple it could get. The
code looks nice to me, but it's sllw :) like 1 hour on the 3200
chars string.. http://paste.factorcode.org/paste?id=3843

I noticed we have 1 function in common : remove-after-underscore. You
can can see how head and when* make my implementation shorter (and
more readable?)

Cheers,
Jon


On Wed, Feb 24, 2016 at 5:50 AM, Sankaranarayanan Viswanathan
 wrote:
> 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=/4140
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk

--
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=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How would I solve this better in Factor?

2016-02-24 Thread Alexander Ilin
Hi!

> 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.

  I don't think your pseudocode is correct:
  if count == 1, you don't need to "check if str(first c to end) is longest".

  Anyway, providing a good answer requires solving the original problem,
  which I'm in the process of.

---=---
 Александр

--
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=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk