The most fun way of carrying text over long lines, typical to that of your favorite , in the language of your choice.
Let's kick off with some simple FIOCthp:
# -*- coding: cp1252 -*-
MAX_LINE_LENGTH = 60
a="""The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:"""
def cut_lines(words,result,line=''):
if len(words):
if len(words[0])>MAX_LINE_LENGTH:
result.append(words.pop(0))
else:
test = line+words[0]+' '
if len(test)<=MAX_LINE_LENGTH:
words.pop(0)
cut_lines(words,result,test)
else:
result.append(line)
cut_lines(words,result)
else:
result.append(line)
def carry_lines(a):
words=a.split()
result=[]
cut_lines(words,result)
return result
/* fold -- wrap each input line to fit in specified width.
Copyright (C) 1991, 1995-2006, 2008-2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>;. */
/* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
if (column > width)
{
/* This character would make the line too long.
Print the line plus a newline, and make this character
start the next line. */
if (break_spaces)
{
bool found_blank = false;
size_t logical_end = offset_out;
/* Look for the last blank. */
while (logical_end)
{
--logical_end;
if (isblank (to_uchar (line_out[logical_end])))
{
found_blank = true;
break;
}
}
if (found_blank)
{
size_t i;
/* Found a blank. Don't output the part after it. */
logical_end++;
fwrite (line_out, sizeof (char), (size_t) logical_end,
stdout);
putchar ('\n');
/* Move the remainder to the beginning of the next line.
The areas being copied here might overlap. */
memmove (line_out, line_out + logical_end,
offset_out - logical_end);
offset_out -= logical_end;
for (column = i = 0; i < offset_out; i++)
column = adjust_column (column, line_out[i]);
goto rescan;
}
}
if (offset_out == 0)
{
line_out[offset_out++] = c;
continue;
}
switch (optc)
{
case 'b': /* Count bytes rather than columns. */
count_bytes = true;
break;
case 's': /* Break at word boundaries. */
break_spaces = true;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (optarg)
optarg--;
else
{
optargbuf[0] = optc;
optargbuf[1] = '\0';
optarg = optargbuf;
}
/* Fall through. */
case 'w': /* Line width. */
{
unsigned long int tmp_ulong;
if (! (xstrtoul (optarg, NULL, 10, &tmp_ulong, "") == LONGINT_OK
&& 0 < tmp_ulong && tmp_ulong < SIZE_MAX - TAB_WIDTH))
error (EXIT_FAILURE, 0,
_("invalid number of columns: %s"), quote (optarg));
width = tmp_ulong;
}
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
if (argc == optind)
ok = fold_file ("-", width);
else
{
ok = true;
for (i = optind; i < argc; i++)
ok &= fold_file (argv[i], width);
}
#lang racket
(define MAX_LINE_LENGTH 60)
(define wordlist "The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:")
(define (everything wrds)
(define-values (_jk lst rst)
(for/fold ([cnt 0] [lac '()] [tac '()])
([w (regexp-split #rx" +" wrds)])
(let ([a (+ 1 cnt (string-length w))])
(if (> a MAX_LINE_LENGTH)
(values (+ 1 0 (string-length w)) (cons w '()) (cons (reverse lac) tac))
(values a (cons w lac) tac)))))
(for ([x (reverse (cons lst rst))])
(printf "~a~n" (string-append* (add-between x " ")))))
(everything wordlist)
Name:
Frozen Maiden2011-12-15 6:10
>>1
for element in carry_lines(a * 20):
print element
Here's a quick hack I just wrote (bad formatting to make it into an "one"-liner, use Emacs to format it if you want to actually read it):
(defun word-wrap (string max-length &aux (tlen max-length))
(reduce #'(lambda (a b) (if b (concatenate 'string a (string (if (< (+ (length a) (length b)) tlen) #\ (prog1 #\Newline (setq tlen (+ max-length (length a)))))) b) a)) (split-sequence #\ string)))
The only major issue with it is that it won't eat excess whitespace if you have any (instead they are added to the line as seen fit).
I usually just write the >>7-equivalent for most of my word-wrapping needs (manually walking through spaces and seeking until the end of the next word), but that's boring and no fun to post on /prog/.
>>14
Symta-guy, are you hating it merely because it's used in Haskell? Your logic does not hold much water, it's the same kind of 'shun by association' broken logic you're using when hating on various math that people of a certain nationality have worked on.
>>16
Except you can always verify the results by hand or even verify them formally with a theorem prover (there is quite a lot of formalized Category Theory).
However, it's true that CT is quite an abstract and probably not enough people know/understand it in detail.
Name:
Anonymous2011-12-15 9:12
>>17
If the result depends on "Infinity" (wihch is primary requirement for CT), then the result is false by definition of being false.
>>18
I don't know a single mathematical theory that doesn't have some implied infinities lurking around. Even the most finitary systems will tend to have at least up to countable infinities appearing in their models and thus lacking finite consistency proofs.
Even computation which is universal has a countable infinity of programs.
>>19
Don't feed the ``finitist'' troll, please. He is boring.
Also, CT does not depend on the notion of "infinity" or "set", so you should probably abstain from defending it, seeing as you are not really qualified for that.
>>20
CT can talk about 'Infinity' or some specific 'Set theory', as it can talk about most mathematical structures. I've only read introductory material about CT, and as far as I can tell, it's mostly about morphisms, functions and finding all kinds of regularities within mathematical structures which map well to those concepts (and concepts building upon them).
Name:
Anonymous2011-12-15 9:48
CT does not depend on the notion of "infinity" or "set"
Due to hype around CT and total uselessness of wiki-article, I asked forcer-jews "what is ther category theory good for?" and was pointed to the "categories for the working mathematician" book, which I torrented. The book used notions of "set" and "infinity" from the first pages, so I stopped reading it immidiately. Such books should be burned.
>>24
It's imposible to properly define a formal system that works with ultra-finitism, not only that, it makes sense for me to talk about any finite, but unbounded values (classical finitism), but it makes no sense to pick some particular value and say that that's it and no number exists beyond that. I have no problem with someone rejecting uncountable infinities, but rejecting the potential or countable infinity seems untenable.
>>26 rejecting the potential or countable infinity seems untenable.
It should be banned by the law, like sharia Islam bans usury and alcoholism. All arguing jews should be sent to their "God".
>>26 but it makes no sense to pick some particular value and say that that's it and no number exists beyond that
You're absolutely right. Good thing ultrafinitists don't do that!
def fold(text, width=80)
folded = []; line = ""
text.split().each do |word|
word.chars.each_slice(width) do |slice| # slice huge words
slice = slice.join
joiner = line.empty? ? "" : " " # no space at line start
if (line + joiner + slice).length > width then
folded << line; line = slice # start new line
else
line += joiner + slice # append to current line
end
end
end
return (folded << line).join "\n"
end
puts fold ARGF.read
Name:
Anonymous2011-12-16 6:46
>>28 You'll break most of the math theorems.
How so? I wouldn't break any theorems concerned with finite or countable initial segments of N, would I?
>>27
Except Aleph-Null isn't a number, it's a cardinal.
It may be the cardinal of the natural numbers, but Peano Arithmetic works fine even if you don't consider it within the context of some set theory. >>30
If you keep the finite numbers unbounded, you still get countable infinity. Even if you limit numbers up to some bound, you'd still get countable infinity when you talk about theorems within the system.
This all depends on what axiomatic system you pick. You're free to pick something that doesn't talk about infinity directly (such as Peano Arithmetic or lesser), but you still get infinity in some forms when you analyze the metamathematical properties of such systems.
>>39 Dunno, try it and see.
But you're talking about an abstract system that shouldn't depend on physical limits, how much money you have for hardware or how big this universe happens to be. If you refuse to talk about such abstract systems on the virtue of them not always being physically implementable, then just say so. Another thing that might be worth talking about is this: if the universe is computable, you cannot compute it from the inside, thus an ultra-finitist can never seriously talk about physical law in his systems.