Community Page
- phildawes.net/blog/ Jump to website »
-
Subscribe -
Community
-
Top Commenters
-
Popular Threads
-
Recent Comments
- Hi, Do you feel that your agility in Factor has improved since this post? Roger
- Thanks for the pointer - I've cleaned up the spam and regrettably added some moderation
- I'm loving the comments thread for this post. Can't decide whether to get my upholstery cleaned or do something about my fast food obesity.
- Cool - thanks Eric
- I pasted some code that does the moving sum in factor. http://paste.factorcode.org/paste?id=569#282
Jump to original thread »
I’ve been playing with Factor for a couple of weeks. I’m finding that it takes me quite a bit longer to write stuff with factor than with other languages, but the process is enjoyable and I get the feeling that I’m learning something useful each time. The questi
... Continue reading »
1 year ago
let insertList x = map (insert x) where insert x (y,z) = (x,y,z)
let cols = ["col1","col2","col3"]
let rows = [["a","b","c"],["e","f","g"]]
concat $ zipWith insertList [0..] $ map (zip cols) rows
This prints [(0,"col1","a"),(0,"col2","b"),(0,"col3","c"),(1,"col1","e"),(1,"col2","f"),(1,"col3","g")]
No preview available so I don't know if your blog software is going to mangle that!
1 year ago
>>> from pprint import pprint as pp
>>> tabular = [["col1","col2","col3"],[["a","b","c"],["e","f","g"]]]
>>> triples = [(row[0], col[1], row[1][col[0]])
for row in enumerate(tabular[1])
for col in enumerate(tabular[0])]
>>> pp(triples)
[(0, 'col1', 'a'),
(0, 'col2', 'b'),
(0, 'col3', 'c'),
(1, 'col1', 'e'),
(1, 'col2', 'f'),
(1, 'col3', 'g')]
>>>
(I added a couple of newlines to the list comprehension to get it into the comment box).
If I were using it then I might wrap it in a function and add a docstring to test/explain it.
- Paddy.
1 year ago
Small typo in the first sentence: "I’ve been playing with for a couple of weeks.", I believe you meant "factor" instead of "for".
If you are interested in concatenative languages and the semantic web have you seen Ripple? http://ripple.fortytwo.net/
There is also my own relatively immature language Cat (http://www.cat-language.com), which will be stable "real soon now".
Cheers,
Christopher Diggins
1 year ago
I'll definitely check out both Cat and ripple - thanks for the links
1 year ago
I had a go at implementing this. The key, as you've noticed, is to decompose the problem into steps where you're working with no more than a handful of values at once.
Don'write a big loop, instead break it up into multiple iterations over the data and use library words where possible.
USE: math.ranges
: (column-names) ( cols row -- pairs )
2array flip ;
: column-names ( cols rows -- seqs-of-pairs )
[ (column-names) ] curry* map ;
: (number-rows) ( pairs n -- triples )
[ add* ] curry map ;
: number-rows ( seqs-of-pairs -- triples )
tuck length [a,b] [ (number-rows) ] 2map concat ;
: tabular>triples ( start-rowid cols rows -- triples )
column-names number-rows ;
1 year ago
[(i,col,cell)|(i,row)<-zip [startid..] rows, (col,cell) <- zip cols row]
Now I wonder how this will get formatted? Need a preview button.