Hi,
I needed to get the list of all combinaisons of n elements chosen in
a sequence.
Quite simple in fact but disturbing for a beginner.
First problem, how to know if there is already a word doing this in
factor?
RTFM => I read "Sequence operations" several times.
Documentation is very nice and interesting to read but it's sometime
difficult for me to find a word doing what I want.
(Or there are too many words doing quite the same, I don't know)
Nevertheless reading definitions often give new ideas on how to
solve problems.
Second problem, maybe the needed word is undocumented somewhere in
the extra directory.
In that case it's maybe simpler to rewrite than to try to find it.
With doc freshly reminded I wrote without too many difficulties the
next two definitions (lean back and think)
: columnize ( seq -- seq )
[ 1array ] map
; inline
: among ( seq n -- seq )
2dup swap length
{
{ [ over 1 = ] [ 3drop columnize ] }
{ [ 2dup < ] [ 2drop [ 1 cut ] dip
[ 1- among [ append ] with map ]
[ among append ] 2bi
] }
{ [ 2dup = ] [ 3drop 1array ] }
{ [ 2dup > ] [ 2drop 2drop { } ] }
} cond
;
Real problems appear now. I have a solution but:
- how to be sure it's efficient ?
- if after writing it I discover word "unclip" is it useful, or more
elegant, to reshape definition to replace "1 cut" ?
- how to be sure that my definition is readable ?
- without knowing deeply factor, how i know which part of code I must
optimise, and how ?
The only obvious point is that there is a simpler and more idiomatic
way to do but experience comes slowly...
JF
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk